robustML.advertrain package

Subpackages

Submodules

robustML.advertrain.constants module

robustML.advertrain.metrics module

class robustML.advertrain.metrics.Metrics[source]

Bases: object

Class to track performance metrics for binary classification tasks.

This class tracks true positives, true negatives, false positives, false negatives, and cumulative loss across batches. It calculates metrics like accuracy, precision, recall, and F1-score.

display(title: str) None[source]

Display the calculated metrics with a title.

Parameters:

title (str) – The title for the metrics display.

display_table(title: str) None[source]

Display the metrics in a tabular format with a title.

Parameters:

title (str) – The title for the table.

get_metrics() tuple[source]

Calculate and return key performance metrics.

Returns:

Tuple containing accuracy, loss, precision, recall, and F1-score.

Return type:

tuple

load_metrics(checkpoint: str) Dict[str, Any][source]

Load metrics from a JSON file located at <checkpoint>/metrics.json.

This function reads the β€˜metrics.json’ file from the specified checkpoint directory and returns the contents as a dictionary.

Parameters:

checkpoint (str) – The directory path from where the metrics.json file will be loaded.

Returns:

A dictionary containing the loaded metrics.

Return type:

Dict[str, Any]

reset_metrics() None[source]

Reset all metrics to zero.

save_metrics(metrics: Dict[str, Any], checkpoint: str) None[source]

Save metrics in a JSON file located at <checkpoint>/metrics.json.

This function serializes the provided metrics dictionary into JSON format and writes it to a file named β€˜metrics.json’ in the specified checkpoint directory.

Parameters:
  • metrics (Dict[str, Any]) – A dictionary containing metric names as keys and their corresponding values.

  • checkpoint (str) – The directory path where the metrics.json file will be saved.

update(x: Tensor, y: Tensor, pred: Tensor, loss: Tensor) None[source]

Update metrics based on inputs, ground truth, model predictions, and loss.

Parameters:
  • x (Tensor) – Input tensor

  • y (Tensor) – target labels

  • pred (Tensor) – Model predictions

  • loss (Tensor) – Batch loss

robustML.advertrain.models module

class robustML.advertrain.models.ConvNet(device: device, p: float = 0.2)[source]

Bases: Module

Convolutional Neural Network with dropout layers, designed for processing images of size 64x128.

This network includes a normalization layer, several convolutional layers with ReLU activation and max pooling, followed by fully connected layers with dropout for regularization. It is suited for tasks like image classification where dropout can help reduce overfitting.

norm

Normalization layer to preprocess the input images.

Type:

Normalize

conv1, conv2_1, conv3_1, conv4_1

Convolutional layers for feature extraction.

Type:

nn.Conv2d

pooling

Max pooling layer to reduce spatial dimensions.

Type:

nn.MaxPool2d

activation

Activation function.

Type:

nn.ReLU

dropout

Dropout layer for regularization.

Type:

nn.Dropout

linear1, linear2, linear3

Fully connected layers for classification.

Type:

nn.Linear

forward(x: Tensor) Tensor[source]

Defines the forward pass of the ConvNetDropout.

The input tensor is processed through normalization, convolutional layers, pooling layers, dropout layers, and fully connected layers sequentially to produce the output tensor.

Parameters:

x (Tensor) – Input tensor of shape (batch_size, 3, 64, 128).

Returns:

Output tensor after processing through the network.

Return type:

Tensor

class robustML.advertrain.models.ConvNetDropblock(device: device, p: float = 0.2, drop_prob: float = 0.0, n_steps: int = 10)[source]

Bases: Module

Convolutional Neural Network with DropBlock regularization, designed for processing images of size 64x128.

This network includes a normalization layer, several convolutional layers with ReLU activation and max pooling, followed by fully connected layers with dropout and DropBlock for regularization. It is suited for tasks like image classification where advanced regularization techniques can help reduce overfitting.

norm

Normalization layer to preprocess the input images.

Type:

Normalize

conv1, conv2_1, conv3_1, conv4_1

Convolutional layers for feature extraction.

Type:

nn.Conv2d

pooling

Max pooling layer to reduce spatial dimensions.

Type:

nn.MaxPool2d

activation

Activation function.

Type:

nn.ReLU

dropout

Dropout layer for regularization.

Type:

nn.Dropout

dropblock

DropBlock layer for structured dropout.

Type:

DropBlock2d

linear1, linear2, linear3

Fully connected layers for classification.

Type:

nn.Linear

forward(x: Tensor) Tensor[source]

Defines the forward pass of the ConvNetDropblock.

The input tensor is processed through normalization, convolutional layers, pooling layers, DropBlock layers, dropout layers, and fully connected layers sequentially to produce the output tensor.

Parameters:

x (Tensor) – Input tensor of shape.

Returns:

Output tensor after processing through the network.

Return type:

Tensor

class robustML.advertrain.models.Normalize(mean: Tensor, std: Tensor, device: device)[source]

Bases: Module

forward(x: Tensor) Tensor[source]

Normalize the input tensor.

Applies the normalization operation on the input tensor using the mean and standard deviation provided during initialization.

Parameters:

x (Tensor) – The input tensor to be normalized.

Returns:

The normalized tensor.

Return type:

Tensor

class robustML.advertrain.models.ResNet(device: device, p: float = 0.2)[source]

Bases: Module

A custom implementation of a Residual Network (ResNet) for processing images.

This network consists of multiple convolutional layers, each followed by batch normalization, and some layers include dropout for regularization. The network uses skip connections similar to a ResNet architecture, adding the output of one layer to another layer.

forward(inp: Tensor) Tensor[source]

Defines the forward pass of the ResNet.

The input tensor is processed through a series of convolutional layers with skip connections, batch normalization, and dropout, followed by fully connected layers to produce the output tensor.

Parameters:
  • inp (Tensor) – Input tensor of appropriate shape, typically matching the input size of the first

  • layer. (convolutional)

Returns:

Output tensor after processing through the network.

Return type:

Tensor

class robustML.advertrain.models.ResNetDropblock(device: device, p: float = 0.2, drop_prob: float = 0.0)[source]

Bases: Module

A custom implementation of a Residual Network (ResNet) for processing images.

This network consists of multiple convolutional layers, each followed by batch normalization, and some layers include dropout for regularization. The network uses skip connections similar to a ResNet architecture, adding the output of one layer to another layer.

forward(inp: Tensor) Tensor[source]

Defines the forward pass of the ResNet.

The input tensor is processed through a series of convolutional layers with skip connections, batch normalization, and dropout, followed by fully connected layers to produce the output tensor.

Parameters:
  • inp (Tensor) – Input tensor of appropriate shape, typically matching the input size of the first

  • layer. (convolutional)

Returns:

Output tensor after processing through the network.

Return type:

Tensor

robustML.advertrain.transforms module

class robustML.advertrain.transforms.DataTransformations(train_prob: float = 0.5)[source]

Bases: object

Class to create and return training and test data transformations.

This class encapsulates the creation of data transformations used in training and testing. It provides methods to get composed series of transformations for both scenarios.

get_test_transforms() Compose[source]

Creates and returns a series of test data transformations.

Returns:

A composed series of transformations for test data.

Return type:

Compose

get_train_transforms() Compose[source]

Creates and returns a series of training data transformations.

Returns:

A composed series of transformations for training data.

Return type:

Compose

Module contents