特記事項: Horizon の公式ドキュメントを参照してください。権利侵害がある場合は、お知らせの上、削除してください。よろしくお願いします。
完全な変換コードについては、github を参照してください。コード
1 ツールチェーン
Horizon ツールチェーンは、Docker と手動インストールの両方をサポートしています。「Horizon では、インストール プロセス中に発生する問題を回避するために、Docker 方式を使用することを強く推奨します。」
Horizon が提供する Docker には CPU 版と GPU 版があり、公式のダウンロードリンクのツールチェーンが提供されています。
Horizon ツールチェーンは現在、Caffe と ONNX の読み込み変換をサポートしています。以下では、例として ONNX 変換を使用して説明しており、ツールチェーン バージョン v1.11.4 が使用されています。次の例は ONNX を示しています。
Docker イメージ経由でインストールする
# 镜像 load
sudo docker load -i docker_cpu_openexplorer_centos_7_xj3_v1.11.4.tar
# 启动镜像
sudo docker run -v `pwd`/convert_model:/data -it hub.hobot.cc/aitools/ai_toolchain_centos_7_xj3:v1.11.4
2 モデル変換
Horizon Toolchain の完全なモデル開発プロセスには、浮動小数点モデルの準備、モデル検証、モデル変換、パフォーマンス評価、精度評価といういくつかの重要な段階が必要です。
2.1 浮動小数点モデルの準備
caffemodel または onnx モデルを準備します。onnx で現在サポートされている主な opset バージョンは、opset10 と opset11 です。
2.2 検証モデル
Horizon プラットフォーム上でモデルをスムーズかつ効率的に実行できるようにするには、モデルで使用されるオペレーターがプラットフォームのオペレーター制約に従う必要があります。
各オペレーターの特定のパラメーター制限については、supported_op_list_and_restrictions/ パスにある「supported_op_list_and_restrictions_release」Excel シートを参照してください。
Horizon が多数のオペレーターをサポートしていることを考慮して、1 つずつ手動で検証する手間を避けるために、モデルで使用されているオペレーターのサポートを検証する hb_mapper チェッカー ツールが提供されています。
hb_mapper チェッカー ツールを使用してモデルを検証する
hb_mapper checker 参数解释:
--model-type
用于指定检查输入的模型类型,目前只支持设置 caffe 或者 onnx。
--march
用于指定需要适配的AI芯片类型,X/J3芯片应设置为 bernoulli2。
--proto
此参数仅在 model-type 指定 caffe 时有效,取值为Caffe模型的prototxt文件名称。
--model
在 model-type 被指定为 caffe 时,取值为Caffe模型的caffemodel文件名称。 在 model-type 被指定为 onnx 时,取值为ONNX模型文件名称。
--input-shape
可选参数,明确指定模型的输入shape。 取值为 {input_name} {NxHxWxC/NxCxHxW} ,input_name 与shape之间以空格分隔。 例如模型输入名称为 data1,输入shape为 [1,224,224,3], 则配置应该为 --input_shape data1 1x224x224x3。 如果此处配置shape与模型内shape信息不一致,以此处配置为准。
***说明***
注意一个 --input-shape 只接受一个name和shape组合,如果您的模型有多个输入节点, 在命令中多次配置 --input-shape 参数即可。
--output
可选参数,接受设置值为一个日志文件名称。 指定该参数情况下,检查的结果将输出到指定的日志文件。
デモの 01_chek.sh の例を使用すると、モデルの基本情報と BPU サポートがログに出力されます。これらの層は BPU で動作し、これらの層は CPU で動作します。効率を考慮して、調整することをお勧めします。 BPU 上のすべてのレイヤーが実行されます。
この例のモデルは適応されているため、すべての層が BPU 上で実行されます。
2.3 モデル変換
2.3.1 校正データの準備
モデル変換を実行する場合、キャリブレーション フェーズでは約 100 個のサンプル入力が必要となり、各サンプルは独立したデータ ファイルになります。変換されたモデルの精度を確保するために、これらのキャリブレーション サンプルはトレーニング モデルで使用されるトレーニング セットまたは検証セットから取得され、シーン カバレッジは可能な限り豊富になっています。単色のような非常にまれな異常なサンプルは使用しないでください。画像、検出または分類対象のない画像は待機します。
データ変換には 02_preprocess.sh を使用します。
構成ファイル内の preprocess_on パラメータは、モデル変換の次のステップに使用されます。これは、パラメータが有効な場合と無効な場合の 2 つの異なる前処理サンプル要件に対応します。preprocess_on がオフの場合、トレーニング セット/検証セットから取得したサンプルは推論前と同じ方法で前処理する必要があり、処理されたキャリブレーション サンプルは同じデータ型 (input_type_train)、サイズ (input_shape)、およびレイアウトになります。元のモデル (input_layout_train) として。
以下では例として rgb 形式の入力を使用し、前処理されたデータは cal_data ファイルに配置されます。前処理の特定のコードは data_preprocess.py ファイルにあります。
特記事項: 次の例では、モデルによって使用される実際の入力は bgr イメージです。この例では、.jpg イメージは cal_dataw フォルダーに直接配置されます。
2.3.2 hb_mapper Makertbin ツールを使用してモデルを変換する
hb_mapper makertbin参数解释:
--model-type
用于指定转换输入的模型类型,目前支持设置 caffe 或者 onnx。
--config
模型编译的配置文件,内容采用yaml格式,文件名使用.yaml后缀。一份完整的配置文件模板如下:
说明:
此处配置文件仅作展示,在实际模型配置文件中 caffe_model 与 onnx_model 两个参数只存在其中之一。 即,要么是Caffe模型,要么是ONNX模型。
03_build.sh スクリプトを使用して変換します 変換
完了例: ログを出力します
INFO ランタイム bin ファイルに正常に変換します!
INFO End Model
生成されたモデルを変換します
設定ファイルの説明:
# 模型转化相关的参数
# ------------------------------------
# model conversion related parameters
model_parameters:
# Onnx浮点网络数据模型文件
# -----------------------------------------------------------
# the model file of floating-point ONNX neural network data
onnx_model: './model/yolov5_p6_512x512_6head.onnx'
# 适用BPU架构
# --------------------------------
# the applicable BPU architecture
march: "bernoulli2"
# 指定模型转换过程中是否输出各层的中间结果,如果为True,则输出所有层的中间输出结果,
# --------------------------------------------------------------------------------------
# specifies whether or not to dump the intermediate results of all layers in conversion
# if set to True, then the intermediate results of all layers shall be dumped
layer_out_dump: False
# 日志文件的输出控制参数,
# debug输出模型转换的详细信息
# info只输出关键信息
# warn输出警告和错误级别以上的信息
# ----------------------------------------------------------------------------------------
# output control parameter of log file(s),
# if set to 'debug', then details of model conversion will be dumped
# if set to 'info', then only important imformation will be dumped
# if set to 'warn', then information ranked higher than 'warn' and 'error' will be dumped
log_level: 'debug'
# 模型转换输出的结果的存放目录
# -----------------------------------------------------------
# the directory in which model conversion results are stored
working_dir: 'model_output'
# 模型转换输出的用于上板执行的模型文件的名称前缀
# -----------------------------------------------------------------------------------------
# model conversion generated name prefix of those model files used for dev board execution
output_model_file_prefix: 'yolov5_p6_512x512'
# 模型输入相关参数, 若输入多个节点, 则应使用';'进行分隔, 使用默认缺省设置则写None
# --------------------------------------------------------------------------
# model input related parameters,
# please use ";" to seperate when inputting multiple nodes,
# please use None for default setting
input_parameters:
# (选填) 模型输入的节点名称, 此名称应与模型文件中的名称一致, 否则会报错, 不填则会使用模型文件中的节点名称
# --------------------------------------------------------------------------------------------------------
# (Optional) node name of model input,
# it shall be the same as the name of model file, otherwise an error will be reported,
# the node name of model file will be used when left blank
input_name: ""
# 网络实际执行时,输入给网络的数据格式,包括 nv12/rgb/bgr/yuv444/gray/featuremap,
# ------------------------------------------------------------------------------------------
# the data formats to be passed into neural network when actually performing neural network
# available options: nv12/rgb/bgr/yuv444/gray/featuremap,
input_type_rt: 'rgb'
# 网络实际执行时输入的数据排布, 可选值为 NHWC/NCHW
# 若input_type_rt配置为nv12,则此处参数不需要配置
# ------------------------------------------------------------------
# the data layout formats to be passed into neural network when actually performing neural network, available options: NHWC/NCHW
# If input_type_rt is configured as nv12, then this parameter does not need to be configured
input_layout_rt: 'NCHW'
# 网络训练时输入的数据格式,可选的值为rgb/bgr/gray/featuremap/yuv444
# --------------------------------------------------------------------
# the data formats in network training
# available options: rgb/bgr/gray/featuremap/yuv444
input_type_train: 'rgb'
# 网络训练时输入的数据排布, 可选值为 NHWC/NCHW
# ------------------------------------------------------------------
# the data layout in network training, available options: NHWC/NCHW
input_layout_train: 'NCHW'
# (选填) 模型网络的输入大小, 以'x'分隔, 不填则会使用模型文件中的网络输入大小,否则会覆盖模型文件中输入大小
# -------------------------------------------------------------------------------------------
# (Optional)the input size of model network, seperated by 'x'
# note that the network input size of model file will be used if left blank
# otherwise it will overwrite the input size of model file
input_shape: ''
# 网络实际执行时,输入给网络的batch_size, 默认值为1
# ---------------------------------------------------------------------
# the data batch_size to be passed into neural network when actually performing neural network, default value: 1
#input_batch: 1
# 网络输入的预处理方法,主要有以下几种:
# no_preprocess 不做任何操作
# data_mean 减去通道均值mean_value
# data_scale 对图像像素乘以data_scale系数
# data_mean_and_scale 减去通道均值后再乘以scale系数
# -------------------------------------------------------------------------------------------
# preprocessing methods of network input, available options:
# 'no_preprocess' indicates that no preprocess will be made
# 'data_mean' indicates that to minus the channel mean, i.e. mean_value
# 'data_scale' indicates that image pixels to multiply data_scale ratio
# 'data_mean_and_scale' indicates that to multiply scale ratio after channel mean is minused
norm_type: 'data_scale'
# 图像减去的均值, 如果是通道均值,value之间必须用空格分隔
# --------------------------------------------------------------------------
# the mean value minused by image
# note that values must be seperated by space if channel mean value is used
mean_value: ''
# 图像预处理缩放比例,如果是通道缩放比例,value之间必须用空格分隔
# ---------------------------------------------------------------------------
# scale value of image preprocess
# note that values must be seperated by space if channel scale value is used
scale_value: 0.003921568627451
# 模型量化相关参数
# -----------------------------
# model calibration parameters
calibration_parameters:
# 模型量化的参考图像的存放目录,图片格式支持Jpeg、Bmp等格式,输入的图片
# 应该是使用的典型场景,一般是从测试集中选择20~100张图片,另外输入
# 的图片要覆盖典型场景,不要是偏僻场景,如过曝光、饱和、模糊、纯黑、纯白等图片
# 若有多个输入节点, 则应使用';'进行分隔
# -------------------------------------------------------------------------------------------------
# the directory where reference images of model quantization are stored
# image formats include JPEG, BMP etc.
# should be classic application scenarios, usually 20~100 images are picked out from test datasets
# in addition, note that input images should cover typical scenarios
# and try to avoid those overexposed, oversaturated, vague,
# pure blank or pure white images
# use ';' to seperate when there are multiple input nodes
cal_data_dir: './cal_data'
# 如果输入的图片文件尺寸和模型训练的尺寸不一致时,并且preprocess_on为true,
# 则将采用默认预处理方法(skimage resize),
# 将输入图片缩放或者裁减到指定尺寸,否则,需要用户提前把图片处理为训练时的尺寸
# ---------------------------------------------------------------------------------
# In case the size of input image file is different from that of in model training
# and that preprocess_on is set to True,
# shall the default preprocess method(skimage resize) be used
# i.e., to resize or crop input image into specified size
# otherwise user must keep image size as that of in training in advance
preprocess_on: True
# 模型量化的算法类型,支持kl、max、default、load,通常采用default即可满足要求, 若为QAT导出的模型, 则应选择load
# ----------------------------------------------------------------------------------
# types of model quantization algorithms, usually default will meet the need
# available options:kl, max, default and load
# if converted model is quanti model exported from QAT , then choose `load`
calibration_type: 'default'
# 编译器相关参数
# ----------------------------
# compiler related parameters
compiler_parameters:
# 编译策略,支持bandwidth和latency两种优化模式;
# bandwidth以优化ddr的访问带宽为目标;
# latency以优化推理时间为目标
# -------------------------------------------------------------------------------------------
# compilation strategy, there are 2 available optimization modes: 'bandwidth' and 'lantency'
# the 'bandwidth' mode aims to optimize ddr access bandwidth
# while the 'lantency' mode aims to optimize inference duration
compile_mode: 'latency'
# 设置debug为True将打开编译器的debug模式,能够输出性能仿真的相关信息,如帧率、DDR带宽占用等
# -----------------------------------------------------------------------------------
# the compiler's debug mode will be enabled by setting to True
# this will dump performance simulation related information
# such as: frame rate, DDR bandwidth usage etc.
debug: False
# 编译模型指定核数,不指定默认编译单核模型, 若编译双核模型,将下边注释打开即可
# -------------------------------------------------------------------------------------
# specifies number of cores to be used in model compilation
# as default, single core is used as this value left blank
# please delete the "# " below to enable dual-core mode when compiling dual-core model
# core_num: 2
# 优化等级可选范围为O0~O3
# O0不做任何优化, 编译速度最快,优化程度最低,
# O1-O3随着优化等级提高,预期编译后的模型的执行速度会更快,但是所需编译时间也会变长。
# 推荐用O2做最快验证
# ----------------------------------------------------------------------------------------------------------
# optimization level ranges between O0~O3
# O0 indicates that no optimization will be made
# the faster the compilation, the lower optimization level will be
# O1-O3: as optimization levels increase gradually, model execution, after compilation, shall become faster
# while compilation will be prolonged
# it is recommended to use O2 for fastest verification
optimize_level: 'O3'
2.3.3 モデルのパフォーマンス分析と最適化
このセクションでは、Horizon が提供するツールを使用したモデルのパフォーマンスの評価を紹介します。これらのツールのパフォーマンス結果は、基本的に実際の実行と同じです。この段階で評価結果が期待を満たしていないことが判明した場合は、次のことを強くお勧めします。この段階では、Horizon に従って最適化を試みます。パフォーマンスの問題を解決するために推奨されます。
# 使用 hb_perf
hb_perf yolov5_p6_512x512.bin
実行結果の詳細な分析について
は公式ドキュメントを参照してください。ここでは説明しません。
2.3.4 モデルの精度分析とチューニング (出力に基づく推論)
モデル変換の出力を推論に使用する方法。前述したように、モデル変換が成功した場合の出力には、次の 4 つの部分が含まれます。
***_original_float_model.onnx
***_optimized_float_model.onnx
***_quantized_model.onnx
***.bin
bin モデルは AI チップに展開されるモデルですが、Ubuntu/CentOS 開発マシン上で精度評価を完了する利便性を考慮して、精度評価プロセスを完了するための **_quantized_model.onnx が提供されます。量子化モデルは量子化が完了しており、最終的なビン モデルと同じ精度効果があります。
この例では後処理コードを書き換えます。詳細については github を参照してください。完全なコードコード リンク
# 仿真
04_inference.sh
# 输出检测结果 result.jpg
参考文献
[1] 《Horizon AI ツールチェーン ユーザーガイド》
[2] https://developer.horizon.ai/forumDetail/136488103547258769