qumphy.models.basic_conv1d module

File: qumphy/models/basic_conv1d.py Project: 22HLT01 QUMPHY Contact: oskar.pfeffer@ptb.de Gitlab: https://gitlab.com/qumphy Description: Convolutional Neural Network.

class qumphy.models.basic_conv1d.AdaptiveConcatPool1d(*args: Any, **kwargs: Any)[source]

Bases: Module

Concatenate adaptive average pooling and adaptive max pooling.

Parameters:

sz (int) – Output size of each pooling layer. If None, the output size is 1.

forward(x)[source]

Run a forward pass through the adaptive concatenated pooling layer.

Parameters:

x (torch.Tensor) – Input tensor.

Returns:

Tensor obtained by concatenating max-pooled and average-pooled features along the channel dimension.

Return type:

torch.Tensor

class qumphy.models.basic_conv1d.Basic_Conv1d(*args: Any, **kwargs: Any)[source]

Bases: Sequential

Basic configurable 1D convolutional neural network.

Parameters:
  • filters (list) – List containing the number of filters for each convolutional layer.

  • kernel_size (int or list) – Kernel size used for the convolutional layers.

  • stride (int) – Stride used for the convolutional layers.

  • dilation (int) – Dilation factor used for the convolutional layers.

  • pool (int) – Max pooling kernel size. If 0, no max pooling is applied.

  • pool_stride (int) – Stride used for max pooling.

  • squeeze_excite_reduction (int) – Reduction factor for squeeze-and-excitation blocks. If 0, no squeeze-and-excitation block is applied.

  • num_classes (int) – Number of output classes or output values.

  • input_channels (int) – Number of input channels.

  • act (str) – Activation function used in the convolutional blocks.

  • bn (bool) – If True, use batch normalization in convolutional blocks.

  • headless (bool) – If True, remove the final prediction head and return extracted features.

  • split_first_layer (bool) – If True, split the first convolutional layer into two convolutional steps.

  • drop_p (float) – Dropout probability used in convolutional blocks.

  • lin_ftrs_head (list) – Hidden layer sizes used in the model head.

  • ps_head (float or Iterable) – Dropout probability or probabilities used in the model head.

  • bn_final_head (bool) – Indicates whether final batch normalization should be used in the head.

  • bn_head (bool) – If True, use batch normalization in the model head.

  • act_head (str) – Activation function used in the model head.

  • concat_pooling (bool) – If True, use concatenated adaptive average and max pooling in the head.

get_layer_groups()[source]

Get layer groups of the model.

Returns:

Tuple containing selected convolutional layers and the model head.

Return type:

tuple

get_output_layer()[source]

Get the output layer of the model.

Returns:

Output layer if the model is not headless. Otherwise, None.

Return type:

nn.Module or None

set_output_layer(x)[source]

Set the output layer of the model.

Parameters:

x (nn.Module) – New output layer.

Returns:

The function modifies the output layer in place.

Return type:

None

class qumphy.models.basic_conv1d.Flatten(full=False)[source]

Bases: Module

Flatten an input tensor to a single dimension.

Parameters:

full (bool) – If True, flatten the complete tensor into a rank-1 tensor. If False, keep the batch dimension and flatten all remaining dimensions.

forward(x)[source]

Run a forward pass through the flatten layer.

Parameters:

x (torch.Tensor) – Input tensor to be flattened.

Returns:

Flattened tensor. If full is True, the output has shape (-1,). Otherwise, the output has shape (batch_size, -1).

Return type:

torch.Tensor

class qumphy.models.basic_conv1d.LambdaLayer(*args: Any, **kwargs: Any)[source]

Bases: Module

Layer that applies a given lambda function to the input.

Parameters:

lambd (callable) – Function applied to the input tensor.

forward(x)[source]

Run a forward pass through the lambda layer.

Parameters:

x (torch.Tensor) – Input tensor.

Returns:

Output tensor after applying the lambda function.

Return type:

torch.Tensor

class qumphy.models.basic_conv1d.SqueezeExcite1d(*args: Any, **kwargs: Any)[source]

Bases: Module

Squeeze-and-excitation block for 1D inputs.

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

  • reduction (int) – Reduction factor used to compute the number of hidden channels.

forward(x)[source]

Run a forward pass through the squeeze-and-excitation block.

Parameters:

x (torch.Tensor) – Input tensor of shape (batch_size, channels, sequence_length).

Returns:

Reweighted tensor with the same shape as the input.

Return type:

torch.Tensor

qumphy.models.basic_conv1d.basic1d(filters=[128, 128, 128, 128, 128], kernel_size=3, stride=2, dilation=1, pool=0, pool_stride=1, squeeze_excite_reduction=0, num_classes=2, input_channels=8, act='relu', bn=True, headless=False, drop_p=0.0, lin_ftrs_head=None, ps_head=0.5, bn_final_head=False, bn_head=True, act_head='relu', concat_pooling=True, **kwargs)[source]

Create a basic configurable 1D convolutional network.

Parameters:
  • filters (list) – List containing the number of filters for each convolutional layer.

  • kernel_size (int or list) – Kernel size used for the convolutional layers.

  • stride (int) – Stride used for the convolutional layers.

  • dilation (int) – Dilation factor used for the convolutional layers.

  • pool (int) – Max pooling kernel size. If 0, no max pooling is applied.

  • pool_stride (int) – Stride used for max pooling.

  • squeeze_excite_reduction (int) – Reduction factor for squeeze-and-excitation blocks. If 0, no squeeze-and-excitation block is applied.

  • num_classes (int) – Number of output classes or output values.

  • input_channels (int) – Number of input channels.

  • act (str) – Activation function used in the convolutional blocks.

  • bn (bool) – If True, use batch normalization in convolutional blocks.

  • headless (bool) – If True, remove the final prediction head and return extracted features.

  • drop_p (float) – Dropout probability used in convolutional blocks.

  • lin_ftrs_head (list) – Hidden layer sizes used in the model head.

  • ps_head (float or Iterable) – Dropout probability or probabilities used in the model head.

  • bn_final_head (bool) – Indicates whether final batch normalization should be used in the head.

  • bn_head (bool) – If True, use batch normalization in the model head.

  • act_head (str) – Activation function used in the model head.

  • concat_pooling (bool) – If True, use concatenated adaptive average and max pooling in the head.

  • **kwargs – Additional keyword arguments.

Returns:

Basic configurable 1D convolutional model.

Return type:

Basic_Conv1d

qumphy.models.basic_conv1d.bn_drop_lin(n_in, n_out, bn=True, p=0.0, actn=None, layer_norm=False, permute=False)[source]

Create batch normalization, dropout, linear, and activation layers.

Parameters:
  • n_in (int) – Number of input features.

  • n_out (int) – Number of output features.

  • bn (bool) – If True, add a normalization layer before the linear layer.

  • p (float) – Dropout probability.

  • actn (nn.Module) – Activation function added after the linear layer.

  • layer_norm (bool) – If True, use layer normalization instead of batch normalization.

  • permute (bool) – If True, permute the input dimensions before and after normalization. This is useful for inputs of shape (batch_size, sequence, features).

Returns:

List of PyTorch layers.

Return type:

list

qumphy.models.basic_conv1d.create_head1d(nf, nc, lin_ftrs=None, ps=0.5, bn=True, act='relu', concat_pooling=True)[source]

Create a 1D model classification or regression head.

Parameters:
  • nf (int) – Number of input features.

  • nc (int) – Number of output classes or output values.

  • lin_ftrs (list) – List of hidden layer sizes used in the head.

  • ps (float or Iterable) – Dropout probability or list of dropout probabilities.

  • bn (bool) – If True, use batch normalization in the linear blocks.

  • act (str) – Activation function used between linear layers.

  • concat_pooling (bool) – If True, use concatenated adaptive average and max pooling. If False, use adaptive average pooling only.

Returns:

Sequential model head.

Return type:

nn.Sequential

qumphy.models.basic_conv1d.fcn(filters=[128, 128, 128, 128, 128], num_classes=2, input_channels=8, **kwargs)[source]

Create a fully convolutional 1D network.

Parameters:
  • filters (list) – List containing the number of filters for the convolutional layers.

  • num_classes (int) – Number of output classes or output values.

  • input_channels (int) – Number of input channels.

  • **kwargs – Additional keyword arguments.

Returns:

Fully convolutional 1D model.

Return type:

Basic_Conv1d

qumphy.models.basic_conv1d.fcn_wang(num_classes=2, input_channels=8, lin_ftrs_head=None, ps_head=0.5, bn_final_head=False, bn_head=True, act_head='relu', concat_pooling=True, **kwargs)[source]

Create a Wang-style fully convolutional 1D network.

Parameters:
  • num_classes (int) – Number of output classes or output values.

  • input_channels (int) – Number of input channels.

  • lin_ftrs_head (list) – Hidden layer sizes used in the model head.

  • ps_head (float or Iterable) – Dropout probability or probabilities used in the model head.

  • bn_final_head (bool) – Indicates whether final batch normalization should be used in the head.

  • bn_head (bool) – If True, use batch normalization in the model head.

  • act_head (str) – Activation function used in the model head.

  • concat_pooling (bool) – If True, use concatenated adaptive average and max pooling in the head.

  • **kwargs – Additional keyword arguments.

Returns:

Wang-style fully convolutional 1D model.

Return type:

Basic_Conv1d

qumphy.models.basic_conv1d.schirrmeister(num_classes=2, input_channels=8, kernel_size=10, lin_ftrs_head=None, ps_head=0.5, bn_final_head=False, bn_head=True, act_head='relu', concat_pooling=True, **kwargs)[source]

Create a Schirrmeister-style 1D convolutional network.

Parameters:
  • num_classes (int) – Number of output classes or output values.

  • input_channels (int) – Number of input channels.

  • kernel_size (int) – Kernel size used in the convolutional layers.

  • lin_ftrs_head (list) – Hidden layer sizes used in the model head.

  • ps_head (float or Iterable) – Dropout probability or probabilities used in the model head.

  • bn_final_head (bool) – Indicates whether final batch normalization should be used in the head.

  • bn_head (bool) – If True, use batch normalization in the model head.

  • act_head (str) – Activation function used in the model head.

  • concat_pooling (bool) – If True, use concatenated adaptive average and max pooling in the head.

  • **kwargs – Additional keyword arguments.

Returns:

Schirrmeister-style 1D convolutional model.

Return type:

Basic_Conv1d

qumphy.models.basic_conv1d.sen(filters=[128, 128, 128, 128, 128], num_classes=2, input_channels=8, kernel_size=3, squeeze_excite_reduction=16, drop_p=0.0, lin_ftrs_head=None, ps_head=0.5, bn_final_head=False, bn_head=True, act_head='relu', concat_pooling=True, **kwargs)[source]

Create a squeeze-and-excitation 1D convolutional network.

Parameters:
  • filters (list) – List containing the number of filters for each convolutional layer.

  • num_classes (int) – Number of output classes or output values.

  • input_channels (int) – Number of input channels.

  • kernel_size (int or list) – Kernel size used in the convolutional layers.

  • squeeze_excite_reduction (int) – Reduction factor used in squeeze-and-excitation blocks.

  • drop_p (float) – Dropout probability used in convolutional blocks.

  • lin_ftrs_head (list) – Hidden layer sizes used in the model head.

  • ps_head (float or Iterable) – Dropout probability or probabilities used in the model head.

  • bn_final_head (bool) – Indicates whether final batch normalization should be used in the head.

  • bn_head (bool) – If True, use batch normalization in the model head.

  • act_head (str) – Activation function used in the model head.

  • concat_pooling (bool) – If True, use concatenated adaptive average and max pooling in the head.

  • **kwargs – Additional keyword arguments.

Returns:

Squeeze-and-excitation 1D convolutional model.

Return type:

Basic_Conv1d

qumphy.models.basic_conv1d.weight_init(m)[source]

Initialize weights of supported model layers.

Parameters:

m (nn.Module) – Module whose weights should be initialized.

Returns:

The function modifies the module weights in place.

Return type:

None