ON THIS PAGE

  • ModelConverter
  • Overview
  • Installation
  • Preparation
  • Model Source
  • Calibration Data (optional)
  • Shared Folder
  • Conversion
  • Parameters
  • Top-level
  • Inputs
  • Outputs
  • Calibration
  • Random
  • Guided
  • Platform-Specific
  • RVC2
  • RVC3
  • RVC4
  • Hailo
  • Config File

ModelConverter

Overview

ModelConverter is our open-source tool that supports conversion to all of the RVC Compiled Formats. It consists of:
  • ModelConverter Docker images - a set of Docker images for running conversions in a container. There are several images available for each target platform based on the required versions of the underlying conversion tools;
  • ModelConverter CLI - a command line interface for interracting with the ModelConverter Docker images. The CLI looks for an image with the name luxonis/modelconverter-<platform>:latest to run the conversion for the target RVC Platform.

Installation

ModelConverter requires Docker to be installed on your system. It is recommended to use Ubuntu OS for the best compatibility. On Windows or MacOS, it is recommended to install it using the Docker Desktop. Otherwise follow the installation instructions for your OS from the official website.The ModelConverter CLI can be installed from the Python Package Index (PyPI) as:
Command Line
1pip install modelconv
The ModelConverter Docker images are pulled automatically when the ModelConverter CLI commands are executed. If you do not have access to the specific image for your target RVC Platform, please check the following instructions.

Preparation

Model Source

The model of interest should be in one of the following formats:
  • ONNX (.onnx),
  • OpenVINO IR (.xml and .bin), or
  • TensorFlow Lite (.tflite).
The model file(s) can be standalone or packed as a NN Archive.

Calibration Data (optional)

If you do not plan to quantize your model during conversion, you can skip this step. Otherwise, prepare a directory of image files (.jpg, .png, or .jpeg) used to guide quantization. They will be automatically processed to the apporiate format by the ModelConverter. Optionally, you can also provide calibration data as .npy or .raw files. However, beware that no automatic processing is performed on these files so make sure to prepare them correctly (e.g. NHWC layout for RVC4 conversion).

Shared Folder

A shared folder must be created for file sharing between the host and the conversion containers. ModelConverter will automatically search the root for a folder named shared_with_container and mount it as /app/shared_with_container/ inside the conversion container. The folder should contain the following subfolders:
  • archives for storing NN Archives,
  • calibration_data for storing calibration data,
  • configs for storing configuration files,
  • models for storing model files,
  • outputs for storing conversion outputs.
Run the folder creation as:
Command Line
1mkdir shared_with_container
2cd shared_with_container
3mkdir archives calibration_data configs models outputs

Conversion

Conversion is ran using the ModelConverter CLI. The process is guided using the conversion parameters. You can define them in a Config file or provide the model source as a NN Archive and the ModelConverter will automatically fill-in the conversion parameters for you. Additionally, you can define the overrides option through CLI to change conversion parameters on the run.To run the conversion:
  1. Prepare Config file or NN Archive for the model of interest;
  2. Place the relevant model file(s), Config file, NN Archive file, and calibration data in the respective sub-directories of the shared_with_container folder. There is no need to provide the model file(s) in case of converting from a NN Archive. Calibration data is optional and should only be provided if you plan to quantize the model during conversion.
  3. Run the conversion using the --path argument pointing to the Config file or NN Archive inside the mounted folder. Alternatively, s3 and GCS URLs can also provided.
Command Line
1modelconverter convert <platform> --path <url_or_path> [ conversion parameter overrides ]
Alternatively, you can also run the conversion without ModelConverter CLI by using the docker run command:
Command Line
1docker run --rm -it \
2    -v $(pwd)/shared_with_container:/app/shared_with_container/ \
3    luxonis/modelconverter-<platform>:latest \
4    convert <platform> \
5    --path <s3_url_or_path> [ conversion parameter overrides ]

Parameters

The following sections list all the conversion parameters with their descriptions and the respective default values.

Top-level

ParameterDescriptionDefault
input_modelPath to the model source file. Can be a local path or s3 or gcs url. Required.-
inputsList of inputs to the model.-
outputsList of outputs from the model.-
disable_onnx_simplificationDisable ONNX simplification.False
keep_intermediate_outputsDo not delete intermediate files.True

Inputs

inputs is a list of parameters for each input to the model. Each input can have the following parameters:
ParameterDescriptionDefaultExample
nameName of the input.-"input_0"
shapeShape of the input.-[1, 3, 224, 224]
layoutLayout of the input.-"NCHW"
data_typeData type of the input.-"float32"
encodingEncoding of the input. Can be one of: RGB, BGR, GRAY, or NONE-"RGB"
encoding.fromWhat encoding does the input of the source model expect."RGB""RGB"
encoding.toWhat encoding should the exported model expect."BGR""BGR"
mean_valuesMean values for normalizing the input, following the channel order of the original model. Can be a single number, a list of numbers, or "imagenet" string for imagenet normalization.-[ 123.675, 116.28, 103.53 ]
scale_valuesScale values for normalizing the input, following the channel order of the original model. Can be a single number, a list of numbers, or "imagenet" string for imagenet normalization.-[ 58.395, 57.12, 57.375 ]
calibrationSpecifies how to calibrate the input. For details, see Calibration Parameters --

Outputs

ParameterDescriptionDefaultExample
nameName of the output.-"output_0"
shapeShape of the output.-[1, 1000]
layoutLayout of the output.-"NC"
data_typeData type of the output.-"float32"
If the outputs are not specified, they will be inferred from the model.

Calibration

The calibration data can be specified in two ways:

Random

If the calibration data is not provided, the tool will generate random data for calibration. Using random calibration data is not recommended as it may lead to suboptimal quantization results. This option serves as a quick way to test the conversion process.
ParameterDescriptionDefault
max_imagesNumber of images to use for calibration.20
min_valueMinimum value of the input data.0.0
max_valueMaximum value of the input data.255.0
meanMean value of the input data.127.5
stdStandard deviation of the input data.35.0
data_typeData type of the input data."float32"

Guided

This calibration method uses real images to calibrate the model.The images used for calibration should be different from the images used for training the model.
ParameterDescriptionDefault
pathPath to the calibration data. Can be a local path or s3 or gcs url.-
max_imagesNumber of images to use for calibration. The entire set is used by default.-1
resize_methodHow to resize the images, so they fit the model input shape. Can be one of: "resize", "pad", or "crop""resize"

Platform-Specific

The following parameters are specific to individual target platforms:

RVC2

ParameterDescriptionDefaultExample
superblobCompile the model to the superblob format.True-
compress_to_fp16Compress the model weights to FP16 precision.True-
number_of_shavesNumber of SHAVEs to use. Used only if superblob conversion is disabled.8-
number_of_cmx_slicesNumber of CMX slices to use. Used only if superblob conversion is disabled.8-
mo_argsList of ddditional arguments for the Model Optimizer. Arguments provided this way will always take precedence over parameters that would otherwise be specified by the ModelConverter's internal logic.[]["--use_legacy_frontend"]
compile_tool_argsList of additional arguments for the Compile Tool. Arguments provided this way will always take precedence over parameters that would otherwise be specified by the ModelConverter's internal logic.[]["-ov_api_1_0"]

RVC3

ParameterDescriptionDefaultExample
compress_to_fp16Compress the model weights to FP16 precision.True-
pot_target_deviceTarget device for the POT tool. Can be either "VPU", or "ANY"."VPU"-
mo_argsList of additional arguments for the Model Optimizer. Arguments provided this way will always take precedence over parameters that would otherwise be specified by the ModelConverter's internal logic.-["--use_legacy_frontend"]
compile_tool_argsList of additional arguments for the Compile Tool. Arguments provided this way will always take precedence over parameters that would otherwise be specified by the ModelConverter's internal logic.-["-ov_api_1_0"]

RVC4

ParameterDescriptionDefaultExample
snpe_onnx_to_dlc_argsList of additional arguments for the SNPE ONNX to DLC tool.-["--batch", "6"]
snpe_dlc_quant_argsList of additional arguments for the SNPE DLC quantization tool.-["--weights_bitwidth=16"]
snpe_dlc_graph_prepare_argsList of additional arguments for the SNPE DLC graph prepare tool.-["--optimization_level=3"]
keep_raw_imagesKeep the raw images used for calibration in the intermediate data.False-
use_per_channel_quantizationEnables per-axis-element quantization for weights and biases in Convolution, Deconvolution, and FullyConnected ops.True-
use_per_row_quantizationEnables row wise quantization of Matmul and FullyConnected opsFalse-
htp_socsList of HTP SoCs to target.["sm8550"]["sm8550", "sm8650", "qcs6490"]

Hailo

ParameterDescriptionDefaultExample
optimization_levelOptimization level.2-
compression_levelCompression level.2-
batch_sizeBatch size.8-
disable_compilationSkips the compilation of .har file to the final .hef model.False-
allsList of additional custom lines added to the alls.--
hw_archHardware architecture."hailo8"-

Config File

The conversion parameters can be defined in a .yaml Config file. See the illustration below or consult additional examples for more information.
Yaml
1# local path relative to the `shared_with_container` directory
2input_model: models/model.onnx
3
4mean_values: imagenet
5scale_values: imagenet
6data_type: float32
7shape: [ 1, 3, 256, 256 ]
8
9# the ONNX model expects RGB input,
10# the exported model will expect BGR input
11encoding:
12  from: RGB
13  to: BGR
14
15calibration:
16  # calibration path can be an s3 url
17  path: s3://url/to/calibration_data.zip
18  max_images: 20