writing-skills
writing-skills
Use when creating new skills, editing existing skills, or verifying skills work before deployment
model-exportSave, reload, share, and deployment-check segmentation_models_pytorch models with local directories, Hugging Face Hub, ONNX, TorchScript, torch.export, and torch.compile.
Independent GitHub snapshot · Automated classification · Original documentation governs
Use this sub-skill when a task mentions save_pretrained, from_pretrained, changed class counts on load, Hugging Face Hub sharing, ONNX export, TorchScript, tracing, torch.export, or torch.compile readiness for Segmentation Models PyTorch (SMP).
../model-building/SKILL.md first.../encoders-preprocessing/SKILL.md first.../training-evaluation/SKILL.md first.SMP segmentation model classes inherit Hugging Face Hub mixin behavior and can be saved with model.save_pretrained(...). The package root exposes segmentation_models_pytorch.from_pretrained(...), which reads the saved SMP config, reconstructs the original model class, and loads weights.
import segmentation_models_pytorch as smp
model = smp.Unet("resnet34", encoder_weights=None, classes=1)
model.save_pretrained("my-smp-model", dataset="my-dataset", metrics={"miou": 0.91})
restored = smp.from_pretrained("my-smp-model")
Use strict=False when intentionally changing shape-dependent constructor arguments, especially classes, while reusing all compatible weights:
model = smp.from_pretrained("my-smp-model", classes=5, strict=False)
That path drops mismatched tensors such as segmentation_head.0.weight and segmentation_head.0.bias, warns that the new head must be trained, and preserves matching weights.
references/deployment-and-sharing.md: local save/load, Hub publishing/loading, model-card metadata, preprocessing transform notes, and changed-class reloads.references/export-compatibility.md: ONNX, TorchScript, tracing, torch.export, and torch.compile readiness checks and caveats.references/troubleshooting.md: optional dependency, Hub, class mismatch, path/repo-id, and backend/export failure guidance.scripts/check_save_load.py: creates a tiny local model, saves it, reloads with smp.from_pretrained, optionally checks strict=False class mismatch behavior, and prints JSON.scripts/check_export_readiness.py: reports installed Torch/SMP/export features, optional onnx presence, model compatibility flags, and optional dry-run forward output as JSON.Run scripts from any project environment where torch and segmentation_models_pytorch are importable:
python path/to/model-export/scripts/check_save_load.py --class-mismatch
python path/to/model-export/scripts/check_export_readiness.py --dry-run
encoder_weights=None for offline smoke tests and export probes; pretrained encoder weights can require network/cache access.dataset="..." and metrics={...} when sharing a trained model.huggingface-hub, credentials for private or write operations, and network access.