Skip to content
class

CrossViT

extendsPretrainedModelBackboneMixin
CrossViT(config: CrossViTConfig)

CrossViT backbone (Chen et al., ICCV 2021).

Builds two parallel ViT branches that operate on different resolutions and exchange information through K=3 cross-attention fusion blocks. See CrossViTConfig for the per-variant hyperparameters.

Examples

The backbone alone, built from a config rather than a factory. It
answers with a pooled vector, not a feature map — the two branches
have already been fused and their class tokens concatenated.
>>> import lucid
>>> from lucid.models.vision.crossvit import CrossViT, CrossViTConfig
>>> model = CrossViT(CrossViTConfig()).eval()
>>> out = model(lucid.randn(1, 3, 240, 240))
>>> out.last_hidden_state.shape
(1, 288)
240 pixels rather than 224: the branches take 12- and 16-pixel
patches, and 240 is the smaller size both divide.

Used by 2

Constructors

1
dunder

__init__

→None
__init__(config: CrossViTConfig)

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.