Skip to content
class

CoAtNet

extendsPretrainedModelBackboneMixin
CoAtNet(config: CoAtNetConfig)

CoAtNet backbone (Dai et al., 2021).

CoAtNet is a hybrid backbone that interleaves depthwise convolutions and relative self-attention in a single four-stage pyramid (preceded by a two-layer convolutional stem). The first two stages (S1,S2S_1, S_2) use MBConv blocks (squeeze-and- excitation, expansion ratio 4) and the last two stages (S3,S4S_3, S_4) use relative-attention transformer blocks operating on flattened token sequences:

Attn(Q,K,V)ij=softmax ⁣(QiKj⊤d+ri−j)Vj,\mathrm{Attn}(Q, K, V)_{ij} = \mathrm{softmax}\!\left( \frac{Q_i K_j^\top}{\sqrt{d}} + r_{i - j} \right) V_j,

where ri−jr_{i-j} is a learned bias indexed by the relative spatial offset between tokens. This recovers the translation equivariance that convolutions provide while still permitting global, data-dependent mixing. Each stage downsamples 2×2\times, so the final feature map is (B,dS4,H/32,W/32)(B, d_{S_4}, H/32, W/32).

forward_features returns the raw spatial feature map from the last attention stage. Use this backbone when you need multi-scale or spatial features for detection / segmentation; for end-to-end classification use CoAtNetForImageClassification.

Parameters

Frozen dataclass specifying blocks_per_stage, dims, stem_width, attn_heads, mbconv_expand, image_size, and in_channels. See CoAtNetConfig.

Attributes

Two-layer stride-2 convolutional stem (3×3 Conv,BN,GELU)2(3\times3 \,\mathrm{Conv}, \mathrm{BN}, \mathrm{GELU})^2.
First MBConv stage with downsampling.
Second MBConv stage with downsampling.
s3_TransformerStage
First relative-attention transformer stage.
s4_TransformerStage
Second relative-attention transformer stage.
feature_infolist[FeatureInfo]
Four-stage feature description with reductions (4,8,16,32)(4, 8, 16, 32).

Notes

Reference: Zihang Dai et al., "CoAtNet: Marrying Convolution and Attention for All Data Sizes", NeurIPS 2021, arXiv:2106.04803.

Examples

Build a CoAtNet-0 backbone and run a forward pass:
>>> import lucid
>>> from lucid.models.vision.coatnet import CoAtNet, CoAtNetConfig
>>> model = CoAtNet(CoAtNetConfig())
>>> x = lucid.randn(1, 3, 224, 224)
>>> feat = model.forward_features(x)
>>> feat.shape                       # (B, dims[-1], H/32, W/32)
(1, 768, 7, 7)

Used by 2

Constructors

1
dunder

__init__

→None
__init__(config: CoAtNetConfig)

Instance methods

2
fn

forward_features

→Tensor
forward_features(x: Tensor)

Run the backbone's feature-extraction pass.

Parameters

Input image batch, shape (B, C, H, W).

Returns

Tensor

Feature map at the deepest stage. Concrete subclasses may additionally stash earlier stages on the instance so multi-stage consumers can fetch them.

Examples

The map returned is the one feature_info lists last, so a
neck can size itself from that entry without running the model.
>>> import lucid
>>> from lucid.models import create_model
>>> backbone = create_model("resnet_18").eval()
>>> last = backbone.feature_info[-1]
>>> last.num_channels, last.reduction
(512, 32)
>>> backbone.forward_features(lucid.randn((1, 3, 64, 96))).shape
(1, 512, 2, 3)
Each side is divided by the advertised reduction on its own, so
a non-square input keeps its aspect ratio.