Skip to content
class

EfficientFormer

extendsPretrainedModelBackboneMixin
EfficientFormer(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 (B,C,H,W)(B, C, H, W) layout — no reshape, no self-attention:

x←x+γ1⊙(AvgPool3×3(x)−x),x←x+γ2⊙ConvMLP(x),\begin{aligned} x &\leftarrow x + \gamma_1 \odot \bigl( \mathrm{AvgPool}_{3 \times 3}(x) - x\bigr), \\ x &\leftarrow x + \gamma_2 \odot \mathrm{ConvMLP}(x), \end{aligned}

with γ1,γ2\gamma_1, \gamma_2 initialised to 10−510^{-5} (CaiT-style layer scale). The last stage switches its trailing num_vit\texttt{num\_vit} 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 (B,embed_dims[-1])(B, \texttt{embed\_dims[-1]}) feature.

Parameters

Frozen dataclass specifying depths, embed_dims, mlp_ratios, num_vit, drop_path_rate, layer_scale_init, in_channels, and num_classes. See EfficientFormerConfig.

Attributes

stem_Stem
Two stride-2 3×33 \times 3 convolutions reducing the input by 4x.
Four stages — three pooling stages and one mixed pooling/attention stage; stages 1-3 carry a downsample.
Final LayerNorm applied to the channel-last token sequence.
feature_infolist[FeatureInfo]
Four-stage feature description with reductions (4,8,16,32)(4, 8, 16, 32).

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

1
dunder

__init__

→None
__init__(config: EfficientFormerConfig)

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.