| 78 | |
| 79 | |
| 80 | class Bottleneck(nn.Module): |
| 81 | # Bottleneck in torchvision places the stride for downsampling at 3x3 convolution(self.conv2) |
| 82 | # while original implementation places the stride at the first 1x1 convolution(self.conv1) |
| 83 | # according to "Deep residual learning for image recognition"https://arxiv.org/abs/1512.03385. |
| 84 | # This variant is also known as ResNet V1.5 and improves accuracy according to |
| 85 | # https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch. |
| 86 | |
| 87 | expansion = 4 |
| 88 | |
| 89 | def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1, |
| 90 | base_width=64, dilation=1, norm_layer=None): |
| 91 | super(Bottleneck, self).__init__() |
| 92 | if norm_layer is None: |
| 93 | norm_layer = nn.BatchNorm2d |
| 94 | width = int(planes * (base_width / 64.)) * groups |
| 95 | # Both self.conv2 and self.downsample layers downsample the input when stride != 1 |
| 96 | self.conv1 = conv1x1(inplanes, width) |
| 97 | self.bn1 = norm_layer(width) |
| 98 | self.conv2 = conv3x3(width, width, stride, groups, dilation) |
| 99 | self.bn2 = norm_layer(width) |
| 100 | self.conv3 = conv1x1(width, planes * self.expansion) |
| 101 | self.bn3 = norm_layer(planes * self.expansion) |
| 102 | self.relu = nn.ReLU(inplace=True) |
| 103 | self.downsample = downsample |
| 104 | self.stride = stride |
| 105 | |
| 106 | def forward(self, x): |
| 107 | identity = x |
| 108 | |
| 109 | out = self.conv1(x) |
| 110 | out = self.bn1(out) |
| 111 | out = self.relu(out) |
| 112 | |
| 113 | out = self.conv2(out) |
| 114 | out = self.bn2(out) |
| 115 | out = self.relu(out) |
| 116 | |
| 117 | out = self.conv3(out) |
| 118 | out = self.bn3(out) |
| 119 | |
| 120 | if self.downsample is not None: |
| 121 | identity = self.downsample(x) |
| 122 | |
| 123 | out += identity |
| 124 | out = self.relu(out) |
| 125 | |
| 126 | return out |
| 127 | |
| 128 | |
| 129 | class ResNet(nn.Module): |
nothing calls this directly
no outgoing calls
no test coverage detected