opensi·si

timm/models/swintransformer.py

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

  • Swin Transformer: Hierarchical Vision Transformer using Shifted Windows
  • Searching the Search Space of Vision Transformer

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

Feature SwinTransformer
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

Architecture Params (M) GMACs Input size Pretrained weights
swin_base_patch4_window7_224 87.77 15.47 224 3
swin_base_patch4_window12_384 87.90 47.19 384 3
swin_large_patch4_window7_224 196.53 34.53 224 2
swin_large_patch4_window12_384 196.74 104.08 384 2
swin_s3_base_224 71.13 13.69 224 1
swin_s3_small_224 49.74 9.43 224 1
swin_s3_tiny_224 28.33 4.64 224 1
swin_small_patch4_window7_224 49.61 8.77 224 3
swin_tiny_patch4_window7_224 28.29 4.51 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.

表格(20 行)
Weight Input size Top-1 (%) Eval size Classes License Hub
swin_base_patch4_window7_224.ms_in1k 224 83.604 224 1000 mit timm
swin_base_patch4_window7_224.ms_in22k 224 - - 21841 mit timm
swin_base_patch4_window7_224.ms_in22k_ft_in1k 224 85.276 224 1000 mit timm
swin_base_patch4_window12_384.ms_in1k 384 84.478 384 1000 mit timm
swin_base_patch4_window12_384.ms_in22k 384 - - 21841 mit timm
swin_base_patch4_window12_384.ms_in22k_ft_in1k 384 86.442 384 1000 mit timm
swin_large_patch4_window7_224.ms_in22k 224 - - 21841 mit timm
swin_large_patch4_window7_224.ms_in22k_ft_in1k 224 86.320 224 1000 mit timm
swin_large_patch4_window12_384.ms_in22k 384 - - 21841 mit timm
swin_large_patch4_window12_384.ms_in22k_ft_in1k 384 87.142 384 1000 mit timm
swin_s3_base_224.ms_in1k 224 83.920 224 1000 mit timm
swin_s3_small_224.ms_in1k 224 83.778 224 1000 mit timm
swin_s3_tiny_224.ms_in1k 224 82.128 224 1000 mit timm
swin_small_patch4_window7_224.ms_in1k 224 83.228 224 1000 mit timm
swin_small_patch4_window7_224.ms_in22k 224 - - 21841 mit timm
swin_small_patch4_window7_224.ms_in22k_ft_in1k 224 83.330 224 1000 mit timm
swin_tiny_patch4_window7_224.ms_in1k 224 81.388 224 1000 mit timm
swin_tiny_patch4_window7_224.ms_in22k 224 - - 21841 mit timm
swin_tiny_patch4_window7_224.ms_in22k_ft_in1k 224 80.966 224 1000 mit timm

Source and license

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