第一个例子是 图片分类的应用
因第一个,直接获取已训练好的开源模型,选择Caffe框架的ResNet-50模型。
ResNet-50模型的基本介绍如下:
输入数据:RGB格式、224*224分辨率的输入图片
输出数据:图片的类别标签及其对应置信度
AscendCL(Ascend Computing Language)是一套用于在CANN(Compute Architecture for Neural Networks)上开发深度神经网络推理应用的C语言API库,提供模型加载与执行、媒体数据处理、算子加载与执行等API,能够实现在昇腾CANN平台上进行深度学习推理计算、图形图像预处理、单算子加速计算等能力。
图片流程来自昇腾社区

昇腾 需要专用的模型,第一步 需要对开源模型进行转换。
使用ATC(Ascend Tensor Compiler)工具将开源框架的网络模型转换为适配昇腾AI处理器的离线模型(*.om文件)。
ATC 参数说明
npu-smi info
+--------------------------------------------------------------------------------------------------------+
| npu-smi 23.0.rc2 Version: 23.0.rc2 |
+-------------------------------+-----------------+------------------------------------------------------+
| NPU Name | Health | Power(W) Temp(C) Hugepages-Usage(page) |
| Chip Device | Bus-Id | AICore(%) Memory-Usage(MB) |
+===============================+=================+======================================================+
| 0 310P3 | OK | NA 44 0 / 0 |
| 0 0 | 0000:01:00.0 | 0 1698 / 21527 |
+===============================+=================+======================================================+
| 32 310P3 | OK | NA 41 0 / 0 |
| 0 1 | 0000:02:00.0 | 0 1699 / 21527 |
+===============================+=================+======================================================+
| 32800 310P3 | OK | NA 45 0 / 0 |
| 0 2 | 0000:82:00.0 | 0 1697 / 21527 |
+===============================+=================+======================================================+
| 32896 310P3 | OK | NA 47 0 / 0 |
| 0 3 | 0000:85:00.0 | 0 1698 / 21527 |
+===============================+=================+======================================================+
Name 310P3
即soc_version 为Ascend310P3
下载文件:
atc --model=model/resnet50.prototxt --weight=model/resnet50.caffemodel --framework=0 --output=model/resnet50 --soc_version=Ascend310P3
非root用户转换 失败 这里一定要用root
root# atc --model=model/resnet50.prototxt --weight=model/resnet50.caffemodel --framework=0 --output=model/resnet50 --soc_version=Ascend310P3
ATC start working now, please wait for a moment.
...
ATC run success, welcome to the next use.
初始化
Device管理
Context管理
Stream管理
内存管理
数据传输
调用aclrtMemcpy接口通过内存复制的方式实现数据传输。
模型推理
创建main.cpp
#include "acl/acl.h"
#include
#include
#include
#include bash sample_build.sh
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/g++
-- Check for working CXX compiler: /usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- env INC_PATH: /usr/local/Ascend/ascend-toolkit/latest
-- env LIB_PATH: /usr/local/Ascend/ascend-toolkit/latest/aarch64-linux/devlib
-- Configuring done
-- Generating done
-- Build files have been written to: /home/shaolin/demo/build/intermediates/host
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable /home/shaolin/demo/out/main
[100%] Built target main
make for app MyFirstApp_build Successfully
bash sample_run.sh
top 1: index[161] value[0.766602]
top 2: index[162] value[0.155762]
top 3: index[167] value[0.038452]
top 4: index[163] value[0.021576]
top 5: index[166] value[0.011642]
有疑问 欢迎加公众号 讨论
