class
MobileNetV2ForImageClassification
extends
PretrainedModelClassificationHeadMixinMobileNetV2ForImageClassification(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
configMobileNetV2ConfigArchitecture spec. Use the
*_cls factory functions
(mobilenet_v2_cls, mobilenet_v2_075_cls) for
paper-cited configurations.Attributes
configMobileNetV2ConfigStored copy of the config that built this model.
featuresnn.SequentialSame inverted-residual stack as on
MobileNetV2.avgpoolnn.AdaptiveAvgPool2dGlobal average pool collapsing the final feature map to
1 × 1.dropnn.DropoutDropout layer (probability
config.dropout) applied
before the linear classifier.classifiernn.LinearLinear projection from the 1280-ch head (scaled by
max(1.0, width_mult)) to config.num_classes.Notes
The classification flow is
where 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
10Used by 2
Constructors
1Instance methods
1forward(x: Tensor, labels: Tensor | None = None)