Adapted model architecture

This commit is contained in:
2025-04-08 09:43:56 +02:00
parent 996268990c
commit 53d45abd1a
5 changed files with 307 additions and 16 deletions
Binary file not shown.
+2 -2
View File
@@ -291,9 +291,9 @@ if __name__ == '__main__':
target_image_sizes = [128, 256]
n_tiles_per_side_list = [4, 8, 16, 32]
output_channels_ds_conv_list = [32, 64, 128, 256]
output_channels_ds_conv_list = [16, 32, 64, 128]
kernel_sizes = [3, 5, 7, 9]
hidden_units_list = [32, 64, 128, 256]
hidden_units_list = [16, 32, 64, 128]
dropout_rates = [0, 0.25, 0.5, 0.75] # [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
num_experiments = (len(grayscales) * len(n_train_val_images_per_class_list) * len(target_image_sizes) *
+49 -12
View File
@@ -1,29 +1,52 @@
import torch.nn as nn
class ForeignObjectClassificationModel(nn.Module):
def __init__(self, input_channels, output_channels_ds_conv, kernel_size, hidden_units, dropout_rate):
super(ForeignObjectClassificationModel, self).__init__()
# Depthwise convolution: set groups=input_channels so that each channel is convolved separately.
self.depthwise = nn.Conv2d(
# DW1
self.depthwise_1 = nn.Conv2d(
in_channels=input_channels,
out_channels=input_channels,
kernel_size=kernel_size,
padding=kernel_size // 2,
#padding=kernel_size // 2,
groups=input_channels
)
self.batch_norm_depth_1 = nn.BatchNorm2d(input_channels)
self.relu = nn.ReLU()
self.batch_norm_depth = nn.BatchNorm2d(input_channels)
# Pointwise convolution (1x1 conv)
self.pointwise = nn.Conv2d(
self.pointwise_1 = nn.Conv2d(
in_channels=input_channels,
out_channels=output_channels_ds_conv,
kernel_size=1
)
self.batch_norm_point = nn.BatchNorm2d(output_channels_ds_conv)
self.batch_norm_point_1 = nn.BatchNorm2d(output_channels_ds_conv)
# DW2
kernel_size_2 = kernel_size
output_channels_ds_conv_2 = output_channels_ds_conv * 2
self.depthwise_2 = nn.Conv2d(
in_channels=output_channels_ds_conv,
out_channels=output_channels_ds_conv,
kernel_size=kernel_size_2,
#padding=kernel_size // 2,
groups=output_channels_ds_conv
)
self.batch_norm_depth_2 = nn.BatchNorm2d(output_channels_ds_conv)
# Pointwise convolution (1x1 conv)
self.pointwise_2 = nn.Conv2d(
in_channels=output_channels_ds_conv,
out_channels=output_channels_ds_conv_2,
kernel_size=1
)
self.batch_norm_point_2 = nn.BatchNorm2d(output_channels_ds_conv_2)
# Global average pooling to output (batch, channels, 1, 1)
self.global_avg_pool = nn.AdaptiveAvgPool2d(1)
# Fully connected layers
self.fc1 = nn.Linear(output_channels_ds_conv, hidden_units)
self.fc1 = nn.Linear(output_channels_ds_conv_2, hidden_units)
self.dropout = nn.Dropout(dropout_rate)
self.fc2 = nn.Linear(hidden_units, 1)
self.sigmoid = nn.Sigmoid()
@@ -31,13 +54,27 @@ class ForeignObjectClassificationModel(nn.Module):
def forward(self, x):
# Originial Mobile-Net uses Depthwise-Separable Convolution based on:
# 3x3 Depthwise Conv, bn, relu, 1x1 Conv (pointwise), bn, relu
x = self.relu(self.depthwise(x))
x = self.batch_norm_depth(x)
x = self.relu(self.pointwise(x))
x = self.batch_norm_point(x)
#DW 1
x = self.depthwise_1(x)
x = self.batch_norm_depth_1(x)
x = self.relu(x)
x = self.pointwise_1(x)
x = self.batch_norm_point_1(x)
x = self.relu(x)
#DW 2
x = self.depthwise_2(x)
x = self.batch_norm_depth_2(x)
x = self.relu(x)
x = self.pointwise_2(x)
x = self.batch_norm_point_2(x)
x = self.relu(x)
#Pooling/Flattening
x = self.global_avg_pool(x)
x = x.view(x.size(0), -1) # flatten (batch, channels)
x = self.relu(self.fc1(x))
#CLS-Head
x = self.fc1(x)
x = self.relu(x)
x = self.dropout(x)
x = self.sigmoid(self.fc2(x))
x = self.fc2(x)
x = self.sigmoid(x)
return x