EfficientFormer
PretrainedModelBackboneMixinEfficientFormer(config: EfficientFormerConfig)EfficientFormer backbone (Li et al., 2022).
EfficientFormer is a mobile-grade vision backbone designed so that its on-device latency, rather than its FLOP count, matches MobileNet on the same hardware while retaining transformer-level accuracy. The trunk has four stages preceded by a two stride-2 convolutional stem. Stages 1-3 are MetaFormer-style pooling blocks operating in layout — no reshape, no self-attention:
with initialised to (CaiT-style layer scale). The last stage switches its trailing blocks to standard multi-head self-attention with a learned relative-position bias on the now-tiny token grid.
forward_features returns the mean-pooled
feature.
Parameters
configEfficientFormerConfigdepths, embed_dims,
mlp_ratios, num_vit, drop_path_rate,
layer_scale_init, in_channels, and num_classes.
See EfficientFormerConfig.Attributes
stem_Stemstagesnn.ModuleListnormnn.LayerNormfeature_ infolist[FeatureInfo]Notes
Reference: Yanyu Li et al., "EfficientFormer: Vision Transformers at MobileNet Speed", NeurIPS 2022, arXiv:2206.01191.
Examples
Build an EfficientFormer-L1 backbone and run a forward pass:
>>> import lucid
>>> from lucid.models.vision.efficientformer import (
... EfficientFormer, EfficientFormerConfig,
... )
>>> model = EfficientFormer(EfficientFormerConfig())
>>> x = lucid.randn(1, 3, 224, 224)
>>> feat = model.forward_features(x)
>>> feat.shape # (B, embed_dims[-1])
(1, 448)Used by 2
Constructors
1Instance methods
2forward(x: Tensor)Override in subclasses to define the computation.
Run the backbone's feature-extraction pass.
Parameters
(B, C, H, W).Returns
TensorFeature 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.