【CANN训练营笔记】Atlas 200I DK A2体验手写数字识别模型训练&推理

环境介绍

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

准备环境

下载编译好的torch_npu

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

安装PyTorch2.1.0、torchvision0.16.0

pip install torch==2.1.0 torchvision==0.16.0

设置环境变量

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

安装ACLLite库

安装ffmpeg
Ubuntu

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

欧拉系统

yum install ffmpeg ffmpeg-devel

将yum安装的opencv头文件软链到系统能默认识别的路径

ln -s /usr/include/ffmpeg/* /usr/include/

源码安装方式:

wget https://ffmpeg.org/releases/ffmpeg-4.2.9.tar.gz
tar -zxvf ffmpeg-4.2.9.tar.gz
cd ffmpeg-4.2.9
./configure --disable-static --enable-shared --disable-doc --enable-ffplay --enable-ffprobe --enable-avdevice --disable-debug --enable-demuxers --enable-parsers --enable-protocols --enable-small --enable-avresample
make -j8
make install

为保证程序能识别动态库,请在/etc/ld.so.conf.d下添加ffmpeg.conf配置

cd /etc/ld.so.conf.d
vim ffmpeg.conf

添加内容

/usr/local/lib

生效配置文件:

ldconfig

设置ffmpeg安装路径环境变量,请替换为ffmpeg的实际安装路径

export FFMPEG_PATH=/usr/local/lib

克隆ACLLite代码仓

git clone https://gitee.com/ascend/ACLLite.git
cd ACLLite

安装

bash build_so.sh

安装依赖

克隆代码仓

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

进入代码文件夹

cd EdgeAndRobotics/Samples/HandWritingTrainAndInfer

安装依赖

pip3 install -r requirements.txt

模型训练

设置环境变量减小算子编译内存占用

export TE_PARALLEL_COMPILER=1
export MAX_COMPILE_CORE_NUMBER=1

运行训练脚本

python main.py

开始运行
image.png
NPU使用情况
image.png

在线推理

训练生成的mnist.pt转换mnist.onnx模型

python3 export.py

获取测试图片

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

测试图片:
image.png

执行在线推理

cd ../onnxInfer/
python3 infer.py

推理结果

[image_path:data/8.jpg] [inferssession_time:1349 pictures/s] [output:8]

离线推理

获取测试图片

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

获取PyTorch框架的ResNet50模型(.onnx),并转换为昇腾AI处理器能识别的模型(.om)
为了方便下载,在这里直接给出原始模型下载及模型转换命令,可以直接拷贝执行。
将在线推理时导出的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

运行结果

[INFO] value[1.000000] output[8]

最近更新

  1. TCP协议是安全的吗?

    2024-03-31 23:00:03       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-31 23:00:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-31 23:00:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-31 23:00:03       20 阅读

热门阅读

  1. 输入 wo xiang he ni jao peng you.,倒着打。

    2024-03-31 23:00:03       18 阅读
  2. NC20128 不重复数字

    2024-03-31 23:00:03       16 阅读
  3. ES6:Map()与WeakMap()

    2024-03-31 23:00:03       15 阅读
  4. 探索Vue脚手架:构建现代化Web应用的利器

    2024-03-31 23:00:03       16 阅读
  5. 网页的皮肤——css

    2024-03-31 23:00:03       19 阅读
  6. 【对图书的ISBN 号码进行识别处理】

    2024-03-31 23:00:03       17 阅读
  7. 达梦数据库 创建外部表 [-7082]:外部表数据错误.

    2024-03-31 23:00:03       15 阅读