A curated list of adversarial attacks in PyTorch, with a focus on transferable black-box attacks
Project description
🛡 torchattack - A curated list of adversarial attacks in PyTorch, with a focus on transferable black-box attacks.
pip install torchattack
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
attack = FGSM(model, normalize, device)
# Initialize an attack with extra params
attack = 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
attack = create_attack('FGSM', model, normalize, device)
# Initialize PGD attack with specific eps with create_attack
attack = 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}
attack = create_attack('MIFGSM', model, normalize, device, eps=0.03, **attack_args)
Check out examples/ and torchattack.evaluate.runner for full examples.
Attacks
Development
On how to install dependencies, run tests, and build documentation. See Development - torchattack.
License
Related
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torchattack-1.5.1.tar.gz.
File metadata
- Download URL: torchattack-1.5.1.tar.gz
- Upload date:
- Size: 63.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f46fb5e31bfe82b4e5677d4ba5f5eea6dd2b9310d17ed598720b441a80618abe
|
|
| MD5 |
b1fd11367cc69a126c407d6a5d2ed5be
|
|
| BLAKE2b-256 |
df58a0704ec4691faf6c578b508c6929a2c3217266b8b9aac2781f651e87fb84
|
Provenance
The following attestation bundles were made for torchattack-1.5.1.tar.gz:
Publisher:
pypi-publish.yml on spencerwooo/torchattack
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchattack-1.5.1.tar.gz -
Subject digest:
f46fb5e31bfe82b4e5677d4ba5f5eea6dd2b9310d17ed598720b441a80618abe - Sigstore transparency entry: 172066244
- Sigstore integration time:
-
Permalink:
spencerwooo/torchattack@d698c52c48193278ce72a85b7e73226a8098460d -
Branch / Tag:
refs/tags/v1.5.1 - Owner: https://github.com/spencerwooo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@d698c52c48193278ce72a85b7e73226a8098460d -
Trigger Event:
release
-
Statement type:
File details
Details for the file torchattack-1.5.1-py3-none-any.whl.
File metadata
- Download URL: torchattack-1.5.1-py3-none-any.whl
- Upload date:
- Size: 94.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b05363118eec92356c918b5af352054a1a6ab49f0a8bc97e043ae1e6e83e612
|
|
| MD5 |
e2a860248d7bb928b385504f2f50ec49
|
|
| BLAKE2b-256 |
2b3e6b854552502f8af3899aa20da8c20ade10d3cdd72730180862d6f8cbd5d7
|
Provenance
The following attestation bundles were made for torchattack-1.5.1-py3-none-any.whl:
Publisher:
pypi-publish.yml on spencerwooo/torchattack
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchattack-1.5.1-py3-none-any.whl -
Subject digest:
9b05363118eec92356c918b5af352054a1a6ab49f0a8bc97e043ae1e6e83e612 - Sigstore transparency entry: 172066245
- Sigstore integration time:
-
Permalink:
spencerwooo/torchattack@d698c52c48193278ce72a85b7e73226a8098460d -
Branch / Tag:
refs/tags/v1.5.1 - Owner: https://github.com/spencerwooo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@d698c52c48193278ce72a85b7e73226a8098460d -
Trigger Event:
release
-
Statement type: