class
CvTForImageClassification
extends
PretrainedModelClassificationHeadMixinCvTForImageClassification(config: CvTConfig)CvT with a linear classification head (Wu et al., 2021).
Wraps the same three-stage trunk as CvT and adds a global
mean pool over tokens, a final LayerNorm, and a single
nn.Linear classification head:
Pass labels to forward to compute the cross-entropy
loss in the same pass.
Parameters
configCvTConfigArchitecture specification. Must set
num_classes to the
desired number of output categories. See CvTConfig.Attributes
stagesnn.ModuleListThree CvT stages.
head_normnn.LayerNormFinal LayerNorm applied to the mean-pooled feature.
classifiernn.LinearFinal linear projection of width
(num_classes, dims[-1]).Notes
Reference: Wu et al., "CvT: Introducing Convolutions to Vision Transformers", ICCV 2021. CvT-13 / CvT-21 reach 81.6% / 82.5% top-1 on ImageNet-1k at 224x224 (Table 1 of the paper).
Examples
End-to-end inference with the default CvT-13 classifier:
>>> import lucid
>>> from lucid.models.vision.cvt import (
... CvTConfig, CvTForImageClassification,
... )
>>> model = CvTForImageClassification(CvTConfig())
>>> 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)