|
|
před 1 dnem | |
|---|---|---|
| .. | ||
| deploy | před 1 dnem | |
| README.md | před 1 dnem | |
| metafile.yml | před 1 dnem | |
| riformer-m36_8xb128_in1k.py | před 1 dnem | |
| riformer-m36_8xb64_in1k-384px.py | před 1 dnem | |
| riformer-m48_8xb64_in1k-384px.py | před 1 dnem | |
| riformer-m48_8xb64_in1k.py | před 1 dnem | |
| riformer-s12_8xb128_in1k-384px.py | před 1 dnem | |
| riformer-s12_8xb128_in1k.py | před 1 dnem | |
| riformer-s24_8xb128_in1k-384px.py | před 1 dnem | |
| riformer-s24_8xb128_in1k.py | před 1 dnem | |
| riformer-s36_8xb128_in1k.py | před 1 dnem | |
| riformer-s36_8xb64_in1k-384px.py | před 1 dnem | |
RIFormer: Keep Your Vision Backbone Effective But Removing Token Mixer
RIFormer is a way to keep a vision backbone effective while removing token mixers in its basic building blocks. Equipped with our proposed optimization strategy, we are able to build an extremely simple vision backbone with encouraging performance, while enjoying the high efficiency during inference. RIFormer shares nearly the same macro and micro design as MetaFormer, but safely removing all token mixers. The quantitative results show that our networks outperform many prevailing backbones with faster inference speed on ImageNet-1K.
The checkpoints provided are all training-time models. Use the reparameterize tool or switch_to_deploy interface to switch them to more efficient inference-time architecture, which not only has fewer parameters but also less calculations.
Predict image
Use classifier.backbone.switch_to_deploy() interface to switch the RIFormer models into inference mode.
>>> import torch
>>> from mmpretrain import get_model, inference_model
>>>
>>> model = get_model("riformer-s12_in1k", pretrained=True)
>>> results = inference_model(model, 'demo/demo.JPEG')
>>> print( (results['pred_class'], results['pred_score']) )
('sea snake', 0.7827484011650085)
>>>
>>> # switch to deploy mode
>>> model.backbone.switch_to_deploy()
>>> results = inference_model(model, 'demo/demo.JPEG')
>>> print( (results['pred_class'], results['pred_score']) )
('sea snake', 0.7827480435371399)
Use the model
>>> import torch
>>>
>>> model = get_model("riformer-s12_in1k", pretrained=True)
>>> model.eval()
>>> inputs = torch.rand(1, 3, 224, 224).to(model.data_preprocessor.device)
>>> # To get classification scores.
>>> out = model(inputs)
>>> print(out.shape)
torch.Size([1, 1000])
>>> # To extract features.
>>> outs = model.extract_feat(inputs)
>>> print(outs[0].shape)
torch.Size([1, 512])
>>>
>>> # switch to deploy mode
>>> model.backbone.switch_to_deploy()
>>> out_deploy = model(inputs)
>>> print(out.shape)
torch.Size([1, 1000])
>>> assert torch.allclose(out, out_deploy, rtol=1e-4, atol=1e-5) # pass without error
Test Command
Place the ImageNet dataset to the data/imagenet/ directory, or prepare datasets according to the docs.
224×224
Download Checkpoint:
wget https://download.openmmlab.com/mmclassification/v1/riformer/riformer-s12_32xb128_in1k_20230406-6741ce71.pth
Test use unfused model:
python tools/test.py configs/riformer/riformer-s12_8xb128_in1k.py riformer-s12_32xb128_in1k_20230406-6741ce71.pth
Reparameterize checkpoint:
python tools/model_converters/reparameterize_model.py configs/riformer/riformer-s12_8xb128_in1k.py riformer-s12_32xb128_in1k_20230406-6741ce71.pth riformer-s12_deploy.pth
Test use fused model:
python tools/test.py configs/riformer/deploy/riformer-s12-deploy_8xb128_in1k.py riformer-s12_deploy.pth
For more configurable parameters, please refer to the API.
6741ce71.pth
# reparameterize unfused weight to fused weight
python tools/model_converters/reparameterize_model.py configs/riformer/riformer-s12_8xb128_in1k.py riformer-s12_32xb128_in1k_20230406-6741ce71.pth riformer-s12_deploy.pth
```
To use reparameterized weights, you can use the deploy model config file such as the [s12_deploy example](./deploy/riformer-s12-deploy_8xb128_in1k.py):
```text
# in riformer-s12-deploy_8xb128_in1k.py
_base_ = '../deploy/riformer-s12-deploy_8xb128_in1k.py' # basic s12 config
model = dict(backbone=dict(deploy=True)) # switch model into deploy mode
```
```shell
python tools/test.py configs/riformer/deploy/riformer-s12-deploy_8xb128_in1k.py riformer-s12_deploy.pth
```
| Model | resolution | Params(M) | Flops(G) | Top-1 (%) | Top-5 (%) | Config | Download |
|---|---|---|---|---|---|---|---|
| riformer-s12_in1k | 224x224 | 11.92 | 1.82 | 76.90 | 93.06 | config | model |
| riformer-s24_in1k | 224x224 | 21.39 | 3.41 | 80.28 | 94.80 | config | model |
| riformer-s36_in1k | 224x224 | 30.86 | 5.00 | 81.29 | 95.41 | config | model |
| riformer-m36_in1k | 224x224 | 56.17 | 8.80 | 82.57 | 95.99 | config | model |
| riformer-m48_in1k | 224x224 | 73.47 | 11.59 | 82.75 | 96.11 | config | model |
| riformer-s12_384_in1k | 384x384 | 11.92 | 5.36 | 78.29 | 93.93 | config | model |
| riformer-s24_384_in1k | 384x384 | 21.39 | 10.03 | 81.36 | 95.40 | config | model |
| riformer-s36_384_in1k | 384x384 | 30.86 | 14.70 | 82.22 | 95.95 | config | model |
| riformer-m36_384_in1k | 384x384 | 56.17 | 25.87 | 83.39 | 96.40 | config | model |
| riformer-m48_384_in1k | 384x384 | 73.47 | 34.06 | 83.70 | 96.60 | config | model |
The config files of these models are only for inference.
@inproceedings{wang2023riformer,
title={RIFormer: Keep Your Vision Backbone Effective But Removing Token Mixer},
author={Wang, Jiahao and Zhang, Songyang and Liu, Yong and Wu, Taiqiang and Yang, Yujiu and Liu, Xihui and Chen, Kai and Luo, Ping and Lin, Dahua},
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
year={2023}
}