ONNX

class tract.onnx.OnnxOptions(*, ignore_value_info: bool = False, assertions: List[str] | None = None)[source]

Options for the ONNX loader, passed to Onnx.load().

Mirrors OnnxOptions in the Rust API and serializes to the same JSON.

# turn the downsample_ratio input into a constant, so the shapes it
# drives can be inferred
options = tract.OnnxOptions().constant_input(
    "downsample_ratio", numpy.array([0.5], dtype=numpy.float32)
)
model = tract.onnx().load("./rvm.onnx", options)
ignore_value_info(ignore: bool = True) → OnnxOptions[source]

Discard the shapes the model file declares and let tract infer them. Returns self for chaining.

assertion(assertion: str) → OnnxOptions[source]

Add one assertion over the model symbols, e.g. "h>=0". Returns self for chaining.

constant_input(name: str, value: ndarray) → OnnxOptions[source]

Replace the input called name by a constant holding value. Returns self for chaining.

The input stops being a model input, so analysis can resolve shapes that depend on its value rather than only its shape. value must hold real numbers or booleans.

to_json() → str[source]

Serialize to the JSON the loader expects.

class tract.onnx.Onnx[source]

Represent the ONNX context in tract.

It essentially allows to load ONNX models. Note that an ONNX model is loaded as an InferenceModel and not as a Model: many ONNX models come with partial shape and element type information, while tract’s Model assume full shape and element type knownledge. In this case, it is generally sufficient to inform tract about the input shape and type, then let tract infer the rest of the missing shape information before converting the InferenceModel to a regular Model.

# load the model as an InferenceModel
model = tract.onnx().load("./mobilenetv2-7.onnx")

# set the shape and type of its first and only input
model.set_input_fact(0, "1,3,224,224,f32")

# get ready to run the model
model = model.into_model().into_runnable()
load(path: str | Path, options: OnnxOptions | str | None = None) → InferenceModel[source]

Load an ONNX file as an InferenceModel

options is an OnnxOptions or the JSON for one, and configures the loader.