|
|
import numpy as np |
|
|
import torch.nn as nn |
|
|
|
|
|
|
|
|
|
|
|
class BasicClassificationModel(nn.Module): |
|
|
def __init__(self, image_size): |
|
|
super().__init__() |
|
|
self.image_size = image_size |
|
|
|
|
|
|
|
|
self.first_convolutional_layer = nn.Conv2d(3, 16, 5, padding=0) |
|
|
self.second_convolutional_layer = nn.Conv2d(16, 32, 5, padding=0) |
|
|
self.third_convolutional_layer = nn.Conv2d(32, 64, 5, padding=0) |
|
|
|
|
|
self.fully_connected_layer = self.create_dynamic_output_layer() |
|
|
|
|
|
|
|
|
self.pooling_layer = nn.MaxPool2d(5) |
|
|
self.activation_layer = nn.LeakyReLU(0.01) |
|
|
|
|
|
|
|
|
|
|
|
def create_dynamic_output_layer(self): |
|
|
output_image_size = self.image_size |
|
|
|
|
|
for layer in range(2): |
|
|
output_image_size = ((output_image_size - 4) // 5) |
|
|
|
|
|
|
|
|
output_image_size = output_image_size - 4 |
|
|
fully_connected_layer = nn.Linear(output_image_size * output_image_size * 64, 2) |
|
|
return fully_connected_layer |
|
|
|
|
|
def forward(self, x): |
|
|
x = self.first_convolutional_layer(x) |
|
|
x = self.pooling_layer(x) |
|
|
|
|
|
x = self.second_convolutional_layer(x) |
|
|
x = self.pooling_layer(x) |
|
|
|
|
|
x = self.third_convolutional_layer(x) |
|
|
x = self.activation_layer(x) |
|
|
|
|
|
x = x.view(x.size(0), -1) |
|
|
|
|
|
|
|
|
class_predictions = self.fully_connected_layer(x) |
|
|
|
|
|
return class_predictions |
|
|
|
|
|
|
|
|
classifierModel = BasicClassificationModel(image_size=416) |
|
|
print(classifierModel) |
|
|
|