Skip to content
module

CrossViT

21 members
lucid.models.vision.crossvit

CrossViT family — backbone + image classification (Chen et al., 2021).

Image Classification
Paper

Chen, Chun-Fu (Richard), et al. "CrossViT: Cross-Attention Multi-Scale Vision Transformer for Image Classification." Proceedings of the IEEE/CVF International Conference on Computer Vision, 2021, pp. 357-366.

Overview

CrossViT explicitly models multi-scale visual information by running two parallel ViT branches with different patch sizes and fusing them through a lightweight cross-attention mechanism. A small-patch branch (patch size 12) processes a long sequence of fine-grained tokens, while a large-patch branch (patch size 16) processes a short sequence of coarse tokens. The two branches operate on different input resolutions — the small-patch branch on the full image (e.g. 240×240240\times240), the large-patch branch on a slightly down-sampled version (e.g. 224×224224\times224) — and each has its own class token plus learnable positional embedding.

Each branch is independently transformer-encoded for NsN_s / NlN_l standard self-attention blocks; the two branches then exchange information at the end of every stage via a MultiScaleBlock: the CLS token from one branch is projected into the other branch's embedding space, cross-attends to that branch's patch tokens, and is projected back. Concretely, for the small-to-large direction,

x~cls l=xclsl+CrossAttn ⁣(Wqxclsl,Wkxpatchs,Wvxpatchs),\tilde{x}_{\text{cls}}^{\,l} = x_{\text{cls}}^{l} + \mathrm{CrossAttn}\!\bigl(W_q x_{\text{cls}}^{l}, W_k x_{\text{patch}}^{s}, W_v x_{\text{patch}}^{s}\bigr),

and symmetrically for large-to-small. Because cross-attention is only O(Ns+Nl)\mathcal{O}(N_s + N_l) per direction, multi-scale fusion is essentially free relative to the per-branch self-attention cost. The complete network is K=3K=3 such MultiScaleBlocks stacked back-to-back; the final classifier head averages the logits of the two per-branch linear layers.

The variants CrossViT-Ti / -S / -B / -9 / -15 / -18 trade depth and width along this two-branch skeleton (paper Table 2).

Classes

Functions

fncrossvit_9→ CrossViT8.2M

CrossViT-9 backbone — embed_dims=(128, 256), depths ((1, 3, 0))×3, 4 heads. ~8.6M params (paper Table 2).

fncrossvit_9_cls→ CrossViTForImageClassification8.6M

CrossViT-9 image classifier — embed_dims=(128, 256), depths ((1, 3, 0)) x 3, 4 heads per branch. ~8.6M params; paper Table 2 reports 73.9% ImageNet-1k top-1 at 240x240.

fncrossvit_tiny→ CrossViT6.7M

CrossViT-Ti backbone — embed_dims=(96, 192), depths ((1, 4, 0))×3, 3 heads. ~7M params (paper Table 2).

fncrossvit_tiny_cls→ CrossViTForImageClassification7M

CrossViT-Ti image classifier (Chen et al., ICCV 2021).

fncrossvit_small→ CrossViT26.3M

CrossViT-S backbone — embed_dims=(192, 384), depths ((1, 4, 0))×3, 6 heads. ~26.7M params (paper Table 2).

fncrossvit_small_cls→ CrossViTForImageClassification26.9M

CrossViT-S image classifier — embed_dims=(192, 384), depths ((1, 4, 0)) x 3, 6 heads per branch. ~26.7M params; paper Table 2 reports 81.0% ImageNet-1k top-1 at 240x240.

fncrossvit_base→ CrossViT104M

CrossViT-B backbone — embed_dims=(384, 768), depths ((1, 4, 0))×3, 12 heads. ~105M params (paper Table 2).

fncrossvit_base_cls→ CrossViTForImageClassification105M

CrossViT-B image classifier — embed_dims=(384, 768), depths ((1, 4, 0)) x 3, 12 heads per branch. ~105M params; paper Table 2 reports 82.2% ImageNet-1k top-1 at 240x240.

fncrossvit_15→ CrossViT27M

CrossViT-15 backbone — embed_dims=(192, 384), depths ((1, 5, 0))×3, 6 heads. ~27.4M params (paper Table 2).

fncrossvit_15_cls→ CrossViTForImageClassification27.5M

CrossViT-15 image classifier — embed_dims=(192, 384), depths ((1, 5, 0)) x 3, 6 heads per branch. ~27.4M params; paper Table 2 reports 81.5% ImageNet-1k top-1 at 240x240.

fncrossvit_18→ CrossViT42.6M

CrossViT-18 backbone — embed_dims=(224, 448), depths ((1, 6, 0))×3, 7 heads. ~43.3M params (paper Table 2).

fncrossvit_18_cls→ CrossViTForImageClassification43.3M

CrossViT-18 image classifier — embed_dims=(224, 448), depths ((1, 6, 0)) x 3, 7 heads per branch. ~43.3M params; paper Table 2 reports 82.5% ImageNet-1k top-1 at 240x240.

Weights