Adversarial_Observation package
Submodules
Adversarial_Observation.Attacks module
- class Adversarial_Observation.Attacks.Config(epsilon=0.1, attack_method='fgsm')[source]
Bases:
object
- 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.
- Adversarial_Observation.Attacks.generate_adversarial_examples(input_batch_data, model, method='fgsm', **kwargs)[source]
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