AdversarialTensors package#

Subpackages#

Submodules#

AdversarialTensors.LightningModel module#

class AdversarialTensors.LightningModel.LightningModel(model_name, lr=0.001, batch_size=1, num_batches=1, num_classes=10, adv_settings=None, normalize_settings=None, denoiser_settings=None, *args, **kwargs)[source]#

Bases: LightningModule

PyTorch Lightning Module for training various ResNet architectures and their Wide ResNet and Original variants.

model_name#

The name of the model architecture to use.

Type:

str

lr#

Learning rate.

Type:

float, default=1e-3

batch_size#

Batch size for training and evaluation.

Type:

int, default=1

num_batches#

Number of batches per epoch.

Type:

int, default=1

num_classes#

Number of output classes.

Type:

int, default=10

adv_settings#

Settings for adversarial attack generation. If None, no attacks are used.

Type:

dict, optional

normalize_settings#

Settings for input normalization. If None, no normalization is applied.

Type:

dict, optional

denoiser_settings#

Settings for denoising the inputs. If None, no denoising is applied.

Type:

dict, optional

...
forward(x: torch.Tensor):

Forward pass through the model.

on_train_start():

Actions to perform when training starts.

generate_attack(batch: Any):

Generate adversarial attack samples.

model_step(batch: Any):

Defines a single step during model training.

training_step(batch: Any, batch_idx: int):

Defines a single step during training loop.

on_train_epoch_end():

Actions to perform at the end of each training epoch.

validation_step(batch: Any, batch_idx: int):

Defines a single step during validation loop.

on_validation_epoch_end():

Actions to perform at the end of each validation epoch.

test_step(batch: Any, batch_idx: int):

Defines a single step during testing loop.

on_test_epoch_end():

Actions to perform at the end of each test epoch.

resnet_configure_optimizers():

Configure optimizers and learning rate schedules for ResNet models.

wide_resnet_configure_optimizers():

Configure optimizers and learning rate schedules for Wide ResNet models.

configure_optimizers():

General configuration of optimizers based on model types.

Notes

This class integrates with PyTorch Lightning to enable training, validation, and testing loops, and also allows for adversarial attack generation, normalization, and denoising.

Example

model = LightningModel('resnet18', lr=0.001) trainer = Trainer(max_epochs=10) trainer.fit(model)

configure_optimizers()[source]#

Configure optimizers and learning rate schedulers based on the model architecture.

Examples and more info: https://lightning.ai/docs/pytorch/latest/common/lightning_module.html#configure-optimizers

Returns:

The configuration for the optimizer and learning rate scheduler.

Return type:

dict or list

forward(x: Tensor)[source]#

Perform forward pass through the model.

Parameters:

x (torch.Tensor) -- Input tensor.

Returns:

Output tensor.

Return type:

torch.Tensor

generate_attack(batch)[source]#

Generate adversarial tests for a given batch.

Parameters:

batch (tuple) -- A tuple containing input tensor x and target tensor y.

Returns:

A tuple containing adversarial tests adv_x and the target tensor y.

Return type:

tuple

model_step(batch: Any)[source]#

Forward pass through the model and compute loss and predictions.

Parameters:

batch (Any) -- The input batch data.

Returns:

A tuple containing the loss tensor, predicted labels, and ground truth labels.

Return type:

tuple

on_test_epoch_end()[source]#

Operations to perform at the end of each test epoch.

Currently a placeholder with no actions.

on_train_epoch_end()[source]#

Operations to perform at the end of each training epoch.

Currently a placeholder with no actions.

on_train_start()[source]#

Reset all validation metrics before the training starts.

This is necessary because PyTorch Lightning runs validation sanity checks before the actual training, and we don't want these checks to affect our metrics.

on_validation_epoch_end()[source]#

Operations to perform at the end of each validation epoch.

Computes and logs the best validation accuracy achieved so far.

resnet_configure_optimizers()[source]#

Configure optimizers and learning rate schedulers for ResNet architectures.

Returns:

A dictionary containing the optimizer and learning rate scheduler.

Return type:

dict

test_step(batch: Any, batch_idx: int)[source]#

Perform a single test step.

Parameters:
  • batch (Any) -- The input batch data.

  • batch_idx (int) -- The index of the current batch.

training_step(batch: Any, batch_idx: int)[source]#

Perform a single training step.

Parameters:
  • batch (Any) -- The input batch data.

  • batch_idx (int) -- The index of the current batch.

Returns:

The training loss.

Return type:

torch.Tensor

validation_step(batch: Any, batch_idx: int)[source]#

Perform a single validation step.

Parameters:
  • batch (Any) -- The input batch data.

  • batch_idx (int) -- The index of the current batch.

wide_resnet_configure_optimizers()[source]#

Configure optimizers and learning rate schedulers for Wide ResNet architectures.

Returns:

A dictionary containing the optimizer and learning rate scheduler.

Return type:

dict

AdversarialTensors.LightningModel.learning_rate_cifar10(init, epoch)[source]#

Computes the learning rate for CIFAR-10 training based on the initial learning rate and the current epoch.

Parameters:
  • init (float) -- The initial learning rate at epoch 0.

  • epoch (int) -- The current training epoch.

Returns:

The adjusted learning rate for the current epoch.

Return type:

float

Notes

This function computes the learning rate based on a piecewise schedule. It divides the training into 4 phases and scales the learning rate by 0.2 raised to a factor. The factor is determined by the current epoch:

  • 0-60: factor = 0

  • 61-120: factor = 1

  • 121-160: factor = 2

  • 161-∞: factor = 3

AdversarialTensors.TensorDecomposition module#

class AdversarialTensors.TensorDecomposition.TensorDecomposition(method='tucker', params={'factors': None, 'init': 'svd', 'n_iter_max': 1000, 'svd': 'truncated_svd', 'tol': 1e-05}, verbose=True, data_mode='batch')[source]#

Bases: Module

A class to perform tensor decomposition using various methods such as CPD, Tucker, TT, etc.

Parameters:
  • method (str, optional) -- The method to use for decomposition. Default is 'tucker'.

  • params (dict, optional) -- Dictionary of parameters for the decomposition method. Default is {'factors': None, 'init': 'svd', 'tol': 1e-5, 'n_iter_max': 1000}.

  • verbose (bool, optional) -- Whether or not to print status messages during decomposition. Default is True.

  • data_mode (str, optional) -- Whether the tensor is in batch mode ('batch') or single mode ('single'). Default is 'batch'.

method#

The decomposition method.

Type:

str

params#

Dictionary of decomposition method parameters.

Type:

dict

X#

The input tensor.

Type:

torch.Tensor

ranks#

Desired ranks for decomposition.

Type:

tuple or int

reconstruct#

Whether to reconstruct tensor after decomposition.

Type:

bool

verbose#

Verbose flag.

Type:

bool

data_mode#

Data mode ('batch' or 'single').

Type:

str

Initialize the TensorDecomposition class.

Parameters:
  • method (str, optional) -- The method to use for decomposition. Default is 'tucker'.

  • params (dict, optional) -- Dictionary of parameters for the decomposition method. Default is {'factors': None, 'init': 'svd', 'tol': 1e-5, 'n_iter_max': 1000}.

  • verbose (bool, optional) -- Whether or not to print status messages during decomposition. Default is True.

  • data_mode (str, optional) -- Whether the tensor is in batch mode ('batch') or single mode ('single'). Default is 'batch'.

NMF()[source]#

Performs NMF.

Returns:

A tuple containing: - factors (list): a list of factor matrices - X_recon (torch.Tensor): the reconstructed tensor (if self.reconstruct=True) - err (float): the relative error between X and X_recon (if self.reconstruct=True)

Return type:

tuple

NNSVD()[source]#

Performs NNSVD.

Returns:

A tuple containing: - factors (list): a list of factor matrices - X_recon (torch.Tensor): the reconstructed tensor (if self.reconstruct=True) - err (float): the relative error between X and X_recon (if self.reconstruct=True)

Return type:

tuple

SVD()[source]#

Performs SVD.

Returns:

A tuple containing: - factors (list): a list of factor matrices - X_recon (torch.Tensor): the reconstructed tensor (if self.reconstruct=True) - err (float): the relative error between X and X_recon (if self.reconstruct=True)

Return type:

tuple

cpd()[source]#

Performs CPD decomposition using tensorly library.

Returns:

A tuple containing: - core (torch.Tensor): the core tensor - factors (list): a list of factor matrices - X_recon (torch.Tensor): the reconstructed tensor (if self.reconstruct=True) - err (float): the relative error between X and X_recon (if self.reconstruct=True)

Return type:

tuple

fit()[source]#

Fits the decomposition model to the input tensor X using various decomposition methods supported by tensorly.

Parameters:

X (torch.Tensor) -- The input tensor to fit.

Returns:

A tuple containing: - X_recon (torch.Tensor): the reconstructed tensor - err (float): the relative error between X and X_recon

Return type:

tuple

forward(X, ranks)[source]#

Performs forward pass through the model for both batch and single mode.

Parameters:
  • X (torch.Tensor) -- Input tensor of shape (batch_size, num_items).

  • ranks (list) -- List of rank values for each tensor in the CP decomposition.

Returns:

A tuple containing: - X_recon (torch.Tensor): the reconstructed tensor of shape (batch_size, num_items) - err (float): the reconstruction error between input tensor and reconstructed tensor

Return type:

tuple

ncpd()[source]#

Performs Non-negative CPD decomposition using tensorly library.

Returns:

A tuple containing: - core (torch.Tensor): the core tensor - factors (list): a list of factor matrices - X_recon (torch.Tensor): the reconstructed tensor (if self.reconstruct=True) - err (float): the relative error between X and X_recon (if self.reconstruct=True)

Return type:

tuple

ntucker()[source]#

Performs Non-negative Tucker decomposition using the tensorly library.

Returns:

A tuple containing: - core (torch.Tensor): the core tensor - factors (list): a list of factor matrices - X_recon (torch.Tensor): the reconstructed tensor (if self.reconstruct=True) - err (float): the relative error between X and X_recon (if self.reconstruct=True)

Return type:

tuple

relative_error(data, data_recon)[source]#

Calculates the relative error between two tensors data and data_recon.

Parameters:
  • data (torch.Tensor) -- The original tensor.

  • data_recon (torch.Tensor) -- The reconstructed tensor.

Returns:

The relative error between data and data_recon.

Return type:

float

training: bool#
tt()[source]#

Performs Tensor-train decomposition using tensorly library.

Returns:

A tuple containing: - factors (list): a list of factor matrices - X_recon (torch.Tensor): the reconstructed tensor (if self.reconstruct=True) - err (float): the relative error between X and X_recon (if self.reconstruct=True)

Return type:

tuple

ttm(X, v, mode, transpose=False)[source]#

Performs tensor-time-matrix multiplication along a given mode using the tensorly library.

Parameters:
  • X (torch.Tensor) -- The tensor to be multiplied.

  • v (torch.Tensor or list of torch.Tensor) -- The matrix or matrices to multiply with.

  • mode (int or list of int) -- The modes in which to multiply the tensors.

  • transpose (bool, optional) -- Whether or not to transpose the matrix before multiplication.

Returns:

The tensor after performing the tensor-time-matrix multiplication.

Return type:

torch.Tensor

tucker()[source]#

AdversarialTensors.adv_attacks module#

We will utilize the models and attacks from https://robustbench.github.io/ pip install -q foolbox pip install autoattack

class AdversarialTensors.adv_attacks.Attacks(model=None, attack='fgsm', eps=0.03137254901960784, norm='Linf', device=0, bounds=(0, 1))[source]#

Bases: object

Initializes the Attacks class.

Parameters:
  • model (torch.nn.Module) -- A PyTorch model that takes image tensors as input.

  • attack (str, optional) -- The type of attack to perform. Default is 'fgsm'.

  • eps (float, optional) -- The maximum allowed perturbation to the image. Default is 8/255.

  • norm (str, optional) -- The Lp-norm used in the attack. Default is 'Linf'.

  • device (int, optional) -- The device to perform the attack on. Default is 0.

  • bounds (tuple, optional) -- The minimum and maximum values for the image tensor. Default is (0, 1).

generate(X, y)[source]#

Executes the attack on the given inputs and labels.

Parameters:
  • X (torch.Tensor) -- Input images.

  • y (torch.Tensor) -- True labels.

Returns:

x_adv_resnet -- Adversarially perturbed images.

Return type:

torch.Tensor

init_attack()[source]#

Initializes the attack based on the provided attack type.

AdversarialTensors.attacks module#

We will utilize the models and attacks from https://robustbench.github.io/ pip install -q foolbox pip install autoattack

class AdversarialTensors.attacks.Attacks(model=None, attack='all', attack_params={'eps': 0.03137254901960784, 'exp': 'all', 'log_dir': 'autoattack/', 'norm': 2, 'seed': 99, 'version': 'standard'}, device=0)[source]#

Bases: Module

Initializes the Attacks class.

Parameters:
  • model (torch.nn.Module) -- A PyTorch model that takes image tensors as input.

  • attack (str, optional) -- The type of attack to perform. Options include 'all', 'fgsm', 'pgd', 'bim', 'uniform', 'deepfool', 'autoattack'. Default is 'all'.

  • attack_params (dict, optional) -- Additional parameters for the attack method. Includes keys for 'norm', 'eps', 'version', 'log_dir', 'seed', and 'exp'.

  • device (int, optional) -- The device to perform the attack on. Default is 0.

Initializes internal Module state, shared by both nn.Module and ScriptModule.

attack_eval_all(X, y, batch=128, verbose=True)[source]#

Evaluate the adversarial robustness of the model against all specified attacks on a given dataset.

Parameters:
  • X (numpy.ndarray) -- Input data of shape (N, C, H, W).

  • y (numpy.ndarray) -- Ground-truth labels for the input data of shape (N,).

  • batch (int, optional) -- Batch size for generating adversarial tests. Default is 128.

  • verbose (bool, optional) -- Whether to print verbose output. Default is True.

Returns:

x_adv_resnet -- A list of adversarial tests generated for each attack method.

Return type:

list

attack_eval_dataloader(dataloader, batch=128, verbose=True)[source]#

Evaluate the adversarial robustness of the model on a given dataset using a PyTorch DataLoader.

Parameters:
  • dataloader (torch.utils.data.DataLoader) -- A PyTorch DataLoader object.

  • batch (int, optional) -- Batch size for generating adversarial tests. Default is 128.

  • verbose (bool, optional) -- Whether to print verbose output. Default is True.

Returns:

  • clean_acc (float) -- Clean accuracy of the model.

  • acc (float) -- Adversarial accuracy of the model.

forward(X, y)[source]#

Generates adversarial tests for a given batch of images.

Parameters:
  • X (torch.Tensor) -- Input images of shape (batch_size, C, H, W).

  • y (torch.Tensor) -- Ground-truth labels of shape (batch_size,).

Returns:

x_adv_resnet -- Adversarial tests generated for each attack method.

Return type:

torch.Tensor

init_attack()[source]#

Initializes the attack model based on the chosen type of attack.

training: bool#

AdversarialTensors.datasets module#

class AdversarialTensors.datasets.DatasetLoader(name='cifar10', params={'batch_size': 64, 'nfolds': 1, 'normalize': True, 'num_workers': 16, 'shuffle': True})[source]#

Bases: object

A utility class to load different types of datasets for machine learning experiments.

...

name#

The name of the dataset to be loaded

Type:

str

params#

A dictionary containing hyperparameters for the dataloader

Type:

dict

preprocess_data(mean, std):

Applies standard preprocessing transformations to the dataset.

cifar10_dataload():

Loads the CIFAR-10 dataset.

cifar100_dataload():

Loads the CIFAR-100 dataset.

MNIST_dataload():

Loads the MNIST dataset.

FashionMNIST_dataload():

Loads the FashionMNIST dataset.

Imagenet_dataload():

Loads the ImageNet dataset.

fit():

Calls the appropriate dataload method based on the dataset name.

kfold_split(dataset, transform_train=None, transform_valid=None):

Splits the dataset into training and validation sets for k-fold cross-validation.

Constructor to initialize the DatasetLoader.

Parameters:
  • name (str) -- The name of the dataset to be loaded.

  • params (dict) -- A dictionary of hyperparameters for dataset loading.

FashionMNIST_dataload()[source]#

Loads the FashionMNIST dataset using the torchvision library and applies preprocessing transformations.

Returns:

  • trainloader (torch.utils.data.DataLoader) -- Dataloader for the training dataset.

  • testloader (torch.utils.data.DataLoader) -- Dataloader for the test dataset.

  • classes (tuple) -- Tuple containing class names.

  • mean (float) -- Mean for grayscale normalization.

  • std (float) -- Standard deviation for grayscale normalization.

Imagenet_dataload()[source]#

Loads the ImageNet dataset using the torchvision library and applies preprocessing transformations.

Returns:

  • trainloader (torch.utils.data.DataLoader) -- Dataloader for the training dataset.

  • testloader (torch.utils.data.DataLoader) -- Dataloader for the test dataset.

  • classes (tuple) -- Tuple containing class names.

  • mean (tuple) -- Tuple containing channel-wise mean for normalization.

  • std (tuple) -- Tuple containing channel-wise standard deviation for normalization.

MNIST_dataload()[source]#

Loads the MNIST dataset using the torchvision library and applies preprocessing transformations.

Returns:

  • trainloader (torch.utils.data.DataLoader) -- Dataloader for the training dataset.

  • testloader (torch.utils.data.DataLoader) -- Dataloader for the test dataset.

  • classes (tuple) -- Tuple containing class names.

  • mean (float) -- Mean for grayscale normalization.

  • std (float) -- Standard deviation for grayscale normalization.

cifar100_dataload()[source]#

Loads the CIFAR-100 dataset using the torchvision library and applies preprocessing transformations.

Returns:

  • trainloader (torch.utils.data.DataLoader) -- Dataloader for the training dataset.

  • testloader (torch.utils.data.DataLoader) -- Dataloader for the test dataset.

  • classes (tuple) -- Tuple containing class names.

  • mean (tuple) -- Tuple containing channel-wise mean for normalization.

  • std (tuple) -- Tuple containing channel-wise standard deviation for normalization.

cifar10_dataload()[source]#

Loads the CIFAR-10 dataset using the torchvision library and applies preprocessing transformations.

Returns:

  • trainloader (torch.utils.data.DataLoader) -- Dataloader for the training dataset.

  • testloader (torch.utils.data.DataLoader) -- Dataloader for the test dataset.

  • classes (tuple) -- Tuple containing class names.

  • mean (tuple) -- Tuple containing channel-wise mean for normalization.

  • std (tuple) -- Tuple containing channel-wise standard deviation for normalization.

fit()[source]#

Calls the appropriate dataload method based on the dataset name specified during initialization.

Return type:

A DataLoader object for the chosen dataset.

kfold_split(dataset, transform_train=None, transform_valid=None)[source]#

Splits the dataset into training and validation sets for k-fold cross-validation.

Parameters:
  • dataset (torch.utils.data.Dataset) -- The dataset to be split.

  • transform_train (torchvision.transforms.Compose, optional) -- Transformations to be applied on training dataset.

  • transform_valid (torchvision.transforms.Compose, optional) -- Transformations to be applied on validation dataset.

Returns:

all_loaders -- List of tuples where each tuple contains a DataLoader for the training set and a DataLoader for the validation set for each fold.

Return type:

list of tuple

preprocess_data(mean, std)[source]#

Applies standard preprocessing transformations to the dataset.

Parameters:
  • mean (tuple or float) -- The mean for each channel for normalization.

  • std (tuple or float) -- The standard deviation for each channel for normalization.

Returns:

  • transform_train (torchvision.transforms.Compose) -- The transformations to apply to the training set.

  • transform_test (torchvision.transforms.Compose) -- The transformations to apply to the test set.

AdversarialTensors.denoiser module#

class AdversarialTensors.denoiser.Denoiser(method='tucker', device='cuda', tensor_params={'factors': None, 'init': 'svd', 'max_iter': 1000, 'svd': 'truncated_svd', 'tol': 1e-05}, verbose=True, patch_params={'channels': 3, 'patch_size': 8, 'stride': 4}, data_mode='single', ranks=None)[source]#

Bases: Module

Initializes the Denoiser module.

Parameters:#

method: str, optional (default='tucker')

The tensor decomposition method to use. Valid options are 'tucker' and 'parafac'.

device: int, optional (default=0)

The GPU device to use for computations.

tensor_params: dict, optional (default={'factors': None, 'init': 'svd', 'tol': 1e-5, 'max_iter': 1000})

Dictionary containing parameters for the tensor decomposition method.

verbose: bool, optional (default=True)

Whether to print verbose output during computation.

patch_params: dict, optional (default={'patch_size': 8, 'stride': 4, 'channels': 3})

Dictionary containing parameters for patch transformation of data.

data_mode: str, optional (default='single')

The data mode, whether to use single or double precision arithmetic.

ranks: list, optional (default=None)

Rank for decomposition

forward(X, ranks=None, recon_err=False)[source]#

Computes the forward pass of the denoiser.

Parameters:#

X: tensor, shape (batch_size, channels, height, width)

The input tensor.

ranks: list of ints, length 5

The ranks to use for the tensor decomposition. The ranks should be for dimensions N x P x C x W x H.

Returns:#

X_recon: tensor, shape (batch_size, channels, height, width)

The output tensor, denoised by the tensor decomposition.

err: float

The error between the input and output tensors.

training: bool#

AdversarialTensors.model module#

class AdversarialTensors.model.FinalModel(model, denoiser=None)[source]#

Bases: Module

Initializes the FinalModel instance.

Parameters:
  • model (torch.nn.Module) -- The PyTorch model to be used for inference.

  • denoiser (Denoiser, optional) -- The denoising model to be applied to the input. Defaults to None.

forward(x, ranks=None, recon_err=False)[source]#

Forward pass through the model, with optional denoising.

Parameters:
  • x (torch.Tensor) -- Input tensor.

  • ranks (list or tuple, optional) -- Multi-ranks for the denoiser's Tucker decomposition. Defaults to None.

  • recon_err (bool, optional) -- Whether to return reconstruction error from the denoiser. Defaults to False.

Returns:

x -- The output tensor from the model.

Return type:

torch.Tensor

training: bool#

AdversarialTensors.my_progress_bar module#

Created on Tue Apr 25 19:35:02 2023

@author: cagri

class AdversarialTensors.my_progress_bar.MyProgressBar(refresh_rate: int = 1, process_position: int = 0)[source]#

Bases: TQDMProgressBar

A subclass of TQDMProgressBar to customize the progress bar behavior.

This class disables the progress bar during the validation phase.

init_validation_tqdm()[source]#

Initialize the TQDM progress bar for the validation phase.

Returns:

bar -- The TQDM progress bar instance for validation.

Return type:

tqdm.tqdm

AdversarialTensors.normalize module#

Created on Thu Mar 30 17:15:33 2023

@author: cagri

class AdversarialTensors.normalize.Normalize(mean, std)[source]#

Bases: Module

A PyTorch Module to normalize an input tensor using mean and standard deviation.

mean#

The mean values for each channel.

Type:

torch.Tensor

std#

The standard deviation values for each channel.

Type:

torch.Tensor

Initialize the Normalize module.

Parameters:
  • mean (list or tuple) -- The mean values for each channel.

  • std (list or tuple) -- The standard deviation values for each channel.

forward(x)[source]#

Executes the normalization on the given tensor.

Parameters:

x (torch.Tensor) -- Input tensor.

Returns:

x -- Normalized tensor.

Return type:

torch.Tensor

training: bool#

AdversarialTensors.patcher module#

class AdversarialTensors.patcher.Patcher(patch_size, stride=1, padding=0, dilation=1, channels=3)[source]#

Bases: object

A utility class to handle image patch extraction and merging.

Parameters:
  • patch_size (int) -- The size of the patch to be extracted.

  • stride (int, optional) -- The stride of the patch. Default is 1.

  • padding (int, optional) -- The amount of padding to add to the image. Default is 0.

  • dilation (int, optional) -- The spacing between the kernel points. Default is 1.

  • channels (int, optional) -- The number of channels in the image. Default is 3.

w#

The width of the image.

Type:

int

h#

The height of the image.

Type:

int

stds#

The standard deviations of the patches, if calculated.

Type:

tensor, optional

means#

The means of the patches, if calculated.

Type:

tensor, optional

extract_patches(data)[source]#

Extracts patches from the given image data.

Parameters:

data (torch.Tensor) -- The image data from which patches are to be extracted.

Returns:

The patches extracted from the image data.

Return type:

torch.Tensor

merge_patches(data_patches, mode='avg')[source]#

Merges the extracted patches to form an image.

Parameters:
  • data_patches (torch.Tensor) -- The patches to be merged.

  • mode (str, optional) -- The merging mode. It can be 'avg' or 'sum'. Default is 'avg'.

Returns:

The merged image.

Return type:

torch.Tensor

class AdversarialTensors.patcher.patch_transform(patch_size, stride=1, padding=0, dilation=1, channels=3)[source]#

Bases: object

A utility class for transforming images into patches and vice versa.

Parameters:
  • patch_size (int) -- The size of the patch.

  • stride (int, optional) -- The stride of the patch. Default is 1.

  • padding (int, optional) -- The padding of the patch. Default is 0.

  • dilation (int, optional) -- The dilation of the patch. Default is 1.

  • channels (int, optional) -- The number of channels in the image. Default is 3.

patcher#

A Patcher object configured with the given parameters.

Type:

Patcher

fit(x, mode='patch')[source]#

Extracts patches from the input image or merges patches into an image.

Parameters:
  • x (torch.Tensor) -- The input image to be transformed.

  • mode (str, optional) -- The mode of transformation. 'patch' to extract patches, 'merge' to merge patches. Default is 'patch'.

Returns:

A batch of patches if mode is 'patch', or a merged image if mode is 'merge'.

Return type:

torch.Tensor

AdversarialTensors.scheduler module#

class AdversarialTensors.scheduler.WarmupCosineLR(optimizer: Optimizer, warmup_epochs: int, max_epochs: int, warmup_start_lr: float = 1e-08, eta_min: float = 1e-08, last_epoch: int = -1)[source]#

Bases: LRScheduler

A PyTorch learning rate scheduler that combines linear warmup and cosine annealing schedules.

Parameters:
  • optimizer (torch.optim.Optimizer) -- The optimizer whose learning rates are to be scheduled.

  • warmup_epochs (int) -- The maximum number of epochs for the linear warmup phase.

  • max_epochs (int) -- The maximum number of epochs for the cosine annealing phase.

  • warmup_start_lr (float, optional) -- The initial learning rate for the linear warmup phase. Default is 1e-8.

  • eta_min (float, optional) -- The minimum learning rate during the cosine annealing phase. Default is 1e-8.

  • last_epoch (int, optional) -- The index of the last epoch. Default is -1.

warmup_epochs#

Stores the maximum number of epochs for the warmup phase.

Type:

int

max_epochs#

Stores the maximum number of epochs for the cosine annealing phase.

Type:

int

warmup_start_lr#

Stores the initial learning rate for the warmup phase.

Type:

float

eta_min#

Stores the minimum learning rate during the cosine annealing phase.

Type:

float

Warning

  1. Calling .step(): It's recommended to call .step() after each iteration. If called only after each epoch, the starting learning rate will remain at warmup_start_lr for the first epoch, which is often 0.

  2. Passing epoch to .step(): This is deprecated and triggers an EPOCH_DEPRECATION_WARNING. Make sure to call .step() before your training and validation routines when using this approach.

# Initialize optimizer and scheduler
optimizer = Adam(model.parameters(), lr=0.01)
scheduler = WarmupCosineLR(optimizer, warmup_epochs=5, max_epochs=50)

# Training loop
for epoch in range(50):
    # train(...)
    # validate(...)
    scheduler.step()
get_lr() List[float][source]#

Compute learning rate using chainable form of the scheduler

AdversarialTensors.simple_attack module#

class AdversarialTensors.simple_attack.Attacks(model=None, attack='all', attack_params={'eps': 0.03137254901960784, 'exp': 'all', 'log_dir': 'autoattack/', 'norm': 2, 'seed': 99, 'version': 'standard'}, device='cuda')[source]#

Bases: Module

A PyTorch Module to perform adversarial attacks on a given model.

Parameters:
  • model (torch.nn.Module) -- A PyTorch model that takes image tensors as input.

  • attack (str, optional) -- The type of attack to be performed. Default is 'all'.

  • attack_params (dict, optional) -- Additional parameters for the attack. Default includes L2 norm, epsilon value, etc.

  • device (str, optional) -- The computation device. Default is 'cuda'.

model_wrapper#

Wrapped version of the PyTorch model compatible with Foolbox.

Type:

PyTorchModel

attack_list#

List of attacks to be used when using AutoAttack.

Type:

list of str

adversary_model#

Foolbox attack object.

Type:

object

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(X, y)[source]#

Executes the attack on the given inputs and labels.

Parameters:
  • X (torch.Tensor) -- Input images.

  • y (torch.Tensor) -- True labels.

Returns:

x_adv_resnet -- Adversarially perturbed images.

Return type:

torch.Tensor

init_attack()[source]#

Initializes the Foolbox attack model based on the type of attack selected.

training: bool#

AdversarialTensors.subset module#

class AdversarialTensors.subset.Subset(dataset, indices, transform=None)[source]#

Bases: Dataset

Subset of a dataset at specified indices.

Parameters:
  • dataset (Dataset) -- The whole Dataset

  • indices (sequence) -- Indices in the whole set selected for subset

  • transform (callable, optional) -- A function/transform that takes in a sample and returns a transformed version.

AdversarialTensors.utils module#

AdversarialTensors.utils.eval_accuracy(model, X, y, correct=0, total=0)[source]#

Evaluate the model's accuracy on provided tensors.

Parameters:
  • model (torch.nn.Module) -- The neural network model for evaluation.

  • X (torch.Tensor) -- Tensor containing the input data.

  • y (torch.Tensor) -- Tensor containing the ground truth labels.

  • correct (int, optional) -- Count of correctly classified samples. Default is 0.

  • total (int, optional) -- Count of total samples evaluated. Default is 0.

Returns:

  • accuracy (float) -- Accuracy of the model on the input data.

  • correct (int) -- Updated count of correctly classified samples.

  • total (int) -- Updated count of total samples evaluated.

AdversarialTensors.utils.eval_accuracy_dataloader(model, dataloader, device='cuda')[source]#

Evaluate the model's accuracy using a given DataLoader.

Parameters:
  • model (torch.nn.Module) -- The neural network model for evaluation.

  • dataloader (torch.utils.data.DataLoader) -- DataLoader instance containing test data.

  • device (str, optional) -- Device to run the model on. Default is 'cuda'.

Returns:

accuracy -- Accuracy of the model on the test data.

Return type:

float

AdversarialTensors.utils.eval_accuracy_dataloader_with_attack(model, dataloader, attacker, device='cuda')[source]#

Evaluate the model's accuracy under adversarial attack using a given DataLoader.

Parameters:
  • model (torch.nn.Module) -- The neural network model for evaluation.

  • dataloader (torch.utils.data.DataLoader) -- DataLoader instance containing test data.

  • attacker (object) -- Instance responsible for generating adversarial attacks.

  • device (str, optional) -- Device to run the model on. Default is 'cuda'.

Returns:

accuracy -- Accuracy of the model on the test data under adversarial attack.

Return type:

float

AdversarialTensors.utils.eval_accuracy_w_reconst_dataloader(model, dataloader, device='cuda')[source]#

Evaluate the model's accuracy and average reconstruction error using a given DataLoader.

Parameters:
  • model (torch.nn.Module) -- The neural network model for evaluation.

  • dataloader (torch.utils.data.DataLoader) -- DataLoader instance containing test data.

  • device (str, optional) -- Device to run the model on. Default is 'cuda'.

Returns:

  • accuracy (float) -- Accuracy of the model on the test data.

  • avg_rec_err (float) -- Average reconstruction error on the test data.

Module contents#