Home

Awesome

Focal Transformer [NeurIPS 2021 Spotlight]

PWC PWC PWC PWC PWC PWC

This is the official implementation of our Focal Transformer -- "Focal Self-attention for Local-Global Interactions in Vision Transformers", by Jianwei Yang, Chunyuan Li, Pengchuan Zhang, Xiyang Dai, Bin Xiao, Lu Yuan and Jianfeng Gao.

Introduction

focal-transformer-teaser

Our Focal Transfomer introduced a new self-attention mechanism called focal self-attention for vision transformers. In this new mechanism, each token attends the closest surrounding tokens at fine granularity but the tokens far away at coarse granularity, and thus can capture both short- and long-range visual dependencies efficiently and effectively.

With our Focal Transformers, we achieved superior performance over the state-of-the-art vision Transformers on a range of public benchmarks. In particular, our Focal Transformer models with a moderate size of 51.1M and a larger size of 89.8M achieve 83.6 and 84.0 Top-1 accuracy, respectively, on ImageNet classification at 224x224 resolution. Using Focal Transformers as the backbones, we obtain consistent and substantial improvements over the current state-of-the-art methods for 6 different object detection methods trained with standard 1x and 3x schedules. Our largest Focal Transformer yields 58.7/58.9 box mAPs and 50.9/51.3 mask mAPs on COCO mini-val/test-dev, and 55.4 mIoU on ADE20K for semantic segmentation.

:film_strip: Video by The AI Epiphany

Next Generation Architecture

We had developed FocalNet, a next generation of architecture built based on the focal mechanism. It is much faster and more effective. Check it out at: https://github.com/microsoft/FocalNet!

Faster Focal Transformer

As you may notice, though the theoritical GFLOPs of our Focal Transformer is comparable to prior works, its wall-clock efficiency lags behind. Therefore, we are releasing a faster version of Focal Transformer, which discard all the rolling and unfolding operations used in our first version.

ModelPretrainUse ConvResolutionacc@1acc@5#paramsFLOPsThroughput (imgs/s)CheckpointConfig
Focal-TIN-1KNo22482.295.928.9M4.9G319downloadyaml
Focal-fast-TIN-1KYes22482.496.030.2M5.0G483downloadyaml
Focal-SIN-1KNo22483.696.251.1M9.4G192downloadyaml
Focal-fast-SIN-1KYes22483.696.451.5M9.4G293downloadyaml
Focal-BIN-1KNo22484.096.589.8M16.4G138downloadyaml
Focal-fast-BIN-1KYes22484.096.691.2M16.4G203downloadyaml

Benchmarking

Image Classification Throughput with Image Resolution

ModelTop-1 Acc.GLOPs (224x224)224x224448x448896 x 896
DeiT-Small/1679.84.693910120
PVT-Small79.83.879417231
CvT-1381.64.574612514
ViL-Small82.05.13978717
Swin-Tiny81.24.576018948
Focal-Tiny82.24.931910527
PVT-Medium81.26.751711120
CvT-2182.57.14808510
ViL-Medium83.39.1251538
Swin-Small83.18.743511128
Focal-Small83.69.41926317
ViT-Base/1677.917.6291578
Deit-Base/1681.817.6291578
PVT-Large81.79.83527714
ViL-Base83.213.4145355
Swin-Base83.415.42917017
Focal-Base84.016.41384411

Image Classification on ImageNet-1K

ModelPretrainUse ConvResolutionacc@1acc@5#paramsFLOPsCheckpointConfig
Focal-TIN-1KNo22482.295.928.9M4.9Gdownloadyaml
Focal-TIN-1KYes22482.796.130.8M5.2Gdownloadyaml
Focal-SIN-1KNo22483.696.251.1M9.4Gdownloadyaml
Focal-SIN-1KYes22483.896.553.1M9.7Gdownloadyaml
Focal-BIN-1KNo22484.096.589.8M16.4Gdownloadyaml
Focal-BIN-1KYes22484.297.193.3M16.8Gdownloadyaml

Object Detection and Instance Segmentation on COCO

Mask R-CNN

BackbonePretrainLr Schd#paramsFLOPsbox mAPmask mAP
Focal-TImageNet-1K1x49M291G44.841.0
Focal-TImageNet-1K3x49M291G47.242.7
Focal-SImageNet-1K1x71M401G47.442.8
Focal-SImageNet-1K3x71M401G48.843.8
Focal-BImageNet-1K1x110M533G47.843.2
Focal-BImageNet-1K3x110M533G49.043.7

RetinaNet

BackbonePretrainLr Schd#paramsFLOPsbox mAP
Focal-TImageNet-1K1x39M265G43.7
Focal-TImageNet-1K3x39M265G45.5
Focal-SImageNet-1K1x62M367G45.6
Focal-SImageNet-1K3x62M367G47.3
Focal-BImageNet-1K1x101M514G46.3
Focal-BImageNet-1K3x101M514G46.9

Other detection methods

BackbonePretrainMethodLr Schd#paramsFLOPsbox mAP
Focal-TImageNet-1KCascade Mask R-CNN3x87M770G51.5
Focal-TImageNet-1KATSS3x37M239G49.5
Focal-TImageNet-1KRepPointsV23x45M491G51.2
Focal-TImageNet-1KSparse R-CNN3x111M196G49.0

Semantic Segmentation on ADE20K

BackbonePretrainMethodResolutionIters#paramsFLOPsmIoUmIoU (MS)
Focal-TImageNet-1KUPerNet512x512160k62M998G45.847.0
Focal-SImageNet-1KUPerNet512x512160k85M1130G48.050.0
Focal-BImageNet-1KUPerNet512x512160k126M1354G49.050.5
Focal-LImageNet-22KUPerNet640x640160k240M3376G54.055.4

Getting Started

Citation

If you find this repo useful to your project, please consider to cite it with following bib:

@misc{yang2021focal,
    title={Focal Self-attention for Local-Global Interactions in Vision Transformers}, 
    author={Jianwei Yang and Chunyuan Li and Pengchuan Zhang and Xiyang Dai and Bin Xiao and Lu Yuan and Jianfeng Gao},
    year={2021},
    eprint={2107.00641},
    archivePrefix={arXiv},
    primaryClass={cs.CV}
}

Acknowledgement

Our codebase is built based on Swin-Transformer. We thank the authors for the nicely organized code!

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.