timm documentation

Swin Transformer

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v1.0.31).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Swin Transformer

Category: ViT · Code: timm/models/swin_transformer.py · Architectures: 9 · Pretrained weights: 19 (13 with ImageNet-1k results)

Swin Transformer is a hierarchical vision transformer that computes self-attention within local windows, shifted between consecutive blocks for cross-window connections. Includes the S3 (AutoFormerV2) weights.

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('swin_tiny_patch4_window7_224.ms_in1k', pretrained=True)
>>> data_config = timm.data.resolve_model_data_config(model)

Model API support

FeatureSwinTransformer
features_only=True feature maps✓ (strides 4, 8, 16, 32)
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 (SwinTransformer with swin_tiny_patch4_window7_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
swin_base_patch4_window7_22487.7715.472243
swin_base_patch4_window12_38487.9047.193843
swin_large_patch4_window7_224196.5334.532242
swin_large_patch4_window12_384196.74104.083842
swin_s3_base_22471.1313.692241
swin_s3_small_22449.749.432241
swin_s3_tiny_22428.334.642241
swin_small_patch4_window7_22449.618.772243
swin_tiny_patch4_window7_22428.294.512243

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
swin_base_patch4_window7_224.ms_in1k22483.6042241000mittimm
swin_base_patch4_window7_224.ms_in22k224--21841mittimm
swin_base_patch4_window7_224.ms_in22k_ft_in1k22485.2762241000mittimm
swin_base_patch4_window12_384.ms_in1k38484.4783841000mittimm
swin_base_patch4_window12_384.ms_in22k384--21841mittimm
swin_base_patch4_window12_384.ms_in22k_ft_in1k38486.4423841000mittimm
swin_large_patch4_window7_224.ms_in22k224--21841mittimm
swin_large_patch4_window7_224.ms_in22k_ft_in1k22486.3202241000mittimm
swin_large_patch4_window12_384.ms_in22k384--21841mittimm
swin_large_patch4_window12_384.ms_in22k_ft_in1k38487.1423841000mittimm
swin_s3_base_224.ms_in1k22483.9202241000mittimm
swin_s3_small_224.ms_in1k22483.7782241000mittimm
swin_s3_tiny_224.ms_in1k22482.1282241000mittimm
swin_small_patch4_window7_224.ms_in1k22483.2282241000mittimm
swin_small_patch4_window7_224.ms_in22k224--21841mittimm
swin_small_patch4_window7_224.ms_in22k_ft_in1k22483.3302241000mittimm
swin_tiny_patch4_window7_224.ms_in1k22481.3882241000mittimm
swin_tiny_patch4_window7_224.ms_in22k224--21841mittimm
swin_tiny_patch4_window7_224.ms_in22k_ft_in1k22480.9662241000mittimm
Update on GitHub