export_weights

export_weights(model, spec, path, *, extra=None)

Write a model’s weights as safetensors, with the sidecar that describes them.

The sidecar is not optional here. Weights without a record of what they are for are the thing this module exists to prevent, and the moment to write it is while the information is still at hand.

Parameters

Name Type Description Default
model The trained network. required
spec The model’s specification, which is what the sidecar records. required
path str | Path Where to write. The sidecar goes beside it. required
extra dict | None Anything else worth recording - for a detector, the anchors, which are not optional metadata: the same weights read with different anchors decode every box scaled by a fixed factor, and nothing in the output says so. None

Returns

Name Type Description
Path The path written.

The sidecar records the architecture, the shape, the channels, the classes, the blocks, the filters and trained_with - the engine version that produced the file. Loading refuses a model the weights do not belong to before touching torch, because a wrong channel count fails in load_state_dict anyway while weights trained on a different colormap with the same class count load cleanly and predict nonsense.

safetensors rather than a pickle, which would make a weights file a program somebody else wrote.