当前位置:   article > 正文

华为昇腾310B1平台深度学习算法模型转换_value ascend310b1 is invalid for the soc version.

value ascend310b1 is invalid for the soc version.

目录

1 模型转换(集成nms算子到模型中)

1.1 基础模型说明

1.2 模型转换

1.2.1 设置环境变量

1.2.2 安装yolov5依赖(gcc需要>7.5)

1.2.3 转换fp16模型

2 模型转换(使用atc,不集成nms算子)

参考文献:


1 模型转换(集成nms算子到模型中)

1.1 基础模型说明

对于Yolov5模型,华为提供了单独的脚本执行转换,目的通过自定义的Yolov5后处理算子将NMS操作集成到离线模型中,提高推理性能。Yolov5模型转换脚本位于计算库的ascend_yolov5_pt2om,模型转换时使用官方原始yolov5s-v6.1为基础训练的人车非.pt模型,该模型有3个输出。转换工具会自动将3个输出合并为一个输出,并转为onnx模型,之后再转为om模型。默认精度为FP16。

对于INT8,当前转换工具量化后准确率有下降,对于实时性强的场景不适合,暂不使用。

ascend_yolov5_pt2om已上传到csdn资源(你自己的百度网盘里面也有一份)https://download.csdn.net/download/u013171226/89286331?spm=1001.2014.3001.5501

1.2 模型转换

模型转换主要是将.pt模型转为Ascentd可推理的.om模型,包括三种数据类型(fp16、fp32和int8),模型转换过程下。模型转换使用ascend_yolov5_pt2om工程实现。

1.2.1 设置环境变量

source /usr/local/Ascend/ascend-toolkit/set_env.sh 

1.2.2 安装yolov5依赖(gcc需要>7.5)

  1. pip install -r requirements.txt
  2. pip install onnx
  3. pip install onnxruntime==1.6.0
  4. pip install onnxsim
  5. pip install opc-tool==0.1.0
  6. pip install decorator
  7. pip install protobuf==3.20.3
  8. pip install numpy

1.2.3 转换fp16模型

bash common/pth2om.sh --version 6.1 --type fp16 --model yolov5_pcb_608_out3 --img_size 608 --class_num 3 --bs 1 --soc Ascend310B1

其中pth2om.sh脚本内容如下,在ascend_yolov5_pt2om文件夹里面有

  1. ## 帮助信息
  2. ### === Model Options ===
  3. ### --version yolov5 tags [2.0/3.1/4.0/5.0/6.0/6.1], default: 6.1
  4. ### --model yolov5[n/s/m/l/x], default: yolov5s
  5. ### --bs batch size, default: 4
  6. ### === Build Options ===
  7. ### --type data type [fp16/int8], default: fp16
  8. ### --calib_bs batch size of calibration data (int8 use only), default: 16
  9. ### === Inference Options ===
  10. ### --mode infer/val, default: infer
  11. ### --conf confidence threshold, default: 0.4
  12. ### --iou NMS IOU threshold, default: 0.5
  13. ### --output_dir output dir, default: output
  14. ### === Environment Options ===
  15. ### --soc soc version [Ascend310/Ascend310P?], default: Ascend310
  16. ### === Help Options ===
  17. ### -h print this message
  18. help() {
  19. sed -rn 's/^### ?//;T;p;' "$0"
  20. }
  21. ## 参数设置
  22. GETOPT_ARGS=`getopt -o 'h' -al version:,model:,img_size:,channel_num:,bs:,class_num:,type:,calib_bs:,mode:,conf:,iou:,output_dir:,soc: -- "$@"`
  23. eval set -- "$GETOPT_ARGS"
  24. while [ -n "$1" ]
  25. do
  26. case "$1" in
  27. -h) help; exit 0 ;;
  28. --version) version=$2; shift 2;;
  29. --model) model=$2; shift 2;;
  30. --img_size) img_size=$2; shift 2;;
  31. --channel_num) channel_num=$2; shift 2;;
  32. --bs) bs=$2; shift 2;;
  33. --class_num) class_num=$2; shift 2;;
  34. --type) type=$2; shift 2;;
  35. --calib_bs) calib_bs=$2; shift 2;;
  36. --mode) mode=$2; shift 2;;
  37. --conf) conf=$2; shift 2;;
  38. --iou) iou=$2; shift 2;;
  39. --output_dir) output_dir=$2; shift 2;;
  40. --soc) soc=$2; shift 2;;
  41. --) break ;;
  42. esac
  43. done
  44. if [[ -z $version ]]; then version=6.1; fi
  45. if [[ -z $model ]]; then model=yolov5s; fi
  46. if [[ -z $img_size ]]; then img_size=608; fi
  47. if [[ -z $channel_num ]]; then channel_num=3; fi
  48. if [[ -z $bs ]]; then bs=4; fi
  49. if [[ -z $class_num ]]; then class_num=3; fi
  50. if [[ -z $type ]]; then type=fp16; fi
  51. if [[ -z $calib_bs ]]; then calib_bs=16; fi
  52. if [[ -z $mode ]]; then mode=infer; fi
  53. if [[ -z $conf ]]; then conf=0.4; fi
  54. if [[ -z $iou ]]; then iou=0.5; fi
  55. if [[ -z $output_dir ]]; then output_dir=output; fi
  56. if [[ -z $soc ]]; then echo "error: missing 1 required argument: 'soc'"; exit 1 ; fi
  57. if [[ ${type} == fp16 ]] ; then
  58. args_info="=== pth2om args === \n version: $version \n model: $model \n bs: $bs \n type: $type \n
  59. mode: $mode \n conf: $conf \n iou: $iou \n output_dir: $output_dir \n soc: $soc"
  60. echo -e $args_info
  61. else
  62. args_info="=== pth2om args === \nversion: $version \n model: $model \n bs: $bs \n type: $type \n calib_bs: $calib_bs \n
  63. mode: $mode \n conf: $conf \n iou: $iou \n output_dir: $output_dir \n soc: $soc"
  64. echo -e $args_info
  65. fi
  66. if [ ! -d ${output_dir} ]; then
  67. mkdir ${output_dir}
  68. fi
  69. ## pt导出om模型
  70. echo "Starting 修改pytorch源码"
  71. git checkout . && git checkout v${version}
  72. git apply v${version}/v${version}.patch
  73. echo "Starting 导出onnx模型并简化"
  74. if [[ ${version} == 6* ]] ; then
  75. python3 export.py --weights=${model}.pt --imgsz=${img_size} --batch-size=${bs} --opset=11 --dynamic || exit 1
  76. else
  77. python3 models/export.py --weights=${model}.pt --img-size=${img_size} --batch-size=${bs} --opset=11 --dynamic || exit 1
  78. fi
  79. python3 -m onnxsim ${model}.onnx ${model}.onnx --dynamic-input-shape --input-shape images:${bs},${channel_num},${img_size},${img_size} || exit 1
  80. model_tmp=${model}
  81. if [ ${type} == int8 ] ; then
  82. echo "Starting 生成量化数据"
  83. python3 common/quantize/generate_data.py --img_info_file=common/quantize/img_info_amct.txt --save_path=amct_data --batch_size=${calib_bs} --img_size=${img_size} || exit 1
  84. if [[ ${version} == 6.1 && ${model} == yolov5[nl] ]] ; then
  85. echo "Starting pre_amct"
  86. python3 common/quantize/calibration_scale.py --input=${model}.onnx --output=${model}_cali.onnx --mode=pre_amct || exit 1
  87. echo "Starting onnx模型量化"
  88. bash common/quantize/amct.sh ${model}_cali.onnx || exit 1
  89. if [[ -f ${output_dir}/result_deploy_model.onnx ]];then
  90. mv ${output_dir}/result_deploy_model.onnx ${model}_amct.onnx
  91. fi
  92. rm -rf ${model}_cali.onnx
  93. echo "Starting after_amct"
  94. python3 common/quantize/calibration_scale.py --input=${model}_amct.onnx --output=${model}_amct.onnx --mode=after_amct || exit 1
  95. else
  96. echo "Starting onnx模型量化"
  97. bash common/quantize/amct.sh ${model}.onnx || exit 1
  98. if [[ -f ${output_dir}/result_deploy_model.onnx ]];then
  99. mv ${output_dir}/result_deploy_model.onnx ${model}_amct.onnx
  100. fi
  101. fi
  102. model_tmp=${model}_amct
  103. if [[ -f ${output_dir}/result_* ]];then
  104. rm -rf ${output_dir}/result_result_fake_quant_model.onnx
  105. rm -rf ${output_dir}/result_quant.json
  106. fi
  107. fi
  108. echo "Starting 修改onnx模型,添加NMS后处理算子"
  109. python3 common/util/modify_model.py --pt=${model}.pt --onnx=${model_tmp}.onnx --img-size=${img_size} --class-num=${class_num} --conf-thres=${conf} --iou-thres=${iou} || exit 1
  110. echo "Starting onnx导出om模型(有后处理)"
  111. bash common/util/atc.sh infer ${model_tmp}_nms.onnx ${output_dir}/${model_tmp}_nms ${img_size} ${channel_num} ${bs} ${soc} || exit 1
  112. rm -rf ${model_tmp}_nms.onnx
  113. if [[ ${mode} == val ]] ; then
  114. echo "Starting onnx导出om模型(无后处理)"
  115. bash common/util/atc.sh val ${model_tmp}.onnx ${output_dir}/${model_tmp} ${bs} ${soc} || exit 1
  116. rm -rf ${model_tmp}.onnx
  117. fi
  118. echo -e "pth导出om模型 Success \n"

然后atc.sh脚本内容如下,在ascend_yolov5_pt2om文件夹里面也有

  1. mode=$1
  2. onnx=$2
  3. om=$3
  4. img_size=$4
  5. channel_num=$5
  6. bs=$6
  7. soc=$7
  8. if [ ${mode} == val ];then
  9. input_shape="images:${bs},${channel_num},${img_size},${img_size}"
  10. input_fp16_nodes="images"
  11. elif [ ${mode} == infer ];then
  12. input_shape="images:${bs},${channel_num},${img_size},${img_size};img_info:${bs},4"
  13. input_fp16_nodes="images;img_info"
  14. fi
  15. if [[ ${soc} == Ascend310 ]];then
  16. atc --model=${onnx} \
  17. --framework=5 \
  18. --output=${om}_bs${bs} \
  19. --input_format=NCHW \
  20. --input_shape=${input_shape} \
  21. --log=error \
  22. --soc_version=${soc} \
  23. --input_fp16_nodes=${input_fp16_nodes} \
  24. --output_type=FP16
  25. fi
  26. if [[ ${soc} == Ascend310B1 ]];then
  27. atc --model=${onnx} \
  28. --framework=5 \
  29. --output=${om}_bs${bs} \
  30. --input_format=NCHW \
  31. --input_shape=${input_shape} \
  32. --log=error \
  33. --soc_version=${soc} \
  34. --optypelist_for_implmode="Sigmoid" \
  35. --op_select_implmode=high_performance \
  36. --fusion_switch_file=common/util/fusion.cfg \
  37. --insert_op_conf=aipp_yolov5.cfg
  38. #--input_fp16_nodes=${input_fp16_nodes}
  39. #--output_type=FP16
  40. fi
  41. if [[ ${soc} == Ascend310P? ]];then
  42. atc --model=${onnx} \
  43. --framework=5 \
  44. --output=${om}_bs${bs} \
  45. --input_format=NCHW \
  46. --input_shape=${input_shape} \
  47. --log=error \
  48. --soc_version=${soc} \
  49. --optypelist_for_implmode="Sigmoid" \
  50. --op_select_implmode=high_performance \
  51. --fusion_switch_file=common/util/fusion.cfg \
  52. --insert_op_conf=aipp_yolov5.cfg
  53. #--input_fp16_nodes=${input_fp16_nodes}
  54. #--output_type=FP16
  55. fi
  56. if [[ ${soc} == Ascend710 ]];then
  57. atc --model=${onnx} \
  58. --framework=5 \
  59. --output=${om}_bs${bs} \
  60. --input_format=NCHW \
  61. --input_shape=${input_shape} \
  62. --log=error \
  63. --soc_version=${soc} \
  64. --optypelist_for_implmode="Sigmoid" \
  65. --op_select_implmode=high_performance \
  66. --fusion_switch_file=common/util/fusion.cfg
  67. #--insert_op_conf=aipp_yolov5.cfg
  68. # --insert_op_conf=aipp.cfg
  69. # --insert_op_conf=aipp_yolov5.cfg
  70. fi
  71. if [[ ${soc} == Ascend910 ]];then
  72. atc --model=${onnx} \
  73. --framework=5 \
  74. --output=${om}_bs${bs} \
  75. --input_format=NCHW \
  76. --input_shape=${input_shape} \
  77. --log=error \
  78. --soc_version=${soc} \
  79. --optypelist_for_implmode="Sigmoid" \
  80. --op_select_implmode=high_performance \
  81. --fusion_switch_file=common/util/fusion.cfg
  82. #--insert_op_conf=aipp_yolov5.cfg
  83. # --insert_op_conf=aipp.cfg
  84. # --insert_op_conf=aipp_yolov5.cfg
  85. fi

2 模型转换(使用atc,不集成nms算子)

      上述模型转换都是基于.pt文件转换为.om模型文件。另外,还可以直接应用atc工具将onnx模型转为.om模型。

bash common/util/atc.sh infer yolov5_pcb_608_out3_nms.onnx output/yolov5_pcb_608_out3_nms 1 Ascend310B1

或者直接使用atc工具转换

      (1)人车非模型
        atc --model=yolov5_pcb_608_out3_nms.onnx \
            --framework=5 \
            --output=yolov5_pcb_608_out3_bs4 \
            --input_format=NCHW \
            --input_shape="images:1,3,640,640;img_info:1,4" \
            --log=error \
            --soc_version=Ascend710 \
            --optypelist_for_implmode="Sigmoid" \
            --op_select_implmode=high_performance 
        
      (2)行人结构化模型
        atc --model=pedes_structure.onnx \
            --framework=5 \
            --output=pedes_structure \
            --input_format=NCHW \
            --input_shape="x:-1,3,224,224" \
            --dynamic_batch_size="1,2,4,8" \
            --log=error \
            --soc_version=Ascend710 \
            --optypelist_for_implmode="Sigmoid" \
            --op_select_implmode=high_performance 

参考文献:

海思Hi3519 DV500 部署yolov5并加速优化_dv500移植yolov5-CSDN博客

samples: CANN Samples - Gitee.com

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号