Skip to content

Repository files navigation

ONNX Utilities

A collection of small, focused Python CLI utilities for inspecting, converting, quantizing, and translating ONNX models.

Each script is self-contained, takes its inputs via argparse (no hard-coded paths), and follows the conventions in the python-sdlc skill: type-annotated signatures, Google-style docstrings, and an if __name__ == "__main__": guard.

Scripts

Script Purpose
find_opset.py Print the opset domains and versions imported by a model.
inspect_onnx.py Print name/inputs/outputs of nodes matching an op type (default Conv).
onnx_find_nodes.py List input/output tensor names via onnxruntime.
set_onnx_opset_version.py Convert a model to a different (usually earlier) opset version.
quantize_onnx.py Dynamically quantize a model to int8 weights.
preprocess_quantize_onnx.py Run shape-inference pre-processing, then dynamic quantization.
convert_onnx_pb.py Convert an ONNX model to a TensorFlow .pb graph (onnx_tf).

Requirements

  • Python 3.10–3.13 (pinned to 3.12 via .python-version)
  • onnx, onnxruntime (core)
  • onnx-tf, tensorflow (only for convert_onnx_pb.py, optional extra)

This project uses uv for dependency management. Dependencies, the Python version, the lockfile, and dev tooling (ruff, mypy, pytest) are all declared in pyproject.toml.

Setup

Create the virtual environment and install the core + dev dependencies:

uv sync

uv reads .python-version (3.12) and creates .venv/ automatically.

For the TensorFlow conversion script, install the optional tf extra:

uv sync --extra tf

Running scripts

Always run scripts through the uv-managed environment with uv run: uv run python find_opset.py --help

uv run python find_opset.py --help

Dev tooling

uv run ruff check .          # lint
uv run ruff format .          # format
uv run mypy *.py              # type check
uv run pytest                 # tests (none yet)

Usage

Every script supports --help:

uv run python find_opset.py --help

Inspect a model's opset

uv run python find_opset.py --model-path model.onnx

Output:

Domain: ai.onnx, Opset Version: 17

Inspect graph nodes

# All Conv nodes (default)
uv run python inspect_onnx.py --model-path model.onnx

# A different op type
uv run python inspect_onnx.py --model-path model.onnx --op-type Relu

List input/output tensor names

uv run python onnx_find_nodes.py --model-path model.onnx

Convert to an earlier opset

# Default target opset is 13; output defaults to model_op13.onnx
uv run python set_onnx_opset_version.py --model-input model.onnx --target-opset 13

# Explicit output path
uv run python set_onnx_opset_version.py --model-input model.onnx --target-opset 13 --model-output model_op13.onnx

Quantize to int8

# Quick quantization — output defaults to model_int8.onnx
uv run python quantize_onnx.py --model-input model.onnx

# Unsigned weights + explicit output
uv run python quantize_onnx.py --model-input model.onnx --weight-type QUInt8 --model-output model.uint8.onnx

Pre-process then quantize (recommended)

Shape inference before quantization often yields a better-behaved model. Output paths embed the detected opset version by default:

uv run python preprocess_quantize_onnx.py --model-input model.onnx
# -> model_preprocessed_opset17.onnx  and  model_opset17_int8.onnx

Override the derived paths if needed:

uv run python preprocess_quantize_onnx.py \
    --model-input model.onnx \
    --preprocessed-output model.pre.onnx \
    --quantized-output model.int8.onnx \
    --weight-type QUInt8

Convert to TensorFlow protobuf

# Output defaults to model_tf_model.pb
uv run python convert_onnx_pb.py --model-input model.onnx

# Explicit output
uv run python convert_onnx_pb.py --model-input model.onnx --model-output graph.pb

Note: convert_onnx_pb.py produces a TensorFlow graph (.pb), not a PyTorch model.

Project Conventions

These utilities follow the python-sdlc skill (a global Pi skill distilled from rishapgandhi/python-skills):

  • No hard-coded paths or magic constants. Every file path, opset version, and output suffix is an argparse flag with a sensible default.
  • Type annotations on all function signatures (from __future__ import annotations for forward compatibility).
  • Google-style docstrings with Args / Returns / Raises sections.
  • if __name__ == "__main__": guards with sys.exit(main()) so each script is importable and testable.

License

Unspecified — see the original repository history for provenance.

Contributors

Languages