Skip to content

Export ICLabel to ONNX and add the onnxruntime backend - #381

Merged
neuromechanist merged 8 commits into
epic/324-pyodide-browserfrom
phase/377-iclabel-onnx
Sep 17, 2026
Merged

neuromechanist merged 8 commits into
epic/324-pyodide-browserfrom
phase/377-iclabel-onnx

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Phase 4 of epic #324. Closes #377.

Summary

  • tools/iclabel/export_iclabel_onnx.py exports the torch ICLabelNet (built from netICL.mat) to iclabel.onnx at a pinned opset, with a self-check comparing torch and onnxruntime output on random inputs.
  • iclabel.py's engine=None path now runs iclabel.onnx through a new onnxruntime backend (iclabel_net_onnx.py) instead of building the torch network at call time. torch no longer appears anywhere in the runtime classification path.
  • New [iclabel] extra provides onnxruntime; all includes it. [torch] gains onnx and is now export-only (used only to regenerate the artifact).
  • The wheel ships iclabel.onnx (11.1 MB) in place of netICL.mat (10.3 MB); netICL.mat stays in the repo as the export source but is excluded from packaging. pyproject.toml package-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/beta still raise the existing NotImplementedError; only the artifact name in the message changed.
  • Feature extraction, input normalization, the 4-way augmentation, and the post-network averaging in iclabel.py are unchanged.

Opset and artifact size

  • Pinned opset: 17 (supported by onnxruntime since 1.14; the network only uses Conv2d/LeakyReLU/Softmax/Concat/Reshape, all available since opset 7-9, so the choice is about runtime compatibility, not op coverage).
  • 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_data in tests/test_iclabel.py compares the torch network directly against iclabel.onnx on real sample_data features (same augmentation, same averaging, only the network call swapped). Measured:

  • Raw per-augmented-sample output: max abs diff 4.35e-06.
  • Final classification (after the 4-way average): max abs diff 1.43e-06, max rel diff 2.59e-05.

This test reuses the existing rtol=1e-4, atol=1e-5 tolerance already established in TestICLabelEngines.test_basic for 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_extractor output for sample_data/eeglab_data_with_ica_tmp.set into mne-icalabel's own exported network (mne_icalabel.iclabel.network.onnx._run_iclabel, opset 14, exported from pytorch 1.13) and compared to iclabel(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 checks onnxruntime instead of torch, since that is what engine=None actually needs. Could not exercise it for real in this environment (no EEGLAB checkout available), so it remains skip-covered via EEGPREP_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 from eegprep[iclabel] only, no torch; output is bit-identical to the onnx run in the main dev env and to mne-icalabel's).
  • lite/beta still raise NotImplementedError under engine=None.
  • ./pre-commit.py --changed-from origin/develop clean.
  • uv run --no-sync ruff check ., uv run --no-sync ruff format --check ., uv run --no-sync ty check clean.
  • 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 in reref.py, untouched by this PR and reproducible in isolation on this branch's base).
  • uv build wheel contains plugins/ICLabel/iclabel.onnx and not netICL.mat.

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).

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Code review skipped — your organization's extra usage balance is too low to start another review, so this review was not started.

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.

@claude

claude Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
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.
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