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 をパッケージとしてインポートすることで、カスタム損失関数、ロギング、データ読み込みなどのサポートを備えたニューラルネットワークのトレーニングを簡単に行えます。良い入門としては、2部構成のウォークスルー(パート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を評価しています。この2つの精度は非常に近いため、それ以上の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-ロバスト精度 | +--------------+----------------+-----------------+---------------------+---------------------+ | | ε-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-ロバスト精度 | +--------------+-----------------+---------------------+ | | ε-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-ロバスト精度 | +--------------+-----------------+---------------------+ | | ε-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-ロバスト精度 | +--------------+-----------------+---------------------+---------------------+ | | ε-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>_ を参照してください