Models

Bidirectional LSTM module.

Copied from https://github.com/jongwook/onsets-and-frames/blob/master/onsets_and_frames/lstm.py

class libdamp.models.lstm.BiLSTM(input_features: int, recurrent_features: int, bidirectional: bool = True)[source]

Bases: Module

inference_chunk_length: int = 512
forward(x: Tensor) Tensor[source]

Process input sequence through the LSTM.

During training, processes the entire sequence at once. During evaluation, processes the sequence in chunks of inference_chunk_length to support longer sequences while maintaining memory efficiency.

Parameters:

x (torch.Tensor) – Input tensor of shape (batch, sequence_length, input_features)

Returns:

Output of shape (batch, sequence_length, num_directions * recurrent_features) where num_directions is 2 for bidirectional LSTM, 1 for unidirectional

Return type:

torch.Tensor

ResNet implementation

All of this code is copied from torchvision.models.resnet. Only the input size of the first layer of the resnet

architecture is changes to allow for a 2D input

libdamp.models.resnet.conv3x3(in_planes: int, out_planes: int, stride: int = 1, groups: int = 1, dilation: int = 1) Conv2d[source]

Create a 3x3 convolutional layer with padding.

This is a standard building block for ResNet, applying convolution with kernel size 3x3 and padding to preserve spatial dimensions when stride=1.

Parameters:
  • in_planes (int) – Number of input channels

  • out_planes (int) – Number of output channels

  • stride (int) – Stride for the convolution (default: 1)

  • groups (int) – Number of groups for grouped convolution (default: 1)

  • dilation (int) – Dilation rate for dilated convolution (default: 1)

Returns:

A Conv2d module configured as a 3x3 convolution

Return type:

nn.Conv2d

libdamp.models.resnet.conv1x1(in_planes: int, out_planes: int, stride: int = 1) Conv2d[source]

Create a 1x1 convolutional layer.

Used in ResNet for bottleneck layers to change the number of channels with minimal computational cost. Can also be used for spatial downsampling when stride > 1.

Parameters:
  • in_planes (int) – Number of input channels

  • out_planes (int) – Number of output channels

  • stride (int) – Stride for the convolution (default: 1)

Returns:

A Conv2d module configured as a 1x1 convolution

Return type:

nn.Conv2d

class libdamp.models.resnet.BasicBlock(inplanes: int, planes: int, stride: int = 1, downsample=None, groups: int = 1, base_width: int = 64, dilation: int = 1, norm_layer=None)[source]

Bases: Module

ResNet BasicBlock with two 3x3 convolutions.

This is the building block for ResNet-18 and ResNet-34. It consists of two consecutive 3x3 convolutional layers with batch normalization and ReLU activation, plus a residual connection (skip connection) around the block.

expansion: int = 1

Factor by which the output channels are expanded relative to input (always 1 for BasicBlock).

forward(x: Tensor) Tensor[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

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 libdamp.models.resnet.ResNet(block, layers, input_channels=1, num_classes: int = 1000, zero_init_residual: bool = False, groups: int = 1, width_per_group: int = 64, replace_stride_with_dilation=None, norm_layer=None)[source]

Bases: Module

forward(x: Tensor) Tensor[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

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.

libdamp.models.resnet.resnet18(**kwargs) ResNet[source]

ResNet-18 from Deep Residual Learning for Image Recognition.

Parameters:

**kwargs – Parameters passed to the torchvision.models.resnet.ResNet base class. Please refer to the source code for more details about this class.