class
MobileNetForImageClassification
extends
ImageClassificationModelClassificationHeadMixinMobileNetForImageClassification(config: MobileNetConfig)MobileNet v1 with global-average-pooled linear classification head.
Combines a MobileNet 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.001 per the paper), 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
configMobileNetConfigArchitecture spec. Use the
*_cls factory functions
(mobilenet_cls, mobilenet_075_cls, …)
to obtain a paper-cited configuration; pass a custom config
to retarget num_classes or change width_mult.Attributes
configMobileNetConfigStored copy of the config that built this model.
featuresnn.SequentialSame depthwise-separable stack as on
MobileNet.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
round(1024 * 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 import mobilenet_cls
>>> model = mobilenet_cls()
>>> x = lucid.randn(4, 3, 224, 224)
>>> out = model(x)
>>> out.logits.shape
(4, 1000)
>>> out.loss is None
True
Compute a loss given labels (e.g. during training):
>>> labels = lucid.tensor([0, 1, 2, 3], dtype=lucid.int64)
>>> out = model(x, labels=labels)
>>> out.loss.shape
()Used by 2
Constructors
1Instance methods
1forward(x: Tensor, labels: Tensor | None = None)