Save and load ONNX models
Exporting to onnx
-
void save_net_to_onnx_file(Net *net, string path, int seq_len = 0)
Saves a model with the onnx format in the file path provided.
- Parameters
net – Net to be saved
path – Path to the file where the net in ONNX format will be saved
seq_len – In the case of exporting a recurrent model, set the sequence length of the model input to the provided value. By default is 0, which means that the sequence length will be generic. But in some cases, the export function will need a fixed value to export the model. We recommend using this argument only when the function asks for it
- Returns
(void)
Example:
save_net_to_onnx_file(net, "my_model.onnx");
Importing onnx files
-
Net *import_net_from_onnx_file(string path, int mem = 0, LOG_LEVEL log_level = LOG_LEVEL::INFO)
Imports ONNX Net from file.
- Parameters
path – Path to the file where the net in ONNX format is saved
mem – (default 0)
log_level – Available: LOG_LEVEL::{TRACE, DEBUG, INFO, WARN, ERROR, NO_LOGS}. (default LOG_LEVEL::INFO)
- Returns
Net
Example:
Net* net = import_net_from_onnx_file("my_model.onnx");
-
Net *import_net_from_onnx_file(string path, vector<int> input_shape, int mem = 0, LOG_LEVEL log_level = LOG_LEVEL::INFO)
Imports ONNX Net from file and changes its input shape. Note that this function is for models with only one input layer, in other case it will fail.
- Parameters
path – Path to the file where the net in ONNX format is saved
input_shape – Shape of the input data (without batch dimension)
mem – (default 0)
log_level – Available: LOG_LEVEL::{TRACE, DEBUG, INFO, WARN, ERROR, NO_LOGS}. (default LOG_LEVEL::INFO)
- Returns
Net
Example:
Net* net = import_net_from_onnx_file("my_model.onnx", {3, 32, 32});
Simplifying onnx models
Not all onnx models can be loaded by the EDDL since in order to read the model correctly, all its layers and arguments must be supported by our library.
The best way to know if a model is supported by our current version is to try to load it. If import step produces an error, you try to “standardize” or “simplifying” using onnx_simplifier_.
Installation
This is only needed is you are not using the conda environment for the EDDL, as we ship ONNX simplifier with it.
pip3 install -U pip && pip3 install onnx-simplifier
Example #1: Simplifying a “ResNet18” (one input)
# Download onnx model from: https://github.com/onnx/models/tree/master/vision/classification/resnet
# Simplify
python3 -m onnxsim resnet18-v1-7.onnx resnet18-v1-7_simplify.onnx
Example #2: Simplifying a “MobileNet” (input’s shape required)
# Download onnx model from: https://github.com/onnx/models/tree/master/vision/classification/mobilenet
# Simplify
python3 -m onnxsim mobilenetv2-7.onnx mobilenetv2-7_simplified.onnx --input-shape 1,3,224,224
Example #3: Simplifying a “TinyYOLOv3” (two inputs, one dynamic)
# Download onnx model from: https://github.com/onnx/models/tree/master/vision/object_detection_segmentation/tiny-yolov3
# Simplify
python3 -m onnxsim tiny-yolov3-11.onnx tiny-yolov3-11_simplified.onnx --dynamic-input-shape --input-shape input_1:1,3,416,416 image_shape:1,2