class
SwinTransformerForImageClassification
extends
PretrainedModelClassificationHeadMixinSwinTransformerForImageClassification(config: SwinConfig)Swin Transformer with a linear classification head (Liu et al., 2021).
Wraps the same hierarchical trunk as SwinTransformer (patch
embedding → four stages of window / shifted-window attention →
LayerNorm) and adds a global average pool plus a single
nn.Linear classification head that maps the
feature to config.num_classes logits:
Pass labels to forward to compute the cross-entropy
loss in the same pass.
Parameters
configSwinConfigArchitecture specification. Must set
num_classes to the
desired number of output categories (default 1000 for ImageNet).
See SwinConfig.Attributes
patch_embed_PatchEmbedStrided patch-extraction convolution + LayerNorm.
stagesnn.ModuleListFour hierarchical Swin stages with patch-merging downsamplers.
normnn.LayerNormFinal LayerNorm applied to the channel-last feature map.
avgpoolnn.AdaptiveAvgPool2d adaptive average pool over spatial dims.
classifiernn.LinearFinal linear projection of width
(num_classes, 8 * embed_dim)
built via ClassificationHeadMixin._build_classifier.Notes
Reference: Ze Liu et al., "Swin Transformer: Hierarchical Vision Transformer using Shifted Windows", ICCV 2021, arXiv:2103.14030. Swin-T / S / B / L reach 81.3 / 83.0 / 83.5 / 86.4 % top-1 on ImageNet-1k (Table 1 of the paper, 224x224 input).
Examples
End-to-end inference with the default Swin-T classifier:
>>> import lucid
>>> from lucid.models.vision.swin import (
... SwinConfig, SwinTransformerForImageClassification,
... )
>>> model = SwinTransformerForImageClassification(SwinConfig())
>>> x = lucid.randn(1, 3, 224, 224)
>>> out = model(x)
>>> out.logits.shape
(1, 1000)Used by 2
Constructors
1Instance methods
1forward(x: Tensor, labels: Tensor | None = None)