tensorflow/models · error · ClassifierError
Failed to load state dict into model
Error message
Failed to load state dict into model: {error} What it means
RuntimeError from model.load_state_dict translated to ClassifierError in from_config: the checkpoint loaded and passed key-presence checks, but PyTorch rejected it when copying parameters into the model — shape mismatches (a different number_of_classes changing head.weight, or a different backbone variant) or missing/unexpected keys under strict loading. The original error text is preserved so the exact offending parameter is identifiable.
Solutions
- Match number_of_classes in the config to the classes the checkpoint was trained on (head.weight shape differs otherwise)
- Use a checkpoint from the same backbone variant and pooling strategy as configured
- Read the appended error for the exact parameter mismatch and align the config accordingly
- Re-export the checkpoint from a matching training run rather than force-loading a mismatched one
Defensive patterns
Strategy: try-catch
When it happens
Trigger: Thrown at official/projects/waste_identification_ml/model_inference_with_tracking/sam3_dinov3_tracking_pipeline/dinov3_classifier.py:351 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24).
Data as JSON: /api/errors/93788091839b187c.
Report an issue: GitHub.
Appendix: source
Thrown at official/projects/waste_identification_ml/model_inference_with_tracking/sam3_dinov3_tracking_pipeline/dinov3_classifier.py:351
# DINOv3 Vision Transformers store token embedding dimension in
# norm.normalized_shape[0].
hidden_size = backbone_model.norm.normalized_shape[0]
pooling = _infer_pooling_from_state_dict(
saved_state_dict=saved_state_dict, hidden_size=hidden_size
)
model = Dinov3ClassificationModule(
backbone_model=backbone_model,
hidden_size=hidden_size,
number_of_classes=len(class_names),
pooling=pooling,
).to(resolved_device)
try:
model.load_state_dict(saved_state_dict)
except RuntimeError as error:
raise ClassifierError(
f"Failed to load state dict into model: {error}"
) from error
model.eval()
return cls(
model=model,
class_names=class_names,
image_transform=_build_image_transform(config),
device=resolved_device,
)
@torch.no_grad()
def predict_batch(self, images: Sequence[Image.Image]) -> list[Prediction]:
"""Classifies a batch of PIL images in a single forward pass.
Args:
images: Sequence of PIL images to classify.
View on GitHub (pinned to e006f5f0d5)