Skip to content
class

MobileNetV2ForImageClassification

extendsImageClassificationModelClassificationHeadMixin
MobileNetV2ForImageClassification(config: MobileNetV2Config)

MobileNet v2 with global-average-pooled linear classification head.

Combines a MobileNetV2 backbone with the standard ImageNet classification head: an lucid.nn.AdaptiveAvgPool2d to pool every spatial location into a single feature vector, a lucid.nn.Dropout (probability config.dropout, default 0.2), and a lucid.nn.Linear projection to config.num_classes logits. When labels are supplied to forward, a cross-entropy loss is computed and returned alongside the logits.

Parameters

Architecture spec. Use the *_cls factory functions (mobilenet_v2_cls, mobilenet_v2_075_cls) for paper-cited configurations.

Attributes

Stored copy of the config that built this model.
Same inverted-residual stack as on MobileNetV2.
Global average pool collapsing the final feature map to 1 × 1.
Dropout layer (probability config.dropout) applied before the linear classifier.
classifiernn.Linear
Linear projection from the 1280-ch head (scaled by max(1.0, width_mult)) to config.num_classes.

Notes

The classification flow is

logits=W Drop⁡ ⁣(GAP⁡( backbone(x) ))+b,\text{logits} = W \,\operatorname{Drop}\!\left( \operatorname{GAP}(\,\mathrm{backbone}(x)\,) \right) + b,

where GAP⁡\operatorname{GAP} is the global average pool. Loss is the standard categorical cross-entropy, computed only when labels is not None.

Examples

Run inference on a batch of 224×224 RGB images:
>>> import lucid
>>> from lucid.models.vision.mobilenet_v2 import mobilenet_v2_cls
>>> model = mobilenet_v2_cls()
>>> x = lucid.randn(4, 3, 224, 224)
>>> out = model(x)
>>> out.logits.shape
(4, 1000)
Retarget to CIFAR-10:
>>> model = mobilenet_v2_cls(num_classes=10)
>>> model.config.num_classes
10

Used by 2

Constructors

1
dunder

__init__

→None
__init__(config: MobileNetV2Config)

Instance methods

1