通过 pip 安装:pip install robustness
阅读文档:https://robustness.readthedocs.io/en/latest/index.html
robustness 是我们(MadryLab <http://madry-lab.ml>_ 的学生)创建的一个软件包,旨在让神经网络的训练、评估和探索变得灵活而简单。我们几乎在所有项目中使用它(无论是否涉及对抗训练!),并且它将成为我们许多即将发布的代码的依赖项。使用该库的一些项目包括:
《Learning Perceptually-Aligned Representations via Adversarial Robustness》的代码 <https://github.com/MadryLab/robust_representations>_ (https://arxiv.org/abs/1906.00945)《Image Synthesis with a Single (Robust) Classifier》的代码 <https://github.com/MadryLab/robustness_applications>_ (https://arxiv.org/abs/1906.09453)《Do Adversarially Robust ImageNet Models Transfer Better?》的代码 <https://github.com/microsoft/robust-models-transfer>_ (https://arxiv.org/abs/2007.08489)《BREEDS: Benchmarks for Subpopulation Shift》的代码 <https://github.com/MadryLab/BREEDS-Benchmarks>_ (https://arxiv.org/abs/2008.04859)《Certified Patch Robustness via Smoothed Vision Transformers.》的代码 <https://github.com/MadryLab/smoothed-vit>_ (https://arxiv.org/abs/2110.07719)《Unadversarial Examples: Designing Objects for Robust Vision.》的代码 <https://github.com/microsoft/unadversarial>_ (https://arxiv.org/abs/2012.12235)我们在一系列教程和 API 参考中演示了如何使用该库。该库提供的功能包括:
CLI 接口 <https://robustness.readthedocs.io/en/latest/example_usage/cli_usage.html>_ 训练和评估各种数据集/架构上的标准和鲁棒模型。该库还支持添加
自定义数据集 <https://robustness.readthedocs.io/en/latest/example_usage/training_lib_part_2.html#training-on-custom-datasets>_ 和 模型架构 <https://robustness.readthedocs.io/en/latest/example_usage/training_lib_part_2.html#training-with-custom-architectures>_。.. code-block:: bash
python -m robustness.main --dataset cifar --data /path/to/cifar
--adv-train 0 --arch resnet18 --out-dir /logs/checkpoints/dir/
输入操纵 <https://robustness.readthedocs.io/en/latest/example_usage/input_space_manipulation.html>_ ——包括生成对抗样本、反转表示、
特征可视化等。该库提供了多种优化
选项(例如在真实/估计梯度、傅里叶/像素基、
自定义损失函数等之间进行选择),并且易于扩展。.. code-block:: python
import torch as ch from robustness.datasets import CIFAR from robustness.model_utils import make_and_restore_model
ds = CIFAR('/path/to/cifar') model, _ = make_and_restore_model(arch='resnet50', dataset=ds, resume_path='/path/to/model', state_dict_path='model') model.eval() attack_kwargs = { 'constraint': 'inf', # L-inf PGD 'eps': 0.05, # Epsilon constraint (L-inf norm) 'step_size': 0.01, # Learning rate for PGD 'iterations': 100, # Number of PGD steps 'targeted': True # Targeted attack 'custom_loss': None # Use default cross-entropy loss }
_, test_loader = ds.make_loaders(workers=0, batch_size=10) im, label = next(iter(test_loader)) target_label = (label + ch.randint_like(label, high=9)) % 10 adv_out, adv_im = model(im, target_label, make_adv, **attack_kwargs)
robustness 作为软件包导入,可以轻松训练神经网络,并支持自定义损失函数、日志记录、数据加载等!在我们的两部分教程(第 1 部分 <https://robustness.readthedocs.io/en/latest/example_usage/training_lib_part_1.html>、
第 2 部分 <https://robustness.readthedocs.io/en/latest/example_usage/training_lib_part_2.html>)中可以找到很好的介绍。.. code-block:: python
from robustness import model_utils, datasets, train, defaults from robustness.datasets import CIFAR
from cox.utils import Parameters import cox.store
ds = CIFAR('/path/to/cifar') m, _ = model_utils.make_and_restore_model(arch='resnet50', dataset=ds) train_loader, val_loader = ds.make_loaders(batch_size=128, workers=8)
out_store = cox.store.Store(OUT_DIR)
train_kwargs = { 'out_dir': "train_out", 'adv_train': 1, 'constraint': '2', 'eps': 0.5, 'attack_lr': 1.5, 'attack_steps': 20 } train_args = Parameters(train_kwargs)
train_args = defaults.check_and_fill_args(train_args, defaults.TRAINING_ARGS, CIFAR) train_args = defaults.check_and_fill_args(train_args, defaults.PGD_ARGS, CIFAR)
train.train_model(train_args, m, (train_loader, val_loader), store=out_store)
注意:robustness 要求安装支持 CUDA 的 PyTorch。
与训练代码一起,我们发布了多个针对不同数据集、范数和 ε-train 值的预训练模型。随着我们发布更多或改进的模型,此列表将不断更新。如果您在研究中使用这些模型,请引用本库(参见下面的 bibtex 条目)。
对于每个(模型,ε-test)组合,我们使用 2.5 * ε-test / num_steps 的步长评估 20 步和 100 步 PGD。由于这两个准确率非常接近,我们不再考虑更多的 PGD 步骤。对于每个 ε-test 值,我们用粗体突出显示在不同的 ε-train 上实现的最佳鲁棒准确率。
注意 #1:我们没有进行任何超参数调整,只是使用了与标准训练相同的超参数。探索不同的训练超参数可能会使这些鲁棒准确率提高几个百分点。
注意 #2:以下 PyTorch 检查点(.pt)文件是使用以下版本的 PyTorch 和 Dill 保存的:
.. code-block::
torch==1.1.0 dill==0.2.9
CIFAR10 L2 范数 (ResNet50):
ε = 0.0 <https://www.dropbox.com/s/yhpp4yws7sgi6lj/cifar_nat.pt?dl=0>_ (标准训练)ε = 0.25 <https://www.dropbox.com/s/2qsp7pt6t7uo71w/cifar_l2_0_25.pt?dl=0>_ε = 0.5 <https://www.dropbox.com/s/1zazwjfzee7c8i4/cifar_l2_0_5.pt?dl=0>_ε = 1.0 <https://www.dropbox.com/s/s2x7thisiqxz095/cifar_l2_1_0.pt?dl=0>_+--------------+----------------+-----------------+---------------------+---------------------+ | CIFAR10 L2-robust accuracy | +--------------+----------------+-----------------+---------------------+---------------------+ | | ε-train | +--------------+----------------+-----------------+---------------------+---------------------+ | ε-test | 0.0 | 0.25 | 0.5 | 1.0 | +==============+================+=================+=====================+=====================+ | 0.0 | 95.25% / - | 92.77% / - | 90.83% / - | 81.62% / - | +--------------+----------------+-----------------+---------------------+---------------------+ | 0.25 | 8.66% / 7.34% | 81.21% / 81.19% | 82.34% / 82.31% | 75.53% / 75.53% | +--------------+----------------+-----------------+---------------------+---------------------+ | 0.5 | 0.28% / 0.14% | 62.30% / 62.13% | 70.17% / 70.11% | 68.63% / 68.61% | +--------------+----------------+-----------------+---------------------+---------------------+ | 1.0 | 0.00% / 0.00% | 21.18% / 20.66% | 40.47% / 40.22% | 52.72% / 52.61% | +--------------+----------------+-----------------+---------------------+---------------------+ | 2.0 | 0.00% / 0.00% | 0.58% / 0.46% | 5.23% / 4.97% | 18.59% / 18.05% | +--------------+----------------+-----------------+---------------------+---------------------+
CIFAR10 Linf 范数 (ResNet50):
ε = 8/255 <https://www.dropbox.com/s/c9qlt1lbdnu9tlo/cifar_linf_8.pt?dl=0>_+--------------+-----------------+---------------------+ | CIFAR10 Linf-robust accuracy | +--------------+-----------------+---------------------+ | | ε-train | +--------------+-----------------+---------------------+ | ε-test | 0 / 255 | 8 / 255 | +==============+=================+=====================+ | 0 / 255 | 95.25% / - | 87.03% / - | +--------------+-----------------+---------------------+ | 8 / 255 | 0.00% / 0.00% | 53.49% / 53.29% | +--------------+-----------------+---------------------+ | 16 / 255 | 0.00% / 0.00% | 18.13% / 17.62% | +--------------+-----------------+---------------------+
ImageNet L2 范数 (ResNet50):
ε = 3.0 <https://www.dropbox.com/s/knf4uimlqsi1yz8/imagenet_l2_3_0.pt?dl=0>_+--------------+-----------------+---------------------+ | ImageNet L2-robust accuracy | +--------------+-----------------+---------------------+ | | ε-train | +--------------+-----------------+---------------------+ | ε-test | 0.0 | 3.0 | +==============+=================+=====================+ | 0.0 | 76.13% / - | 57.90% / - | +--------------+-----------------+---------------------+ | 0.5 | 3.35% / 2.98% | 54.42% / 54.42% | +--------------+-----------------+---------------------+ | 1.0 | 0.44% / 0.37% | 50.67% / 50.67% | +--------------+-----------------+---------------------+ | 2.0 | 0.16% / 0.14% | 43.04% / 43.02% | +--------------+-----------------+---------------------+ | 3.0 | 0.13% / 0.12% | 35.16% / 35.09% | +--------------+-----------------+---------------------+
ImageNet Linf 范数 (ResNet50):
ε = 4 / 255 <https://www.dropbox.com/s/axfuary2w1cnyrg/imagenet_linf_4.pt?dl=0>_ε = 8 / 255 <https://www.dropbox.com/s/yxn15a9zklz3s8q/imagenet_linf_8.pt?dl=0>_+--------------+-----------------+---------------------+---------------------+ | ImageNet Linf-robust accuracy | +--------------+-----------------+---------------------+---------------------+ | | ε-train | +--------------+-----------------+---------------------+---------------------+ | ε-test | 0.0 | 4 / 255 | 8 / 255 | +==============+=================+=====================+=====================+ | 0 / 255 | 76.13% / - | 62.42% / - | 47.91% / - | +--------------+-----------------+---------------------+---------------------+ | 4 / 255 | 0.04% / 0.03% | 33.58% / 33.38% | 33.06% / 33.03% | +--------------+-----------------+---------------------+---------------------+ | 8 / 255 | 0.01% / 0.01% | 13.13% / 12.73% | 19.63% / 19.52% | +--------------+-----------------+---------------------+---------------------+ | 16 / 255 | 0.01% / 0.01% | 1.53% / 1.37% | 5.00% / 4.82% | +--------------+-----------------+---------------------+---------------------+
如果您的科研使用了本库,请按照以下方式引用:
.. code-block:: bibtex
@misc{robustness, title={Robustness (Python Library)}, author={Logan Engstrom and Andrew Ilyas and Hadi Salman and Shibani Santurkar and Dimitris Tsipras}, year={2019}, url={https://github.com/MadryLab/robustness} }
(您已经使用过这个包并且觉得它有用吗?请告诉我们!)。
Andrew Ilyas <https://twitter.com/andrew_ilyas>_Logan Engstrom <https://twitter.com/logan_engstrom>_Shibani Santurkar <https://twitter.com/ShibaniSan>_Dimitris Tsipras <https://twitter.com/tsiprasd>_Hadi Salman <https://twitter.com/hadisalmanX>_贡献者/提交者 '''''''''''''''''''''''
此处 <https://github.com/MadryLab/robustness/pulse>_