Adversarial_Observation package

Submodules

Adversarial_Observation.Attacks module

Adversarial_Observation.Attacks.fgsm_attack(input_batch_data: tensor, model: Module, input_shape: tuple, epsilon: float) Tensor[source]

Apply the FGSM attack to input images given a pre-trained PyTorch model.

Parameters:
  • input_batch_data (ndarray) – Batch of input images as a 4D numpy array.

  • model (nn.Module) – Pre-trained PyTorch model to be attacked.

  • input_shape (tuple) – Shape of the input array.

  • epsilon (float) – Magnitude of the perturbation for the attack.

Returns:

Adversarial images generated by the FGSM attack.

Return type:

adversarial_batch_data (ndarray)

Adversarial_Observation.Attacks.gradient_ascent(input_batch_data: tensor, model: Module, input_shape: tuple, target_neuron: int, num_iterations: int = 100, step_size: float = 1.0) Tensor[source]

Perform gradient ascent to generate an image that maximizes the activation of a target neuron given a pre-trained PyTorch model.

Parameters:
  • input_batch_data (ndarray) – Batch of input images as a 4D numpy array.

  • model (nn.Module) – Pre-trained PyTorch model used for gradient ascent.

  • input_shape (tuple) – Shape of the input array.

  • target_neuron (int) – Index of the target neuron to maximize activation.

  • num_iterations (int, optional) – Number of gradient ascent iterations. Defaults to 100.

  • step_size (float, optional) – Step size for each iteration. Defaults to 1.0.

Returns:

Generated images that maximize the activation of the target neuron.

Return type:

generated_images (ndarray)

Adversarial_Observation.Attacks.gradient_map(input_batch_data: tensor, model: Module, input_shape: tuple, backprop_type: str = 'guided') Tensor[source]

Generate a gradient map for an input image given a pre-trained PyTorch model.

Parameters:
  • input_batch_data (ndarray) – Batch of input images as a 4D numpy array.

  • model (nn.Module) – Pre-trained PyTorch model used to generate the gradient map.

  • input_shape (tuple) – Shape of the input array.

  • backprop_type (str, optional) – Type of backpropagation. Supported values: ‘vanilla’, ‘guided’, ‘relu’. Defaults to ‘vanilla’.

Returns:

Gradient map for the input images.

Return type:

gradient_maps (ndarray)

Adversarial_Observation.Attacks.saliency_map(input_image, model, target_class=None)[source]

Generate a saliency map for an input image given a pre-trained PyTorch model.

Parameters:
  • input_image (torch.Tensor) – Input image as a 3D torch.Tensor.

  • model (torch.nn.Module) – Pre-trained PyTorch model used to generate the saliency map.

  • target_class (int, optional) – Index of the target class for saliency computation. If None, the class with the highest predicted score will be used.

Returns:

Saliency map for the input image.

Return type:

saliency_map (torch.Tensor)

Adversarial_Observation.utils module

Adversarial_Observation.utils.compute_success_rate(original_preds: Tensor, adversarial_preds: Tensor) float[source]

Computes the success rate of the attack, which is the fraction of adversarial examples where the model was misled (i.e., adversarial prediction does not match the original prediction).

Parameters:
  • original_preds (torch.Tensor) – The original model predictions.

  • adversarial_preds (torch.Tensor) – The predictions on adversarial examples.

Returns:

The success rate of the attack.

Return type:

float

Adversarial_Observation.utils.fgsm_attack(input_data: Tensor, model: Module, epsilon: float, device: device) Tensor[source]

Performs FGSM attack on the input data.

Parameters:
  • input_data (torch.Tensor) – The original input batch.

  • model (torch.nn.Module) – The model to attack.

  • epsilon (float) – The perturbation magnitude.

  • device (torch.device) – The device to perform the attack on (cuda or cpu).

Returns:

The adversarially perturbed batch.

Return type:

torch.Tensor

Adversarial_Observation.utils.load_MNIST_data(batch_size=32)[source]

Loads MNIST train and test data and prepares it for evaluation.

Parameters:

batch_size (int) – The batch size for data loading.

Returns:

The training and testing data loaders.

Return type:

TrinLoader, TestLoader

Adversarial_Observation.utils.load_MNIST_model()[source]

Loads a sequential CNN model for MNIST dataset.

Returns:

The CNN model.

Return type:

torch.nn.Module

Adversarial_Observation.utils.log_metrics(success_rate: float, avg_perturbation: float)[source]

Logs the success rate and average perturbation of the attack.

Parameters:
  • success_rate (float) – The success rate of the attack.

  • avg_perturbation (float) – The average perturbation magnitude.

Adversarial_Observation.utils.seed_everything(seed: int)[source]

Sets the seed for random number generation to ensure reproducibility.

Parameters:

seed (int) – The seed value to use.

Adversarial_Observation.utils.visualize_adversarial_examples(original_images: Tensor, adversarial_images: Tensor, original_image_path: str, adversarial_image_path: str)[source]

Visualizes and saves the original and adversarial images side by side.

Parameters:
  • original_images (torch.Tensor) – Original input images.

  • adversarial_images (torch.Tensor) – Adversarially perturbed images.

  • original_image_path (str) – Path to save the original images.

  • adversarial_image_path (str) – Path to save the adversarial images.

Adversarial_Observation.visualize module

Adversarial_Observation.visualize.visualize_gif(filenames: List[str], output_file: str = 'output.gif') None[source]

Create a GIF from a list of image filenames.

Parameters:
  • filenames (List[str]) – List of image filenames.

  • output_file (str) – Output filename for the GIF (default: ‘output.gif’).

Returns:

None

Module contents