# Codeblock 9 class Block(nn.Module): def __init__(self, in_channels, add_channel=False, channel_multiplier=2, downsample=False): super().__init__() self.add_channel = add_channel self.channel_multiplier = channel_multiplier self.downsample = downsample if self.add_channel: out_channels = in_channels*self.channel_multiplier else: out_channels = in_channels mid_channels = out_channels//2 if self.downsample: stride = 2 else: stride = 1 if self.add_channel or self.downsample: self.projection = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=stride, padding=0, bias=False) nn.init.kaiming_normal_(self.projection.weight, nonlinearity='relu') self.bn_proj = nn.BatchNorm2d(num_features=out_channels) self.conv0 = nn.Conv2d(in_channels=in_channels, out_channels=mid_channels, kernel_size=1, stride=1, padding=0, bias=False) nn.init.kaiming_normal_(self.conv0.weight, nonlinearity='relu') self.bn0 = nn.BatchNorm2d(num_features=mid_channels) self.conv1 = nn.Conv2d(in_channels=mid_channels, out_channels=mid_channels, kernel_size=3, stride=stride, padding=1, bias=False, groups=CARDINALITY) nn.init.kaiming_normal_(self.conv1.weight, nonlinearity='relu') self.bn1 = nn.BatchNorm2d(num_features=mid_channels) self.conv2 = nn.Conv2d(in_channels=mid_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0, bias=False) nn.init.kaiming_normal_(self.conv2.weight, nonlinearity='relu') self.bn2 = nn.BatchNorm2d(num_features=out_channels) self.relu = nn.ReLU() self.cbam = CBAM(num_channels=out_channels) #(1) def forward(self, x): print(f'original\t\t: {x.size()}') if self.add_channel or self.downsample: residual = self.bn_proj(self.projection(x)) print(f'after projection\t: {residual.size()}') else: residual = x print(f'no projection\t\t: {residual.size()}') x = self.conv0(x) x = self.bn0(x) x = self.relu(x) print(f'after conv0-bn0-relu\t: {x.size()}') x = self.conv1(x) x = self.bn1(x) x = self.relu(x) print(f'after conv1-bn1-relu\t: {x.size()}') x = self.conv2(x) x = self.bn2(x) print(f'after conv2-bn2\t\t: {x.size()}') x = self.cbam(x) #(2) print(f'after cbam\t\t: {x.size()}') x = x + residual x = self.relu(x) print(f'after summation\t\t: {x.size()}') return x