resnet
Module containing ResNets adapted for use in minerva
.
- class ResNet(block: Type[BasicBlock | Bottleneck], layers: list[int] | tuple[int, int, int, int], in_channels: int = 3, n_classes: int = 8, zero_init_residual: bool = False, groups: int = 1, width_per_group: int = 64, replace_stride_with_dilation: tuple[bool, bool, bool] | None = None, norm_layer: Callable[[...], Module] | None = None, encoder: bool = False)
Modified version of the ResNet network to handle multi-spectral inputs and cross-entropy.
- encoder_on
Whether to initialise the
ResNet
as an encoder or end-to-end classifier. IfTrue
, forward method returns the output of each layer block. avgpool and fc are not initialised. IfFalse
, adds a global average pooling layer after the last block, flattens the output and passes through a fully connected layer for classification output.- Type:
- base_width
Modifies the number of feature maps in convolutional layers of Bottleneck Blocks.
- Type:
- maxpool
3x3 Max-pooling layer with stride 2 of the Conv1 input block to the network.
- Type:
- avgpool
Global average pooling layer taking the output from the last block. Only initialised if
encoder_on=False
.- Type:
- fc
Fully connected layer that takes the flattened output from average pooling to a classification output. Only initialised if
encoder_on=False
.- Type:
Warning
Layers using
BasicBlock
are not compatible with anything other than the default values forgroups
andwidth_per_group
.- Parameters:
block (BasicBlock | Bottleneck) – Type of block operations to use throughout network.
layers (list[int] | tuple[int, int, int, int]) – Number of blocks in each of the 4
layers
.in_channels (int) – Optional; Number of channels (or bands) in the input imagery.
n_classes (int) – Optional; Number of classes in data to be classified.
zero_init_residual (bool) – Optional; If
True
, zero-initialise the last BN in each residual branch, so that the residual branch starts with zeros, and each residual block behaves like an identity.groups (int) – Optional; Number of convolutions in grouped convolutions of Bottleneck Blocks. Not compatible with Basic Block!
width_per_group (int) – Optional; Modifies the number of feature maps in convolutional layers of Bottleneck Blocks. Not compatible with Basic Block!
replace_stride_with_dilation (tuple[bool, bool, bool]) – Optional; Each element in the tuple indicates whether to replace the
2x2
stride with a dilated convolution instead. Must be a three element tuple of bools.norm_layer (Callable[..., Module]) – Optional; Normalisation layer to use in each block. Typically,
BatchNorm2d
.encoder (bool) – Optional; Whether to initialise the
ResNet
as an encoder or end-to-end classifier. IfTrue
, forward method returns the output of each layer block. avgpool and fc are not initialised. IfFalse
, adds a global average pooling layer after the last block, flattens the output and passes through a fully connected layer for classification output.
- Raises:
ValueError – If
replace_stride_with_dilation
is not None or a 3-element tuple.
- class ResNet101(criterion: Any | None = None, input_size: tuple[int, int, int] = (4, 256, 256), n_classes: int = 8, zero_init_residual: bool = False, groups: int = 1, width_per_group: int = 64, replace_stride_with_dilation: tuple[bool, bool, bool] | None = None, norm_layer: Callable[[...], Module] | None = None, encoder: bool = False, torch_weights: bool = False)
ResNet101 modified from source to have customisable number of input channels and to be used as a backbone by stripping classification layers away.
- block_type
alias of
Bottleneck
- class ResNet152(criterion: Any | None = None, input_size: tuple[int, int, int] = (4, 256, 256), n_classes: int = 8, zero_init_residual: bool = False, groups: int = 1, width_per_group: int = 64, replace_stride_with_dilation: tuple[bool, bool, bool] | None = None, norm_layer: Callable[[...], Module] | None = None, encoder: bool = False, torch_weights: bool = False)
ResNet152 modified from source to have customisable number of input channels and to be used as a backbone by stripping classification layers away.
- block_type
alias of
Bottleneck
- class ResNet18(criterion: Any | None = None, input_size: tuple[int, int, int] = (4, 256, 256), n_classes: int = 8, zero_init_residual: bool = False, groups: int = 1, width_per_group: int = 64, replace_stride_with_dilation: tuple[bool, bool, bool] | None = None, norm_layer: Callable[[...], Module] | None = None, encoder: bool = False, torch_weights: bool = False)
ResNet18 modified from source to have customisable number of input channels and to be used as a backbone by stripping classification layers away.
- class ResNet34(criterion: Any | None = None, input_size: tuple[int, int, int] = (4, 256, 256), n_classes: int = 8, zero_init_residual: bool = False, groups: int = 1, width_per_group: int = 64, replace_stride_with_dilation: tuple[bool, bool, bool] | None = None, norm_layer: Callable[[...], Module] | None = None, encoder: bool = False, torch_weights: bool = False)
ResNet34 modified from source to have customisable number of input channels and to be used as a backbone by stripping classification layers away.
- class ResNet50(criterion: Any | None = None, input_size: tuple[int, int, int] = (4, 256, 256), n_classes: int = 8, zero_init_residual: bool = False, groups: int = 1, width_per_group: int = 64, replace_stride_with_dilation: tuple[bool, bool, bool] | None = None, norm_layer: Callable[[...], Module] | None = None, encoder: bool = False, torch_weights: bool = False)
ResNet50 modified from source to have customisable number of input channels and to be used as a backbone by stripping classification layers away.
- block_type
alias of
Bottleneck
- class ResNetX(criterion: Any | None = None, input_size: tuple[int, int, int] = (4, 256, 256), n_classes: int = 8, zero_init_residual: bool = False, groups: int = 1, width_per_group: int = 64, replace_stride_with_dilation: tuple[bool, bool, bool] | None = None, norm_layer: Callable[[...], Module] | None = None, encoder: bool = False, torch_weights: bool = False)
Helper class to allow for easy creation of ResNet variant classes of the base
ResNet
class.Example
To build a
ResNet
variant class, just simply set the appropiate attributes in the definition of your new variant class that inherits fromResNetX
.>>> from minerva.models import ResNetX >>> from torchvision.models.resnet import Bottleneck >>> >>> class MyResNet101(ResNetX): >>> layer_struct = [3, 4, 23, 3] >>> block_type = BottleNeck >>> weights_name = "ResNet101_Weights.IMAGENET1K_V1"
You can then construct an instance of your new class like any other
ResNet
with the added bonus of being able to use pre-trained torch weights:>>> model = ResNet101(*args, **kwargs, torch_weights=True)
- Parameters:
criterion –
torch
loss function model will use.input_size (tuple[int, int, int]) – Optional; Defines the shape of the input data in order of number of channels, image width, image height.
n_classes (int) – Optional; Number of classes in data to be classified.
zero_init_residual (bool) – Optional; If
True
, zero-initialise the last BN in each residual branch, so that the residual branch starts with zeros, and each residual block behaves like an identity.replace_stride_with_dilation (tuple[bool, bool, bool]) – Optional; Each element in the tuple indicates whether to replace the
2x2
stride with a dilated convolution instead. Must be a three element tuple of bools.norm_layer (Callable[..., Module]) – Optional; Normalisation layer to use in each block. Typically
BatchNorm2d
.encoder (bool) – Optional; Whether to initialise the
ResNet
as an encoder or end-to-end classifier. IfTrue
, forward method returns the output of each layer block.avgpool
andfc
are not initialised. IfFalse
, adds a global average pooling layer after the last block, flattens the output and passes through a fully connected layer for classification output.torch_weights (bool) – Optional; Whether to use the pre-trained weights from
torchvision
. See note.
Note
If using
torch_weights
, the weightstate_dict
is modified to remove incompatible layers (such as theconv1
layer) ifinput_size
is non-RGB (i.e not 3-channel) and/or images smaller than224x224
with the randomly initialisedconv1
that is appropiate forinput_size
.- block_type
alias of
BasicBlock