华为昇腾310B1平台深度学习算法模型转换

2024-05-10 11:20

本文主要是介绍华为昇腾310B1平台深度学习算法模型转换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

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)

pip install -r requirements.txtpip install onnx
pip install onnxruntime==1.6.0
pip install onnxsimpip install opc-tool==0.1.0
pip install decorator 
pip install protobuf==3.20.3
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文件夹里面有

## 帮助信息
### === Model Options ===
###  --version      yolov5 tags [2.0/3.1/4.0/5.0/6.0/6.1], default: 6.1
###  --model        yolov5[n/s/m/l/x], default: yolov5s
###  --bs           batch size, default: 4
### === Build Options ===
###  --type         data type [fp16/int8], default: fp16
###  --calib_bs     batch size of calibration data (int8 use only), default: 16
### === Inference Options ===
###  --mode         infer/val, default: infer
###  --conf         confidence threshold, default: 0.4
###  --iou          NMS IOU threshold, default: 0.5
###  --output_dir   output dir, default: output
### === Environment Options ===
###  --soc          soc version [Ascend310/Ascend310P?], default: Ascend310
### === Help Options ===
###  -h             print this messagehelp() {sed -rn 's/^### ?//;T;p;' "$0"
}## 参数设置
GETOPT_ARGS=`getopt -o 'h' -al version:,model:,img_size:,channel_num:,bs:,class_num:,type:,calib_bs:,mode:,conf:,iou:,output_dir:,soc: -- "$@"`
eval set -- "$GETOPT_ARGS"
while [ -n "$1" ]
docase "$1" in-h) help; exit 0 ;; --version) version=$2; shift 2;;--model) model=$2; shift 2;;--img_size) img_size=$2; shift 2;;--channel_num) channel_num=$2; shift 2;;--bs) bs=$2; shift 2;;--class_num) class_num=$2; shift 2;;--type) type=$2; shift 2;;--calib_bs) calib_bs=$2; shift 2;;--mode) mode=$2; shift 2;;--conf) conf=$2; shift 2;;--iou) iou=$2; shift 2;;--output_dir) output_dir=$2; shift 2;;--soc) soc=$2; shift 2;;--) break ;;esac
doneif [[ -z $version ]]; then version=6.1; fi
if [[ -z $model ]]; then model=yolov5s; fi
if [[ -z $img_size ]]; then img_size=608; fi
if [[ -z $channel_num ]]; then channel_num=3; fi
if [[ -z $bs ]]; then bs=4; fi
if [[ -z $class_num ]]; then class_num=3; fi
if [[ -z $type ]]; then type=fp16; fi
if [[ -z $calib_bs ]]; then calib_bs=16; fi
if [[ -z $mode ]]; then mode=infer; fi
if [[ -z $conf ]]; then conf=0.4; fi
if [[ -z $iou ]]; then iou=0.5; fi
if [[ -z $output_dir ]]; then output_dir=output; fi
if [[ -z $soc ]]; then echo "error: missing 1 required argument: 'soc'"; exit 1 ; fiif [[ ${type} == fp16 ]] ; thenargs_info="=== pth2om args === \n version: $version \n model: $model \n bs: $bs \n type: $type \n mode: $mode \n conf: $conf \n iou: $iou \n output_dir: $output_dir \n soc: $soc"echo -e $args_info
elseargs_info="=== pth2om args === \nversion: $version \n model: $model \n bs: $bs \n type: $type \n calib_bs: $calib_bs \n mode: $mode \n conf: $conf \n iou: $iou \n output_dir: $output_dir \n soc: $soc"echo -e $args_info
fiif [ ! -d ${output_dir} ]; thenmkdir ${output_dir}
fi## pt导出om模型
echo "Starting 修改pytorch源码"
git checkout . && git checkout v${version}
git apply v${version}/v${version}.patchecho "Starting 导出onnx模型并简化"
if [[ ${version} == 6* ]] ; thenpython3 export.py --weights=${model}.pt --imgsz=${img_size} --batch-size=${bs} --opset=11 --dynamic || exit 1
elsepython3 models/export.py --weights=${model}.pt --img-size=${img_size} --batch-size=${bs} --opset=11 --dynamic || exit 1
fi
python3 -m onnxsim ${model}.onnx ${model}.onnx --dynamic-input-shape --input-shape images:${bs},${channel_num},${img_size},${img_size} || exit 1
model_tmp=${model}if [ ${type} == int8 ] ; thenecho "Starting 生成量化数据"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 1if [[ ${version} == 6.1 && ${model} == yolov5[nl] ]] ; thenecho "Starting pre_amct"python3 common/quantize/calibration_scale.py --input=${model}.onnx --output=${model}_cali.onnx --mode=pre_amct || exit 1echo "Starting onnx模型量化"bash common/quantize/amct.sh ${model}_cali.onnx || exit 1if [[ -f ${output_dir}/result_deploy_model.onnx ]];thenmv ${output_dir}/result_deploy_model.onnx ${model}_amct.onnxfirm -rf ${model}_cali.onnxecho "Starting after_amct"python3 common/quantize/calibration_scale.py --input=${model}_amct.onnx --output=${model}_amct.onnx --mode=after_amct || exit 1elseecho "Starting onnx模型量化"bash common/quantize/amct.sh ${model}.onnx || exit 1if [[ -f ${output_dir}/result_deploy_model.onnx ]];thenmv ${output_dir}/result_deploy_model.onnx ${model}_amct.onnxfifimodel_tmp=${model}_amctif [[ -f ${output_dir}/result_* ]];thenrm -rf  ${output_dir}/result_result_fake_quant_model.onnxrm -rf  ${output_dir}/result_quant.jsonfi
fiecho "Starting 修改onnx模型,添加NMS后处理算子"
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 1echo "Starting onnx导出om模型(有后处理)"
bash common/util/atc.sh infer ${model_tmp}_nms.onnx ${output_dir}/${model_tmp}_nms ${img_size} ${channel_num} ${bs} ${soc} || exit 1
rm -rf ${model_tmp}_nms.onnxif [[ ${mode} == val ]] ; thenecho "Starting onnx导出om模型(无后处理)"bash common/util/atc.sh val ${model_tmp}.onnx ${output_dir}/${model_tmp} ${bs} ${soc} || exit 1rm -rf ${model_tmp}.onnx
fiecho -e "pth导出om模型 Success \n"

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

mode=$1
onnx=$2
om=$3
img_size=$4
channel_num=$5
bs=$6
soc=$7if [ ${mode} == val ];theninput_shape="images:${bs},${channel_num},${img_size},${img_size}"input_fp16_nodes="images"
elif [ ${mode} == infer ];theninput_shape="images:${bs},${channel_num},${img_size},${img_size};img_info:${bs},4"input_fp16_nodes="images;img_info"
fiif [[ ${soc} == Ascend310 ]];thenatc --model=${onnx} \--framework=5 \--output=${om}_bs${bs} \--input_format=NCHW \--input_shape=${input_shape} \--log=error \--soc_version=${soc} \--input_fp16_nodes=${input_fp16_nodes} \--output_type=FP16
fiif [[ ${soc} == Ascend310B1 ]];thenatc --model=${onnx} \--framework=5 \--output=${om}_bs${bs} \--input_format=NCHW \--input_shape=${input_shape} \--log=error \--soc_version=${soc} \--optypelist_for_implmode="Sigmoid" \--op_select_implmode=high_performance \--fusion_switch_file=common/util/fusion.cfg \--insert_op_conf=aipp_yolov5.cfg#--input_fp16_nodes=${input_fp16_nodes} #--output_type=FP16 
fiif [[ ${soc} == Ascend310P? ]];thenatc --model=${onnx} \--framework=5 \--output=${om}_bs${bs} \--input_format=NCHW \--input_shape=${input_shape} \--log=error \--soc_version=${soc} \--optypelist_for_implmode="Sigmoid" \--op_select_implmode=high_performance \--fusion_switch_file=common/util/fusion.cfg \--insert_op_conf=aipp_yolov5.cfg#--input_fp16_nodes=${input_fp16_nodes} #--output_type=FP16 
fiif [[ ${soc} == Ascend710 ]];thenatc --model=${onnx} \--framework=5 \--output=${om}_bs${bs} \--input_format=NCHW \--input_shape=${input_shape} \--log=error \--soc_version=${soc} \--optypelist_for_implmode="Sigmoid" \--op_select_implmode=high_performance \--fusion_switch_file=common/util/fusion.cfg #--insert_op_conf=aipp_yolov5.cfg
#       --insert_op_conf=aipp.cfg
#       --insert_op_conf=aipp_yolov5.cfg
fiif [[ ${soc} == Ascend910 ]];thenatc --model=${onnx} \--framework=5 \--output=${om}_bs${bs} \--input_format=NCHW \--input_shape=${input_shape} \--log=error \--soc_version=${soc} \--optypelist_for_implmode="Sigmoid" \--op_select_implmode=high_performance \--fusion_switch_file=common/util/fusion.cfg #--insert_op_conf=aipp_yolov5.cfg
#       --insert_op_conf=aipp.cfg
#       --insert_op_conf=aipp_yolov5.cfg
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

这篇关于华为昇腾310B1平台深度学习算法模型转换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/976294

相关文章

Java中Redisson 的原理深度解析

《Java中Redisson的原理深度解析》Redisson是一个高性能的Redis客户端,它通过将Redis数据结构映射为Java对象和分布式对象,实现了在Java应用中方便地使用Redis,本文... 目录前言一、核心设计理念二、核心架构与通信层1. 基于 Netty 的异步非阻塞通信2. 编解码器三、

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

深入理解Mysql OnlineDDL的算法

《深入理解MysqlOnlineDDL的算法》本文主要介绍了讲解MysqlOnlineDDL的算法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小... 目录一、Online DDL 是什么?二、Online DDL 的三种主要算法2.1COPY(复制法)

Java轻松实现PDF转换为PDF/A的示例代码

《Java轻松实现PDF转换为PDF/A的示例代码》本文将深入探讨Java环境下,如何利用专业工具将PDF转换为PDF/A格式,为数字文档的永续保存提供可靠方案,文中的示例代码讲解详细,感兴趣的小伙伴... 目录为什么需要将PDF转换为PDF/A使用Spire.PDF for Java进行转换前的准备通过

Linux五种IO模型的使用解读

《Linux五种IO模型的使用解读》文章系统解析了Linux的五种IO模型(阻塞、非阻塞、IO复用、信号驱动、异步),重点区分同步与异步IO的本质差异,强调同步由用户发起,异步由内核触发,通过对比各模... 目录1.IO模型简介2.五种IO模型2.1 IO模型分析方法2.2 阻塞IO2.3 非阻塞IO2.4

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

深度解析Python中递归下降解析器的原理与实现

《深度解析Python中递归下降解析器的原理与实现》在编译器设计、配置文件处理和数据转换领域,递归下降解析器是最常用且最直观的解析技术,本文将详细介绍递归下降解析器的原理与实现,感兴趣的小伙伴可以跟随... 目录引言:解析器的核心价值一、递归下降解析器基础1.1 核心概念解析1.2 基本架构二、简单算术表达