Skip to main content

ResBlock

Get started

Import dependencies:

from neetbox.torch.arch import cnn

Build a ResBlock:

res_block = cnn.ResBlock(
inplanes=64, outplanes=128, kernel_size=3, stride=2, residual=True, dilation=2
)
res_block.eval()

output:

ResBlock(
(conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(2, 2), dilation=(2, 2))
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu_inplace): ReLU(inplace=True)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(2, 2), dilation=(2, 2))
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(residual): Sequential(
(0): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), dilation=(2, 2))
(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)

Structure

image-20230315191242352

A sample ResBlock consists of:

  • conv1 represents a down sampling(if stride is not 1) Conv2d layer. It will be a depth-wise convolution if depthwise was set to true, or it will be a dilation convolution if dilation is not set to 1.
  • conv2 represents a Conv2d layer with stride 1. It will be a depth-wise convolution if depthwise was set to true, or it will be a dilation convolution if dilation is not set to 1.
  • if residual was set to true, then residual represents a Conv2d layer followed by a BatchNorm2d if pool_on_residual_downsample is set to false. Other wise it represents a MaxPool2d layer if pool_on_residual_downsample is set to 'maxpool' or a AvgPool2d layer if pool_on_residual_downsample is set to 'averagepool'.
  • if skip_last_relu was set to false, then there is a ReLu in the end.

params for building a ResBlock

NameTypeDescription
inplanesintnum of channel input
outplanesintnum of channel output
kernel_sizeint (optional)kernel size. Defaults to 3.
strideint (optional)stride for downsampling layer. Defaults to 1.
paddingbool (optional)decide if use padding. If true, then the padding size will be calculated automatically. Defaults to True.
residualbool (optional)wether use residual. Defaults to False.
spatial_separablebool (optional)set spatial separable for non-downsamping layers. Defaults to False.
dilationint (optional)dilation rate. Defaults to 1 means do not use dilation convolution.
depthwisebool (optional)wether to use depthwise convolution. Defaults to False.
pool_on_residual_downsamplebool (optional)'maxpool' or 'averagepool' if you want to use pooliinstead of conv2d on residual path. Defaults to False.
bn_momentumfloat (optional)momentum of batch norms. Defaults to 0.1.
skip_last_relubool (optional)wether to skip the last relu. Defaults to False.

Examples

A 3x3 ResBlock with residual, no down-sample

model = cnn.ResBlock(inplanes=64, outplanes=128, kernel_size=3, stride=1, residual=True)

A 5x5 ResBlock using dilation convolution(dilation rate = 2) without residual, 2 times down-sample

model = cnn.ResBlock(
inplanes=64, outplanes=128, kernel_size=5, stride=2, residual=False, dilation=2
)

A 7x7 ResBlock using spatial-separable convolution without residual, 2 times down-sample

model = cnn.ResBlock(
inplanes=64,
outplanes=128,
kernel_size=7,
stride=2,
spatial_separable=True,
residual=True,
)

A 3x3 ResBlock with a max-pooling-residual, 2 times down-sample

model = cnn.ResBlock(
inplanes=64,
outplanes=64,
stride=2,
kernel_size=3,
pool_on_residual_downsample="maxpool",
residual=True,
)
caution

If you want a pooling operation as a residual path, you need to have your outplanes equal to inplanes.