model-export
Save, reload, share, and deployment-check segmentation_models_pytorch models with local directories, Hugging Face Hub, ONNX, TorchScript, torch.export, and torch.compile.
- License 许可
- MIT
- 最近更新
- 2026-09-03
- Stars
- ★ 351
- Forks
- 27
01 前言 Front Matter
- name
- model-export
- license
- MIT
- metadata
- { "disco-role": "operating" }
- description
- Save, reload, share, and deployment-check segmentation_models_pytorch models with local directories, Hugging Face Hub, ONNX, TorchScript, torch.export, and torch.compile.
- disable-model-invocation
- true
02 资源文件 5
- 参考文档references/deployment-and-sharing.md3.5 KB
- 参考文档references/export-compatibility.md3.8 KB
- 参考文档references/troubleshooting.md3.4 KB
- 脚本scripts/check_export_readiness.py4.3 KB
- 脚本scripts/check_save_load.py3.7 KB
03 SKILL.md 正文
Model Export
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).
Route The Work
- For architecture, decoder, auxiliary-head, or model-constructor choices, read
../model-building/SKILL.mdfirst. - For encoder selection, preprocessing functions, encoder weights, or encoder compatibility trade-offs, read
../encoders-preprocessing/SKILL.mdfirst. - For training loops, losses, metrics, validation, or fine-tuning procedure, read
../training-evaluation/SKILL.mdfirst. - For local save/load, Hub sharing, changed-output-head reloads, or deployment export checks, stay here.
Core APIs
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
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, andtorch.compilereadiness checks and caveats.references/troubleshooting.md: optional dependency, Hub, class mismatch, path/repo-id, and backend/export failure guidance.
Bundled Checks
scripts/check_save_load.py: creates a tiny local model, saves it, reloads withsmp.from_pretrained, optionally checksstrict=Falseclass mismatch behavior, and prints JSON.scripts/check_export_readiness.py: reports installed Torch/SMP/export features, optionalonnxpresence, 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
Practical Rules
- Prefer
encoder_weights=Nonefor offline smoke tests and export probes; pretrained encoder weights can require network/cache access. - Save model-card metadata with
dataset="..."andmetrics={...}when sharing a trained model. - Save an inference preprocessing transform separately with a compatible library such as Albumentations when downstream users need identical normalization/resizing.
- Treat Hub push/load as optional: it requires
huggingface-hub, credentials for private or write operations, and network access. - Treat ONNX and runtime backends as optional deployment targets: check dependencies and run a representative input before promising compatibility.
04 同仓库更多 Skills
passnet-triton-opt
Make a single PassNet Triton kernel fast AND numerically faithful: performance model (when a replacement can win at all), block/grid/warp tuning, autotune policy, launch overhead, and per-op numeric recipes to pass the dtype baseline tolerances. Use when a pass matches and is correct but speedup below expected, or when correctness fails by small numeric margins.
passnet-solve
END-TO-END playbook for solving one PassNet sample: analyze the computation graphs, decide the optimization strategy (what to fuse, what to replace, what to leave alone), drive the iteration loop, and maximize the sample score. This is the ENTRY skill — invoke it first for any PassNet optimization task; it tells you when to use passnet-pattern-fusion, passnet-triton-opt and passnet-feedback.
passnet-skill
PassNet GPU kernel optimization via compiler passes. Design and implement Triton-based optimization passes, create pass files under ./pass_dir/, self-evaluate with pass_evaluator, and iterate to maximize GPU speedup.
passnet-pattern-fusion
Author PassNet pass files: write patterns that actually MATCH the FX graph, pick fusion regions (which ops to absorb into one Triton kernel), and structure multi-pass submissions with the mandatory shared-dispatch architecture. Use when creating or fixing pattern()/replacement_args()/replacement_func() files, when a pass "failed to match", or when deciding how to fuse multiple kernels/ops.
passnet-orchestrate
Round/batch planning and decision support for PassNet graph optimization. Use for multi-sample triage, worker allocation, eval-budget policy, and post-eval keep/revert decisions. For solving one concrete sample end to end, use passnet-solve as the entry skill; consult this skill only when you need broader planning or a specific decision gate.
passnet-feedback
Fast, deterministic feedback for PassNet work: pre-flight pattern/match verification WITHOUT burning a GPU evaluation, per-node bottleneck analysis of a sample's graphs, and eval-log parsing into per-variant status + estimated score + failure classification. Use BEFORE every GPU evaluation (check_pattern), at the START of a sample (analyze_graph), and AFTER every evaluation (parse_eval_log).