【Atlas200】华为适配ATC模型转换工具

ATC模型转换工具

ATC模型转换工具是华为推出的一款用于将各种深度学习模型转换为华为AI芯片(包括Ascend和Kirin)可执行的模型格式的工具。该工具支持TensorFlow、Caffe、ONNX、MindSpore和TensorFlow Lite等多种深度学习框架,可以将这些框架的模型转换为华为AI芯片可执行的OM模型格式。ATC模型转换工具具有高效、快速、准确的特点,可以帮助用户将深度学习模型快速部署到华为AI芯片上,从而实现高效的推理和计算。同时,ATC模型转换工具还提供了多种优化和转换选项,可以根据用户需求进行灵活配置,以获得更好的转换效果和性能。

参数说明

generate offline model example:
./atc --model=./alexnet.prototxt --weight=./alexnet.caffemodel 
--framework=0 --output=./domi 
generate offline model for single op example:
./atc --singleop=./op_list.json --output=./op_model 
===== Basic Functionality =====
[General]
  --h/help            Show this help message
  --mode              Run mode. 0(default): generate offline model; 1: convert model to JSON format; 3: only pre-check; 5: convert ge dump txt file to JSON format; 6: display model info

[Input]
  --model             Model file
  --weight            Weight file. Required when framework is Caffe
  --om                The model file to be converted to json
  --framework         Framework type. 0:Caffe; 1:MindSpore; 3:Tensorflow; 5:Onnx
  --input_format      Format of input data. E.g.: "NCHW"
  --input_shape       Shape of input data. Separate multiple nodes with semicolons (;). Use double quotation marks (") to enclose each argument.
E.g.: "input_name1:n1,c1,h1,w1;input_name2:n2,c2,h2,w2"
  --input_shape_range Shape range of input data. Separate multiple nodes with semicolons (;).Use double quotation marks (") to enclose each argument.
E.g.: "input_name1:[n1~n2,c1,h1,w1];input_name2:[n2,c2~c3,h2,w2]"
  --dynamic_batch_size Set dynamic batch size. E.g.: "batchsize1,batchsize2,batchsize3"
  --dynamic_image_size Set dynamic image size. Separate multiple nodes with semicolons (;). Use double quotation marks (") to enclose each argument.
E.g.: "imagesize1_height,imagesize1_width;imagesize2_height,imagesize2_width"
  --dynamic_dims      Set dynamic dims. Separate multiple nodes with semicolons (;). Use double quotation marks (") to enclose each argument.
E.g.: "dims1_n1,dims1_n2;dims2_n1,dims2_n2"
  --singleop          Single op definition file. atc will generate offline model(s) for single op if --singleop is set.

[Output]
  --output            Output file path&name(needn't suffix, will add .om automatically). 
                      If --singleop is set, this arg specifies the directory to which the single op offline model will be generated
  --output_type       Set net output type. Support FP32, FP16, UINT8. E.g.: FP16, indicates that all out nodes are set to FP16.
"node1:0:FP16;node2:1:FP32", indicates setting the datatype of multiple out nodes.
  --check_report      The pre-checking report file. Default value is: "check_result.json"
  --json              The output json file path&name which is converted from a model

问题解决:he requied parameter [–soc_version] for ATC is empty.

这个错误提示意味着在运行ATC工具时没有指定–soc_version参数或该参数的值为空。–soc_version参数用于指定目标AI芯片的版本,ATC需要根据芯片版本对模型进行优化和转换。如果缺少该参数或者参数值为空,ATC将无法进行模型转换。
要解决该问题,需要在运行ATC工具时指定正确的–soc_version参数。例如,如果您要将模型转换为HiSilicon NPU(Ascend)芯片上运行,可以使用以下命令:

atc --model=model/resnet50.prototxt --weight=model/resnet50.caffemodel --framework=0 --soc_version=Ascend310 --output=model/resnet50.om

其中,–soc_version参数指定芯片版本为Ascend310。您可以根据实际芯片版本指定该参数的值。如果您不确定芯片版本,可以参考芯片的规格书或者联系芯片供应商获取相关信息。
如果您已经正确指定了–soc_version参数但仍然遇到该错误,可以检查该参数的值是否正确或者尝试重新运行ATC工具。

- 动态BatchSize

atc --framework=0 --soc_version=${
    
    soc_version} 
--model=$HOME/mod/resnet50.prototxt 
--weight=$HOME/mod/resnet50.caffemodel 
--output=$HOME/mod/out/caffe_resnet50 
--input_shape="data:-1,3,224,224" --dynamic_batch_size="1,2,4,8" 

其中,“–input_shape ”中的“-1”表示设置动态BatchSize,具体支持哪些BatchSize由“–dynamic_batch_size”决定。
“-dynamic_batch_size”:指定有限个batch档位。动态batch特性不是说batch数可以随便选多少都行,要指定有限个batch档位,并在模型转换时就配置好。在上述指令中,我们指定了“1,2,4,8”这几个档位,那么实际推理时,就只能送这几个档位数量的图片进模型。“dynamic_batch_size”最多同时配置100个档位,多个不同档位之间用英文半角逗号分隔开即可。

- 动态分辨率

atc --framework=0 --soc_version=${
    
    soc_version} 
--model=$HOME/mod/resnet50.prototxt 
--weight=$HOME/mod/resnet50.caffemodel 
--output=$HOME/mod/out/caffe_resnet50 
--input_shape="data:1,3,-1,-1" --dynamic_image_size="224,224;448,448"

其中,“–input_shape ”中的“-1,-1”表示设置动态分辨率,具体支持哪些分辨率由“–dynamic_image_size ”决定。
“–input_shape”:参数中“?”代表着这一维度是动态的,“?”放在batch的位置,就是动态batch模型。

精度模式设置

–precision_mode

force_fp16:表示算子支持fp16和fp32时,强制选择fp16。
allow_fp32_to_fp16:如果算子支持fp32,则保留原始精度fp32;如果算子不支持fp32,则选择fp16。
must_keep_origin_dtype:表示保持原图精度:如果网络模型中存在部分算子,并且该算子实现不支持fp32,比如Conv2D卷积算子,则不能使用该参数。
allow_mix_precision:表示使用混合精度模式:
若配置了该种模式,则可以在OPP软件包安装路径
${install_path}/opp/built-in/op_impl/ai_core/tbe/config/${soc_version}/aic-${soc_version}-ops-info.json
文件中查看“precision_reduce”参数的取值:

若该参数取值为true,则表示相应算子使用fp16精度。
若该参数取值为false,则表示相应算子使用fp32精度。
若算子没有配置该参数,则表示跟随前一个算子的精度。

推荐配置及收益

所配置的精度模式不同,网络模型精度以及性能有所不同,具体为:
精度高低排序:must_keep_origin_dtype>allow_fp32_to_fp16>allow_mix_precision>force_fp16
性能优劣排序:force_fp16>=allow_mix_precision>allow_fp32_to_fp16>must_keep_origin_dtype
参数默认值:force_fp16

例子

fsanet转换

atc --model=fsanet_dynamic.onnx --framework=5 --output=fsanet_dynamic --soc_version=Ascend310 --input_format=NCHW -input_shape="input:-1,3,64,64" --dynamic_batch_size="1,2,3,4,5,6,7,8" 

猜你喜欢

转载自blog.csdn.net/hh1357102/article/details/130703513
今日推荐