class
CoAtNetForImageClassification
extends
PretrainedModelClassificationHeadMixinCoAtNetForImageClassification(config: CoAtNetConfig)CoAtNet with a linear classification head (Dai et al., 2021).
Wraps the same conv + attention trunk as CoAtNet (stem
- two MBConv stages + two relative-attention transformer stages) and adds the standard reference recipe head: global average pool → LayerNorm → optional pre-logits Linear + Tanh → linear classifier.
Pass labels to forward to compute the cross-entropy
loss in the same pass.
Parameters
configCoAtNetConfigArchitecture specification. Must set
num_classes to the
desired number of output categories. Set
head_hidden_size=None to drop the pre-logits projection.
See CoAtNetConfig.Attributes
stemnn.SequentialTwo-layer stride-2 convolutional stem.
s1, s2nn.SequentialTwo MBConv stages.
s3, s4_TransformerStageTwo relative-attention transformer stages.
avgpoolnn.AdaptiveAvgPool2d adaptive average pool over spatial dims.
normnn.LayerNormLayerNorm applied to the pooled feature.
pre_logitsnn.ModuleEither
Linear + Tanh (when config.head_hidden_size is
set) or an identity nn.Sequential.classifiernn.LinearFinal linear projection of width
(num_classes, head_in)
where head_in is either config.head_hidden_size or
config.dims[-1].Notes
Reference: Zihang Dai et al., "CoAtNet: Marrying Convolution and Attention for All Data Sizes", NeurIPS 2021. CoAtNet-0 reaches 81.6% top-1 on ImageNet-1k at 224x224 (Table 5).
Examples
End-to-end inference with the default CoAtNet-0 classifier:
>>> import lucid
>>> from lucid.models.vision.coatnet import (
... CoAtNetConfig, CoAtNetForImageClassification,
... )
>>> model = CoAtNetForImageClassification(CoAtNetConfig())
>>> 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)