{"record":{"id":"54c81e9df9904f0f","repo":"huggingface/pytorch-image-models","slug":"mobilenetv5encoder-does-not-support-classification","errorCode":null,"errorMessage":"MobileNetV5Encoder does not support classification use cases.","messagePattern":"MobileNetV5Encoder does not support classification use cases\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"timm/models/mobilenetv5.py","lineNumber":594,"sourceCode":"    def forward_features(self, x: torch.Tensor) -> torch.Tensor:\n        feat_idx = 0    # offset by one from blocks index due to stem feature\n        intermediates = []\n\n        x = self.conv_stem(x)\n        if feat_idx in self.msfa_indices:\n            intermediates.append(x)\n\n        for blk in self.blocks:\n            feat_idx += 1\n            # FIXME fix grad checkpointing\n            x = blk(x)\n            if feat_idx in self.msfa_indices:\n                intermediates.append(x)\n\n        return self.msfa(intermediates)\n\n    def forward_head(self, x: torch.Tensor) -> torch.Tensor:\n        raise NotImplementedError(\"MobileNetV5Encoder does not support classification use cases.\")\n\n    def forward(self, x: torch.Tensor) -> torch.Tensor:\n        return self.forward_features(x)\n\n\ndef checkpoint_filter_fn(\n        state_dict: Dict[str, torch.Tensor],\n        model,\n) -> Dict[str, torch.Tensor]:\n    \"\"\" convert weights from gemma encoders \"\"\"\n    state_dict = state_dict.get('model', state_dict)\n    state_dict = state_dict.get('state_dict', state_dict)\n    if 'model.vision_tower.timm_model.conv_stem.conv.weight' in state_dict:\n        prefix = 'model.vision_tower.timm_model.'\n        state_dict = {k.replace(prefix, ''): v for k, v in state_dict.items() if prefix in k}\n    return state_dict\n\n","sourceCodeStart":576,"sourceCodeEnd":612,"githubUrl":"https://github.com/huggingface/pytorch-image-models/blob/9a5261e31b3b5128526eb2658333b4c0a54464ae/timm/models/mobilenetv5.py#L576-L612","documentation":"MobileNetV5Encoder is an encoder-only timm model exposing multi-scale features through an MSFA (multi-scale feature aggregation) module. It deliberately implements forward_head to raise NotImplementedError since there is no classifier head; any code path that calls forward_head on it fails.","triggerScenarios":"Calling model.forward_head(x) on a MobileNetV5Encoder instance, or generic classification code that does logits = model.forward_head(model.forward_features(x)).","commonSituations":"Plugging a timm features_only/encoder backbone into a classifier that assumes the full Model API; library helpers (e.g. timm's build_model_with_cfg pre_logits wiring or downstream frameworks) probing the head; migrating from a full MobileNetV3 to the V5 encoder without adapting the head code.","solutions":["Use the non-encoder MobileNetV5 model if you need built-in classification","Consume the encoder output (from forward/forward_features) with your own task head","Guard shared pipelines with hasattr(model, 'forward_head') checks and skip head application for encoders"],"exampleFix":"# before\nenc = timm.create_model('mobilenetv5_encoder_100', features_only=True)\nlogits = enc.forward_head(enc.forward_features(x))\n# after\nfeats = enc(x)  # multi-scale features from MSFA\nlogits = my_head(feats)","handlingStrategy":"type-guard","validationCode":"is_encoder = getattr(type(model), '__name__', '').endswith('Encoder')\nif is_encoder or not hasattr(model, 'head'):\n    feats = model(x)  # encoder output; apply custom head\n    out = my_head(feats)\nelse:\n    out = model(x)","typeGuard":"def is_encoder_only(model) -> bool:\n    return type(model).__name__.endswith('Encoder')","tryCatchPattern":"try:\n    logits = model.forward_head(x)\nexcept NotImplementedError:\n    logits = my_head(model(x))","preventionTips":["Branch pipeline logic on encoder vs full-model variants","Attach your own head for encoder backbones by design","Use hasattr(model, 'head') as a quick capability probe"],"tags":["timm","mobilenetv5","encoder-only","unsupported-operation"],"backgroundTag":"encoder-has-no-classification-head","analyzedSha":"9a5261e31b3b5128526eb2658333b4c0a54464ae","analyzedAt":"2026-08-27T02:34:25.417Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}