Home

Awesome

[NeurIPS 2021] Twins: Revisiting the Design of Spatial Attention in Vision Transformers

NeurIPS

Very recently, a variety of vision transformer architectures for dense prediction tasks have been proposed and they show that the design of spatial attention is critical to their success in these tasks. In this work, we revisit the design of the spatial attention and demonstrate that a carefully-devised yet simple spatial attention mechanism performs favourably against the state-of-the-art schemes. As a result, we propose two vision transformer architectures, namely, Twins- PCPVT and Twins-SVT. Our proposed architectures are highly-efficient and easy to implement, only involving matrix multiplications that are highly optimized in modern deep learning frameworks. More importantly, the proposed architectures achieve excellent performance on a wide range of visual tasks including image- level classification as well as dense detection and segmentation. The simplicity and strong performance suggest that our proposed architectures may serve as stronger backbones for many vision tasks.

Twins-SVT-S Figure 1. Twins-SVT-S Architecture (Right side shows the inside of two consecutive Transformer Encoders).

Usage

First, clone the repository locally:

git clone https://github.com/Meituan-AutoML/Twins.git

Then, install PyTorch 1.7.0+ and torchvision 0.8.1+ and pytorch-image-models==0.3.2:

conda install -c pytorch pytorch torchvision
pip install timm==0.3.2

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 datasets.ImageFolder, 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

Model Zoo

Image Classification

We provide a series of Twins models pretrained on ILSVRC2012 ImageNet-1K dataset.

ModelAlias in the paperAcc@1FLOPs(G)#Params (M)URLLog
PCPVT-SmallTwins-PCPVT-S81.23.724.1pcpvt_small.pthpcpvt_s.txt
PCPVT-BaseTwins-PCPVT-B82.76.443.8pcpvt_base.pthpcpvt_b.txt
PCPVT-LargeTwins-PCPVT-L83.19.560.9pcpvt_large.pthpcpvt_l.txt
ALTGVT-SmallTwins-SVT-S81.72.824alt_gvt_small.pthsvt_s.txt
ALTGVT-BaseTwins-SVT-B83.28.356alt_gvt_base.pthsvt_b.txt
ALTGVT-LargeTwins-SVT-L83.714.899.2alt_gvt_large.pthsvt_l.txt

Training

To train Twins-SVT-B on ImageNet using 8 gpus for 300 epochs, run

python -m torch.distributed.launch --nproc_per_node=8 --use_env main.py --model alt_gvt_base --batch-size 128 --data-path path_to_imagenet --dist-eval --drop-path 0.3

Evaluation

To evaluate the performance of Twins-SVT-L on ImageNet using one GPU, run

python main.py --eval --resume alt_gvt_large.pth  --model alt_gvt_large --data-path path_to_imagenet

This should give

* Acc@1 83.660 Acc@5 96.512 loss 0.722
Accuracy of the network on the 50000 test images: 83.7%

Semantic Segmentation

Our code is based on mmsegmentation. Please install mmsegmentation first.

We provide a series of Twins models and training logs trained on the Ade20k dataset. It's easy to extend it to other datasets and segmentation methods.

ModelAlias in the papermIoU(ss/ms)FLOPs(G)#Params (M)URLLog
PCPVT-SmallTwins-PCPVT-S46.2/47.523454.6pcpvt_small.pthpcpvt_s.txt
PCPVT-BaseTwins-PCPVT-B47.1/48.425074.3pcpvt_base.pthpcpvt_b.txt
PCPVT-LargeTwins-PCPVT-L48.6/49.826991.5pcpvt_large.pthpcpvt_l.txt
ALTGVT-SmallTwins-SVT-S46.2/47.122854.4alt_gvt_small.pthsvt_s.txt
ALTGVT-BaseTwins-SVT-B47.4/48.926188.5alt_gvt_base.pthsvt_b.txt
ALTGVT-LargeTwins-SVT-L48.8/50.2297133alt_gvt_large.pthsvt_l.txt

Training

To train Twins-PCPVT-Large on Ade20k using 8 gpus for 160k iterations with a global batch size of 16, run

 bash dist_train.sh configs/upernet_pcpvt_l_512x512_160k_ade20k_swin_setting.py 8

Evaluation

To evaluate Twins-PCPVT-Large on Ade20k using 8 gpus (single scale), run

bash dist_test.sh configs/upernet_pcpvt_l_512x512_160k_ade20k_swin_setting.py checkpoint_file 8 --eval mIoU

To evaluate Twins-PCPVT-Large on Ade20k using 8 gpus (multi scale), run

bash dist_test.sh configs/upernet_pcpvt_l_512x512_160k_ade20k_swin_setting.py checkpoint_file 8 --eval mIoU --aug-test

Detection

Our code is based on mmdetection. Please install mmdetection first (we use v2.8.0). We use both Mask R-CNN and RetinaNet to evaluate our method. It's easy to apply Twins in other detectors provided by mmdetection based on our examples.

Training

To train Twins-SVT-Small on COCO with 8 gpus for 1x schedule (PVT setting) under the framework of Mask R-CNN, run

 bash dist_train.sh configs/mask_rcnn_alt_gvt_s_fpn_1x_coco_pvt_setting.py 8

To train Twins-SVT-Small on COCO with 8 gpus for 3x schedule (Swin setting) under the framework of Mask R-CNN, run

 bash dist_train.sh configs/mask_rcnn_alt_gvt_s_fpn_3x_coco_swin_setting.py 8

Evaluation

To evaluate the mAP of Twins-SVT-Small on COCO using 8 gpus based on the Retina framework, run

bash dist_test.sh configs/retinanet_alt_gvt_s_fpn_1x_coco_pvt_setting.py checkpoint_file 8   --eval mAP

Citation

If you find this project useful in your research, please consider cite the following,

Twins:

@inproceedings{chu2021Twins,
	title={Twins: Revisiting the Design of Spatial Attention in Vision Transformers},
	author={Xiangxiang Chu and Zhi Tian and Yuqing Wang and Bo Zhang and Haibing Ren and Xiaolin Wei and Huaxia Xia and Chunhua Shen},
	booktitle={NeurIPS 2021},
  url={https://openreview.net/forum?id=5kTlVBkzSRx},
	year={2021}
}

CPVT:

@inproceedings{chu2023CPVT,
	title={Conditional Positional Encodings for Vision Transformers},
	author={Xiangxiang Chu and Zhi Tian and Bo Zhang and Xinlong Wang and Chunhua Shen},
	booktitle={ICLR 2023},
	url={https://openreview.net/forum?id=3KWnuT-R1bh},
	year={2023}
}

Acknowledgement

We heavily borrow the code from DeiT and PVT. We test throughputs as in Swin Transformer.

License

This repository is released under the Apache 2.0 license as found in the LICENSE file.