(self, block, layers, num_classes=1000, zero_init_residual=False,
groups=1, width_per_group=64, replace_stride_with_dilation=None,
norm_layer=None)
| 119 | class ResNet(nn.Module): |
| 120 | |
| 121 | def __init__(self, block, layers, num_classes=1000, zero_init_residual=False, |
| 122 | groups=1, width_per_group=64, replace_stride_with_dilation=None, |
| 123 | norm_layer=None): |
| 124 | super(ResNet, self).__init__() |
| 125 | if norm_layer is None: |
| 126 | norm_layer = nn.BatchNorm2d |
| 127 | self._norm_layer = norm_layer |
| 128 | |
| 129 | self.inplanes = 64 |
| 130 | self.dilation = 1 |
| 131 | if replace_stride_with_dilation is None: |
| 132 | # each element in the tuple indicates if we should replace |
| 133 | # the 2x2 stride with a dilated convolution instead |
| 134 | replace_stride_with_dilation = [False, False, False] |
| 135 | if len(replace_stride_with_dilation) != 3: |
| 136 | raise ValueError("replace_stride_with_dilation should be None " |
| 137 | "or a 3-element tuple, got {}".format(replace_stride_with_dilation)) |
| 138 | self.groups = groups |
| 139 | self.base_width = width_per_group |
| 140 | self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3, |
| 141 | bias=False) |
| 142 | self.bn1 = norm_layer(self.inplanes) |
| 143 | self.relu = nn.ReLU(inplace=True) |
| 144 | self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) |
| 145 | self.layer1 = self._make_layer(block, 64, layers[0]) |
| 146 | self.layer2 = self._make_layer(block, 128, layers[1], stride=2, |
| 147 | dilate=replace_stride_with_dilation[0]) |
| 148 | self.layer3 = self._make_layer(block, 256, layers[2], stride=2, |
| 149 | dilate=replace_stride_with_dilation[1]) |
| 150 | self.layer4 = self._make_layer(block, 512, layers[3], stride=2, |
| 151 | dilate=replace_stride_with_dilation[2]) |
| 152 | #self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) |
| 153 | #self.fc = nn.Linear(512 * block.expansion, num_classes) |
| 154 | |
| 155 | for m in self.modules(): |
| 156 | if isinstance(m, nn.Conv2d): |
| 157 | nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') |
| 158 | elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)): |
| 159 | nn.init.constant_(m.weight, 1) |
| 160 | nn.init.constant_(m.bias, 0) |
| 161 | |
| 162 | # Zero-initialize the last BN in each residual branch, |
| 163 | # so that the residual branch starts with zeros, and each residual block behaves like an identity. |
| 164 | # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677 |
| 165 | if zero_init_residual: |
| 166 | for m in self.modules(): |
| 167 | if isinstance(m, Bottleneck): |
| 168 | nn.init.constant_(m.bn3.weight, 0) |
| 169 | elif isinstance(m, BasicBlock): |
| 170 | nn.init.constant_(m.bn2.weight, 0) |
| 171 | |
| 172 | def _make_layer(self, block, planes, blocks, stride=1, dilate=False): |
| 173 | norm_layer = self._norm_layer |
no test coverage detected