Home

Awesome

DeepViT

This repo is the official implementation of "DeepViT: Towards Deeper Vision Transformer". The repo is based on the timm library (https://github.com/rwightman/pytorch-image-models) by Ross Wightman

Introduction

Deep Vision Transformer is initially described in arxiv, which observes the attention collapese phenomenon when training deep vision transformers: In this paper, we show that, unlike convolution neural networks (CNNs)that can be improved by stacking more convolutional layers, the performance of ViTs saturate fast when scaled to be deeper. More specifically, we empirically observe that such scaling difficulty is caused by the attention collapse issue: as the transformer goes deeper, the attention maps gradually become similar and even much the same after certain layers. In other words, the feature maps tend to be identical in the top layers of deep ViT models. This fact demonstrates that in deeper layers of ViTs, the self-attention mechanism fails to learn effective concepts for representation learning and hinders the model from getting expected performance gain. Based on above observation, we propose a simple yet effective method, named Re-attention, to re-generate the attention maps to increase their diversity at different layers with negligible computation and memory cost. The pro-posed method makes it feasible to train deeper ViT models with consistent performance improvements via minor modification to existing ViT models. Notably, when training a deep ViT model with 32 transformer blocks, the Top-1 classification accuracy can be improved by 1.6% on ImageNet.

<p align="center"> <img src="https://github.com/zhoudaquan/DeepViT_ICCV21/blob/master/figures/performance_comparison.png" | width=500> </p>

2. DeepViT Models

  1. Comparison with ViTs
ModelRe-attentionTop1 Acc (%)#params#Similar BlocksCheckpointAttention Map
ViT-16NA78.8824.5M5[download here](comming soon)
DeepViT-16FC79.1024.5M0[download here](comming soon)
ViT-24NA79.3536.3M11[download here](comming soon)
DeepViT-24FC79.9936.3M0download here
ViT-32NA79.2748.1M15[download here](comming soon)
DeepViT-32FC80.9048.1M0download here
  1. DeepViTs with CNNs for patch processing and optimized training hyper-parameters
ModelRe-attentionTop1 Acc (%)#ParamsImage SizeCheckpoint
DeepViT-16FC + BN82.324.5M224download here
DeepViT-32FC + BN83.148.1M224download here
DeepViT-32FC + BN84.2548.1M384[download here](comming soon)

Evaluation

To evaluate a pre-trained DeepViT models on ImageNet val run:

bash eval.sh

Attention Map Visualization

To visualize the self-attention map, first save the attention map matrix into a pickle file and run

python attn_visualize.py

The pickle file of the vit baseline model can be downloaded here. The visualization rsults is shown below:

<div align="center"> <img width="100%" alt="Self-attention map visualization a Vision Transformer with 16x16 patches trained with basic training hyper-parameter" src="https://github.com/zhoudaquan/dvit_repo/blob/master/figures/attention_map_visualization.png"> </div>

Citing DeepVit

@article{zhou2021deepvit,
  title={DeepViT: Towards Deeper Vision Transformer},
  author={Zhou, Daquan and Kang, Bingyi and Jin, Xiaojie and Yang, Linjie and Lian, Xiaochen and Hou, Qibin and Feng, Jiashi},
  journal={arXiv preprint arXiv:2103.11886},
  year={2021}
}