kornia.onnx#
Run and compose ONNX models with ONNX Runtime: ONNXSequential chains several
ONNX graphs (local files, onnx.ModelProto objects, or pre-exported Kornia operators and models from the
kornia/ONNX_models Hugging Face repository) into one model with a PyTorch-like call interface. The
ONNX support guide walks through installation, chaining, export and running on CUDA.
- class kornia.onnx.module.ONNXModule(op, providers=None, session_options=None, cache_dir=None, target_ir_version=None, target_opset_version=None)[source]#
ONNXModule to wrap an ONNX operator.
- Parameters:
arg – A variable number of ONNX models (either ONNX ModelProto objects or file paths). For Hugging Face-hosted models, use the format ‘hf://model_name’. Valid model_name can be found on https://huggingface.co/kornia/ONNX_models. Or a URL to the ONNX model.
providers (optional) – A list of execution providers for ONNXRuntime (e.g., [‘CUDAExecutionProvider’, ‘CPUExecutionProvider’]). Default:
Nonesession_options (optional) – Optional ONNXRuntime session options for optimizing the session. Default:
Nonecache_dir (optional) – The directory where downloaded ONNX models (
hf://names and URLs) are cached locally. Defaults to None, which will use a default kornia.config.hub_onnx_dir directory. Default:Nonetarget_ir_version (optional) – The target IR version to convert to. Default:
Nonetarget_opset_version (optional) – The target OPSET version to convert to. Default:
None
- add_metadata(additional_metadata=None)[source]#
Attach metadata entries to the wrapped ONNX model graph.
- create_session(providers=None, session_options=None)[source]#
Create an ONNX Runtime session for the wrapped model graph.
- Parameters:
providers (optional) – Optional ordered list of ONNX Runtime execution providers, such as
"CUDAExecutionProvider"followed by"CPUExecutionProvider". The first available provider is used according to ONNX Runtime’s provider selection rules. Default:Nonesession_options (optional) – Optional ONNX Runtime session options controlling graph optimization, threading, logging, and other runtime execution settings. Default:
None
- Returns:
ONNX Runtime inference session bound to
self.op, the wrapped ONNX model graph.
- class kornia.onnx.sequential.ONNXSequential(*args, providers=None, session_options=None, io_maps=None, cache_dir=None, auto_ir_version_conversion=False, target_ir_version=None, target_opset_version=None)[source]#
ONNXSequential to chain multiple ONNX operators together.
- Parameters:
*args – A variable number of ONNX models (either ONNX ModelProto objects or file paths). For Hugging Face-hosted models, use the format ‘hf://model_name’. Valid model_name can be found on https://huggingface.co/kornia/ONNX_models. Or a URL to the ONNX model.
providers (optional) – A list of execution providers for ONNXRuntime (e.g., [‘CUDAExecutionProvider’, ‘CPUExecutionProvider’]). Default:
Nonesession_options (optional) – Optional ONNXRuntime session options for optimizing the session. Default:
Noneio_maps (optional) – An optional list of list of tuples specifying input-output mappings for combining models. If None, we assume the default input name and output name are “input” and “output” accordingly, and only one input and output node for each graph. If not None, io_maps[0] shall represent the io_map for combining the first and second ONNX models. Default:
Nonecache_dir (optional) – The directory where downloaded ONNX models (
hf://names and URLs) are cached locally. Defaults to None, which will use a default kornia.config.hub_onnx_dir directory. Default:Noneauto_ir_version_conversion (optional) – If True, convert the models to one IR version and one OPSET version before combining them: IR version 9, and the highest OPSET version among the models, to which the others are converted up. Other versions may be pointed to by target_ir_version and target_opset_version. Default:
Falsetarget_ir_version (optional) – The target IR version to convert to. Default:
Nonetarget_opset_version (optional) – The target OPSET version to convert to. Default:
None
- combine(io_maps=None)[source]#
Combine the stored ONNX operators into one executable graph.
- Parameters:
io_maps (
list[tuple[str,str]] |None, optional) – Optional list of(source_output, target_input)name pairs describing how adjacent graphs should be connected. When omitted, the helper assumes the default single input/output names used by the ONNX composition utility. Default:None- Return type:
ModelProto- Returns:
Combined ONNX model graph containing all operators in
self.operatorsconnected in sequence.
- create_session(providers=None, session_options=None)[source]#
Create an ONNX Runtime session for the combined graph.
- Parameters:
providers (optional) – Optional ordered list of execution providers used by ONNX Runtime, for example GPU first and CPU as fallback. Default:
Nonesession_options (optional) – Optional ONNX Runtime session options controlling optimization, threading, and runtime behavior. Default:
None
- Returns:
ONNX Runtime inference session for
self._combined_op.
- class kornia.onnx.utils.ONNXLoader[source]#
Manages ONNX models, handling local caching, downloading from Hugging Face, and loading models.
from kornia.onnx.utils import ONNXLoader # Load a Hugging Face operator ONNXLoader.load_model("hf://operators/kornia.color.gray.GrayscaleToRgb") # Load a local converted/downloaded operator ONNXLoader.load_model("operators/kornia.color.gray.GrayscaleToRgb")
- classmethod list_models()[source]#
List all available ONNX models in the ‘models’ folder of the Hugging Face repository.
- Return type:
- classmethod list_operators()[source]#
List all available ONNX operators in the ‘operators’ folder of the Hugging Face repository.
- Return type:
- classmethod load_config(url, download=True, **kwargs)[source]#
Load JSON config from the specified URL.
- Parameters:
url (
str) – The URL of the preprocessor config to load.download (
bool, optional) – If True, the config will be downloaded if it’s not already in the local cache. Default:Truekwargs (
Any) – Keyword arguments ofCachedDownloader.download_to_cache. Onlycache_dirapplies: the root the config is stored under, at<scheme>/<host>/<directories of the URL's path>/<file name>, with a%3F<hash>directory before the file name when the URL has a query, as forload_model.Noneor""meanskornia_config.hub_cache_dir. Any other keyword raisesTypeError, whateverurlis.
- Returns:
The loaded preprocessor config.
- Return type:
- classmethod load_model(model_name, download=True, with_data=False, **kwargs)[source]#
Load an ONNX model from the local cache or downloads it from Hugging Face if necessary.
- Parameters:
model_name (
str) – The name of the ONNX model or operator. For Hugging Face-hosted models, use the format ‘hf://model_name’. Valid model_name can be found on https://huggingface.co/kornia/ONNX_models. Or a URL to the ONNX model.download (
bool, optional) – If True, the model will be downloaded from Hugging Face if it’s not already in the local cache. Default:Truecache_dir – The root of the cache. An
hf://model is stored under it at the path of its name after the folder (<cache_dir>/a/b.onnxforhf://models/a/b): the kornia/ONNX_models names are fully qualified (kornia.color.gray.RgbToGrayscale), so the folders never share a name. A URL is stored at<cache_dir>/<scheme>/<host>/<directories of the URL's path>/<file name>, with a%3F<hash>directory before the file name when the URL has a query, so that two URLs share a file only when they differ in nothing but credentials, the fragment,.,..or empty directories, or an.onnxextension that one of them lacks. Defaults to None (""too): anhf://model goes to<kornia_config.hub_onnx_dir>/<folder>/, and a URL underkornia_config.hub_onnx_dir. Limitations: on Windows, a URL whose file name is not a valid file name there, or whose cache path is longer thanMAX_PATH(260 characters) while long paths are disabled, cannot be cached, and neither can a directory with a reserved name (con); on a case-insensitive file system, URLs that differ only in letter case share a file.with_data (
bool, optional) – If True, the model will be loaded with its .onnx_data weights, stored beside the model. Default:False**kwargs – Only
cache_dir; any other keyword raisesTypeError, whatevermodel_nameis.
- Returns:
The loaded ONNX model.
- Return type:
onnx.ModelProto