Shortcuts

mmagic.models.archs.resnet

Module Contents

Classes

BasicBlock

Basic block for ResNet.

Bottleneck

Bottleneck block for ResNet.

ResNet

General ResNet.

class mmagic.models.archs.resnet.BasicBlock(inplanes: int, planes: int, stride: int = 1, dilation: int = 1, downsample: Optional[torch.nn.Module] = None, act_cfg: dict = dict(type='ReLU'), conv_cfg: Optional[dict] = None, norm_cfg: dict = dict(type='BN'), with_cp: bool = False)[source]

Bases: torch.nn.Module

Basic block for ResNet.

Parameters
  • inplanes (int) – Number of input channels.

  • planes (int) – Number of output channels.

  • stride (int) – Stride of the first block of one stage. Default: 1.

  • dilation (int) – Dilation of one stage. Default: 1.

  • downsample (nn.Module) – Downsample module. Default: None.

  • act_cfg (dict) – Dictionary to construct and config activation layer. Default: dict(type=’ReLU’).

  • conv_cfg (dict) – Dictionary to construct and config convolution layer. Default: None.

  • norm_cfg (dict) – Dictionary to construct and config norm layer. Default: dict(type=’BN’).

  • with_cp (bool) – Use checkpoint or not. Using checkpoint will save some memory while slowing down the training speed. Default: False.

property norm1: torch.nn.Module[source]

normalization layer after the first convolution layer

Type

nn.Module

property norm2: torch.nn.Module[source]

normalization layer after the second convolution layer

Type

nn.Module

expansion = 1[source]
forward(x: torch.Tensor) torch.Tensor[source]

Forward function.

class mmagic.models.archs.resnet.Bottleneck(inplanes: int, planes: int, stride: int = 1, dilation: int = 1, downsample: Optional[torch.nn.Module] = None, act_cfg: dict = dict(type='ReLU'), conv_cfg: Optional[dict] = None, norm_cfg: dict = dict(type='BN'), with_cp: bool = False)[source]

Bases: torch.nn.Module

Bottleneck block for ResNet.

Parameters
  • inplanes (int) – Number of input channels.

  • planes (int) – Number of output channels.

  • stride (int) – Stride of the first block of one stage. Default: 1.

  • dilation (int) – Dilation of one stage. Default: 1.

  • downsample (nn.Module) – Downsample module. Default: None.

  • act_cfg (dict) – Dictionary to construct and config activation layer. Default: dict(type=’ReLU’).

  • conv_cfg (dict) – Dictionary to construct and config convolution layer. Default: None.

  • norm_cfg (dict) – Dictionary to construct and config norm layer. Default: dict(type=’BN’).

  • with_cp (bool) – Use checkpoint or not. Using checkpoint will save some memory while slowing down the training speed. Default: False.

property norm1: torch.nn.Module[source]

normalization layer after the first convolution layer

Type

nn.Module

property norm2: torch.nn.Module[source]

normalization layer after the second convolution layer

Type

nn.Module

property norm3: torch.nn.Module[source]

normalization layer after the second convolution layer

Type

nn.Module

expansion = 4[source]
forward(x: torch.Tensor) torch.Tensor[source]
class mmagic.models.archs.resnet.ResNet(depth: int, in_channels: int = 3, stem_channels: int = 64, base_channels: int = 64, num_stages: int = 4, strides: Sequence[int] = (1, 2, 2, 2), dilations: Sequence[int] = (1, 1, 2, 4), deep_stem: bool = False, avg_down: bool = False, frozen_stages: int = - 1, act_cfg: dict = dict(type='ReLU'), conv_cfg: Optional[dict] = None, norm_cfg: dict = dict(type='BN'), with_cp: bool = False, multi_grid: Optional[Sequence[int]] = None, contract_dilation: bool = False, zero_init_residual: bool = True)[source]

Bases: torch.nn.Module

General ResNet.

This class is adopted from https://github.com/open-mmlab/mmsegmentation/blob/master/mmseg/models/backbones/resnet.py.

Parameters
  • depth (int) – Depth of resnet, from {18, 34, 50, 101, 152}.

  • in_channels (int) – Number of input image channels. Default” 3.

  • stem_channels (int) – Number of stem channels. Default: 64.

  • base_channels (int) – Number of base channels of res layer. Default: 64.

  • num_stages (int) – Resnet stages, normally 4.

  • strides (Sequence[int]) – Strides of the first block of each stage. Default: (1, 2, 2, 2).

  • dilations (Sequence[int]) – Dilation of each stage. Default: (1, 1, 2, 4).

  • deep_stem (bool) – Replace 7x7 conv in input stem with 3 3x3 conv. Default: False.

  • avg_down (bool) – Use AvgPool instead of stride conv when downsampling in the bottleneck. Default: False.

  • frozen_stages (int) – Stages to be frozen (stop grad and set eval mode). -1 means not freezing any parameters. Default: -1.

  • act_cfg (dict) – Dictionary to construct and config activation layer. Default: dict(type=’ReLU’).

  • conv_cfg (dict) – Dictionary to construct and config convolution layer. Default: None.

  • norm_cfg (dict) – Dictionary to construct and config norm layer. Default: dict(type=’BN’).

  • with_cp (bool) – Use checkpoint or not. Using checkpoint will save some memory while slowing down the training speed. Default: False.

  • multi_grid (Sequence[int]|None) – Multi grid dilation rates of last stage. Default: None.

  • contract_dilation (bool) – Whether contract first dilation of each layer Default: False.

  • zero_init_residual (bool) – Whether to use zero init for last norm layer in resblocks to let them behave as identity. Default: True.

property norm1: torch.nn.Module[source]

normalization layer after the second convolution layer

Type

nn.Module

arch_settings[source]
_make_stem_layer(in_channels: int, stem_channels: int) None[source]

Make stem layer for ResNet.

_make_layer(block: BasicBlock, planes: int, blocks: int, stride: int = 1, dilation: int = 1) torch.nn.Module[source]
_nostride_dilate(m: torch.nn.Module, dilate: int) None[source]
init_weights(pretrained: Optional[str] = None) None[source]

Init weights for the model.

Parameters

pretrained (str, optional) – Path for pretrained weights. If given None, pretrained weights will not be loaded. Defaults to None.

_freeze_stages() None[source]

Freeze stages param and norm stats.

forward(x: torch.Tensor) List[torch.Tensor][source]

Forward function.

Parameters

x (Tensor) – Input tensor with shape (N, C, H, W).

Returns

Output tensor.

Return type

Tensor

Read the Docs v: latest
Versions
latest
stable
0.x
Downloads
pdf
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.