Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add PoolFormer (CVPR'2022) #1537

Merged
merged 10 commits into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Supported backbones:
- [x] [BEiT (ICLR'2022)](configs/beit)
- [x] [ConvNeXt (CVPR'2022)](configs/convnext)
- [x] [MAE (CVPR'2022)](configs/mae)
- [x] [PoolFormer (CVPR'2022)](configs/poolformer)

Supported methods:

Expand Down
1 change: 1 addition & 0 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ MMSegmentation 是一个基于 PyTorch 的语义分割开源工具箱。它是 O
- [x] [BEiT (ICLR'2022)](configs/beit)
- [x] [ConvNeXt (CVPR'2022)](configs/convnext)
- [x] [MAE (CVPR'2022)](configs/mae)
- [x] [PoolFormer (CVPR'2022)](configs/poolformer)

已支持的算法:

Expand Down
42 changes: 42 additions & 0 deletions configs/_base_/models/fpn_poolformer_s12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# model settings
norm_cfg = dict(type='SyncBN', requires_grad=True)
checkpoint_file = 'https://download.openmmlab.com/mmclassification/v0/poolformer/poolformer-s12_3rdparty_32xb128_in1k_20220414-f8d83051.pth' # noqa
custom_imports = dict(imports='mmcls.models', allow_failed_imports=False)
model = dict(
type='EncoderDecoder',
backbone=dict(
type='mmcls.PoolFormer',
arch='s12',
init_cfg=dict(
type='Pretrained', checkpoint=checkpoint_file, prefix='backbone.'),
in_patch_size=7,
in_stride=4,
in_pad=2,
down_patch_size=3,
down_stride=2,
down_pad=1,
drop_rate=0.,
drop_path_rate=0.,
out_indices=(0, 2, 4, 6),
frozen_stages=0,
),
neck=dict(
type='FPN',
in_channels=[256, 512, 1024, 2048],
out_channels=256,
num_outs=4),
decode_head=dict(
type='FPNHead',
in_channels=[256, 256, 256, 256],
in_index=[0, 1, 2, 3],
feature_strides=[4, 8, 16, 32],
channels=128,
dropout_ratio=0.1,
num_classes=19,
norm_cfg=norm_cfg,
align_corners=False,
loss_decode=dict(
type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0)),
# model training and testing settings
train_cfg=dict(),
test_cfg=dict(mode='whole'))
63 changes: 63 additions & 0 deletions configs/poolformer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# PoolFormer

[MetaFormer is Actually What You Need for Vision](https://arxiv.org/abs/2111.11418)

## Introduction

<!-- [BACKBONE] -->

<a href="https://github.com/sail-sg/poolformer/tree/main/segmentation">Official Repo</a>

<a href="https://github.com/open-mmlab/mmclassification/blob/v0.23.0/mmcls/models/backbones/poolformer.py#L198">Code Snippet</a>

## Abstract

<!-- [ABSTRACT] -->

Transformers have shown great potential in computer vision tasks. A common belief is their attention-based token mixer module contributes most to their competence. However, recent works show the attention-based module in transformers can be replaced by spatial MLPs and the resulted models still perform quite well. Based on this observation, we hypothesize that the general architecture of the transformers, instead of the specific token mixer module, is more essential to the model's performance. To verify this, we deliberately replace the attention module in transformers with an embarrassingly simple spatial pooling operator to conduct only the most basic token mixing. Surprisingly, we observe that the derived model, termed as PoolFormer, achieves competitive performance on multiple computer vision tasks. For example, on ImageNet-1K, PoolFormer achieves 82.1% top-1 accuracy, surpassing well-tuned vision transformer/MLP-like baselines DeiT-B/ResMLP-B24 by 0.3%/1.1% accuracy with 35%/52% fewer parameters and 48%/60% fewer MACs. The effectiveness of PoolFormer verifies our hypothesis and urges us to initiate the concept of "MetaFormer", a general architecture abstracted from transformers without specifying the token mixer. Based on the extensive experiments, we argue that MetaFormer is the key player in achieving superior results for recent transformer and MLP-like models on vision tasks. This work calls for more future research dedicated to improving MetaFormer instead of focusing on the token mixer modules. Additionally, our proposed PoolFormer could serve as a starting baseline for future MetaFormer architecture design. Code is available at [this https URL](https://github.com/sail-sg/poolformer)

<!-- [IMAGE] -->

<div align=center>
<img src="https://user-images.githubusercontent.com/15921929/144710761-1635f59a-abde-4946-984c-a2c3f22a19d2.png" width="70%"/>
</div>

## Citation

```bibtex
@inproceedings{yu2022metaformer,
title={Metaformer is actually what you need for vision},
author={Yu, Weihao and Luo, Mi and Zhou, Pan and Si, Chenyang and Zhou, Yichen and Wang, Xinchao and Feng, Jiashi and Yan, Shuicheng},
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
pages={10819--10829},
year={2022}
}
```

### Usage

- PoolFormer backbone needs to install [MMClassification](https://github.com/open-mmlab/mmclassification) first, which has abundant backbones for downstream tasks.

```shell
pip install mmcls>=0.23.0
```

- The pretrained models could also be downloaded from [PoolFormer config of MMClassification](https://github.com/open-mmlab/mmclassification/tree/master/configs/poolformer).

## Results and models

### ADE20K

| Method | Backbone | Crop Size | pretrain | Batch Size | Lr schd | Mem (GB) | Inf time (fps) | mIoU | mIoU(ms+flip) | mIoU\* | mIoU\*(ms+flip) | config | download |
| ------ | -------------- | --------- | ----------- | ---------- | ------- | -------- | -------------- | ----- | ------------: | ------ | --------------: | ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| FPN | PoolFormer-S12 | 512x512 | ImageNet-1K | 32 | 40000 | 4.17 | 23.48 | 36.0 | 36.42 | 37.07 | 38.44 | [config](https://github.com/open-mmlab/mmsegmentation/blob/master/configs/poolformer/fpn_poolformer_s12_8x4_512x512_40k_ade20k.py) | [model](https://download.openmmlab.com/mmsegmentation/v0.5/poolformer/fpn_poolformer_s12_8x4_512x512_40k_ade20k/fpn_poolformer_s12_8x4_512x512_40k_ade20k_20220501_115154-b5aa2f49.pth) \| [log](https://download.openmmlab.com/mmsegmentation/v0.5/poolformer/fpn_poolformer_s12_8x4_512x512_40k_ade20k/fpn_poolformer_s12_8x4_512x512_40k_ade20k_20220501_115154.log.json) |
| FPN | PoolFormer-S24 | 512x512 | ImageNet-1K | 32 | 40000 | 5.47 | 15.74 | 39.35 | 39.73 | 40.36 | 41.08 | [config](https://github.com/open-mmlab/mmsegmentation/blob/master/configs/poolformer/fpn_poolformer_s24_8x4_512x512_40k_ade20k.py) | [model](https://download.openmmlab.com/mmsegmentation/v0.5/poolformer/fpn_poolformer_s24_8x4_512x512_40k_ade20k/fpn_poolformer_s24_8x4_512x512_40k_ade20k_20220503_222049-394a7cf7.pth) \| [log](https://download.openmmlab.com/mmsegmentation/v0.5/poolformer/fpn_poolformer_s24_8x4_512x512_40k_ade20k/fpn_poolformer_s24_8x4_512x512_40k_ade20k_20220503_222049.log.json) |
| FPN | PoolFormer-S36 | 512x512 | ImageNet-1K | 32 | 40000 | 6.77 | 11.34 | 40.64 | 40.99 | 41.81 | 42.72 | [config](https://github.com/open-mmlab/mmsegmentation/blob/master/configs/poolformer/fpn_poolformer_s36_8x4_512x512_40k_ade20k.py) | [model](https://download.openmmlab.com/mmsegmentation/v0.5/poolformer/fpn_poolformer_s36_8x4_512x512_40k_ade20k/fpn_poolformer_s36_8x4_512x512_40k_ade20k_20220501_151122-b47e607d.pth) \| [log](https://download.openmmlab.com/mmsegmentation/v0.5/poolformer/fpn_poolformer_s36_8x4_512x512_40k_ade20k/fpn_poolformer_s36_8x4_512x512_40k_ade20k_20220501_151122.log.json) |
| FPN | PoolFormer-M36 | 512x512 | ImageNet-1K | 32 | 40000 | 8.59 | 8.97 | 40.91 | 41.28 | 42.35 | 43.34 | [config](https://github.com/open-mmlab/mmsegmentation/blob/master/configs/poolformer/fpn_poolformer_m36_8x4_512x512_40k_ade20k.py) | [model](https://download.openmmlab.com/mmsegmentation/v0.5/poolformer/fpn_poolformer_m36_8x4_512x512_40k_ade20k/fpn_poolformer_m36_8x4_512x512_40k_ade20k_20220501_164230-3dc83921.pth) \| [log](https://download.openmmlab.com/mmsegmentation/v0.5/poolformer/fpn_poolformer_m36_8x4_512x512_40k_ade20k/fpn_poolformer_m36_8x4_512x512_40k_ade20k_20220501_164230.log.json) |
| FPN | PoolFormer-M48 | 512x512 | ImageNet-1K | 32 | 40000 | 10.48 | 6.69 | 41.82 | 42.2 | 42.76 | 43.57 | [config](https://github.com/open-mmlab/mmsegmentation/blob/master/configs/poolformer/fpn_poolformer_m48_8x4_512x512_40k_ade20k.py) | [model](https://download.openmmlab.com/mmsegmentation/v0.5/poolformer/fpn_poolformer_m48_8x4_512x512_40k_ade20k/fpn_poolformer_m48_8x4_512x512_40k_ade20k_20220504_003923-64168d3b.pth) \| [log](https://download.openmmlab.com/mmsegmentation/v0.5/poolformer/fpn_poolformer_m48_8x4_512x512_40k_ade20k/fpn_poolformer_m48_8x4_512x512_40k_ade20k_20220504_003923.log.json) |

Note:

- We replace `AlignedResize` in original PoolFormer implementation to `Resize + ResizeToMultiple`.

- `mIoU` with * is collected when `Resize + ResizeToMultiple` is adopted in `test_pipeline`, so do `mIoU` in logs.
11 changes: 11 additions & 0 deletions configs/poolformer/fpn_poolformer_m36_8x4_512x512_40k_ade20k.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
_base_ = './fpn_poolformer_s12_8x4_512x512_40k_ade20k.py'
checkpoint_file = 'https://download.openmmlab.com/mmclassification/v0/poolformer/poolformer-m36_3rdparty_32xb128_in1k_20220414-c55e0949.pth' # noqa

# model settings
model = dict(
backbone=dict(
arch='m36',
init_cfg=dict(
type='Pretrained', checkpoint=checkpoint_file,
prefix='backbone.')),
neck=dict(in_channels=[96, 192, 384, 768]))
11 changes: 11 additions & 0 deletions configs/poolformer/fpn_poolformer_m48_8x4_512x512_40k_ade20k.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
_base_ = './fpn_poolformer_s12_8x4_512x512_40k_ade20k.py'
checkpoint_file = 'https://download.openmmlab.com/mmclassification/v0/poolformer/poolformer-m48_3rdparty_32xb128_in1k_20220414-9378f3eb.pth' # noqa

# model settings
model = dict(
backbone=dict(
arch='m48',
init_cfg=dict(
type='Pretrained', checkpoint=checkpoint_file,
prefix='backbone.')),
neck=dict(in_channels=[96, 192, 384, 768]))
74 changes: 74 additions & 0 deletions configs/poolformer/fpn_poolformer_s12_8x4_512x512_40k_ade20k.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
_base_ = [
'../_base_/models/fpn_poolformer_s12.py', '../_base_/default_runtime.py',
'../_base_/schedules/schedule_40k.py'
]

# model settings
model = dict(
neck=dict(in_channels=[64, 128, 320, 512]),
decode_head=dict(num_classes=150))

# optimizer
optimizer = dict(_delete_=True, type='AdamW', lr=0.0002, weight_decay=0.0001)
optimizer_config = dict()
# learning policy
lr_config = dict(policy='poly', power=0.9, min_lr=0.0, by_epoch=False)

# dataset settings
dataset_type = 'ADE20KDataset'
data_root = 'data/ade/ADEChallengeData2016'
img_norm_cfg = dict(
mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
crop_size = (512, 512)
train_pipeline = [
dict(type='LoadImageFromFile'),
dict(type='LoadAnnotations', reduce_zero_label=True),
dict(type='Resize', img_scale=(2048, 512), ratio_range=(0.5, 2.0)),
dict(type='RandomCrop', crop_size=crop_size, cat_max_ratio=0.75),
dict(type='RandomFlip', prob=0.5),
dict(type='PhotoMetricDistortion'),
dict(type='Normalize', **img_norm_cfg),
dict(type='Pad', size=crop_size, pad_val=0, seg_pad_val=255),
dict(type='DefaultFormatBundle'),
dict(type='Collect', keys=['img', 'gt_semantic_seg']),
]
test_pipeline = [
dict(type='LoadImageFromFile'),
dict(
type='MultiScaleFlipAug',
img_scale=(2048, 512),
# img_ratios=[0.5, 0.75, 1.0, 1.25, 1.5, 1.75],
flip=False,
transforms=[
dict(type='Resize', keep_ratio=True),
dict(type='ResizeToMultiple', size_divisor=32),
dict(type='RandomFlip'),
dict(type='Normalize', **img_norm_cfg),
dict(type='ImageToTensor', keys=['img']),
dict(type='Collect', keys=['img']),
])
]
data = dict(
samples_per_gpu=4,
workers_per_gpu=4,
train=dict(
type='RepeatDataset',
times=50,
dataset=dict(
type=dataset_type,
data_root=data_root,
img_dir='images/training',
ann_dir='annotations/training',
pipeline=train_pipeline)),
val=dict(
type=dataset_type,
data_root=data_root,
img_dir='images/validation',
ann_dir='annotations/validation',
pipeline=test_pipeline),
test=dict(
type=dataset_type,
data_root=data_root,
img_dir='images/validation',
ann_dir='annotations/validation',
pipeline=test_pipeline))
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
_base_ = './fpn_poolformer_s12_8x4_512x512_40k_ade20k.py'
checkpoint_file = 'https://download.openmmlab.com/mmclassification/v0/poolformer/poolformer-s24_3rdparty_32xb128_in1k_20220414-d7055904.pth' # noqa
# model settings
model = dict(
backbone=dict(
arch='s24',
init_cfg=dict(
type='Pretrained', checkpoint=checkpoint_file,
prefix='backbone.')))
10 changes: 10 additions & 0 deletions configs/poolformer/fpn_poolformer_s36_8x4_512x512_40k_ade20k.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
_base_ = './fpn_poolformer_s12_8x4_512x512_40k_ade20k.py'
checkpoint_file = 'https://download.openmmlab.com/mmclassification/v0/poolformer/poolformer-s36_3rdparty_32xb128_in1k_20220414-d78ff3e8.pth' # noqa

# model settings
model = dict(
backbone=dict(
arch='s36',
init_cfg=dict(
type='Pretrained', checkpoint=checkpoint_file,
prefix='backbone.')))
111 changes: 111 additions & 0 deletions configs/poolformer/poolformer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
Models:
- Name: fpn_poolformer_s12_8x4_512x512_40k_ade20k
In Collection: FPN
Metadata:
backbone: PoolFormer-S12
crop size: (512,512)
lr schd: 40000
inference time (ms/im):
- value: 42.59
hardware: V100
backend: PyTorch
batch size: 1
mode: FP32
resolution: (512,512)
Training Memory (GB): 4.17
Results:
- Task: Semantic Segmentation
Dataset: ADE20K
Metrics:
mIoU: 36.0
mIoU(ms+flip): 36.42
Config: configs/poolformer/fpn_poolformer_s12_8x4_512x512_40k_ade20k.py
Weights: https://download.openmmlab.com/mmsegmentation/v0.5/poolformer/fpn_poolformer_s12_8x4_512x512_40k_ade20k/fpn_poolformer_s12_8x4_512x512_40k_ade20k_20220501_115154-b5aa2f49.pth
- Name: fpn_poolformer_s24_8x4_512x512_40k_ade20k
In Collection: FPN
Metadata:
backbone: PoolFormer-S24
crop size: (512,512)
lr schd: 40000
inference time (ms/im):
- value: 63.53
hardware: V100
backend: PyTorch
batch size: 1
mode: FP32
resolution: (512,512)
Training Memory (GB): 5.47
Results:
- Task: Semantic Segmentation
Dataset: ADE20K
Metrics:
mIoU: 39.35
mIoU(ms+flip): 39.73
Config: configs/poolformer/fpn_poolformer_s24_8x4_512x512_40k_ade20k.py
Weights: https://download.openmmlab.com/mmsegmentation/v0.5/poolformer/fpn_poolformer_s24_8x4_512x512_40k_ade20k/fpn_poolformer_s24_8x4_512x512_40k_ade20k_20220503_222049-394a7cf7.pth
- Name: fpn_poolformer_s36_8x4_512x512_40k_ade20k
In Collection: FPN
Metadata:
backbone: PoolFormer-S36
crop size: (512,512)
lr schd: 40000
inference time (ms/im):
- value: 88.18
hardware: V100
backend: PyTorch
batch size: 1
mode: FP32
resolution: (512,512)
Training Memory (GB): 6.77
Results:
- Task: Semantic Segmentation
Dataset: ADE20K
Metrics:
mIoU: 40.64
mIoU(ms+flip): 40.99
Config: configs/poolformer/fpn_poolformer_s36_8x4_512x512_40k_ade20k.py
Weights: https://download.openmmlab.com/mmsegmentation/v0.5/poolformer/fpn_poolformer_s36_8x4_512x512_40k_ade20k/fpn_poolformer_s36_8x4_512x512_40k_ade20k_20220501_151122-b47e607d.pth
- Name: fpn_poolformer_m36_8x4_512x512_40k_ade20k
In Collection: FPN
Metadata:
backbone: PoolFormer-M36
crop size: (512,512)
lr schd: 40000
inference time (ms/im):
- value: 111.48
hardware: V100
backend: PyTorch
batch size: 1
mode: FP32
resolution: (512,512)
Training Memory (GB): 8.59
Results:
- Task: Semantic Segmentation
Dataset: ADE20K
Metrics:
mIoU: 40.91
mIoU(ms+flip): 41.28
Config: configs/poolformer/fpn_poolformer_m36_8x4_512x512_40k_ade20k.py
Weights: https://download.openmmlab.com/mmsegmentation/v0.5/poolformer/fpn_poolformer_m36_8x4_512x512_40k_ade20k/fpn_poolformer_m36_8x4_512x512_40k_ade20k_20220501_164230-3dc83921.pth
- Name: fpn_poolformer_m48_8x4_512x512_40k_ade20k
In Collection: FPN
Metadata:
backbone: PoolFormer-M48
crop size: (512,512)
lr schd: 40000
inference time (ms/im):
- value: 149.48
hardware: V100
backend: PyTorch
batch size: 1
mode: FP32
resolution: (512,512)
Training Memory (GB): 10.48
Results:
- Task: Semantic Segmentation
Dataset: ADE20K
Metrics:
mIoU: 41.82
mIoU(ms+flip): 42.2
Config: configs/poolformer/fpn_poolformer_m48_8x4_512x512_40k_ade20k.py
Weights: https://download.openmmlab.com/mmsegmentation/v0.5/poolformer/fpn_poolformer_m48_8x4_512x512_40k_ade20k/fpn_poolformer_m48_8x4_512x512_40k_ade20k_20220504_003923-64168d3b.pth
1 change: 1 addition & 0 deletions model-index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Import:
- configs/nonlocal_net/nonlocal_net.yml
- configs/ocrnet/ocrnet.yml
- configs/point_rend/point_rend.yml
- configs/poolformer/poolformer.yml
- configs/psanet/psanet.yml
- configs/pspnet/pspnet.yml
- configs/resnest/resnest.yml
Expand Down