Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

目次

Table of Contents
minLevel1
maxLevel4

環境構築

環境構築手順はこちらをご参照ください。

モデルを変換する

モデル変換を行います。

引数の「setting.conf」は変換するモデルに合わせてパラメータを変更してください。

...

Preparation

...

Please refer here for the environment construction procedure.

Convert sample models

...

mobilenetv2

Move directory.

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

Convert the model.

Code Block
$ ./onnx_conversion.sh setting.conf

yolov5 / yolov8

Download sample model.

Code Block
(yolov5)
$ cd /home/cvtool/conversion/onnx/yolov5
$ ./setup_yolov5.sh

(yolov8)
$ cd /home/cvtool/conversion/onnx/yolov8
$ ./setup_yolov8.sh

Convert the model.

Code Block
$ ./onnx_conversion.sh setting.conf

...

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]

Conversion

...

Note

PyTorchのモデルは事前にONNXに変換が必要です。

...

Pytorch model needs to be converted to ONNX in advance.

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

Code Block
# Network Name
NET_NAME=mobilenet_v2

# SSD Model or Not (0:not SSD, 1:SSD)
IS_SSD=0$ cd /home/cvtool/conversion/onnx
$ cp -r mobilenetv2 foo
$ cd foo
  1. Change the parameter of "setting.conf" according to the model to be converted.

  2. Convert the model.

Code Block
$ ./onnx_conversion.sh setting.conf

setting.conf

...

Note

From v1.20, parameter "CAVALRY_VER" is removed that existed in setting.conf until v1.19.
If you use setting.conf from v1.19 or earlier, please remove "CAVALRY_VER" from it.

Code Block
# Network Name
NET_NAME=mobilenet_v2

# Path to Directory for ONNX Models
MODEL_DIR=./sample/mobilenet_v2/models

# Path to Directory for DRA Images
DRA_IMAGE_DIR=../../dra_img

# Path to Directory for Output Data
OUTPUT_DIR=./out

# Quantization Mode
#  FIX8  : Fixed-point  8bit
#  FIX16 : Fixed-point 16bit
#  MIX   : FIX8/FIX16 mixed
PARSER_OPTION=MIX

# Input Data Format (0:NHWC, 1:NCHW)
IN_DATA_FORMAT=1

# Input Data Channel
IN_DATA_CHANNEL=3

# Input Data Width
IN_DATA_WIDTH=224

# Input Data Height
IN_DATA_HEIGHT=224

# Input Data Mean Vector
IN_MEAN=103.94,116.78,123.68

# Input Data Scale
# IN_SCALE=1/Scale
IN_SCALE=58.823529411

# RGB or BGR (0:RGB, 1:BGR)
IS_BGR=1

# Output Layers Name
OUT_LAYER=mobilenetv20_output_flatten0_reshape0

#cavalry version
#if not specified -> ""
CAVALRY_VER="2.1.7"

# Unique preprocess
# if use im2bin -> NONE
# if use unique preprocess -> script path
PREPRO=NONE
PREPRO_ARG=""

# Input file data format
IN_DATA_FILEFORMAT=0,0,0,0

# Transpose indices(NONE:without transpose , 0,3,1,2:transpose (EX))
IN_DATA_TRANSPOSE=NONE
  • NET_NAME:ネットワーク名

    • 任意の名前を設定できます。

  • IS_SSD:使用しません。

  • MODEL_DIR:onnxファイルが格納されたディレクトリ

    • ディレクトリ下の全てのモデルに対して、変換処理を実行します。

  • IMAGE_DIR:量子化の最適化処理で使用する、画像ファイルが格納されたディレクトリ

    • 学習に使った画像を格納してください。100~200枚が推奨枚数です。

    • 画像フォーマットはJPEGやPNGなど、OpenCVで対応しているものです。

    • 任意サイズの画像を使用可能です。

  • OUTPUT_DIR:変換後のデータ出力先ディレクトリ

  • PARSER_OPTION:量子化モード

    : 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混合)から選択します。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:入力画像のフォーマット(RGB or 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:”priorbox”に相当するノード: Node equivalent to “priorbox”

    • Need to set when IS_SSD=1の時に、設定が必要です。1

  • CAVALRY_VER:使用するcavalryバージョン

  • PREPRO:前処理スクリプトパスPREPRO: Path of preprocessing script (python script)

    • スクリプトの作り方は“Refer to “/home/cvtool/ common/prepro.py”を参照してください。py” for how to create a script

  • PREPRO_ARG:前処理スクリプトの引数: Argument of preprocessing script (python script)

  • IN_DATA_FILEFORMAT:入力データのフォーマット

    例:uint8-> 0

    : Input data format

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

    • NWhen the value of IN_DATA_FILEFORMAT を”0changes from “0,0,0,0”から変更した場合はPREPROの設定が必要になります。

    N
    • ,0”, setting PREPRO is needed.

  • IN_DATA_TRANSPOSE:入力データに対してTRANSPOSE を行う場合指定する

...

定数でないノードがある場合、正常に変換ができません。

その場合、下記コマンドを使ってモデルの変換を行い、変換後のモデルで量子化を行ってください。

 -m : 変換前モデル

...

  • : 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.

    Code Block
    $ graph_surgery.py onnx -m

...

サンプルモデルの変換

mobilenetv2

ディレクトリを移動します。

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

モデル変換を実行します。

Code Block
$ ./onnx_conversion.sh setting.conf

yolov5

サンプルモデルをダウンロードします。

Code Block
$ cd /home/cvtool/conversion/onnx/yolov5
$ ./setup_yolov5.sh

モデル変換を実行します。

...

  •  (model name before modification) -o (model name after modification) -t CVFlow

    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.

    Code Block
    $ onnx_print_graph_summary.py -p (model name)

  • Input of the model has variable value

    image-20240416-002601.pngImage Added

    Replace with fixed value by the following command.

    Code Block
    $ graph_surgery.py onnx -m (model name before modification) -o (model name after modification) -isrc "i:input|is:1,3,960,960" -t SetIOShapes

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

    cutInput.jpgImage Added

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

    Code Block
    $ graph_surgery.py onnx -m (model name before modification) -o (model name after modification) -isrc "i:(output name of Transpose node)|is:1,3,224,224" -t CutGraph

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

    Code Block
    $ graph_surgery.py onnx -m (model name before modification) -o (model name after modification) -t "RenameTensors(original_name=new_name)"

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

    Code Block
    -t "RenameTensors(node::1=node1,node::2=node2)"

  • 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.

    Code Block
    $ onnx_print_graph_summary.py -p (model name)
    
    INFO: 08/29/2024 03:22:40.725761 onnx_print_graph_summary.py:384 [PrintGraphSummary] Unsupported tensors with rank > 4 (3):
    INFO: 08/29/2024 03:22:40.725837 onnx_print_graph_summary.py:386 [PrintGraphSummary] -> 'output' [1 x 3 x 52 x 52 x 85]
    INFO: 08/29/2024 03:22:40.725909 onnx_print_graph_summary.py:386 [PrintGraphSummary] -> '1026' [1 x 3 x 26 x 26 x 85]
    INFO: 08/29/2024 03:22:40.725993 onnx_print_graph_summary.py:386 [PrintGraphSummary] -> '1046' [1 x 3 x 13 x 13 x 85]

    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.

    Code Block
    $ graph_surgery.py onnx -m (model name before modification) -o (model name after modification) -on (node name before the node of rank > 4) -t CutGraph


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

    Code Block
    $ graph_surgery.py onnx -m (model name before modification) -o (model name after modification) -t ReplaceSubgraph

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

Note

Althogh the node of rank=4, conversion may not be possible when the number of elements in the first dimension of the node is not 1. Please take the same action as in case of rank > 4.