/
AI model convert tool: ONNX(PyTorch)

AI model convert tool: ONNX(PyTorch)

Preparation


Please refer here for the environment construction procedure.

Please note that the following environment settings must be performed every time the CV Tool container is started.

  • For ambaCV2X camera

$ cd /home/cvtool $ source setup_env.sh
  • For ambaCV5X camera

$ cd /home/cvtool $ source setup_env.sh ipro-ambaCV5X

 

Convert sample models


You can understand the flow of the conversion process by converting a sample model. If you are converting a model that you have developed, you can skip this step.

mobilenetv2

Move directory.

$ cd /home/cvtool/conversion/onnx/mobilenetv2

Convert the model.

 

yolov5 / yolov8

Download sample model.

The file size to be downloaded will be several GB. Please make sure you have enough free space before proceeding.

Convert the model.

Depending on your PC environment, you may need up to 8GB of memory (RAM). If it fails, please check your memory capacity.

 

The model after conversion is output to the following directory.

  • For ambaCV2X camera

${OUTPUT_DIR}/${NET_NAME}/${PARSER_OPTION}/[model name]

  • For ambaCV5X camera

${OUTPUT_DIR}/${NET_NAME}_ambaCV5X/${PARSER_OPTION}/[model name]

 

Convert user AI model


  1. Copy either directory under /home/cvtool/conversion/onnx.

  1. Change the parameter of "setting.conf" according to the model to be converted.

  2. Locate user AI model in “MODEL_DIR“ in “setting.conf”.

  3. Convert the model.

 

setting.conf


  • NET_NAME: The name of network

    • Any name can be set.

  • MODEL_DIR: Path to directory which includes .onnx files

    • All .onnx files under the directory are converted.

  • IMAGE_DIR: Path to directory which includes image files for optimizing quantization

    • Please put the directory image files for training. Recommended number of image files is 100 to 200.

    • Image file format should be the format that are supported by OpenCV (Ex. JPEG, PNG and etc…).

    • Any resolution is supported.

  • OUTPUT_DIR: Path to directory which converted data in placed.

  • PARSER_OPTION: Quantization mode

    • Select from FIX8/FIX16/MIX (FIX8/FIX16 mixed).

  • IN_DATA_CHANNEL: Number of input image channel for target model

  • N_DATA_WIDTH: Width of input image for target model

  • IN_DATA_HEIGHT: Height of input image for target model

  • IN_MEAN: Normalization parameter (mean) of input image

    • Please refrain from using space between “,” as shown below if using numerical value.
      IN_MEAN=127.5,127.5,127.5

  • IN_SCALE: Normalization parameter (scale) of input image

    • Please refrain from using space between “,” and only use “,” to separate values when setting different values for each channel.

  • IS_BGR: Format of input image (RGB or BGR)

  • OUT_LAYER: The name of output layer for target network

    • If two or more layers exists, separate layers by “,”.

    • When the following symbols are contained in the name of input/output node, conversion may not be successful.
      : | ; , ‘

  • PRIORBOX_NODE: Node equivalent to “priorbox”

    • Need to set when IS_SSD=1

  • PREPRO: Path of preprocessing script (python script)

    • Refer to “/home/cvtool/ common/prepro.py” for how to create a script

  • PREPRO_ARG: Argument of preprocessing script (python script)

  • IN_DATA_FILEFORMAT: Input data format

    • Examples : uint8->0,0,0,0, float32->1,2,0,7, float16->1,1,0,4

    • When the value of IN_DATA_FILEFORMAT changes from “0,0,0,0”, setting PREPRO is needed.

  • IN_DATA_TRANSPOSE: Specify when performing TRANSPOSE on the input data

 

If conversion error occurs


Modifying the model by graph_surgery.py included in cvtool may resolve the problem.
Please refer here for details about graph_surgery.py.

  • The model includes unsupported node by cvtool
    Run the following command.

    Please refer here for the list of supported node.

    You can check if the model includes unsupported node, by using onnx_print_graph_summary.py in cvtool.

     

  • Input of the model has variable value

    image-20240416-002601.png

    Replace with fixed value by the following command.

     

  • Input shape of the model is NHWC, and transposed to NCHW first

    cutInput.jpg

    Separate from the beginning of the model to “Tranpose” node.

     

  • Unsupported character ( : | ; , ‘ ) is included in OUT_LAYER
    Rename the node by the following command.

    If multiple nodes are specified, please separate those by ",".

     

  • The model has nodes of rank > 4
    Conversion may not be possible due to SoC constraints.

    You can check if the model has such nodes, by using onnx_print_graph_summary.py. The following message will be output when the nodes are found.

    Please modify the model in one of the following ways.
    ・Modify the nodes before exporting to onnx.
    ・Separate the back of the node from the model.


    ・Replace the node with that of rank <= 4.

    ※If you wish to use “ReplaceSubgraph”, please contact us.

 

Related content

AI model convert tool: Tensorflow
AI model convert tool: Tensorflow
More like this
AI model convert tool: Caffe
AI model convert tool: Caffe
More like this
Install AI model convert tool
Install AI model convert tool
Read with this
AIモデル変換ツール:ONNX(PyTorch)編
AIモデル変換ツール:ONNX(PyTorch)編
More like this
AI model conversion
AI model conversion
Read with this
AI model convert tool: Evaluate AI model
AI model convert tool: Evaluate AI model
More like this