Home

Awesome

Are Transformers More Robust Than CNNs?

Pytorch implementation for NeurIPS 2021 Paper: Are Transformers More Robust Than CNNs?

Our implementation is based on DeiT.

<div align="center"> <img src="resources/teaser.png"/> </div>

Introduction

Transformer emerges as a powerful tool for visual recognition. In addition to demonstrating competitive performance on a broad range of visual benchmarks, recent works also argue that Transformers are much more robust than Convolutions Neural Networks (CNNs). Nonetheless, surprisingly, we find these conclusions are drawn from unfair experimental settings, where Transformers and CNNs are compared at different scales and are applied with distinct training frameworks. In this paper, we aim to provide the first fair & in-depth comparisons between Transformers and CNNs, focusing on robustness evaluations.

With our unified training setup, we first challenge the previous belief that Transformers outshine CNNs when measuring adversarial robustness. More surprisingly, we find CNNs can easily be as robust as Transformers on defending against adversarial attacks, if they properly adopt Transformers' training recipes. While regarding generalization on out-of-distribution samples, we show pre-training on (external) large-scale datasets is not a fundamental request for enabling Transformers to achieve better performance than CNNs. Moreover, our ablations suggest such stronger generalization is largely benefited by the Transformer's self-attention-like architectures per se, rather than by other training setups. We hope this work can help the community better understand and benchmark the robustness of Transformers and CNNs.

Pretrained models

We provide both pretrained vanilla models and adversarially trained models.

Vanilla Training

Main Results

Pretrained ModelImageNetImageNet-AImageNet-CStylized-ImageNet
Res50-Oridownload link76.93.257.98.3
Res50-Aligndownload link76.34.555.68.2
Res50-Bestdownload link75.76.352.310.8
DeiT-Smalldownload link76.812.248.013.0

Model Size

ResNets:

Model SizePretrained ModelImageNetImageNet-AImageNet-CStylized-ImageNet
Res18*11.69Mdownload link67.831.9264.147.92
Res50*25.56Mdownload link76.284.5355.628.17
Res101*44.55Mdownload link77.978.8449.1911.60
Model SizePretrained ModelImageNetImageNet-AImageNet-CStylized-ImageNet
Res18-best11.69Mdownload link66.812.0362.659.45
Res50-best25.56Mdownload link75.746.3252.2510.77
Res101-best44.55Mdownload link77.8311.4947.3513.28

DeiTs:

Model SizePretrained ModelImageNetImageNet-AImageNet-CStylized-ImageNet
DeiT-Mini9.98Mdownload link72.898.1954.689.88
DeiT-Small22.05Mdownload link76.8212.2147.9912.98

Model Distillation

ArchitecturePretrained ModelImageNetImageNet-AImageNet-CStylized-ImageNet
TeacherDeiT-Smalldownload link76.812.248.013.0
StudentRes50*-Distilldownload link76.75.254.29.8
TeacherRes50*download link76.34.555.68.2
StudentDeiT-S-Distilldownload link76.210.949.311.9

Adversarial Training

Pretrained ModelClean AccPGD-100Auto Attack
Res50-ReLUdownload link66.7732.2626.41
Res50-GELUdownload link67.3840.2735.51
DeiT-Smalldownload link66.5040.3235.50

Vanilla Training

Data preparation

Download and extract ImageNet train and val images from http://image-net.org/. The directory structure is the standard layout for the torchvision, and the training and validation data is expected to be in the train folder and val folder respectively:

/path/to/imagenet/
  train/
    class1/
      img1.jpeg
    class2/
      img2.jpeg
  val/
    class1/
      img3.jpeg
    class/2
      img4.jpeg

Environment

Install dependencies:

pip3 install -r requirements.txt

Training Scripts

To train a ResNet model on ImageNet run:

bash script/res.sh

To train a DeiT model on ImageNet run:

bash script/deit.sh

Generalization to Out-of-Distribution Sample

Data Preparation

Download and extract ImageNet-A, ImageNet-C, Stylized-ImageNet val images:

/path/to/datasets/
  val/
    class1/
      img1.jpeg
    class/2
      img2.jpeg

Evaluation Scripts

To evaluate pre-trained models, run:

bash script/generation_to_ood.sh

It is worth noting that for ImageNet-C evaluation, the error rate is calculated based on the Noise, Blur, Weather and Digital categories.

Adversarial Training

To perform adversarial training on ResNet run:

bash script/advres.sh

To do adversarial training on DeiT run:

bash scripts/advdeit.sh

Robustness to Adversarial Example

PGD Attack Evaluation

To evaluate the pre-trained models, run:

bash script/eval_advtraining.sh

AutoAttack Evaluation

./autoattack contains the AutoAttack public package, with a little modification to best support ImageNet evaluation.

cd autoattack/
bash autoattack.sh

Patch Attack Evaluation

Please refer to PatchAttack

Citation

If you use our code, models or wish to refer to our results, please use the following BibTex entry:

@inproceedings{bai2021transformers,
  title     = {Are Transformers More Robust Than CNNs?},
  author    = {Bai, Yutong and Mei, Jieru and Yuille, Alan and Xie, Cihang},
  booktitle = {Thirty-Fifth Conference on Neural Information Processing Systems},
  year      = {2021},
}