ml4gw.nn.resnet package

Submodules

ml4gw.nn.resnet.resnet_1d module

In large part lifted from https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py but with 1d convolutions and arbitrary kernel sizes, and a default norm layer that makes more sense for most GW applications where training-time statistics are entirely arbitrary due to simulations.

class ml4gw.nn.resnet.resnet_1d.BasicBlock(inplanes, planes, kernel_size=3, stride=1, downsample=None, groups=1, base_width=64, dilation=1, norm_layer=None)

Bases: Module

Defines the structure of the blocks used to build the ResNet

expansion: int = 1
forward(x)

Define the computation performed at every call.

Should be overridden by all subclasses. :rtype: Tensor

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class ml4gw.nn.resnet.resnet_1d.Bottleneck(inplanes, planes, kernel_size=3, stride=1, downsample=None, groups=1, base_width=64, dilation=1, norm_layer=None)

Bases: Module

Bottleneck blocks implement one extra convolution compared to basic blocks. In this layers, the planes parameter is generally meant to _downsize_ the number of feature maps first, which then get expanded out to planes * Bottleneck.expansion feature maps at the output of the layer.

expansion: int = 4
forward(x)

Define the computation performed at every call.

Should be overridden by all subclasses. :rtype: Tensor

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class ml4gw.nn.resnet.resnet_1d.BottleneckResNet1D(in_channels, layers, classes, kernel_size=3, zero_init_residual=False, groups=1, width_per_group=64, stride_type=None, norm_layer=None)

Bases: ResNet1D

A version of ResNet that uses bottleneck blocks

block

alias of Bottleneck

class ml4gw.nn.resnet.resnet_1d.ResNet1D(in_channels, layers, classes, kernel_size=3, zero_init_residual=False, groups=1, width_per_group=64, stride_type=None, norm_layer=None)

Bases: Module

1D ResNet architecture

Simple extension of ResNet to 1D convolutions with arbitrary kernel sizes to support the longer timeseries used in BBH detection.

Parameters:
  • in_channels (int) -- The number of channels in input tensor.

  • layers (List[int]) -- A list representing the number of residual blocks to include in each "layer" of the network. Total layers (e.g. 50 in ResNet50) is 2 + sum(layers) * factor, where factor is 2 for vanilla ResNet and 3 for BottleneckResNet.

  • kernel_size (int) -- The size of the convolutional kernel to use in all residual layers. _NOT_ the size of the input kernel to the network, which is determined at run-time.

  • zero_init_residual (bool) -- Flag indicating whether to initialize the weights of the batch-norm layer in each block to 0 so that residuals are initialized as identities. Can improve training results.

  • groups (int) -- Number of convolutional groups to use in all layers. Grouped convolutions induce local connections between feature maps at subsequent layers rather than global. Generally won't need this to be >1, and wil raise an error if >1 when using vanilla ResNet.

  • width_per_group (int) -- Base width of each of the feature map groups, which is scaled up by the typical expansion factor at each layer of the network. Meaningless for vanilla ResNet.

  • stride_type (Optional[List[Literal['stride', 'dilation']]]) -- Whether to achieve downsampling on the time axis by strided or dilated convolutions for each layer. If left as None, strided convolutions will be used at each layer. Otherwise, stride_type should be one element shorter than layers and indicate either stride or dilation for each layer after the first.

block

alias of BasicBlock

forward(x)

Define the computation performed at every call.

Should be overridden by all subclasses. :rtype: Tensor

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

ml4gw.nn.resnet.resnet_1d.conv1(in_planes, out_planes, stride=1)

Kernel-size 1 convolution

Return type:

Conv1d

ml4gw.nn.resnet.resnet_1d.convN(in_planes, out_planes, kernel_size=3, stride=1, groups=1, dilation=1)

1d convolution with padding

Return type:

Conv1d

ml4gw.nn.resnet.resnet_2d module

In large part lifted from https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py but with arbitrary kernel sizes

class ml4gw.nn.resnet.resnet_2d.BasicBlock(inplanes, planes, kernel_size=3, stride=1, downsample=None, groups=1, base_width=64, dilation=1, norm_layer=None)

Bases: Module

Defines the structure of the blocks used to build the ResNet

expansion: int = 1
forward(x)

Define the computation performed at every call.

Should be overridden by all subclasses. :rtype: Tensor

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class ml4gw.nn.resnet.resnet_2d.Bottleneck(inplanes, planes, kernel_size=3, stride=1, downsample=None, groups=1, base_width=64, dilation=1, norm_layer=None)

Bases: Module

Bottleneck blocks implement one extra convolution compared to basic blocks. In this layers, the planes parameter is generally meant to _downsize_ the number of feature maps first, which then get expanded out to planes * Bottleneck.expansion feature maps at the output of the layer.

expansion: int = 4
forward(x)

Define the computation performed at every call.

Should be overridden by all subclasses. :rtype: Tensor

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class ml4gw.nn.resnet.resnet_2d.BottleneckResNet2D(in_channels, layers, classes, kernel_size=3, zero_init_residual=False, groups=1, width_per_group=64, stride_type=None, norm_layer=None)

Bases: ResNet2D

A version of ResNet that uses bottleneck blocks

block

alias of Bottleneck

class ml4gw.nn.resnet.resnet_2d.ResNet2D(in_channels, layers, classes, kernel_size=3, zero_init_residual=False, groups=1, width_per_group=64, stride_type=None, norm_layer=None)

Bases: Module

2D ResNet architecture

Simple extension of ResNet with arbitrary kernel sizes to support the longer timeseries used in BBH detection.

Parameters:
  • in_channels (int) -- The number of channels in input tensor.

  • layers (List[int]) -- A list representing the number of residual blocks to include in each "layer" of the network. Total layers (e.g. 50 in ResNet50) is 2 + sum(layers) * factor, where factor is 2 for vanilla ResNet and 3 for BottleneckResNet.

  • kernel_size (int) -- The size of the convolutional kernel to use in all residual layers. _NOT_ the size of the input kernel to the network, which is determined at run-time.

  • zero_init_residual (bool) -- Flag indicating whether to initialize the weights of the batch-norm layer in each block to 0 so that residuals are initialized as identities. Can improve training results.

  • groups (int) -- Number of convolutional groups to use in all layers. Grouped convolutions induce local connections between feature maps at subsequent layers rather than global. Generally won't need this to be >1, and wil raise an error if >1 when using vanilla ResNet.

  • width_per_group (int) -- Base width of each of the feature map groups, which is scaled up by the typical expansion factor at each layer of the network. Meaningless for vanilla ResNet.

  • stride_type (Optional[List[Literal['stride', 'dilation']]]) -- Whether to achieve downsampling on the time axis by strided or dilated convolutions for each layer. If left as None, strided convolutions will be used at each layer. Otherwise, stride_type should be one element shorter than layers and indicate either stride or dilation for each layer after the first.

  • norm_groups -- The number of groups to use in GroupNorm layers throughout the model. If left as -1, the number of groups will be equal to the number of channels, making this equilavent to LayerNorm

block

alias of BasicBlock

forward(x)

Define the computation performed at every call.

Should be overridden by all subclasses. :rtype: Tensor

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

ml4gw.nn.resnet.resnet_2d.conv1(in_planes, out_planes, stride=1)

Kernel-size 1 convolution

Return type:

Conv2d

ml4gw.nn.resnet.resnet_2d.convN(in_planes, out_planes, kernel_size=3, stride=1, groups=1, dilation=1)

2d convolution with padding

Return type:

Conv2d

Module contents