timm documentation
Vision Transformer (relative position)
Vision Transformer (relative position)
Category: ViT · Code: timm/models/vision_transformer_relpos.py · Architectures: 14 · Pretrained weights: 9 (9 with ImageNet-1k results)
Vision Transformers using relative position biases instead of absolute position embeddings. The module docstring marks these models as experimental.
Papers
- An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
- Swin Transformer V2: Scaling Up Capacity and Resolution
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('vit_relpos_small_patch16_224.sw_in1k', pretrained=True)
>>> data_config = timm.data.resolve_model_data_config(model)Model API support
| Feature | VisionTransformerRelPos |
|---|---|
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 (VisionTransformerRelPos with vit_srelpos_small_patch16_224 at 224), other architectures of the same class are expected to behave the same. See the Model API page for details.
Architectures
| Architecture | Params (M) | GMACs | Input size | Pretrained weights |
|---|---|---|---|---|
test_vit_relpos | 0.85 | - | 160 | 0 |
vit_relpos_base_patch16_224 | 86.43 | 17.51 | 224 | 1 |
vit_relpos_base_patch16_cls_224 | 86.43 | 17.60 | 224 | 0 |
vit_relpos_base_patch16_clsgap_224 | 86.43 | 17.60 | 224 | 1 |
vit_relpos_base_patch16_plus_240 | 117.38 | 27.30 | 240 | 0 |
vit_relpos_base_patch16_rpn_224 | 86.41 | 17.51 | 224 | 0 |
vit_relpos_base_patch32_plus_rpn_256 | 119.42 | 7.68 | 256 | 1 |
vit_relpos_medium_patch16_224 | 38.75 | 7.97 | 224 | 1 |
vit_relpos_medium_patch16_cls_224 | 38.76 | 8.03 | 224 | 1 |
vit_relpos_medium_patch16_rpn_224 | 38.73 | 7.97 | 224 | 1 |
vit_relpos_small_patch16_224 | 21.98 | 4.59 | 224 | 1 |
vit_relpos_small_patch16_rpn_224 | 21.97 | 4.59 | 224 | 0 |
vit_srelpos_medium_patch16_224 | 38.74 | 7.96 | 224 | 1 |
vit_srelpos_small_patch16_224 | 21.97 | 4.59 | 224 | 1 |
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.
| Weight | Input size | Top-1 (%) | Eval size | Classes | License | Hub |
|---|---|---|---|---|---|---|
vit_relpos_base_patch16_224.sw_in1k | 224 | 82.482 | 224 | 1000 | apache-2.0 | timm |
vit_relpos_base_patch16_clsgap_224.sw_in1k | 224 | 82.758 | 224 | 1000 | apache-2.0 | timm |
vit_relpos_base_patch32_plus_rpn_256.sw_in1k | 256 | 79.508 | 256 | 1000 | apache-2.0 | timm |
vit_relpos_medium_patch16_224.sw_in1k | 224 | 82.462 | 224 | 1000 | apache-2.0 | timm |
vit_relpos_medium_patch16_cls_224.sw_in1k | 224 | 82.558 | 224 | 1000 | apache-2.0 | timm |
vit_relpos_medium_patch16_rpn_224.sw_in1k | 224 | 82.314 | 224 | 1000 | apache-2.0 | timm |
vit_relpos_small_patch16_224.sw_in1k | 224 | 81.476 | 224 | 1000 | apache-2.0 | timm |
vit_srelpos_medium_patch16_224.sw_in1k | 224 | 82.256 | 224 | 1000 | apache-2.0 | timm |
vit_srelpos_small_patch16_224.sw_in1k | 224 | 81.108 | 224 | 1000 | apache-2.0 | timm |