Skip to content

feat(models): export the TorchScript models to ONNX, one graph per depth - #7

Merged
vboussot merged 4 commits into
mainfrom
feat/onnx-exports
Oct 29, 2026
Merged

vboussot merged 4 commits into
mainfrom
feat/onnx-exports

Conversation

@vboussot

Copy link
Copy Markdown
Owner

Adds Data/Models/builds/ONNX/export_onnx.py, which exports any IMPACT TorchScript model to ONNX at a given depth (export_onnx.py TS/M730.pt --layers 7), the same choice the .pt takes at run time through nb_layers. Each graph outputs the list of layers the .pt returns for that depth, takes x and stats, and comes with a .json describing its layers (channels, stride, classes), its normalisation and its orientation convention.

Every export is checked against the .pt: features and input gradients in torch (≤ 1.7e-6 relative L2), and ONNX Runtime at another input size (≤ 1.5e-6, head labels identical).

The exports are published next to the .pt files on https://huggingface.co/VBoussot/impact-torchscript-models (commit 590f5da4c), and download_models.sh --onnx fetches them.

builds/ONNX/export_onnx.py exports a model of impact-torchscript-models
at the depth given on the command line (`TS/M730.pt --layers 7` ->
TS/M730_depth7.onnx), with a JSON of metadata beside it: channels and
stride per layer, whether the graph reads stats, the input's multiple,
and the orientation the consumer applies (the .pt's Canonical).

A graph takes x and stats and returns the layers the .pt returns for
that nb_layers, called with no direction. It is checked against the .pt
in torch (values and input gradient) and with ONNX Runtime at another
size than the export's.

The READMEs describe the files and download_models.sh fetches them with
--onnx.
Runs export_onnx.py on TS/M291 at depth 2 and Anatomix at depth 1 with its own checks against the TorchScript models (layers, input gradients, ONNX Runtime at another size), and checks the download script's syntax.
In float32 the TorchScript model and the rebuilt network round differently wherever the CPU's convolution routine does, and an activation at its kink sends the gradient one way or the other: the gradient check passed at 2e-6 on one machine and failed at 2e-4 on a CI runner. In float64 the two agree to 1e-14, so the check now runs there with a 1e-10 bound. The exported graphs are unchanged.
@vboussot
vboussot merged commit c3ee282 into main Oct 29, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant