{"id":13737767,"url":"https://github.com/snap-research/EfficientFormer","last_synced_at":"2025-05-08T15:31:22.016Z","repository":{"id":37211272,"uuid":"499272646","full_name":"snap-research/EfficientFormer","owner":"snap-research","description":"EfficientFormerV2 [ICCV 2023] \u0026 EfficientFormer [NeurIPs 2022]","archived":false,"fork":false,"pushed_at":"2023-08-13T13:28:39.000Z","size":2376,"stargazers_count":966,"open_issues_count":22,"forks_count":93,"subscribers_count":36,"default_branch":"main","last_synced_at":"2024-08-04T03:11:09.474Z","etag":null,"topics":["deep-learning","detection","efficient-inference","efficient-neural-networks","imagenet","mobile-devices","pytorch","semantic-segmentation","transformer","transformers"],"latest_commit_sha":null,"homepage":"https://arxiv.org/abs/2212.08059","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/snap-research.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-06-02T19:59:36.000Z","updated_at":"2024-07-22T06:24:21.000Z","dependencies_parsed_at":"2024-01-07T06:00:00.152Z","dependency_job_id":"314210f7-381f-42f3-800c-ba7b5dd82c90","html_url":"https://github.com/snap-research/EfficientFormer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snap-research%2FEfficientFormer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snap-research%2FEfficientFormer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snap-research%2FEfficientFormer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snap-research%2FEfficientFormer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/snap-research","download_url":"https://codeload.github.com/snap-research/EfficientFormer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224742345,"owners_count":17362229,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","detection","efficient-inference","efficient-neural-networks","imagenet","mobile-devices","pytorch","semantic-segmentation","transformer","transformers"],"created_at":"2024-08-03T03:02:00.295Z","updated_at":"2024-11-15T06:30:57.281Z","avatar_url":"https://github.com/snap-research.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"## EfficientFormerV2\u003cbr\u003e\u003csub\u003eRethinking Vision Transformers for MobileNet Size and Speed\u003c/sub\u003e\n\n[arXiv](https://arxiv.org/abs/2212.08059) | [PDF](https://arxiv.org/pdf/2212.08059.pdf)\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"images/eformerv2.png\" width=70%\u003e \u003cbr\u003e\n  Models are trained on ImageNet-1K and deployed on iPhone 12 with CoreMLTools to get latency.\n\u003c/p\u003e\n\n\u003e[Rethinking Vision Transformers for MobileNet Size and Speed](https://arxiv.org/abs/2212.08059)\u003cbr\u003e\n\u003e[Yanyu Li](https://scholar.google.com/citations?view_op=list_works\u0026hl=en\u0026hl=en\u0026user=XUj8koUAAAAJ\u0026sortby=pubdate)\u003csup\u003e1,2\u003c/sup\u003e, [Ju Hu](https://www.linkedin.com/in/erichuju/)\u003csup\u003e1\u003c/sup\u003e, [Yang Wen](https://www.linkedin.com/in/yang-wen-76749924/)\u003csup\u003e1\u003c/sup\u003e, [Georgios Evangelidis](https://sites.google.com/site/georgeevangelidis/)\u003csup\u003e1\u003c/sup\u003e, [Kamyar Salahi](https://www.linkedin.com/in/kamyarsalahi/)\u003csup\u003e3\u003c/sup\u003e, \u003cbr\u003e[Yanzhi Wang](https://coe.northeastern.edu/people/wang-yanzhi/)\u003csup\u003e2\u003c/sup\u003e, [Sergey Tulyakov](http://www.stulyakov.com/)\u003csup\u003e1\u003c/sup\u003e, [Jian Ren](https://alanspike.github.io/)\u003csup\u003e1\u003c/sup\u003e  \n\u003e\u003csup\u003e1\u003c/sup\u003eSnap Inc., \u003csup\u003e2\u003c/sup\u003eNortheastern University, \u003csup\u003e3\u003c/sup\u003eUC Berkeley\n\n\u003cdetails\u003e\n  \u003csummary\u003e\n  \u003cfont size=\"+1\"\u003eAbstract\u003c/font\u003e\n  \u003c/summary\u003e\nWith 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.\nMultiple approaches are proposed to accelerate attention mechanism, improve inefficient designs, or incorporate mobile-friendly lightweight convolutions to form hybrid architectures.\nHowever, ViT and its variants still have higher latency or considerably more parameters than lightweight CNNs, even true for the years-old MobileNet. \nIn practice, latency and size are both crucial for efficient deployment on resource-constraint hardware. \nIn this work, we investigate a central question, can transformer models run as fast as MobileNet and maintain a similar size? \nWe revisit the design choices of ViTs and propose an improved supernet with low latency and high parameter efficiency. \nWe 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.\n\u003c/details\u003e\n\n\u003cbr\u003e\n\n### Changelog and ToDos\n- [x] Add EfficientFormerV2 model family, including `efficientformerv2_s0`, `efficientformerv2_s1`, `efficientformerv2_s2` and `efficientformerv2_l`. \n- [x] Pretrained checkpoints of EfficientFormerV2 on ImageNet-1K are released. \n- [x] Update EfficientFormerV2 in downstream tasks (detection, segmentation). \n- [x] Release checkpoints in downstream tasks. \n- [x] Add extra tools for profiling and deployment (we use CoreML==5.2 and Torch==1.11), example usage: \n\n```\npython toolbox.py --model efficientformerv2_l --ckpt weights/eformer_l_450.pth --onnx --coreml\n```\n\n\n## EfficientFormer\u003cbr\u003e\u003csub\u003eVision Transformers at MobileNet Speed\u003c/sub\u003e\n\n[arXiv](https://arxiv.org/abs/2206.01191) | [PDF](https://arxiv.org/pdf/2206.01191.pdf)\n\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"images/dot.png\" width=70%\u003e \u003cbr\u003e\n  Models are trained on ImageNet-1K and measured by iPhone 12 with CoreMLTools to get latency.\n\u003c/p\u003e\n\n\n\n\u003e[EfficientFormer: Vision Transformers at MobileNet Speed](https://arxiv.org/abs/2206.01191)\u003cbr\u003e\n\u003e[Yanyu Li](https://scholar.google.com/citations?view_op=list_works\u0026hl=en\u0026hl=en\u0026user=XUj8koUAAAAJ\u0026sortby=pubdate)\u003csup\u003e1,2\u003c/sup\u003e, [Genge Yuan](https://scholar.google.com/citations?user=tBIAgtgAAAAJ\u0026hl)\u003csup\u003e1,2\u003c/sup\u003e, [Yang Wen](https://www.linkedin.com/in/yang-wen-76749924/)\u003csup\u003e1\u003c/sup\u003e, [Eric Hu](https://www.linkedin.com/in/erichuju/)\u003csup\u003e1\u003c/sup\u003e, [Georgios Evangelidis](https://sites.google.com/site/georgeevangelidis/)\u003csup\u003e1\u003c/sup\u003e, \u003cbr\u003e[Sergey Tulyakov](http://www.stulyakov.com/)\u003csup\u003e1\u003c/sup\u003e, [Yanzhi Wang](https://coe.northeastern.edu/people/wang-yanzhi/)\u003csup\u003e2\u003c/sup\u003e, [Jian Ren](https://alanspike.github.io/)\u003csup\u003e1\u003c/sup\u003e  \n\u003e\u003csup\u003e1\u003c/sup\u003eSnap Inc., \u003csup\u003e2\u003c/sup\u003eNortheastern University\n\n\n\u003cdetails\u003e\n  \u003csummary\u003e\n  \u003cfont size=\"+1\"\u003eAbstract\u003c/font\u003e\n  \u003c/summary\u003e\n    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.\n\u003c/details\u003e\n\n\n\u003cbr\u003e\n\n\n\n## Classification on ImageNet-1K\n\n### Models\n\n| Model | Top-1 (300/450)| #params | MACs | Latency | ckpt | ONNX | CoreML |\n|:---------------|:----:|:---:|:--:|:--:|:--:|:--:|:--:|\n| EfficientFormerV2-S0 |   75.7 / 76.2   |     3.5M    |   0.40B   |      0.9ms     |  [S0](https://drive.google.com/file/d/1PXb7b9pv9ZB4cfkRkYEdwgWuVwvEiazq/view?usp=share_link)    |   [S0](https://drive.google.com/file/d/1xWev98ENruLnnxgFXORmrYzNG5Y8b8y6/view?usp=share_link)   |    [S0](https://drive.google.com/file/d/19G3RGcH42gqPBa42CV3yIOmSmVVFqW0X/view?usp=share_link)    |\n| EfficientFormerV2-S1 |   79.0 / 79.7   |     6.1M    |   0.65B   |      1.1ms     |   [S1](https://drive.google.com/file/d/1EKe1vt-3mG7iceVIMaET_DyISzVTJMn8/view?usp=share_link)   |   [S1](https://drive.google.com/file/d/1f4eYkY3Tkmyn_GrDk4QqbpWYcF07CukT/view?usp=share_link)   |    [S1](https://drive.google.com/file/d/1EXI0uq3yt0KpMlItjJlnghYfAzWgdbRj/view?usp=share_link)    |\n| EfficientFormerV2-S2 |   81.6 / 82.0   |    12.6M   |   1.25B   |      1.6ms     |   [S2](https://drive.google.com/file/d/1gjbFyB5T_yAkmzHNuXEljqScYVQZafMQ/view?usp=share_link)   |   [S2](https://drive.google.com/file/d/1kJj2UxUQR-ro4WslcnsukcJF9BXi0FMC/view?usp=share_link)   |    [S2](https://drive.google.com/file/d/1KQw7UMujsHrLUCDOY-pO6rM7zfp7cpDY/view?usp=share_link)    |\n| EfficientFormerV2-L |   83.3 / 83.5   |    26.1M    |   2.56B   |      2.7ms     |  [L](https://drive.google.com/file/d/1sRXNBHl_ewHBMwwYZVsOJo1k6JpcNJn-/view?usp=share_link)    |   [L](https://drive.google.com/file/d/18GfCWWdYYcEz_AOmoO6J7dW8xMtkIpcm/view?usp=share_link)   |     [L](https://drive.google.com/file/d/1Ynu7fTbqPEW9tSVZdR7ZAN1edUFAocGT/view?usp=share_link)   |\n\n\n\n| Model | Top-1 Acc.| Latency | Pytorch Checkpoint | CoreML | ONNX |\n| :------------ | :--: | :---: | :---: |:---: | :---: |\n| EfficientFormer-L1 |79.2 (80.2) | 1.6ms| [L1-300](https://drive.google.com/file/d/1wtEmkshLFEYFsX5YhBttBOGYaRvDR7nu/view?usp=sharing) ([L1-1000](https://drive.google.com/file/d/11SbX-3cfqTOc247xKYubrAjBiUmr818y/view?usp=sharing)) | [L1](https://drive.google.com/file/d/1MEDcyeKCBmrgVGrHX8wew3l4ge2CWdok/view?usp=sharing) | [L1](https://drive.google.com/file/d/10NMPW8SLLiTa2jwTTuILDQRUzMvehmUM/view?usp=sharing) |\n| EfficientFormer-L3 |82.4 | 3.0ms| [L3](https://drive.google.com/file/d/1OyyjKKxDyMj-BcfInp4GlDdwLu3hc30m/view?usp=sharing) | [L3](https://drive.google.com/file/d/12xb0_6pPAy0OWdW39seL9TStIqKyguEj/view?usp=sharing) | [L3](https://drive.google.com/file/d/1DEbsOEzP4ljS6-ka86BtwQWiVxkylCaX/view?usp=sharing) |\n| EfficientFormer-L7 |83.3  | 7.0ms| [L7](https://drive.google.com/file/d/1cVw-pctJwgvGafeouynqWWCwgkcoFMM5/view?usp=sharing) | [L7](https://drive.google.com/file/d/1CnhAyfylpvvebT9Yn3qF8vrUFjZjuO3F/view?usp=sharing) | [L7](https://drive.google.com/file/d/1u6But9JQ9Wd7vlaFTGcYm5FiGnQ8y9eS/view?usp=sharing) |\n\n\n\n## Latency Measurement \n\nThe latency reported in EffcientFormerV2 for iPhone 12 (iOS 16) uses the benchmark tool from [XCode 14](https://developer.apple.com/videos/play/wwdc2022/10027/).\n\nFor EffcientFormerV1, we use the [coreml-performance](https://github.com/vladimir-chernykh/coreml-performance). Thanks for the nice-implemented latency measurement! \n\n*Tips*: MacOS+XCode and a mobile device (iPhone 12) are needed to reproduce the reported speed. \n\n\n\n## ImageNet  \n\n### Prerequisites\n`conda` virtual environment is recommended. \n```\nconda install pytorch torchvision cudatoolkit=11.3 -c pytorch\npip install timm\npip install submitit\n```\n\n### Data preparation\n\nDownload 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:\n```\n|-- /path/to/imagenet/\n    |-- train\n    |-- val\n```\n\n### Single machine multi-GPU training\n\nWe provide an example training script `dist_train.sh` using PyTorch distributed data parallel (DDP). \n\nTo train EfficientFormer-L1 on an 8-GPU machine:\n\n```\nsh dist_train.sh efficientformer_l1 8\n```\n\nTips: specify your data path and experiment name in the script! \n\n### Multi-node training\n\nOn a Slurm-managed cluster, multi-node training can be launched through [submitit](https://github.com/facebookincubator/submitit), for example, \n\n```\nsh slurm_train.sh efficientformer_l1\n```\n\nTips: specify GPUs/CPUs/memory per node in the script based on your resource!\n\n### Testing \n\nWe provide an example test script `dist_test.sh` using PyTorch distributed data parallel (DDP). \nFor example, to test EfficientFormer-L1 on an 8-GPU machine:\n\n```\nsh dist_test.sh efficientformer_l1 8 weights/efficientformer_l1_300d.pth\n```\n\n## Using EfficientFormer as backbone\n[Object Detection and Instance Segmentation](detection/README.md)\u003cbr\u003e\n[Semantic Segmentation](segmentation/README.md)\n## Acknowledgement\n\nClassification (ImageNet) code base is partly built with [LeViT](https://github.com/facebookresearch/LeViT) and [PoolFormer](https://github.com/sail-sg/poolformer). \n\nThe detection and segmentation pipeline is from [MMCV](https://github.com/open-mmlab/mmcv) ([MMDetection](https://github.com/open-mmlab/mmdetection) and [MMSegmentation](https://github.com/open-mmlab/mmsegmentation)). \n\nThanks for the great implementations! \n\n## Citation\n\nIf our code or models help your work, please cite EfficientFormer (NeurIPs 2022) and EfficientFormerV2 (ICCV 2023):\n```BibTeX\n@article{li2022efficientformer,\n  title={Efficientformer: Vision transformers at mobilenet speed},\n  author={Li, Yanyu and Yuan, Geng and Wen, Yang and Hu, Ju and Evangelidis, Georgios and Tulyakov, Sergey and Wang, Yanzhi and Ren, Jian},\n  journal={Advances in Neural Information Processing Systems},\n  volume={35},\n  pages={12934--12949},\n  year={2022}\n}\n```\n```BibTeX\n@inproceedings{li2022rethinking,\n  title={Rethinking Vision Transformers for MobileNet Size and Speed},\n  author={Li, Yanyu and Hu, Ju and Wen, Yang and Evangelidis, Georgios and Salahi, Kamyar and Wang, Yanzhi and Tulyakov, Sergey and Ren, Jian},\n  booktitle={Proceedings of the IEEE international conference on computer vision},\n  year={2023}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsnap-research%2FEfficientFormer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsnap-research%2FEfficientFormer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsnap-research%2FEfficientFormer/lists"}