Skip to main content

A curated list of adversarial attacks in PyTorch, with a focus on transferable black-box attacks

Project description


torchattack banner


Ruff pypi python versions pypi version pypi weekly downloads lint

🛡 torchattack - A curated list of adversarial attacks in PyTorch, with a focus on transferable black-box attacks.

pip install torchattack  # or `torchattack[full]` to install all extra dependencies

Highlights

  • 🛡️ A curated collection of adversarial attacks implemented in PyTorch.
  • 🔍 Focuses on gradient-based transferable black-box attacks.
  • 📦 Easily load pretrained models from torchvision or timm using AttackModel.
  • 🔄 Simple interface to initialize attacks with create_attack.
  • 🔧 Extensively typed for better code quality and safety.
  • 📊 Tooling for fooling rate metrics and model evaluation in eval.
  • 🔁 Numerous attacks reimplemented for readability and efficiency (TGR, VDC, etc.).

Documentation

torchattack's docs are available at docs.swo.moe/torchattack.

Usage

import torch

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

Load a pretrained model to attack from either torchvision or timm.

from torchattack import AttackModel

# Load a model with `AttackModel`
model = AttackModel.from_pretrained(model_name='resnet50').to(device)
# `AttackModel` automatically attach the model's `transform` and `normalize` functions
transform, normalize = model.transform, model.normalize

# Additionally, to explicitly specify where to load the pretrained model from (timm or torchvision),
# prepend the model name with 'timm/' or 'tv/' respectively, or use the `from_timm` argument, e.g.
vit_b16 = AttackModel.from_pretrained(model_name='timm/vit_base_patch16_224').to(device)
inv_v3 = AttackModel.from_pretrained(model_name='tv/inception_v3').to(device)
pit_b = AttackModel.from_pretrained(model_name='pit_b_224', from_timm=True).to(device)

Initialize an attack by importing its attack class.

from torchattack import FGSM, MIFGSM

# Initialize an attack
adversary = FGSM(model, normalize, device)

# Initialize an attack with extra params
adversary = MIFGSM(model, normalize, device, eps=0.03, steps=10, decay=1.0)

Initialize an attack by its name with create_attack().

from torchattack import create_attack

# Initialize FGSM attack with create_attack
adversary = create_attack('FGSM', model, normalize, device)

# Initialize PGD attack with specific eps with create_attack
adversary = create_attack('PGD', model, normalize, device, eps=0.03)

# Initialize MI-FGSM attack with extra args with create_attack
attack_args = {'steps': 10, 'decay': 1.0}
adversary = create_attack('MIFGSM', model, normalize, device, eps=0.03, **attack_args)

Check out examples/ and torchattack.evaluate.runner for full examples.

Attacks

We roughly categorize transferable adversarial attacks into the following categories based on their strategies to improve adversarial transferability:

  • Classic attacks: The line of work that first proposed gradient-based adversarial attacks.
  • Gradient augmentations: Stabilizing or augmenting the gradient flows to improve transferability.
  • Input transformations: Applying all forms of transformations as image augmentations to inputs.
  • Feature disruption: Disrupting intermediate features of the surrogate model.
  • Surrogate self-refinement: Refining the surrogate model, both structure-wise and in forward/backward passes.
  • Generative modelling: Using generative models to generate adversarial examples.
  • Others: Other attacks that do not fit into transfer-based attacks but are important black-box attacks.

We provide a detailed list of all supported attacks below.

Name Class Name Publication Paper (Open Access)
Classic attacks
FGSM FGSM ICLR 2015 Explaining and Harnessing Adversarial Examples
PGD PGD ICLR 2018 Towards Deep Learning Models Resistant to Adversarial Attacks
PGD (L2) PGDL2 ICLR 2018 Towards Deep Learning Models Resistant to Adversarial Attacks
I-FGSM IFGSM ICLR 2019 Adversarial examples in the physical world
Gradient augmentations
MI-FGSM MIFGSM CVPR 2018 Boosting Adversarial Attacks with Momentum
NI-FGSM NIFGSM ICLR 2020 Nesterov Accelerated Gradient and Scale Invariance for Adversarial Attacks
VMI-FGSM VMIFGSM CVPR 2021 Enhancing the Transferability of Adversarial Attacks through Variance Tuning
VNI-FGSM VNIFGSM CVPR 2021 Enhancing the Transferability of Adversarial Attacks through Variance Tuning
MIG MIG ICCV 2023 Transferable Adversarial Attack for Both Vision Transformers and Convolutional Networks via Momentum Integrated Gradients
GRA GRA ICCV 2023 Boosting Adversarial Transferability via Gradient Relevance Attack
MuMoDIG MuMoDIG AAAI 2025 Improving Integrated Gradient-based Transferable Adversarial Examples by Refining the Integration Path
Input transformations
DI-FGSM DIFGSM CVPR 2019 Improving Transferability of Adversarial Examples with Input Diversity
TI-FGSM TIFGSM CVPR 2019 Evading Defenses to Transferable Adversarial Examples by Translation-Invariant Attacks
SI-NI-FGSM SINIFGSM ICLR 2020 Nesterov Accelerated Gradient and Scale Invariance for Adversarial Attacks
Admix Admix ICCV 2021 Admix: Enhancing the Transferability of Adversarial Attacks
SSA SSA ECCV 2022 Frequency Domain Model Augmentation for Adversarial Attack
DeCoWA DeCoWA AAAI 2024 Boosting Adversarial Transferability across Model Genus by Deformation-Constrained Warping
BSR BSR CVPR 2024 Boosting Adversarial Transferability by Block Shuffle and Rotation
L2T L2T CVPR 2024 Learning to Transform Dynamically for Better Adversarial Transferability
Feature disruption
FDA FDA ICCV 2019 FDA: Feature Disruptive Attack
DR DR CVPR 2020 Enhancing Cross-Task Black-Box Transferability of Adversarial Examples With Dispersion Reduction
FIA FIA ICCV 2021 Feature Importance-aware Transferable Adversarial Attacks
NAA NAA CVPR 2022 Improving Adversarial Transferability via Neuron Attribution-Based Attacks
ILPD ILPD NeurIPS 2023 Improving Adversarial Transferability via Intermediate-level Perturbation Decay
DANAA DANAA ADMA 2023 DANAA: Towards transferable attacks with double adversarial neuron attribution
BFA BFA Neurocomputing' 2024 Improving the transferability of adversarial examples through black-box feature attacks
Surrogate self-refinement
SGM SGM ICLR 2020 Skip Connections Matter: On the Transferability of Adversarial Examples Generated with ResNets
PNA-PatchOut PNAPatchOut AAAI 2022 Towards Transferable Adversarial Attacks on Vision Transformers
TGR TGR CVPR 2023 Transferable Adversarial Attacks on Vision Transformers with Token Gradient Regularization
BPA BPA NeurIPS 2023 Rethinking the Backward Propagation for Adversarial Transferability
VDC VDC AAAI 2024 Improving the Adversarial Transferability of Vision Transformers with Virtual Dense Connection
ATT ATT NeurIPS 2024 Boosting the Transferability of Adversarial Attack on Vision Transformer with Adaptive Token Tuning
Generative modelling
CDA CDA NeurIPS 2019 Cross-Domain Transferability of Adversarial Perturbations
LTP LTP NeurIPS 2021 Learning Transferable Adversarial Perturbations
BIA BIA ICLR 2022 Beyond ImageNet Attack: Towards Crafting Adversarial Examples for Black-box Domains
GAMA GAMA NeurIPS 2022 GAMA: Generative Adversarial Multi-Object Scene Attacks
Others
DeepFool DeepFool CVPR 2016 DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks
GeoDA GeoDA CVPR 2020 GeoDA: A Geometric Framework for Black-box Adversarial Attacks
SSP SSP CVPR 2020 A Self-supervised Approach for Adversarial Robustness

Development

On how to install dependencies, run tests, and build documentation. See Development - torchattack.

License

MIT

Related

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

torchattack-1.7.0.tar.gz (77.6 kB view details)

Uploaded Source

Built Distribution

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

torchattack-1.7.0-py3-none-any.whl (116.2 kB view details)

Uploaded Python 3

File details

Details for the file torchattack-1.7.0.tar.gz.

File metadata

  • Download URL: torchattack-1.7.0.tar.gz
  • Upload date:
  • Size: 77.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for torchattack-1.7.0.tar.gz
Algorithm Hash digest
SHA256 5bbf5d5edb14535644d3831afc5701eed4b1def16f9ae99f0017b8f9c2e16d1d
MD5 9cb8d300546dedc786bc7004030f9b59
BLAKE2b-256 b7fabd9c428f512d71d126b5d31fe71414c259059390a93042d361123affbfe2

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchattack-1.7.0.tar.gz:

Publisher: pypi-publish.yml on spencerwooo/torchattack

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torchattack-1.7.0-py3-none-any.whl.

File metadata

  • Download URL: torchattack-1.7.0-py3-none-any.whl
  • Upload date:
  • Size: 116.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for torchattack-1.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ccce29e09f28f92eae44549eb7cae2dc2da47d2e9498c0441365633ec29c6bde
MD5 bd4cde3cb77cbaa7b4570168cd427ecf
BLAKE2b-256 0318b308382db7963e5a7f9eddee27bb5c60cdaae6bc0d20bd1e6befa083f4c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchattack-1.7.0-py3-none-any.whl:

Publisher: pypi-publish.yml on spencerwooo/torchattack

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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