Skip to main content

Patch Viz: A library to test how well your model holds up against adversarial attacks

Project description

Patch Viz: Visualizing the Impact of Adversarial Patches

Patch Viz is a Python package for generating, applying, and visualizing the impact of adversarial patches on Convolutional Neural Networks (CNNs). With this tool, you can create adversarial patches for specific target classes, apply them to input images, and analyze their effects using saliency maps and Grad-CAM visualizations.

How It Works

Here’s an overview of how Patch Viz works:

1. Adversarial Patch Creation

  • Patch Viz trains adversarial patches to fool a pretrained model into misclassifying inputs to a specific target class.

  • You can customize the patch size, target class, and number of training epochs.

2. Patch Application

  • Once trained, the patch can be applied to any input image.
  • For example, a patch trained to classify all images as "dog" can be applied to an image of a "cat," causing the model to predict "dog."

3. Impact Visualization

  • Visualize the effects of adversarial patches on input images using:
    • Saliency Maps: Highlight regions of the image influencing the model's predictions.
    • Grad-CAM: Visualize important areas contributing to the model's decisions.

Features

  • Train patches: Generate adversarial patches for target classes and visualize their effectiveness.
  • Visualize impact: Explore the influence of patches using Grad-CAM and saliency maps.
  • Built-in datasets: Use sample datasets (e.g., Intel Image Classification, Emotions) to test functionality out of the box.

Installation

To install Patch Viz, run:

pip install patchviz

Requirements

  • Requires Python 3.10 or later.
  • The package supports both CPU and GPU.

Usage

To use the patchviz package, you will need access to the following:

  • Dataset
  • Model Weight File (.pth/.pt)
  • Model Architecture Class
  • Number of Classes
  • The Pytorch transforms object used during training

Here’s how you can visualize the impact of adversarial patches using the sample Intel dataset:

# Import required libraries

from patchviz import DatasetLoader, ModelLoader, AdversarialPatch, Visualizer, Explainer, get_sample_model
# Step 1: Load in Dataset (Intel dataset)

model_inst, num_classes, transform = get_sample_model("intel")

data_dir = "path_to_intel_dataset"

loader = DatasetLoader(data_dir=data_dir,transform=transform)

# Get a sample batch
images, labels = loader.get_sample_batch()
print(f"Batch size: {len(images)}")

# Plot a sample batch
loader.plot_sample_batch()

Output

Batch size: 16
tensor([2, 0, 1, 2, 2, 2, 2, 2, 4, 0, 4, 1, 0, 0, 0, 5])

Intel Sample Batch Output

# Step 2: Load in CNN Model (Custom Weights/Pretrained Model)

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

custom_weights = "path_to_intel-class.pth"

model_loader = ModelLoader(
    model_inst=model_inst,
    num_classes=num_classes,
    custom_weights=custom_weights,
    device=device
)

model = model_loader.model

Output

Loaded custom weights from path_to_intel-class.pth
# Step 3: Create and Train Adversarial Patch

adv_patch = AdversarialPatch(
   model=model,
   loader=loader,
   device=device
)

patch_dict = adv_patch.get_patches(
   class_names=["class_name"],
   patch_sizes=[16],
   num_epochs=10,
)

Output

Epoch 10, Loss: 0.26:  100%|█████████| 350/350 [00:03<00:22, 13.51it/s]

Validating...:  100%|█████████| 878/878 [00:07<00:56, 13.76it/s]

Validation results for class_name and (16, 16): {'acc': 0.5214285850524902, 'top5': 0.9979804158210754}
# Step 4: Visualize the effect of the trained patches on predictions

viz = Visualizer(
    idx_to_class=loader.idx_to_class,
    adv_patch=adv_patch,
    model=model,
)


viz.show_predictions(
    images=images,
    labels=labels,
    patch=patch_dict["class_name"][16]['patch'],
)

Output

Original/Patched Predictions Viz

# Step 5: Load in Explainer and visualize the effect of patches on Saliency and GRAD CAM maps

explainer = Explainer(
    model=model,
    adv_patch=adv_patch,
    device=device
)

explainer.explain(
    images=images,
    labels=labels,
    patch=patch_dict["sea"][16]['patch'],
)

Output

GRAD-CAM and Saliency Maps Viz

Example Datasets and Models

Patch Viz is designed to work with two dataset-model pairs to help you get started out of the box.

Datasets

These datasets can be downloaded and used directly with the package:

1. Intel Image Classification Dataset

  • Dataset: Intel Image Classification Dataset
  • Structure: Contains images classified into categories such as forest, glacier, mountain, sea, and street.
  • Loading it in: load the sample in using get_sample_model("intel")

2. Emotions Dataset

  • Dataset: Emotions Dataset
  • Structure: Contains images of faces labeled with emotional categories (e.g., happy, sad, angry).
  • Loading it in: load the sample in using get_sample_model("emot")

Models

Similarly, the model weights for the corresponding dataests have been provided for downloading in the models foler:

  • Intel Image Classification Dataset: intel-class.pth
  • Emotions Dataset: emot-class-v2.pth

How to Interpret the Visualizations

Original/Patched Predictions

The following are the key visualizations provided by Patch Viz:

  • Original Image:

    • Displays the unaltered input image.
  • Original Predictions:

    • Shows the model's prediction on the original image as a graph of confidence for each class
  • Patched Image:

    • Shows the same image after applying the adversarial patch.
  • Patched Predictions:

    • Shows the model's prediction on the patched image as a graph of confidence for each class

Example Visualization Output

  • Each row corresponds to a single input image. The columns show:

    • Original Image → Original Predictions
    • Patched Image → Patched Predictions

GRAD-CAM & Saliency Maps

The following are the key visualizations provided by Patch Viz:

  • Original Image:

    • Displays the unaltered input image.
  • Patched Image:

    • Shows the same image after applying the adversarial patch.
  • Grad-CAM:

    • Highlights areas of the image most important for the model’s predictions.
  • Saliency Maps:

    • Displays pixel-level contributions to the model’s decision.

Example GRAD-CAM & Saliency Maps Output

  • Each row corresponds to a single input image. The columns show:

    • Original Image → Saliency Map → Grad-CAM
    • Patched Image → Patched Saliency Map → Patched Grad-CAM

Contributing

I welcome contributions! Please feel free to open an issue or submit a pull request if you have any improvements or ideas.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgments

Patch Viz builds on:

  • PyTorch for deep learning.
  • Captum for interpretability tools.
  • Matplotlib for visualizations.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

patchviz-1.1.1.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

patchviz-1.1.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file patchviz-1.1.1.tar.gz.

File metadata

  • Download URL: patchviz-1.1.1.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for patchviz-1.1.1.tar.gz
Algorithm Hash digest
SHA256 aed1345e51377f1821c0b1a6c8babf06d8293e0a85440413fb9b05c9c8648372
MD5 e383e92ccd4797d096cd88f209f54e3c
BLAKE2b-256 6bc09c85854cd1fffd0cedb49b96cbd6dba2fecef5fb691242946510fb58bbf4

See more details on using hashes here.

File details

Details for the file patchviz-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: patchviz-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for patchviz-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6b3aeb2c933402c66fd85871c4d229bbcf1a5701fb66c9ab60c6e883fc226045
MD5 7d97fde8e45100855441b9c5c3c5535f
BLAKE2b-256 58a6ea63e0d858eb6c82f6e746b11deb2fdf904295914772d3b486f41a342139

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page