opensi·si

timm/models/mlpmixer.py ·

MLP-Mixer, ResMLP and gMLP

Category: MLP · Code: timm/models/mlp_mixer.py · Architectures: 15 · Pretrained weights: 19 (14 with ImageNet-1k results)

All-MLP architectures that alternate token mixing across patches with channel mixing: MLP-Mixer, ResMLP and gMLP (gating with a spatial projection). The module docstring notes the gMLP implementation is not verified.

Papers

  • MLP-Mixer: An all-MLP Architecture for Vision
  • ResMLP: Feedforward networks for image classification with data-efficient training
  • Pay Attention to MLPs
  • ImageNet-21K Pretraining for the Masses
  • Emerging Properties in Self-Supervised Vision Transformers

Usage

See the Quickstart for loading pretrained weights, image preprocessing and fine-tuning, and Feature Extraction for using the models as backbones.

>>> import timm
>>> model = timm.create_model('gmixer_24_224.ra3_in1k', pretrained=True)
>>> data_config = timm.data.resolve_model_data_config(model)

Model API support

Feature MlpMixer
features_only=True feature maps ✓ (strides 16, 16, 16)
forward_intermediates() ✓
prune_intermediate_layers() ✓
Any input size, no changes needed ✗
set_input_size() ✗
dynamic_img_size=True ✗
set_grad_checkpointing() ✓
Layer-wise LR decay (group_matcher()) ✓
reset_classifier() ✓

✓ supported, ✓ (square) works for square sizes only, ✗ not supported, – not needed (the model already accepts any input size). Checked by running each feature on a randomly initialized model (MlpMixer with gmlp_ti16_224 at 224), other architectures of the same class are expected to behave the same. See the Model API page for details.

Architectures

Table (16 rows)
Architecture Params (M) GMACs Input size Pretrained weights
gmixer_12_224 12.70 2.67 224 0
gmixer_24_224 24.72 5.28 224 1
gmlp_b16_224 73.08 15.78 224 0
gmlp_s16_224 19.42 4.42 224 1
gmlp_ti16_224 5.87 1.34 224 0
mixer_b16_224 59.88 12.62 224 4
mixer_b32_224 60.29 3.24 224 0
mixer_l16_224 208.20 44.60 224 2
mixer_l32_224 206.94 11.27 224 0
mixer_s16_224 18.53 3.79 224 0
mixer_s32_224 19.10 1.00 224 0
resmlp_12_224 15.35 3.01 224 3
resmlp_24_224 30.02 5.96 224 3
resmlp_36_224 44.69 8.91 224 2
resmlp_big_24_224 129.14 100.23 224 3

GMACs and parameter counts are for the default input size and classifier of each architecture, from the benchmark results.

Pretrained weights

Top-1 is ImageNet-1k validation accuracy from results-imagenet.csv at the listed eval size, - if the weight has no ImageNet-1k result (e.g. pretraining or image-text weights). Classes is the size of the pretrained classifier, none for weights without one.

Weights are grouped by source: the paper or origin given in the pretrained config, else a Hub organization other than timm, else the first part of the pretrained tag (architecture.tag). Sources with a single weight are listed under other.

fb tags

Weight Input size Top-1 (%) Eval size Classes License Hub
resmlp_12_224.fb_dino 224 - - 1000 apache-2.0 timm
resmlp_12_224.fb_distilled_in1k 224 77.948 224 1000 apache-2.0 timm
resmlp_12_224.fb_in1k 224 76.660 224 1000 apache-2.0 timm
resmlp_24_224.fb_dino 224 - - 1000 apache-2.0 timm
resmlp_24_224.fb_distilled_in1k 224 80.766 224 1000 apache-2.0 timm
resmlp_24_224.fb_in1k 224 79.380 224 1000 apache-2.0 timm
resmlp_36_224.fb_distilled_in1k 224 81.150 224 1000 apache-2.0 timm
resmlp_36_224.fb_in1k 224 79.772 224 1000 apache-2.0 timm
resmlp_big_24_224.fb_distilled_in1k 224 83.594 224 1000 apache-2.0 timm
resmlp_big_24_224.fb_in1k 224 81.034 224 1000 apache-2.0 timm
resmlp_big_24_224.fb_in22k_ft_in1k 224 84.414 224 1000 apache-2.0 timm

goog tags

Weight Input size Top-1 (%) Eval size Classes License Hub
mixer_b16_224.goog_in21k 224 - - 21843 apache-2.0 timm
mixer_b16_224.goog_in21k_ft_in1k 224 76.616 224 1000 apache-2.0 timm
mixer_l16_224.goog_in21k 224 - - 21843 apache-2.0 timm
mixer_l16_224.goog_in21k_ft_in1k 224 72.084 224 1000 apache-2.0 timm

miil tags

Weight Input size Top-1 (%) Eval size Classes License Hub
mixer_b16_224.miil_in21k 224 - - 11221 apache-2.0 timm
mixer_b16_224.miil_in21k_ft_in1k 224 82.314 224 1000 apache-2.0 timm

ra3 tags

Weight Input size Top-1 (%) Eval size Classes License Hub
gmixer_24_224.ra3_in1k 224 78.048 224 1000 apache-2.0 timm
gmlp_s16_224.ra3_in1k 224 79.652 224 1000 apache-2.0 timm

Citation

@article{tolstikhin2021,
  title={MLP-Mixer: An all-MLP Architecture for Vision},
  author={Tolstikhin, Ilya and Houlsby, Neil and Kolesnikov, Alexander and Beyer, Lucas and Zhai, Xiaohua and Unterthiner,
        Thomas and Yung, Jessica and Keysers, Daniel and Uszkoreit, Jakob and Lucic, Mario and Dosovitskiy, Alexey},
  journal={arXiv preprint arXiv:2105.01601},
  year={2021}
}

@misc{touvron2021resmlp,
      title={ResMLP: Feedforward networks for image classification with data-efficient training},
      author={Hugo Touvron and Piotr Bojanowski and Mathilde Caron and Matthieu Cord and Alaaeldin El-Nouby and
        Edouard Grave and Armand Joulin and Gabriel Synnaeve and Jakob Verbeek and Hervé Jégou},
      year={2021},
      eprint={2105.03404},
}

@misc{liu2021pay,
      title={Pay Attention to MLPs},
      author={Hanxiao Liu and Zihang Dai and David R. So and Quoc V. Le},
      year={2021},
      eprint={2105.08050},
}

Source and license

Source: timm documentation, adapted for this site. Licensing and attribution follow the terms of the upstream project.