import torch import torch.nn as nn import torch.nn.functional as F class DepthWiseSeperableConv(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, padding): super(DepthWiseSeperableConv, self).__init__() # Depthwise convolution (each channel is convolved separately) self.depthwise = nn.Conv2d( in_channels=in_channels, out_channels=in_channels, kernel_size=kernel_size, padding=padding, groups=in_channels ) self.batch_norm_depth = nn.BatchNorm2d(in_channels) self.relu = nn.ReLU() # Pointwise convolution (mixes the channels) self.pointwise = nn.Conv2d( in_channels=in_channels, out_channels=out_channels, kernel_size=1 ) self.batch_norm_pointwise = nn.BatchNorm2d(out_channels) def forward(self, x): x = self.depthwise(x) x = self.batch_norm_depth(x) x = self.relu(x) x = self.pointwise(x) x = self.batch_norm_pointwise(x) x = self.relu(x) return x class ResidualDSBlock(nn.Module): def __init__(self, channels, kernel_size): super(ResidualDSBlock, self).__init__() padding = kernel_size // 2 self.conv = DepthWiseSeperableConv(channels, channels, kernel_size, padding) self.relu = nn.ReLU() def forward(self, x): # Compute the DS conv transformation out = self.conv(x) # Add the residual (skip connection) out = out + x # Apply final activation out = self.relu(out) return out class ForeignObjectClassificationResidualModel(nn.Module): def __init__(self, input_channels, conv_out_channels, kernel_size, num_conv_layers, # number of residual DS conv blocks num_fc_layers, # number of hidden FC layers before final output hidden_units, # number of units per hidden FC layer dropout_rate): super(ForeignObjectClassificationResidualModel, self).__init__() # Input dropout. if dropout_rate > 0: raise NotImplementedError("Do not use dropout right now. Later deep experiments " "with more dropout between all types of layers will be done.") self.dropout = nn.Dropout(dropout_rate) # Initial DS conv to increase the number of channels to conv_out_channels. padding = kernel_size // 2 self.initial_conv = DepthWiseSeperableConv(input_channels, conv_out_channels, kernel_size, padding) # Build a sequence of residual DS conv blocks. residual_blocks = [] for i in range(num_conv_layers): residual_blocks.append(ResidualDSBlock(conv_out_channels, kernel_size)) self.residual_blocks = nn.Sequential(*residual_blocks) # Global average pooling to reduce spatial dimensions. self.global_avg_pool = nn.AdaptiveAvgPool2d(1) # Build the fully connected classification head. fc_layers = [] fc_input_dim = conv_out_channels # from pooling output for i in range(num_fc_layers): fc_layers.append(nn.Linear(fc_input_dim, hidden_units)) fc_layers.append(nn.ReLU()) fc_input_dim = hidden_units # Final classification layer. fc_layers.append(nn.Linear(fc_input_dim, 1)) fc_layers.append(nn.Sigmoid()) self.binary_cls_head = nn.Sequential(*fc_layers) def forward(self, x): #x = self.dropout(x) # Pass through the initial DS convolution. x = self.initial_conv(x) # Pass through the residual DS conv blocks. x = self.residual_blocks(x) x = self.global_avg_pool(x) # Flatten the tensor for the FC layers. x = x.view(x.size(0), -1) x = self.binary_cls_head(x) return x