No project description provided
Project description
Torch Adversarial - Optimizing Inputs to Models
Torch Adversarial is a versatile toolkit for optimizing inputs to models. It provides a wide range of attack methods that can be used for various tasks beyond classification, including object detection and more.
Installation
To install Torch Adversarial from PyPI, simply run the following command:
pip install torchadversarial
To install Torch Adversarial from the source code, use the following command:
pip install git+https://github.com/cat-claws/torchadversarial/
Advantages
Torch Adversarial offers attack codes that are not limited to classification tasks. For example, FGSM (Fast Gradient Sign Method) can be employed to attack object detection models and more.
Table of Contents
How to use
Basic Usage
You can use Torch Adversarial to optimize input tensors as follows:
import torch
from torchadversarial import Fgsm
# Create an input tensor
x = torch.rand(4, 3, 112, 112)
# Clone the input tensor and enable gradient computation
z = x.clone()
z.requires_grad = True
# Initialize the FGSM optimizer with epsilon (perturbation magnitude)
opt = Fgsm([z], epsilon=0.1)
# Define a simple objective function (for example, sum of tensor elements)
y = z.sum()
# Backpropagate through the optimizer
y.backward()
# Perform a step to generate the adversarial example
opt.step()
Using as an Adversarial Attack
You can utilize Torch Adversarial as an adversarial attack as shown below:
import torch
from torchadversarial import Fgsm, Attack
# Create an input tensor
x = torch.rand(4, 3, 112, 112)
# Apply the FGSM attack to the input tensor
for x_ in Attack(Fgsm, [x], epsilon=0.1, foreach=False, maximize=True):
# Define an objective function (e.g., sum of tensor elements)
y = torch.sum(x_[0])
y.backward()
# x_[0] now contains your adversarial example
# print(x_[0])
Please note that in the example above, x_[0] represents your adversarial example. Similar to other optimizers in torch.optim, the input parameters (e.g., [x]) must be an iterable containing tensors, and thus, we extract the adversarial example as x_[0].
With Torch Adversarial, you can efficiently optimize inputs to your models and perform adversarial attacks for a wide range of applications.
import torch
from torchadversarial import Attack
# Create an input tensor
x = torch.rand(4, 3, 112, 112)
# Apply the PGD attack to the input tensor
for x_ in Attack(torch.optim.SGD, [x], steps = 10, foreach=False, maximize=True):
# Project variable to the constraint
with torch.no_grad():
x_[0].copy_(x_[0].clamp(x - 0.1, x + 0.1))
# Define an objective function (e.g., sum of tensor elements)
y = torch.sum(x_[0])
y.backward()
# x_[0] now contains your adversarial example
# print(x_[0])
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 torchadversarial-0.0.2.tar.gz.
File metadata
- Download URL: torchadversarial-0.0.2.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c973d5813d37c0e2e6b833ba953470390abaae077d8ed12d2f31d355c96d56b8
|
|
| MD5 |
69ce36ccdc8d5324569f91bb6e093dc3
|
|
| BLAKE2b-256 |
1a144635327c4c59f8b8b9dbac37b48bb0d36e10d27b1e292db971042cbae77b
|
File details
Details for the file torchadversarial-0.0.2-py3-none-any.whl.
File metadata
- Download URL: torchadversarial-0.0.2-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f80ea457acb129620d610c1ecbe4471bfb9cdc2f48aa4a410cc14a88194f7fe6
|
|
| MD5 |
d239dbcb049d044f3fc6962dd9995426
|
|
| BLAKE2b-256 |
7e777bd3e78d2bab86269fec4ad05ee84e5e76716a9225a3443fd0ba93377b96
|