minerva.models.nets.time_series.ts2vec_classifier

Classes

TS2VecClassifier

A modular Lightning model wrapper for supervised learning tasks.

Module Contents

class minerva.models.nets.time_series.ts2vec_classifier.TS2VecClassifier(input_shape, ts_input_dims, ts_output_dims, ts_hidden_dims=64, ts_depth=10, hidden_dims=128, num_classes=6)[source]

Bases: minerva.models.nets.base.SimpleSupervisedModel

A modular Lightning model wrapper for supervised learning tasks.

This class enables the construction of supervised models by combining a backbone (feature extractor), an optional adapter, and a fully connected (FC) head. It provides a clean interface for setting up custom training, validation, and testing pipelines with pluggable loss functions, metrics, optimizers, and learning rate schedulers.

The architecture is structured as follows:

Backbone Model


v

Adapter (Optional)


(Flatten if needed)

v

Fully Connected Head


v

Loss Function

Training and validation steps comprise the following steps:

  1. Forward pass input through the backbone.

  2. Pass through adapter (if provided).

  3. Flatten the output (if flatten is True) before the FC head.

  4. Forward through the FC head.

  5. Compute loss with respect to targets.

  6. Backpropagate and update parameters.

  7. Compute metrics and log them.

  8. Return loss. train_loss, val_loss, and test_loss are always logged, along with any additional metrics specified in the train_metrics, val_metrics, and test_metrics dictionaries.

This wrapper is especially useful to quickly set up supervised models for various tasks, such as image classification, object detection, and segmentation. It is designed to be flexible and extensible, allowing users to easily swap out components like the backbone, adapter, and FC head as needed. The model is built with a focus on simplicity and modularity, making it easy to adapt to different use cases and requirements. The model is designed to be used with PyTorch Lightning and is compatible with its training loop.

Note: For more complex architectures that does not follow the above structure should not inherit from this class.

Note: Input batches must be tuples (input_tensor, target_tensor).

Initializes the supervised model with training components and configs.

Parameters

backbonetorch.nn.Module or LoadableModule

The backbone (feature extractor) model.

fctorch.nn.Module or LoadableModule

The fully connected head. Use nn.Identity() if not required.

loss_fntorch.nn.Module

Loss function to optimize during training.

adapterCallable, optional

Function to transform backbone outputs before feeding into fc.

learning_ratefloat, default=1e-3

Learning rate used for optimization.

flattenbool, default=True

If True, flattens backbone outputs before fc.

train_metricsdict, optional

TorchMetrics dictionary for training evaluation.

val_metricsdict, optional

TorchMetrics dictionary for validation evaluation.

test_metricsdict, optional

TorchMetrics dictionary for test evaluation.

freeze_backbonebool, default=False

If True, backbone parameters are frozen during training.

optimizer: type

Optimizer class to be instantiated. By default, it is set to torch.optim.Adam. Should be a subclass of torch.optim.Optimizer (e.g., torch.optim.SGD).

optimizer_kwargsdict, optional

Additional kwargs passed to the optimizer constructor.

lr_schedulertype, optional

Learning rate scheduler class to be instantiated. By default, it is set to None, which means no scheduler will be used. Should be a subclass of torch.optim.lr_scheduler.LRScheduler (e.g., torch.optim.lr_scheduler.StepLR).

lr_scheduler_kwargsdict, optional

Additional kwargs passed to the scheduler constructor.

_calculate_fc_input_features(backbone, input_shape)[source]

Run a single forward pass with a random input to get the number of features after the convolutional layers.

Parameters

backbonetorch.nn.Module

The backbone of the network

input_shapeTuple[int, int, int]

The input shape of the network.

Returns

int

The number of features after the convolutional layers.

Parameters:
  • backbone (torch.nn.Module)

  • input_shape (Tuple[int, int])

Return type:

int

fc_input_features
Parameters:
  • input_shape (Tuple[int, int])

  • ts_input_dims (int)

  • ts_output_dims (int)

  • ts_hidden_dims (int)

  • ts_depth (int)

  • hidden_dims (int)

  • num_classes (int)