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