Standard Linear classification head + transfer-learning helper.
Adds two methods to any classifier subclass: _build_classifier
constructs a Linear (optionally wrapped in a Dropout)
according to the config, and reset_classifier swaps in a
freshly-initialised Linear with a new num_classes for transfer
learning.
Notes
The contract: subclasses must call self._build_classifier(...)
inside their __init__ after super().__init__(config) so the
backbone is in place before the head is attached.
The host class exposes self.classifier as either a plain
nn.Linear (no dropout configured) or an nn.Sequential of
[Dropout, Linear]; reset_classifier handles both shapes.
Examples
>>> class MyClassifier(PretrainedModel, ClassificationHeadMixin):
... def __init__(self, config):
... super().__init__(config)
... self.backbone = MyBackbone(config)
... self._build_classifier(config.hidden_size, config.num_classes,
... dropout=config.dropout)
>>> model = MyClassifier(cfg)
>>> model.reset_classifier(num_classes=10)Used by 28
- lucid.models
- lucid.models.vision.alexnet._model
- lucid.models.vision.coatnet._model
- lucid.models.vision.convnext._model
- lucid.models.vision.crossvit._model
- lucid.models.vision.cspnet._model
- lucid.models.vision.cvt._model
- lucid.models.vision.densenet._model
- lucid.models.vision.efficientformer._model
- lucid.models.vision.efficientnet._model
- lucid.models.vision.googlenet._model
- lucid.models.vision.inception._model
… 16 more
Instance methods
1Replace the final Linear with a freshly initialised one.
Parameters
num_classesintin_features is preserved from
the current head.Raises
RuntimeErrorSequential containing no Linear.NotImplementedErrorNotes
Used during transfer learning — typically called immediately
after from_pretrained to adapt a 1000-class ImageNet head
to a downstream task with a different label set.
Examples
>>> model = AutoModelForImageClassification.from_pretrained("resnet_50")
>>> model.reset_classifier(num_classes=10)
>>> model(lucid.randn(1, 3, 224, 224)).logits.shape
(1, 10)