Commit
·
c58c84b
1
Parent(s):
bf843fe
ResNet 50 for CIFAR-10 with more comments
Browse files- resnet_model.py +3 -3
resnet_model.py
CHANGED
@@ -11,7 +11,7 @@ class Bottleneck(nn.Module): # Bottleneck module as a single class which will be
|
|
11 |
self.bn1 = nn.BatchNorm2d(out_channels)
|
12 |
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False) # note this is the convolution which will use a stride of 2 to match the input and output dimensions. This happens in the first block of layers 2, 3 and 4 only. All convolutions in all subsequent blocks in each of the layers use a stride of 1, as per the ResNet model.
|
13 |
self.bn2 = nn.BatchNorm2d(out_channels)
|
14 |
-
self.conv3 = nn.Conv2d(out_channels, out_channels * self.expansion, kernel_size=1, bias=False) # this is the
|
15 |
self.bn3 = nn.BatchNorm2d(out_channels * self.expansion)
|
16 |
self.relu = nn.ReLU(inplace=True) # this will modify the original tensor rather than operating on a copy. Significant memory savings as this module is the fundamental repeating unit. Makes sense to use only in the last layer so that we're not unintentionally corrupting the input tensor in the previous layers.
|
17 |
self.downsample = downsample # helps match the output dimensions to the input dimensions for the special skip connection.
|
@@ -30,11 +30,11 @@ class Bottleneck(nn.Module): # Bottleneck module as a single class which will be
|
|
30 |
out = self.conv3(out)
|
31 |
out = self.bn3(out)
|
32 |
|
33 |
-
# Special skip connection - triggered only in the first block of layers
|
34 |
if self.downsample is not None:
|
35 |
identity = self.downsample(x)
|
36 |
|
37 |
-
out += identity # The ResNet addition is here; H(x) = F(x) + x. Skip connection by virtue of adding identity variable, which is the original input without convolutions.
|
38 |
out = self.relu(out)
|
39 |
|
40 |
return out
|
|
|
11 |
self.bn1 = nn.BatchNorm2d(out_channels)
|
12 |
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False) # note this is the convolution which will use a stride of 2 to match the input and output dimensions. This happens in the first block of layers 2, 3 and 4 only. All convolutions in all subsequent blocks in each of the layers use a stride of 1, as per the ResNet model.
|
13 |
self.bn2 = nn.BatchNorm2d(out_channels)
|
14 |
+
self.conv3 = nn.Conv2d(out_channels, out_channels * self.expansion, kernel_size=1, bias=False) # this is the convolution where number of channels is expanded, as per the ResNet model.
|
15 |
self.bn3 = nn.BatchNorm2d(out_channels * self.expansion)
|
16 |
self.relu = nn.ReLU(inplace=True) # this will modify the original tensor rather than operating on a copy. Significant memory savings as this module is the fundamental repeating unit. Makes sense to use only in the last layer so that we're not unintentionally corrupting the input tensor in the previous layers.
|
17 |
self.downsample = downsample # helps match the output dimensions to the input dimensions for the special skip connection.
|
|
|
30 |
out = self.conv3(out)
|
31 |
out = self.bn3(out)
|
32 |
|
33 |
+
# Special skip connection - triggered only in the first block of all layers, where we need to downsample the dimensions and channels of input x to match those of F(x) after convolutions, to be able to add them up.
|
34 |
if self.downsample is not None:
|
35 |
identity = self.downsample(x)
|
36 |
|
37 |
+
out += identity # The ResNet addition is here; H(x) = F(x) + x. Skip connection by virtue of adding identity variable, which is the original input without convolutions.If special skip connection, downsampling will be applied.
|
38 |
out = self.relu(out)
|
39 |
|
40 |
return out
|