Repository navigation
Export ICLabel to ONNX and add the onnxruntime backend - #381
Merged
Merged
Conversation
The iclabel extra provides onnxruntime, the new runtime backend for ICLabel classification. torch now only needs to install onnx alongside it, since it is used to regenerate iclabel.onnx from netICL.mat, not to run classification. Package data ships plugins/**/*.onnx instead of plugins/**/*.mat, and netICL.mat is explicitly excluded from the wheel now that it is a dev-only export source rather than a runtime resource. Not tested standalone; covered by the packaging and test suite runs in later commits.
iclabel.onnx is a ~11 MB protobuf artifact; without this it fails the 5 MB large-file check that .mat files are already exempt from. Tested: ./pre-commit.py passes with iclabel.onnx staged.
tools/iclabel/export_iclabel_onnx.py exports ICLabelNet (built from netICL.mat) to ONNX at a pinned opset (17: supported by onnxruntime since 1.14, and comfortably within the ops the network actually uses - Conv2d/LeakyReLU/Softmax/Concat/Reshape, all available since opset 7-9). The script verifies the export against torch on random inputs before succeeding. iclabel.onnx (11.1 MB) is the regenerated artifact, committed so the package ships it in place of netICL.mat (10.3 MB). iclabel_net_load_py_measures.py now resolves netICL.mat from the source tree instead of importlib.resources, since it is a dev-only export source and no longer a packaged runtime resource. Tested: uv run python tools/iclabel/export_iclabel_onnx.py regenerates iclabel.onnx deterministically and reports torch-vs-onnxruntime max abs diff 1.00e-05 on random inputs (opset export self-check).
iclabel_net_onnx.py loads the packaged iclabel.onnx and runs it through onnxruntime. iclabel.py's engine=None path now calls it instead of building ICLabelNet from netICL.mat with torch; torch no longer appears anywhere in the runtime classification path. Feature extraction, the 4-way augmentation, and the post-network averaging are unchanged. The lite/beta NotImplementedError is unchanged in behavior; only the artifact name in its message was updated. Tested: iclabel(EEG, engine=None) on sample_data with torch fully uninstalled (separate venv, eegprep[iclabel] only) reproduces the same classification as before this change; see the ONNX-vs-torch test added in a later commit for the numeric comparison.
test_iclabel.py: TestICLabelEngines.test_basic now requires onnxruntime (what engine=None actually needs) instead of torch. Added TestICLabelOnnxExport, which cross-checks the packaged iclabel.onnx against the torch network it was exported from, on sample_data. Measured max abs diff 1.43e-06 / max rel diff 2.59e-05, the same order of magnitude as the existing Python-vs-MATLAB parity gap this file already tolerates at rtol=1e-4/atol=1e-5 (float32 op-ordering noise between eager torch and onnxruntime's fused CPU kernels), so the test reuses that established tolerance rather than introducing a new one. conftest.py: narrowed the matlab-file classification for test_iclabel.py from the whole file to TestICLabelEngines:: only, since the new TestICLabelOnnxExport class does not need MATLAB. test_public_api_examples.py: packaging assertions and comments now expect plugins/ICLabel/iclabel.onnx and the iclabel extra instead of netICL.mat and the torch extra. Tested: EEGPREP_SKIP_MATLAB=1 uv run pytest tests/test_iclabel.py tests/test_public_api_examples.py -v (18 passed, 1 skipped for missing MATLAB).
Updates every user-facing and developer-facing mention of netICL.mat and the torch requirement for ICLabel classification: installation guide (new ICLabel extra section), ICA/component and plugin user guide pages, the pop_iclabel help resource, the public-API example comment, the changelog, the final parity matrix rationale, and AGENTS.md's repo map. No behavior change; docs and comments only.
The release verification gate asserted netICL.mat shipped in the wheel; it now asserts iclabel.onnx ships and netICL.mat does not. Tested: uv build locally and confirmed the wheel contains plugins/ICLabel/iclabel.onnx and not netICL.mat (the same check this workflow step performs).
Contributor
There was a problem hiding this comment.
An organization admin can add extra usage credits at claude.ai/admin-settings/usage.
Once credits are available, reopen this pull request to trigger a review.
Contributor
|
Claude encountered an error after 1s —— View job I'll analyze this and get back to you. |
Resolves three conflicts from the phase 1 extras split: - changelog.rst and installation.rst: both phases appended to the same anchors, so both entries are kept. The extras example now names the iclabel extra rather than torch, since torch became export-only here. - uv.lock: regenerated with uv lock against the merged pyproject.toml rather than hand-merged. pyproject.toml auto-merged; the all extra is a superset of torch, iclabel, gui, console, docs, eeglab, and sys.
This was referenced Sep 17, 2026
This was referenced Nov 12, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Phase 4 of epic #324. Closes #377.
Summary
tools/iclabel/export_iclabel_onnx.pyexports the torchICLabelNet(built fromnetICL.mat) toiclabel.onnxat a pinned opset, with a self-check comparing torch and onnxruntime output on random inputs.iclabel.py'sengine=Nonepath now runsiclabel.onnxthrough a newonnxruntimebackend (iclabel_net_onnx.py) instead of building the torch network at call time. torch no longer appears anywhere in the runtime classification path.[iclabel]extra providesonnxruntime;allincludes it.[torch]gainsonnxand is now export-only (used only to regenerate the artifact).iclabel.onnx(11.1 MB) in place ofnetICL.mat(10.3 MB);netICL.matstays in the repo as the export source but is excluded from packaging.pyproject.tomlpackage-data,tests/test_public_api_examples.py,src/eegprep/resources/help/pop_iclabel.md,tools/iclabel/iclabel_net_load_py_measures.py,.github/workflows/release.yml, and the docs are all updated consistently.lite/betastill raise the existingNotImplementedError; only the artifact name in the message changed.iclabel.pyare unchanged.Opset and artifact size
netICL.mat: 10,815,192 bytes (10.3 MB).iclabel.onnx: 11,626,631 bytes (11.1 MB).Tolerance and justification
New test
TestICLabelOnnxExport.test_onnx_matches_torch_on_sample_dataintests/test_iclabel.pycompares the torch network directly againsticlabel.onnxon realsample_datafeatures (same augmentation, same averaging, only the network call swapped). Measured:This test reuses the existing
rtol=1e-4, atol=1e-5tolerance already established inTestICLabelEngines.test_basicfor Python-vs-MATLAB comparison (which tolerates a comparable ~3.4e-06 abs / ~4.3e-05 rel gap from independent float32 execution paths). No new tolerance was introduced; both diffs are within this existing bound with room to spare, and the gap is attributable to ordinary float32 op-ordering differences between eager torch execution and onnxruntime's fused CPU kernels.mne-icalabel cross-check
Fed the same un-augmented
ICL_feature_extractoroutput forsample_data/eeglab_data_with_ica_tmp.setinto mne-icalabel's own exported network (mne_icalabel.iclabel.network.onnx._run_iclabel, opset 14, exported from pytorch 1.13) and compared toiclabel(EEG, engine=None)'s output. Result: bit-identical (max abs diff 0.0, argmax class agreement 32/32) despite the two independent export toolchains (different torch/opset versions), confirming both are faithful conversions of the same canonical ICLabel network and that onnxruntime executes them deterministically.MATLAB parity
tests/test_iclabel.py::TestICLabelEngines::test_basic(Python-vs-MATLAB) is unchanged except its skip guard now checksonnxruntimeinstead oftorch, since that is whatengine=Noneactually needs. Could not exercise it for real in this environment (no EEGLAB checkout available), so it remains skip-covered viaEEGPREP_SKIP_MATLAB/missing-EEGLAB-checkout behavior, unchanged from before this PR.Verification
iclabel(EEG, engine=None)classifies correctly with torch uninstalled (verified in a separate venv built fromeegprep[iclabel]only, no torch; output is bit-identical to the onnx run in the main dev env and to mne-icalabel's).lite/betastill raiseNotImplementedErrorunderengine=None../pre-commit.py --changed-from origin/developclean.uv run --no-sync ruff check .,uv run --no-sync ruff format --check .,uv run --no-sync ty checkclean.EEGPREP_SKIP_MATLAB=1 uv run pytest tests -m "not slow": 2337 passed, 262 skipped, 1 pre-existing unrelated failure (test_pop_reref.py::PopRerefIcaRegressionTests::test_sample_data_average_reference_emits_no_runtime_warning, a BLAS/platform-dependent RuntimeWarning check inreref.py, untouched by this PR and reproducible in isolation on this branch's base).uv buildwheel containsplugins/ICLabel/iclabel.onnxand notnetICL.mat.