class
MobileNetV1ForImageClassification
extends
PretrainedModelClassificationHeadMixinMobileNetV1ForImageClassification(config: MobileNetV1Config)MobileNet v1 with global-average-pooled linear classification head.
Combines a MobileNetV1 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
configMobileNetV1ConfigArchitecture spec. Use the
*_cls factory functions
(mobilenet_v1_cls, mobilenet_v1_075_cls, …)
to obtain a paper-cited configuration; pass a custom config
to retarget num_classes or change width_mult.Attributes
configMobileNetV1ConfigStored copy of the config that built this model.
featuresnn.SequentialSame depthwise-separable stack as on
MobileNetV1.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_v1_cls
>>> model = mobilenet_v1_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)