模型推理

使用 hrt_model_exec infer 工具模型推理,用户自定义输入,推理一帧。

程序单线程运行单帧数据,输出模型运行的时间。

支持范围

模型支持范围:qat模型,ptq模型。

使用方法

Usage: hrt_model_exec [Option...] [Parameter] [Option] [instruction] --------------------------------------------------------------------------------------------------------------- -h --help Display this information -v --version Display this version [Option] [Parameter] --------------------------------------------------------------------------------------------------------------- --model_file [string]: Model file paths, separate by comma, each represents one model file path. --model_name [string]: Model name. When model_file has one more model and Subcommand is infer or perf, "model_name" must be specified! --input_file [string]: Input file paths, separate by comma, each represents one input. The extension of files should be one of [jpg, JPG, jpeg, JPEG, png, PNG, bin, txt] bin for binary such as image data, nv12 or yuv444 etc. txt for plain data such as image info. --core_id [string]: core id, 0 for any core, 1 for core 0, 2 for core 1 and etc, default is 0. Please confirm the number of bpu cores on the board before setting up. When you need to specify multiple cores, separate them with commas, such as "1,2". --frame_count [int] : frame count for run loop, default 200, valid when perf_time is 0 in perf mode; default 1 for infer mode. --dump_intermediate [string]: dump intermediate layer input and output. The default is 0. Subcommand must be infer. --enable_dump [bool] : flag for dump infer input and output. The default is false. Subcommand must be infer. --dump_precision [int] : Output dump precision for float32/float64 in txt file. Default is 9 decimal places. --dequantize_process [bool] : dequantize the model infer output. The default is false. Subcommand must be infer, enable_dump set as true --remove_padding_process [bool] : remove padding of the model infer output. The default is false. Subcommand must be infer, enable_dump set as true --dump_format [string]: output dump format, only support [bin, txt]. The default is bin. Subcommand must be infer. --dump_txt_axis [int] : The txt file of dump is expanded according to the specified axis; the default is -1, which means there is only one data per line; Subcommand must be infer, dump_format must be txt. range:[0, tensor_dimension]. --enable_cls_post_process [bool] : flag for classfication post process, only for ptq model now. Subcommand must be infer. --dump_path [string]: dump file path, --enable_dump or --dump_intermediate will dump model nodes inputs and outputs files. --input_img_properties [string]: Specify the color space of the image type input. Each image needs to specify the color space, separated by commas. The supported color spaces are [Y, UV]. --input_valid_shape [string]: Complete the validshape of the model input, allowing only the dynamic part to change. Provide two ways to set: 1. This only needs to be set when the validShape of the model input is dynamic. 2. Set for all inputs. Different inputs are separated by semicolons, and different dimensions are separated by commas. For example: --input_valid_shape="1,376,376,1;1,188,188,2". --input_stride [string]: Complete the stride of the model input, allowing only the dynamic part to change. Provide two ways to set: 1. This only needs to be set when the stride of model input is dynamic. 2. Set for all inputs. Different inputs are separated by semicolons, and different dimensions are separated by commas. For example: --input_stride="144384,384,1,1;72192,384,2,1". [Examples] --------------------------------------------------------------------------------------------------------------- hrt_model_exec infer --model_file --model_name --core_id --input_file --input_img_properties --input_valid_shape --input_stride --frame_count --dump_intermediate --enable_dump --dump_precision --dequantize_process --dump_path --remove_padding_process --dump_format --dump_txt_axis --enable_cls_post_process

参数说明

参数参数类型参数说明关联参数
-h, --help无。显示帮助信息。无。
-v, --version无。查看工具的 dnn 预测库版本号。无。
infer无。该参数执行模型推理,获取模型推理结果。该参数需要与 input_file 一起使用,指定输入图片路径,工具根据模型信息resize图片,整理模型输入信息。
model_filestring模型文件路径,多个路径可通过逗号分隔。无。
model_namestring指定某个模型的名称。无。
input_filestring模型输入信息。
输入后缀必须为 PNG/JPG/JPEG/png/jpg/jpeg/bin/txt 中的一种。
每个输入之间需要用英文字符的逗号隔开,,如:xxx.jpg,input.txt
该参数需要与子命令infer配合使用,指定输入图片路径,工具根据模型信息resize图片,整理模型输入信息。
core_idstring指定运行核。0:任意核,1:core0,2:core1,以此类推。默认为 0。当指定多个核运行时,用英文字符的逗号隔开,如 "1,2"无。
input_img_propertiesstring模型图像输入的色彩空间信息,参数范围[Y, UV]。该参数需要与input_file 一起使用,input_file中每一个图片类型输入都需要指定一个Y/UV类型,每个输入色彩空间之间需要用英文字符的逗号隔开,,如:Y,UV
input_valid_shapestring模型动态 validShape 输入信息。
若模型输入属性 validShape 中含有 -1,则需要将 -1 的部分进行补全,多个 validShape 间通过英文分号间隔。
如:--input_valid_shape="1,376,376,1;1,188,188,2"
无。
input_stridestring模型动态 stride 输入信息。多个 stride 间通过英文分号间隔。
若模型输入属性 stride 中含有 -1,则需要将 -1 的部分进行补全,多个 stride 间通过英文分号间隔。
如:--input_stride="50176,224,1,1;25088,224,2,1"
无。
frame_countint执行模型运行帧数。
  • 子命令为infer时,默认为 1。
  • 子命令为perf时,默认为 200。
  • 当子命令为perf时,没有设置perf_time时生效。
    dump_intermediatestringdump模型每一层输入和输出,参数范围[0, 3]。默认为0。
  • dump_intermediate=0 时,默认dump功能关闭。
  • dump_intermediate=1 时,模型中每一层节点输入数据输出数据以 bin 方式保存, 其中节点输入输出为 stride 数据。
  • dump_intermediate=2 时,模型中每一层节点输入数据和输出数据以 bintxt 两种方式保存,其中节点输入输出为 stride 数据。
  • dump_intermediate=3 时,型中每一层节点输入数据和输出数据以 bintxt 两种方式保存,其中节点输入输出为 valid 数据。
  • 无。
    enable_dumpbool使能dump模型输入和输出。默认为 false无。
    dump_precisionint控制txt格式输出float型数据的小数点位数。默认为9。无。
    dequantize_processbool对模型输出进行反量化处理。默认为falseenable_dump参数为true时生效。
    remove_padding_processbool对模型输出进行去padding处理。默认为 falseenable_dump参数为true时生效。
    dump_formatstringdump模型输入和输出的格式,参数范围[bin, txt]。默认为 bin无。
    dump_txt_axisint控制txt格式输入输出的换行规则。
    若输出维度为n,则参数范围为[0, n]。 默认为 -1,一行一个数据。
    无。
    enable_cls_post_processbool使能分类后处理。默认为 false该参数需要与子命令infer配合使用,目前只支持ptq分类模型的后处理,打印分类结果。
    dump_pathstringdump模型输入输出的路径。该参数在设置enable_dumpdump_intermediate时生效。

    使用示例

    模型推理

    单模型推理

    hrt_model_exec infer --model_file=xxx.hbm --input_file=xxx.bin ../aarch64/bin/hrt_model_exec infer --model_file=resnet50_224x224_nv12.hbm --input_file=zebra_cls.jpeg,zebra_cls.jpeg --input_img_properties=Y,UV --input_stride=50176,224,1,1;25088,224,2,1 Load model to DDR cost 1965.03ms. [I][35143][06-28][10:39:51:373][file_util.cpp:527][hrt_model_exec][HRT_MODEL_EXEC] The input valid shape is (1,224,224,1), and the image [zebra_cls.jpeg] will be scaled to 224x224 [I][35143][06-28][10:39:51:377][file_util.cpp:527][hrt_model_exec][HRT_MODEL_EXEC] The input valid shape is (1,112,112,2), and the image [zebra_cls.jpeg] will be scaled to 224x224 ---------------------Frame 0 begin--------------------- Infer time: 1.464 ms ---------------------Frame 0 end---------------------

    多模型推理

    hrt_model_exec infer --model_file=xxx.hbm,xxx.hbm --model_name=xx --input_file=xxx.jpg

    模型输入说明

    多输入模型说明

    工具 infer 推理功能支持多输入模型的推理,支持图片输入、二进制文件输入以及文本文件输入,输入数据用逗号隔开。 模型的输入信息可以通过 model_info 进行查看。

    hrt_model_exec infer --model_file=xxx.hbm --input_file=xxx.jpg,input.txt

    动态输入说明

    若模型的输入是动态的,您需要根据输入实际情况使用 input_valid_shapeinput_stride 参数来补全动态信息。您可以选择以下两种方式指定参数:

    • 只给定动态输入的 validShapestride 信息。

    • 给定所有输入的 validShapestride 信息,非动态输入的信息必须与模型信息保持一致。

    注解

    工具内部会对于动态输入的信息会进行最大限度的自动补全,方便您更简单的进行性能评测,您可以根据实际情况选择是否交由工具内部进行自动补全。

    • 若输入 stride 为动态的,validShape 为固定的,您可以不指定 input_stride 参数,工具会自动按照最小对齐规则进行补齐并打印对齐信息。

    • 若输入 validShapestride 都是动态的:

      • 若指定输入为图片类型,您需要指定图片的色彩空间信息 input_img_properties,其余参数可不指定,工具内部按照图片的大小补全 input_valid_shapeinput_stride 信息。

      • 若指定输入为图片之外的类型或不指定输入,您需要设置 input_valid_shape 信息,内部会自动补全 input_stride 信息并打印。

    动态输入介绍 章节中的模型为例,您可以通过以下命令运行模型:

    # 只给定动态输入的信息 hrt_model_exec infer --model_file=xxx.hbm --input_file="input_y.bin,input_uv.bin,input_roi.bin" --input_valid_shape="1,220,220,1;1,110,110,2" --input_stride="49280,224,1,1;24640,224,2,1" # 给定所有输入的信息 hrt_model_exec infer --model_file=xxx.hbm --input_file="input_y.bin,input_uv.bin,input_roi.bin" --input_valid_shape="1,220,220,1;1,110,110,2;1,4" --input_stride="49280,224,1,1;24640,224,2,1;16,4"

    图像类型输入说明

    input_file 给定图像输入时,需要使用 input_img_properties 参数来指定您想使用该图片的哪个色彩空间当做模型的输入,目前只支持 YUV 两种色彩空间。

    hrt_model_exec infer --model_file=xxx.hbm --input_file="img.jpg,img.jpg,input_roi.bin" --input_img_properties="Y,UV"