【2024·CANN训练营第一季】手写体识别模型训练和推理
收藏回复举报
【2024·CANN训练营第一季】手写体识别模型训练和推理
发表于2024-04-02 16:59:22
0 查看

参考链接 HandWritingTrainAndInfer

环境介绍

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

登录开发板

image.png

克隆代码

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

image.png

切换到样例目录安装依赖

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

image.png

安装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

image.png

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

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

安装离线推理所需的ACLLite库

查询OS版本

lsb_release -a

image.png

安装ffmpeg

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

image.png

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

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

image.png

模型训练

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

export TE_PARALLEL_COMPILER=1
export MAX_COMPILE_CORE_NUMBER=1

运行训练脚本

python3 main.py

image.png

查询npu-smi使用信息 image.png

在线推理

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

python3 export.py

image.png

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

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

image.png

执行在线推理

cd ../onnxInfer/
python3 infer.py

image.png

推理结果 [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

image.png image.png

当设备内存小于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

image.png

编译样例源码

cd ../scripts
bash sample_build.sh

image.png

运行样例

bash sample_run.sh

image.png

我要发帖子