timm documentation

Vision Transformer (relative position)

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

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

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

FeatureVisionTransformerRelPos
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

ArchitectureParams (M)GMACsInput sizePretrained weights
test_vit_relpos0.85-1600
vit_relpos_base_patch16_22486.4317.512241
vit_relpos_base_patch16_cls_22486.4317.602240
vit_relpos_base_patch16_clsgap_22486.4317.602241
vit_relpos_base_patch16_plus_240117.3827.302400
vit_relpos_base_patch16_rpn_22486.4117.512240
vit_relpos_base_patch32_plus_rpn_256119.427.682561
vit_relpos_medium_patch16_22438.757.972241
vit_relpos_medium_patch16_cls_22438.768.032241
vit_relpos_medium_patch16_rpn_22438.737.972241
vit_relpos_small_patch16_22421.984.592241
vit_relpos_small_patch16_rpn_22421.974.592240
vit_srelpos_medium_patch16_22438.747.962241
vit_srelpos_small_patch16_22421.974.592241

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.

WeightInput sizeTop-1 (%)Eval sizeClassesLicenseHub
vit_relpos_base_patch16_224.sw_in1k22482.4822241000apache-2.0timm
vit_relpos_base_patch16_clsgap_224.sw_in1k22482.7582241000apache-2.0timm
vit_relpos_base_patch32_plus_rpn_256.sw_in1k25679.5082561000apache-2.0timm
vit_relpos_medium_patch16_224.sw_in1k22482.4622241000apache-2.0timm
vit_relpos_medium_patch16_cls_224.sw_in1k22482.5582241000apache-2.0timm
vit_relpos_medium_patch16_rpn_224.sw_in1k22482.3142241000apache-2.0timm
vit_relpos_small_patch16_224.sw_in1k22481.4762241000apache-2.0timm
vit_srelpos_medium_patch16_224.sw_in1k22482.2562241000apache-2.0timm
vit_srelpos_small_patch16_224.sw_in1k22481.1082241000apache-2.0timm
Update on GitHub