MCPcopy Create free account
hub / github.com/deepbrainai-research/float / ResNet

Class ResNet

models/__init__.py:281–400  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

279
280
281class ResNet(nn.Module):
282 def __init__(
283 self,
284 block: Type[Union[SEBasicBlock, SEBottleneck]],
285 layers: List[int],
286 num_classes: int = 1000,
287 zero_init_residual: bool = False,
288 groups: int = 1,
289 width_per_group: int = 64,
290 replace_stride_with_dilation: Optional[List[bool]] = None,
291 norm_layer: Optional[Callable[..., nn.Module]] = None,
292 ) -> None:
293 super().__init__()
294 if norm_layer is None:
295 norm_layer = nn.BatchNorm2d
296 self._norm_layer = norm_layer
297
298 self.inplanes = 64
299 self.dilation = 1
300 if replace_stride_with_dilation is None:
301 # each element in the tuple indicates if we should replace
302 # the 2x2 stride with a dilated convolution instead
303 replace_stride_with_dilation = [False, False, False]
304 if len(replace_stride_with_dilation) != 3:
305 raise ValueError(
306 "replace_stride_with_dilation should be None "
307 f"or a 3-element tuple, got {replace_stride_with_dilation}"
308 )
309 self.groups = groups
310 self.base_width = width_per_group
311
312 self.stem = nn.Sequential(
313 nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False),
314 norm_layer(self.inplanes),
315 nn.ReLU(inplace=True),
316 nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
317 )
318
319 self.layer1 = self._make_layer(block, 64, layers[0])
320 self.layer2 = self._make_layer(block, 128, layers[1], stride=2, dilate=replace_stride_with_dilation[0])
321 self.layer3 = self._make_layer(block, 256, layers[2], stride=2, dilate=replace_stride_with_dilation[1])
322 self.layer4 = self._make_layer(block, 512, layers[3], stride=2, dilate=replace_stride_with_dilation[2])
323
324 self.fc = nn.Sequential(
325 nn.AdaptiveAvgPool2d((1, 1)),
326 nn.Flatten(),
327 nn.Linear(512 * block.expansion, num_classes)
328 )
329
330 for m in self.modules():
331 if isinstance(m, nn.Conv2d):
332 nn.init.kaiming_normal_(m.weight, mode="fan_out", nonlinearity="relu")
333 elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
334 nn.init.constant_(m.weight, 1)
335 nn.init.constant_(m.bias, 0)
336
337 # Zero-initialize the last BN in each residual branch,
338 # so that the residual branch starts with zeros, and each residual block behaves like an identity.

Callers 5

se_resnet18Function · 0.85
se_resnet34Function · 0.85
se_resnet50Function · 0.85
se_resnet101Function · 0.85
se_resnet152Function · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected