Skip to main content

Adversarial-Defenses-PyTorch (Under Reconstruction)

MIT License Pypi Latest Release

[Torchdefenses] is a PyTorch library that provides adversarial defenses to obtain robust model against adversarial attacks. It contains PyTorch Lightning-like interface and functions that make it easier for PyTorch users to implement adversarial defenses.

How to use?

import torchdefenses as td
rmodel = td.RobModel(model, n_classes=10, 
                     normalize={'mean':[0.4914, 0.4822, 0.4465], 'std':[0.2023, 0.1994, 0.2010]})
Easy training

import torchdefenses.trainer as tr
trainer = tr.Standard(rmodel)
trainer.record_rob(train_loader, val_loader, eps=0.3, alpha=0.1, steps=5, std=0.1)
trainer.fit(train_loader=train_loader, max_epoch=10, optimizer="SGD(lr=0.01)",
            scheduler="Step([100, 105], 0.1)", scheduler_type="Epoch",
            record_type="Epoch", save_type="Epoch",
            save_path="./_temp/"+"sample", save_overwrite=True)

Supporting Multi-GPU

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1,2" # Possible GPUS

model = td.utils.load_model(model_name="ResNet18", n_classes=10).cuda() # Load model
model = torch.nn.DataParallel(model) # Parallelize

rmodel = td.RobModel(model, n_classes=10, 
                  normalize={'mean':[0.4914, 0.4822, 0.4465], 'std':[0.2023, 0.1994, 0.2010]}) # Wrap it with RobModel
trainer = ... # Define trainer
trainer.fit(..) # Training start

Recording, Saving and Visualizing

trainer.save_all("./_temp/"+"sample", overwrite=True)
trainer.rm.plot(title="A", xlabel="Epoch", ylabel="Accuracy",
                figsize=(6, 4),
                x_key='Epoch',
                y_keys=['Clean(Tr)', 'FGSM(Tr)', 'PGD(Tr)', 'GN(Tr)',
                        'Clean(Val)', 'FGSM(Val)', 'PGD(Val)', 'GN(Val)'],
                ylim=(-10, 110),
                colors=['k', '#D81B60', '#1E88E5', '#004D40']*2,
                labels=['Clean', 'FGSM', 'PGD', 'GN', '', '', '', ''],
                linestyles=['-', '-', '-', '-', '--', '--', '--', '--'],
               )

Easy evaluation

rmodel.eval_accuracy(test_loader)
rmodel.eval_rob_accuracy_pgd(test_loader, eps=1, alpha=0.1,
                             steps=10, random_start=True, restart_num=1)

Useful functions

from torchdefenses.utils import fix_randomness, fix_gpu
fix_randomness(0)
fix_gpu(0)

How to customize?

Metadata

Release files for torchdefenses 0.1.25

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

Built distribution (wheel)

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

Release files / torchdefenses-0.1.25-py3-none-any.whl

Download URL torchdefenses-0.1.25-py3-none-any.whl
Size 41.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
17a82d37d4aa404711ffae172c39fd703e24f8d62c4bce64219d3b8ab7a4947c
BLAKE2b-256 checksum
How to use checksums
92a3f9cc6c3aac2f45333ca6d2fd48f3309985256f929bb194a24708a51042de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8

Security for torchdefenses 0.1.25

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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