ding.torch_utils.network.resnet¶
ding.torch_utils.network.resnet
¶
This implementation of ResNet is a bit modification version of https://github.com/rwightman/pytorch-image-models.git
AvgPool2dSame
¶
Bases: AvgPool2d
Overview
Tensorflow-like 'SAME' wrapper for 2D average pooling.
Interfaces:
__init__, forward
__init__(kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True)
¶
Overview
Initialize the AvgPool2dSame with given arguments.
Arguments:
- kernel_size (:obj:int): The size of the window to take an average over.
- stride (:obj:Optional[Tuple[int, int]]): The stride of the window. If None, default to kernel_size.
- padding (:obj:int): Implicit zero padding to be added on both sides.
- ceil_mode (:obj:bool): When True, will use ceil instead of floor to compute the output shape.
- count_include_pad (:obj:bool): When True, will include the zero-padding in the averaging calculation.
forward(x)
¶
Overview
Forward pass of the AvgPool2dSame.
Argument:
- x (:obj:torch.Tensor): Input tensor.
Returns:
- (:obj:torch.Tensor): Output tensor after average pooling.
ClassifierHead
¶
Bases: Module
Overview
Classifier head with configurable global pooling and dropout.
Interfaces:
__init__, forward
__init__(in_chs, num_classes, pool_type='avg', drop_rate=0.0, use_conv=False)
¶
Overview
Initialize the ClassifierHead with given arguments.
Arguments:
- in_chs (:obj:int): Number of input channels.
- num_classes (:obj:int): Number of classes for the final classification.
- pool_type (:obj:str): The type of pooling to use; 'avg' for Average Pooling.
- drop_rate (:obj:float): The dropout rate.
- use_conv (:obj:bool): Whether to use convolution or not.
forward(x)
¶
Overview
Forward pass of the ClassifierHead.
Argument:
- x (:obj:torch.Tensor): Input tensor.
Returns:
- (:obj:torch.Tensor): Output tensor after classification.
BasicBlock
¶
Bases: Module
Overview
The basic building block for models like ResNet. This class extends pytorch's Module class. It represents a standard block of layers including two convolutions, batch normalization, an optional attention mechanism, and activation functions.
Interfaces:
__init__, forward, zero_init_last_bn
Properties:
- expansion (:obj:int): Specifies the expansion factor for the planes of the conv layers.
__init__(inplanes, planes, stride=1, downsample=None, cardinality=1, base_width=64, reduce_first=1, dilation=1, first_dilation=None, act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d, attn_layer=None, aa_layer=None, drop_block=None, drop_path=None)
¶
Overview
Initialize the BasicBlock with given parameters.
Arguments:
- inplanes (:obj:int): Number of input channels.
- planes (:obj:int): Number of output channels.
- stride (:obj:int): The stride of the convolutional layer.
- downsample (:obj:Callable): Function for downsampling the inputs.
- cardinality (:obj:int): Group size for grouped convolution.
- base_width (:obj:int): Base width of the convolutions.
- reduce_first (:obj:int): Reduction factor for first convolution of each block.
- dilation (:obj:int): Spacing between kernel points.
- first_dilation (:obj:int): First dilation value.
- act_layer (:obj:Callable): Function for activation layer.
- norm_layer (:obj:Callable): Function for normalization layer.
- attn_layer (:obj:Callable): Function for attention layer.
- aa_layer (:obj:Callable): Function for anti-aliasing layer.
- drop_block (:obj:Callable): Method for dropping block.
- drop_path (:obj:Callable): Method for dropping path.
zero_init_last_bn()
¶
Overview
Initialize the batch normalization layer with zeros.
forward(x)
¶
Overview
Defines the computation performed at every call.
Arguments:
- x (:obj:torch.Tensor): The input tensor.
Returns:
- output (:obj:torch.Tensor): The output tensor after passing through the BasicBlock.
Bottleneck
¶
Bases: Module
Overview
The Bottleneck class is a basic block used to build ResNet networks. It is a part of the PyTorch's implementation of ResNet. This block is designed with several layers including a convolutional layer, normalization layer, activation layer, attention layer, anti-aliasing layer, and a dropout layer.
Interfaces:
__init__, forward, zero_init_last_bn
Properties:
expansion, inplanes, planes, stride, downsample, cardinality, base_width, reduce_first, dilation, first_dilation, act_layer, norm_layer, attn_layer, aa_layer, drop_block, drop_path
__init__(inplanes, planes, stride=1, downsample=None, cardinality=1, base_width=64, reduce_first=1, dilation=1, first_dilation=None, act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d, attn_layer=None, aa_layer=None, drop_block=None, drop_path=None)
¶
Overview
Initialize the Bottleneck class with various parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
- inplanes (
|
obj: |
required | |
- planes (
|
obj: |
required | |
- stride (
|
obj: |
required | |
- downsample (
|
obj: |
required | |
- cardinality (
|
obj: |
required | |
- base_width (
|
obj: |
required | |
- reduce_first (
|
obj: |
required | |
- dilation (
|
obj: |
required | |
- first_dilation (
|
obj: |
required | |
- act_layer (
|
obj: |
required | |
- norm_layer (
|
obj: |
required | |
- attn_layer (
|
obj: |
required | |
- aa_layer (
|
obj: |
required | |
- drop_block (
|
obj: |
required | |
- drop_path (
|
obj: |
required |
zero_init_last_bn()
¶
Overview
Initialize the last batch normalization layer with zero.
forward(x)
¶
Overview
Defines the computation performed at every call.
Arguments:
- x (:obj:Tensor): The input tensor.
Returns:
- x (:obj:Tensor): The output tensor resulting from the computation.
ResNet
¶
Bases: Module
Overview
Implements ResNet, ResNeXt, SE-ResNeXt, and SENet models. This implementation supports various modifications based on the v1c, v1d, v1e, and v1s variants included in the MXNet Gluon ResNetV1b model. For more details about the variants and options, please refer to the 'Bag of Tricks' paper: https://arxiv.org/pdf/1812.01187.
Interfaces:
__init__, forward, zero_init_last_bn, get_classifier
__init__(block, layers, num_classes=1000, in_chans=3, cardinality=1, base_width=64, stem_width=64, stem_type='', replace_stem_pool=False, output_stride=32, block_reduce_first=1, down_kernel_size=1, avg_down=False, act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d, aa_layer=None, drop_rate=0.0, drop_path_rate=0.0, drop_block_rate=0.0, global_pool='avg', zero_init_last_bn=True, block_args=None)
¶
Overview
Initialize the ResNet model with given block, layers and other configuration options.
Arguments:
- block (:obj:nn.Module): Class for the residual block.
- layers (:obj:List[int]): Numbers of layers in each block.
- num_classes (:obj:int, optional): Number of classification classes. Default is 1000.
- in_chans (:obj:int, optional): Number of input (color) channels. Default is 3.
- cardinality (:obj:int, optional): Number of convolution groups for 3x3 conv in Bottleneck. Default is 1.
- base_width (:obj:int, optional): Factor determining bottleneck channels. Default is 64.
- stem_width (:obj:int, optional): Number of channels in stem convolutions. Default is 64.
- stem_type (:obj:str, optional): The type of stem. Default is ''.
- replace_stem_pool (:obj:bool, optional): Whether to replace stem pooling. Default is False.
- output_stride (:obj:int, optional): Output stride of the network. Default is 32.
- block_reduce_first (:obj:int, optional): Reduction factor for first convolution output width of residual blocks. Default is 1.
- down_kernel_size (:obj:int, optional): Kernel size of residual block downsampling path. Default is 1.
- avg_down (:obj:bool, optional): Whether to use average pooling for projection skip connection between
stages/downsample. Default is False.
- act_layer (:obj:nn.Module, optional): Activation layer. Default is nn.ReLU.
- norm_layer (:obj:nn.Module, optional): Normalization layer. Default is nn.BatchNorm2d.
- aa_layer (:obj:Optional[nn.Module], optional): Anti-aliasing layer. Default is None.
- drop_rate (:obj:float, optional): Dropout probability before classifier, for training. Default is 0.0.
- drop_path_rate (:obj:float, optional): Drop path rate. Default is 0.0.
- drop_block_rate (:obj:float, optional): Drop block rate. Default is 0.0.
- global_pool (:obj:str, optional): Global pooling type. Default is 'avg'.
- zero_init_last_bn (:obj:bool, optional): Whether to initialize last batch normalization with zero. Default is True.
- block_args (:obj:Optional[dict], optional): Additional arguments for block. Default is None.
init_weights(zero_init_last_bn=True)
¶
Overview
Initialize the weights in the model.
Arguments:
- zero_init_last_bn (:obj:bool, optional): Whether to initialize last batch normalization with zero.
Default is True.
get_classifier()
¶
Overview
Get the classifier module from the model.
Returns:
- classifier (:obj:nn.Module): The classifier module in the model.
reset_classifier(num_classes, global_pool='avg')
¶
Overview
Reset the classifier with a new number of classes and pooling type.
Arguments:
- num_classes (:obj:int): New number of classification classes.
- global_pool (:obj:str, optional): New global pooling type. Default is 'avg'.
forward_features(x)
¶
Overview
Forward pass through the feature layers of the model.
Arguments:
- x (:obj:torch.Tensor): The input tensor.
Returns:
- x (:obj:torch.Tensor): The output tensor after passing through feature layers.
forward(x)
¶
Overview
Full forward pass through the model.
Arguments:
- x (:obj:torch.Tensor): The input tensor.
Returns:
- x (:obj:torch.Tensor): The output tensor after passing through the model.
to_2tuple(item)
¶
Overview
Convert a scalar to a 2-tuple or return the item if it's not a scalar.
Arguments:
- item (:obj:int): An item to be converted to a 2-tuple.
Returns:
- (:obj:tuple): A 2-tuple of the item.
get_same_padding(x, k, s, d)
¶
Overview
Calculate asymmetric TensorFlow-like 'SAME' padding for a convolution.
Arguments:
- x (:obj:int): The size of the input.
- k (:obj:int): The size of the kernel.
- s (:obj:int): The stride of the convolution.
- d (:obj:int): The dilation of the convolution.
Returns:
- (:obj:int): The size of the padding.
pad_same(x, k, s, d=(1, 1), value=0)
¶
Overview
Dynamically pad input x with 'SAME' padding for conv with specified args.
Arguments:
- x (:obj:Tensor): The input tensor.
- k (:obj:List[int]): The size of the kernel.
- s (:obj:List[int]): The stride of the convolution.
- d (:obj:List[int]): The dilation of the convolution.
- value (:obj:float): Value to fill the padding.
Returns:
- (:obj:Tensor): The padded tensor.
avg_pool2d_same(x, kernel_size, stride, padding=(0, 0), ceil_mode=False, count_include_pad=True)
¶
Overview
Apply average pooling with 'SAME' padding on the input tensor.
Arguments:
- x (:obj:Tensor): The input tensor.
- kernel_size (:obj:List[int]): The size of the kernel.
- stride (:obj:List[int]): The stride of the convolution.
- padding (:obj:List[int]): The size of the padding.
- ceil_mode (:obj:bool): When True, will use ceil instead of floor to compute the output shape.
- count_include_pad (:obj:bool): When True, will include the zero-padding in the averaging calculation.
Returns:
- (:obj:Tensor): The tensor after average pooling.
create_classifier(num_features, num_classes, pool_type='avg', use_conv=False)
¶
Overview
Create a classifier with global pooling layer and fully connected layer.
Arguments:
- num_features (:obj:int): The number of features.
- num_classes (:obj:int): The number of classes for the final classification.
- pool_type (:obj:str): The type of pooling to use; 'avg' for Average Pooling.
- use_conv (:obj:bool): Whether to use convolution or not.
Returns:
- global_pool (:obj:nn.Module): The created global pooling layer.
- fc (:obj:nn.Module): The created fully connected layer.
create_attn(layer, plane)
¶
Overview
Create an attention mechanism.
Arguments:
- layer (:obj:nn.Module): The layer where the attention is to be applied.
- plane (:obj:int): The plane on which the attention is to be applied.
Returns:
- None
get_padding(kernel_size, stride, dilation=1)
¶
Overview
Compute the padding based on the kernel size, stride and dilation.
Arguments:
- kernel_size (:obj:int): The size of the kernel.
- stride (:obj:int): The stride of the convolution.
- dilation (:obj:int): The dilation factor.
Returns:
- padding (:obj:int): The computed padding.
downsample_conv(in_channels, out_channels, kernel_size, stride=1, dilation=1, first_dilation=None, norm_layer=None)
¶
Overview
Create a sequential module for downsampling that includes a convolution layer and a normalization layer.
Arguments:
- in_channels (:obj:int): The number of input channels.
- out_channels (:obj:int): The number of output channels.
- kernel_size (:obj:int): The size of the kernel.
- stride (:obj:int, optional): The stride size, defaults to 1.
- dilation (:obj:int, optional): The dilation factor, defaults to 1.
- first_dilation (:obj:int, optional): The first dilation factor, defaults to None.
- norm_layer (:obj:Type[nn.Module], optional): The normalization layer type, defaults to nn.BatchNorm2d.
Returns:
- nn.Sequential: A sequence of layers performing downsampling through convolution.
downsample_avg(in_channels, out_channels, kernel_size, stride=1, dilation=1, first_dilation=None, norm_layer=None)
¶
Overview
Create a sequential module for downsampling that includes an average pooling layer, a convolution layer, and a normalization layer.
Arguments:
- in_channels (:obj:int): The number of input channels.
- out_channels (:obj:int): The number of output channels.
- kernel_size (:obj:int): The size of the kernel.
- stride (:obj:int, optional): The stride size, defaults to 1.
- dilation (:obj:int, optional): The dilation factor, defaults to 1.
- first_dilation (:obj:int, optional): The first dilation factor, defaults to None.
- norm_layer (:obj:Type[nn.Module], optional): The normalization layer type, defaults to nn.BatchNorm2d.
Returns:
- nn.Sequential: A sequence of layers performing downsampling through average pooling.
drop_blocks(drop_block_rate=0.0)
¶
Overview
Generate a list of None values based on the drop block rate.
Arguments:
- drop_block_rate (:obj:float, optional): The drop block rate, defaults to 0.
Returns:
- List[None]: A list of None values.
make_blocks(block_fn, channels, block_repeats, inplanes, reduce_first=1, output_stride=32, down_kernel_size=1, avg_down=False, drop_block_rate=0.0, drop_path_rate=0.0, **kwargs)
¶
Overview
Create a list of blocks for the network, with each block having a given number of repeats. Also, create a feature info list that contains information about the output of each block.
Arguments:
- block_fn (:obj:Type[nn.Module]): The type of block to use.
- channels (:obj:List[int]): The list of output channels for each block.
- block_repeats (:obj:List[int]): The list of number of repeats for each block.
- inplanes (:obj:int): The number of input planes.
- reduce_first (:obj:int, optional): The first reduction factor, defaults to 1.
- output_stride (:obj:int, optional): The total stride of the network, defaults to 32.
- down_kernel_size (:obj:int, optional): The size of the downsample kernel, defaults to 1.
- avg_down (:obj:bool, optional): Whether to use average pooling for downsampling, defaults to False.
- drop_block_rate (:obj:float, optional): The drop block rate, defaults to 0.
- drop_path_rate (:obj:float, optional): The drop path rate, defaults to 0.
Returns:
- Tuple[List[Tuple[str, nn.Module]], List[Dict[str, Union[int, str]]]]: A tuple that includes a list of blocks for the network and a feature info list.
resnet18()
¶
Overview
Creates a ResNet18 model.
Returns:
- model (:obj:nn.Module): ResNet18 model.
Full Source Code
../ding/torch_utils/network/resnet.py