Skip to content
class

MobileNetForImageClassification

extendsImageClassificationModelClassificationHeadMixin
MobileNetForImageClassification(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

Architecture 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

Stored copy of the config that built this model.
Same depthwise-separable stack as on MobileNet.
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 round(1024 * 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 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

1
dunder

__init__

→None
__init__(config: MobileNetConfig)

Instance methods

1