【2024·CANN训练营第一季】手写体识别模型训练和推理

参考链接
HandWritingTrainAndInfer

环境介绍

开发板:Huawei Atals 200I DK A2
内存:4G
NPU:Ascend 310B4
CANN:7.0.RC1

登录开发板

登录开发板

克隆代码

git clone https://gitee.com/ascend/EdgeAndRobotics.git

克隆代码

切换到样例目录安装依赖

 cd EdgeAndRobotics/Samples/HandWritingTrainAndInfer/
 pip install -r requirements.txt 
 pip install torch2.1.0 torchvision0.16.0

安装依赖
安装python3.9_torch2.1版本的torch_npu whl包

wget https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/wanzutao/torch_npu-2.1.0rc1-cp39-cp39-linux_aarch64.whl 
pip3 install torch_npu-2.1.0rc1-cp39-cp39-linux_aarch64.whl

安装torch_npu

配置程序编译依赖的头文件与库文件路径

export DDK_PATH=/usr/local/Ascend/ascend-toolkit/latest 
export NPU_HOST_LIB=$DDK_PATH/runtime/lib64/stub

安装离线推理所需的ACLLite库

查询OS版本

lsb_release -a

查询OS版本
安装ffmpeg

apt-get install ffmpeg libavcodec-dev libswscale-dev libavdevice-dev

安装ffmpeg
拉取ACLLite仓库,并进入目录编译安装

git clone https://gitee.com/ascend/ACLLite.git
cd ACLLite
bash build_so.sh

安装acllite

模型训练

切换到样例目录下,设置环境变量减小算子编译内存占用

export TE_PARALLEL_COMPILER=1
export MAX_COMPILE_CORE_NUMBER=1

运行训练脚本

python3 main.py

模型训练
模型训练
查询npu-smi使用信息
npu-smi info

在线推理

执行以下命令,将训练生成的mnist.pt转换mnist.onnx模型

python3 export.py

pt2onnx
执行以下命令,获取在线推理的测试图片

cd data
wget https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/wanzutao/mnist/8.jpg

get 8.jpg
执行在线推理

cd ../onnxInfer/
python3 infer.py

onnxInfer
推理结果 [image_path:/root/EdgeAndRobotics/Samples/HandWritingTrainAndInfer/data/8.jpg] [inferssession_time:124 pictures/s] [output:8]

离线推理

获取测试图片数据

cd omInfer/data
wget https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/wanzutao/mnist/8.jpg

8.jpg
当设备内存小于8G时,可设置如下两个环境变量减少atc模型转换过程中使用的进程数,减小内存占用。

 export TE_PARALLEL_COMPILER=1
 export MAX_COMPILE_CORE_NUMBER=1

将在线推理时导出的mnist.onnx模型拷贝到model目录下

 cd ../model cp ../../mnist.onnx ./ 

获取AIPP配置文件

 wget https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/wanzutao/mnist/ecs/aipp.cfg

模型转换

 atc --model=mnist.onnx --framework=5 --insert_op_conf=aipp.cfg --output=mnist --soc_version=Ascend310B4

在这里插入图片描述
编译样例源码

cd ../scripts
bash sample_build.sh

编译源码
运行样例

bash sample_run.sh

离线推理

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值