Home

Awesome

EfficientFormerV2<br><sub>Rethinking Vision Transformers for MobileNet Size and Speed</sub>

arXiv | PDF

<p align="center"> <img src="images/eformerv2.png" width=70%> <br> Models are trained on ImageNet-1K and deployed on iPhone 12 with CoreMLTools to get latency. </p>

Rethinking Vision Transformers for MobileNet Size and Speed<br> Yanyu Li<sup>1,2</sup>, Ju Hu<sup>1</sup>, Yang Wen<sup>1</sup>, Georgios Evangelidis<sup>1</sup>, Kamyar Salahi<sup>3</sup>, <br>Yanzhi Wang<sup>2</sup>, Sergey Tulyakov<sup>1</sup>, Jian Ren<sup>1</sup>
<sup>1</sup>Snap Inc., <sup>2</sup>Northeastern University, <sup>3</sup>UC Berkeley

<details> <summary> <font size="+1">Abstract</font> </summary> With the success of Vision Transformers (ViTs) in computer vision tasks, recent arts try to optimize the performance and complexity of ViTs to enable efficient deployment on mobile devices. Multiple approaches are proposed to accelerate attention mechanism, improve inefficient designs, or incorporate mobile-friendly lightweight convolutions to form hybrid architectures. However, ViT and its variants still have higher latency or considerably more parameters than lightweight CNNs, even true for the years-old MobileNet. In practice, latency and size are both crucial for efficient deployment on resource-constraint hardware. In this work, we investigate a central question, can transformer models run as fast as MobileNet and maintain a similar size? We revisit the design choices of ViTs and propose an improved supernet with low latency and high parameter efficiency. We further introduce a fine-grained joint search strategy that can find efficient architectures by optimizing latency and number of parameters simultaneously. The proposed models, EfficientFormerV2, achieve about 4% higher top-1 accuracy than MobileNetV2 and MobileNetV2x1.4 on ImageNet-1K with similar latency and parameters. We demonstrate that properly designed and optimized vision transformers can achieve high performance with MobileNet-level size and speed. </details> <br>

Changelog and ToDos

python toolbox.py --model efficientformerv2_l --ckpt weights/eformer_l_450.pth --onnx --coreml

EfficientFormer<br><sub>Vision Transformers at MobileNet Speed</sub>

arXiv | PDF

<p align="center"> <img src="images/dot.png" width=70%> <br> Models are trained on ImageNet-1K and measured by iPhone 12 with CoreMLTools to get latency. </p>

EfficientFormer: Vision Transformers at MobileNet Speed<br> Yanyu Li<sup>1,2</sup>, Genge Yuan<sup>1,2</sup>, Yang Wen<sup>1</sup>, Eric Hu<sup>1</sup>, Georgios Evangelidis<sup>1</sup>, <br>Sergey Tulyakov<sup>1</sup>, Yanzhi Wang<sup>2</sup>, Jian Ren<sup>1</sup>
<sup>1</sup>Snap Inc., <sup>2</sup>Northeastern University

<details> <summary> <font size="+1">Abstract</font> </summary> Vision Transformers (ViT) have shown rapid progress in computer vision tasks, achieving promising results on various benchmarks. However, due to the massive number of parameters and model design, e.g., attention mechanism, ViT-based models are generally times slower than lightweight convolutional networks. Therefore, the deployment of ViT for real-time applications is particularly challenging, especially on resource-constrained hardware such as mobile devices. Recent efforts try to reduce the computation complexity of ViT through network architecture search or hybrid design with MobileNet block, yet the inference speed is still unsatisfactory. This leads to an important question: can transformers run as fast as MobileNet while obtaining high performance? To answer this, we first revisit the network architecture and operators used in ViT-based models and identify inefficient designs. Then we introduce a dimension-consistent pure transformer (without MobileNet blocks) as a design paradigm. Finally, we perform latency-driven slimming to get a series of final models dubbed EfficientFormer. Extensive experiments show the superiority of EfficientFormer in performance and speed on mobile devices. Our fastest model, EfficientFormer-L1, achieves 79.2% top-1 accuracy on ImageNet-1K with only 1.6 ms inference latency on iPhone 12 (compiled with CoreML), which runs as fast as MobileNetV2x1.4 (1.6 ms, 74.7% top-1), and our largest model, EfficientFormer-L7, obtains 83.3% accuracy with only 7.0 ms latency. Our work proves that properly designed transformers can reach extremely low latency on mobile devices while maintaining high performance. </details> <br>

Classification on ImageNet-1K

Models

ModelTop-1 (300/450)#paramsMACsLatencyckptONNXCoreML
EfficientFormerV2-S075.7 / 76.23.5M0.40B0.9msS0S0S0
EfficientFormerV2-S179.0 / 79.76.1M0.65B1.1msS1S1S1
EfficientFormerV2-S281.6 / 82.012.6M1.25B1.6msS2S2S2
EfficientFormerV2-L83.3 / 83.526.1M2.56B2.7msLLL
ModelTop-1 Acc.LatencyPytorch CheckpointCoreMLONNX
EfficientFormer-L179.2 (80.2)1.6msL1-300 (L1-1000)L1L1
EfficientFormer-L382.43.0msL3L3L3
EfficientFormer-L783.37.0msL7L7L7

Latency Measurement

The latency reported in EffcientFormerV2 for iPhone 12 (iOS 16) uses the benchmark tool from XCode 14.

For EffcientFormerV1, we use the coreml-performance. Thanks for the nice-implemented latency measurement!

Tips: MacOS+XCode and a mobile device (iPhone 12) are needed to reproduce the reported speed.

ImageNet

Prerequisites

conda virtual environment is recommended.

conda install pytorch torchvision cudatoolkit=11.3 -c pytorch
pip install timm
pip install submitit

Data preparation

Download and extract ImageNet train and val images from http://image-net.org/. The training and validation data are expected to be in the train folder and val folder respectively:

|-- /path/to/imagenet/
    |-- train
    |-- val

Single machine multi-GPU training

We provide an example training script dist_train.sh using PyTorch distributed data parallel (DDP).

To train EfficientFormer-L1 on an 8-GPU machine:

sh dist_train.sh efficientformer_l1 8

Tips: specify your data path and experiment name in the script!

Multi-node training

On a Slurm-managed cluster, multi-node training can be launched through submitit, for example,

sh slurm_train.sh efficientformer_l1

Tips: specify GPUs/CPUs/memory per node in the script based on your resource!

Testing

We provide an example test script dist_test.sh using PyTorch distributed data parallel (DDP). For example, to test EfficientFormer-L1 on an 8-GPU machine:

sh dist_test.sh efficientformer_l1 8 weights/efficientformer_l1_300d.pth

Using EfficientFormer as backbone

Object Detection and Instance Segmentation<br> Semantic Segmentation

Acknowledgement

Classification (ImageNet) code base is partly built with LeViT and PoolFormer.

The detection and segmentation pipeline is from MMCV (MMDetection and MMSegmentation).

Thanks for the great implementations!

Citation

If our code or models help your work, please cite EfficientFormer (NeurIPs 2022) and EfficientFormerV2 (ICCV 2023):

@article{li2022efficientformer,
  title={Efficientformer: Vision transformers at mobilenet speed},
  author={Li, Yanyu and Yuan, Geng and Wen, Yang and Hu, Ju and Evangelidis, Georgios and Tulyakov, Sergey and Wang, Yanzhi and Ren, Jian},
  journal={Advances in Neural Information Processing Systems},
  volume={35},
  pages={12934--12949},
  year={2022}
}
@inproceedings{li2022rethinking,
  title={Rethinking Vision Transformers for MobileNet Size and Speed},
  author={Li, Yanyu and Hu, Ju and Wen, Yang and Evangelidis, Georgios and Salahi, Kamyar and Wang, Yanzhi and Tulyakov, Sergey and Ren, Jian},
  booktitle={Proceedings of the IEEE international conference on computer vision},
  year={2023}
}