Skip to main content

Torch Adversarial - Optimizing Inputs to Models

GitHub license PyPI version

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])

Metadata

Release files for torchadversarial 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torchadversarial 0.0.2
File Size Uploaded Provenance
torchadversarial-0.0.2.tar.gz 4.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for torchadversarial 0.0.2
File Interpreter ABI Platform
torchadversarial-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 10.4 kB

Release files / torchadversarial-0.0.2.tar.gz

Download URL torchadversarial-0.0.2.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c973d5813d37c0e2e6b833ba953470390abaae077d8ed12d2f31d355c96d56b8
BLAKE2b-256 checksum
How to use checksums
1a144635327c4c59f8b8b9dbac37b48bb0d36e10d27b1e292db971042cbae77b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / torchadversarial-0.0.2-py3-none-any.whl

Download URL torchadversarial-0.0.2-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f80ea457acb129620d610c1ecbe4471bfb9cdc2f48aa4a410cc14a88194f7fe6
BLAKE2b-256 checksum
How to use checksums
7e777bd3e78d2bab86269fec4ad05ee84e5e76716a9225a3443fd0ba93377b96
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Security for torchadversarial 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page