背景

首先,感谢OrangePi联合华为办的活动以及邀请评测,其实我们观察华为的昇腾芯片很久。在平常项目开发中,都是使用的Nvidia的Jetson系列芯片进行边缘AI计算开发,由于使用场景受限的问题,我们必须得考虑国产替代方案 。这次,我将使用Orange Pi Alpro板卡进行单目标跟踪算法的部署以及算力评测,以此来了解昇腾芯片在今后使用的可行性,同时,也和大家一起分享Orange Pi Alpro的与众不同的地方, 板子具备的电气接口如下图所示:

该芯片的型号可以通过`npu-smi`查看,其芯片型号为310B4, 固件版本为23.0.0, 如下所示:

(tracking_object) HwHiAiUser@orangepiaipro:/mnt/ssd1t/SiamRPN/VOT2016$ npu-smi info
+--------------------------------------------------------------------------------------------------------+
| npu-smi 23.0.0                                   Version: 23.0.0                                       |
+-------------------------------+-----------------+------------------------------------------------------+
| NPU     Name                  | Health          | Power(W)     Temp(C)           Hugepages-Usage(page) |
| Chip    Device                | Bus-Id          | AICore(%)    Memory-Usage(MB)                        |
+===============================+=================+======================================================+
| 0       310B4                 | Alarm           | 0.0          52                15    / 15            |
| 0       0                     | NA              | 0            6411 / 7545                             |
+===============================+=================+======================================================+

准备工作

存储空间扩容

由于orangepi alpro 默认只有32G的SD卡,本次测评的时间有限,暂时没空去做迁移系统,我们为了扩展存储空间, 暂且将ssd硬盘挂载到mnt文件夹。

sudo mount /dev/nvne0n1 /mnt/ssd1t  

在/etc/fstab文件中, 添加以下内容, 来保证ssd重启也能正常挂载。

/dev/nvme0n1 /mnt/ssd1t ext4 defaults 0 0

开启swap空间

由于orange pi aplro的内存空间有限, 到后面使用atc工具转模型的时候, 会出现内存不够的问题,并且板子会出现死机的现象,所以,我们需要通过以下命令进行swap空间的扩充。

# 步骤1: 创建一个新的swap文件,在固态硬盘下创建一个16G的swap空间
sudo fallocate -l 16G /mnt/ssd1t/swapfile
 
# 步骤2: 设置正确的权限,只有root用户可以读写
sudo chmod 600 /swapfile
 
# 步骤3: 格式化新的swap文件
sudo mkswap /swapfile
 
# 步骤4: 启用新的swap文件
sudo swapon /swapfile
 
# 步骤5: 修改/etc/fstab文件,使swap在重启后依然有效
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
 

准备VOT2016数据集和json文件

我们需要准备相关的数据集用于后续的推理测试。 以下链接是百度网盘的链接, 笔者已经都分享出来, 大家可以直接将文件下载下来, 然后放到/home/HwHiAiUser/SiamRPN目录下。

  • VOT2016 数据集下载链接

链接:https://pan.baidu.com/s/1OVgaLndWdlE9f3mVYWm0Bw

提取码:aydp

  • 数据集的Json文件链接:

链接:https://pan.baidu.com/s/1E5pdTKm7TBFPNZgEkAir2Q

提取码:2y7c

 

相关代码下载

首先, 通过以下命令下载昇腾的补丁代码, 并将代码放置在/home/HwHiAiUser/SiamRPN路径下

git clone https://gitee.com/ascend/ModelZoo-PyTorch.git

通过以下命令下载siamrpn的代码, 并将代码拷贝到/home/HwHiAiUser/SiamRPN目录下。

git clone https://github.com/STVIR/pysot.git

通过以下命令, 将代码补丁打到siamrpn的代码仓库,以便其能调用ascend的npu才能真正评测昇腾的ai算力。

patch -N -p1 < ../siamrpn.patch

注意: 补丁别打错了 fix.patch那个补丁有问题

 

模型转换

在华为的昇腾的代码仓库https://gitee.com/ascend/modelzoo, 有很多模型样例可以参考,通过以上链接,我们可以找到华为适配的很多案例。

为了使用华为的NPU以及相应的CANN框架进行加速, 我们必须将开源的模型进行相应的转换。

使用PyTorch将模型权重文件.pth转换为.onnx文件,再使用ATC工具将.onnx文件转为离线推理模型文件.om文件。

  • 获取权重文件。 在PySOT Model Zoo上获取权重文件siamrpn_r50_l234_dwxcorr,相应的配置文件, 放置到以下目录中。

  • 导出onnx文件,执行以下命令,
bash test/pth2onnx.sh

获得SiamRPN.onnx文件。

  • 使用ATC工具将ONNX模型转OM模型, 该脚本中的型号参数, 要改成orange pi aprlo 的Ascend310B4
bash test/onnx2om.sh

如下图, 可以看到我们转换出来的om文件和onnx文件。

安装ais_bench推理工具

为了测试芯片的性能, 我们需要安装ais bench推理工具,用来测试推理模型的性能(包括吞吐率、时延)。

ais_bench推理工具的安装包括aclruntime包ais_bench推理程序包的安装。

  • 首先, 通过以下命令安装aclruntime包:
pip3 install -v 'git+https://gitee.com/ascend/tools.git#egg=aclruntime&subdirectory=ais-bench_workload/tool/ais_bench/backend'
  • 其次, 安装ais bench推理包。
pip3 install -v 'git+https://gitee.com/ascend/tools.git#egg=ais_bench&subdirectory=ais-bench_workload/tool/ais_bench'

如果提示如下信息, 则表示安装成功。

Successfully installed ais_bench-{version}

部署Python环境

首先, 我们的环境固件驱动的版本是23.0.0, CANN版本为7.0.0。

其次, 系统是ubuntu22.04版本, 通过Conda创建一个虚拟环境。

conda install python3.8.19

SiamRPN框架需要配置的环境安装包列表如下, 需要我们都完整安装上。

(tracking_object) @orangepiaipro:/mnt/ssd1t/SiamRPN$ pip3 list
Package           Version
----------------- --------
absl-py           2.1.0
aclruntime        0.0.2
ais_bench         0.0.2
attrs             23.2.0
auto-tune         0.1.0
cloudpickle       3.0.0
colorama          0.4.4
Cython            0.29.24
dataflow          0.0.1
decorator         5.1.1
hccl              0.1.0
hccl-parser       0.1
Jinja2            3.1.4
MarkupSafe        2.1.5
mpmath            1.3.0
msadvisor         1.0.0
networkx          3.1
numpy             1.22.0
onnx              1.16.0
op-compile-tool   0.1.0
op-gen            0.1
op-test-frame     0.1
opc-tool          0.1.0
opencv-python     4.5.4.58
Pillow            8.3.1
pip               24.0
protobuf          5.27.0
psutil            5.9.8
PyYAML            6.0.1
requests          2.32.2
schedule-search   0.0.1
scipy             1.10.1
setuptools        69.5.1
six               1.16.0
sympy             1.12
synr              0.5.0
te                0.4.0
toolkit           0.0.0
torch             1.7.1
tornado           6.4
tqdm              4.50.2
typing_extensions 4.12.0
wheel             0.43.0
yacs              0.1.8

特别需要注意的是torch的版本必须是1.7.1,以免函数库不兼容, 这是一个较低的版本,需要自己指定安装源。

可以参考以下命令进行操作:

pip3  torch==1.7.1 -i https://download.pytorch/whl/torch_stable.html 

算力评测

matrix数据集的追踪效果如下, 在310B上的帧率差不多只有6fps, 软件还有待进一步的优化。

通过以下命令, 我们可以通过310B对VOT2016数据集执行推理、精度验证与性能验证,该命令中的0表示, 我们将使用npu 0进行推理。

bash test/eval_acc_perf.sh VOT2016 0 
整个推理过程, 在310B上运行了将近四十分钟, 最终对芯片的评估结果如下:
 
(tracking_object) HwHiAiUser@orangepiaipro:/mnt/ssd1t/SiamRPN$ bash test/eval_acc_perf.sh VOT2016 0
[INFO] acl init success
[INFO] open device 0 success
[INFO] load model /mnt/ssd1t/SiamRPN/SiamRPN.om success
[INFO] create model description success
loading VOT2016: 100%|███████████████████████████████████████| 60/60 [00:02<00:00, 26.48it/s, wiper]
(  1) Video: bag          Time: 31.8s Speed: 6.1fps Lost: 0
(  2) Video: ball1        Time: 16.8s Speed: 6.2fps Lost: 0
(  3) Video: ball2        Time:  6.5s Speed: 6.2fps Lost: 0
(  4) Video: basketball   Time: 115.6s Speed: 6.3fps Lost: 1
(  5) Video: birds1       Time: 52.9s Speed: 6.4fps Lost: 2
(  6) Video: birds2       Time: 86.6s Speed: 6.2fps Lost: 0
(  7) Video: blanket      Time: 35.2s Speed: 6.4fps Lost: 1
(  8) Video: bmx          Time: 12.3s Speed: 6.1fps Lost: 0
(  9) Video: bolt1        Time: 55.8s Speed: 6.3fps Lost: 0
( 10) Video: bolt2        Time: 46.0s Speed: 6.3fps Lost: 1
( 11) Video: book         Time: 27.2s Speed: 6.4fps Lost: 1
( 12) Video: butterfly    Time: 24.4s Speed: 6.1fps Lost: 0
( 13) Video: car1         Time: 118.8s Speed: 6.2fps Lost: 1
( 14) Video: car2         Time: 62.6s Speed: 6.3fps Lost: 0
( 15) Video: crossing     Time: 21.4s Speed: 6.1fps Lost: 0
( 16) Video: dinosaur     Time: 51.0s Speed: 6.4fps Lost: 2
( 17) Video: fernando     Time: 47.1s Speed: 6.2fps Lost: 1
( 18) Video: fish1        Time: 57.0s Speed: 6.4fps Lost: 2
( 19) Video: fish2        Time: 47.9s Speed: 6.4fps Lost: 2
( 20) Video: fish3        Time: 83.0s Speed: 6.2fps Lost: 0
( 21) Video: fish4        Time: 109.1s Speed: 6.2fps Lost: 0
( 22) Video: girl         Time: 243.2s Speed: 6.2fps Lost: 0
( 23) Video: glove        Time: 18.4s Speed: 6.5fps Lost: 1
( 24) Video: godfather    Time: 58.6s Speed: 6.2fps Lost: 0
( 25) Video: graduate     Time: 136.8s Speed: 6.2fps Lost: 0
( 26) Video: gymnastics1  Time: 91.0s Speed: 6.2fps Lost: 0
( 27) Video: gymnastics2  Time: 38.6s Speed: 6.2fps Lost: 1
( 28) Video: gymnastics3  Time: 18.9s Speed: 6.2fps Lost: 1
( 29) Video: gymnastics4  Time: 75.3s Speed: 6.2fps Lost: 0
( 30) Video: hand         Time: 41.1s Speed: 6.5fps Lost: 2
( 31) Video: handball1    Time: 60.1s Speed: 6.3fps Lost: 0
( 32) Video: handball2    Time: 61.2s Speed: 6.6fps Lost: 4
( 33) Video: helicopter   Time: 115.7s Speed: 6.1fps Lost: 1
( 34) Video: iceskater1   Time: 108.2s Speed: 6.1fps Lost: 0
( 35) Video: iceskater2   Time: 115.7s Speed: 6.1fps Lost: 1
( 36) Video: leaves       Time:  9.0s Speed: 6.9fps Lost: 2
( 37) Video: marching     Time: 32.4s Speed: 6.2fps Lost: 0
( 38) Video: matrix       Time: 16.1s Speed: 6.2fps Lost: 0
( 39) Video: motocross1   Time: 25.8s Speed: 6.3fps Lost: 1
( 40) Video: motocross2   Time: 10.2s Speed: 5.9fps Lost: 0
( 41) Video: nature       Time: 166.3s Speed: 6.0fps Lost: 2
( 42) Video: octopus      Time: 51.6s Speed: 5.6fps Lost: 0
( 43) Video: pedestrian1  Time: 22.3s Speed: 6.2fps Lost: 0
( 44) Video: pedestrian2  Time: 114.4s Speed: 6.2fps Lost: 0
( 45) Video: rabbit       Time: 22.9s Speed: 6.9fps Lost: 3
( 46) Video: racing       Time: 25.0s Speed: 6.2fps Lost: 0
( 47) Video: road         Time: 89.1s Speed: 6.2fps Lost: 0
( 48) Video: shaking      Time: 58.8s Speed: 6.2fps Lost: 0
( 49) Video: sheep        Time: 40.1s Speed: 6.2fps Lost: 0
( 50) Video: singer1      Time: 57.2s Speed: 6.1fps Lost: 0
( 51) Video: singer2      Time: 60.7s Speed: 6.0fps Lost: 0
( 52) Video: singer3      Time: 21.3s Speed: 6.1fps Lost: 0
( 53) Video: soccer1      Time: 60.7s Speed: 6.4fps Lost: 3
( 54) Video: soccer2      Time: 19.1s Speed: 6.7fps Lost: 2
( 55) Video: soldier      Time: 21.6s Speed: 6.4fps Lost: 1
( 56) Video: sphere       Time: 32.7s Speed: 6.1fps Lost: 0
( 57) Video: tiger        Time: 59.1s Speed: 6.2fps Lost: 0
( 58) Video: traffic      Time: 30.5s Speed: 6.2fps Lost: 0
( 59) Video: tunnel       Time: 50.3s Speed: 6.2fps Lost: 0
( 60) Video: wiper        Time: 52.9s Speed: 6.4fps Lost: 2
model total lost: 41
average speed: 6.430868886068378 fps
[INFO] unload model success, model Id is 1
[INFO] end to reset device 0
[INFO] end to finalize acl
loading VOT2016: 100%|███████████████████████████████████████| 60/60 [00:02<00:00, 26.24it/s, wiper]
eval ar: 100%|████████████████████████████████████████████████████████| 1/1 [00:02<00:00,  2.12s/it]
eval eao: 100%|███████████████████████████████████████████████████████| 1/1 [00:02<00:00,  2.63s/it]

在310B中的推理数据如下:

------------------------------------------------------------
|Tracker Name| Accuracy | Robustness | Lost Number |  EAO  |
------------------------------------------------------------
|   model    |  0.639   |   0.191    |    41.0     | 0.447 |
------------------------------------------------------------

Pysot的推理数据如下:


------------------------------------------------------------
|Tracker Name| Accuracy | Robustness |   Average   |  EAO  |
------------------------------------------------------------
|  VOT2016   |  0.642   |   0.196    |    35fps    | 0.464 |
------------------------------------------------------------

通过对比, 我们可以发现, 在310B上, Accuracy精度值有所下降, 整体的鲁棒系数也有所下降, 性能确实有所损失, 但是, 对于边缘AI计算应用, 相应的指标也足够应付了。

发现的问题

  • 首先,官方的这颗核心过于神秘了,最基本的数据手册和性能参数都查找得很费劲,这十分没必要。 因为,只有参与进来的人越多, 昇腾的生态才会越来越活跃。
  • 其次, 昇腾官网的教程资料其实已经严重滞后于英伟达Jetson, 整个论坛除了官方号, 其实并不是非常的活跃。
  • 对于很多初学者来说, 昇腾的很多教程相当随意, 没有英伟达文档的那种规范度。 其实, 昇腾的芯片相比英伟达的并不差, 甚至在某些方面还会超过英伟达, 比如能耗方面。 最大的差距就是生态建设和推广活动, 所以,这次笔者是蛮感谢orange pi举行的这次活动, 让我们用户更多了一些机会去接触这些新片子。

后续

后续, 我们将尝试在板子上挂载摄像头, 进行单目标追踪, 未完待续。

Logo

昇腾万里,让智能无所不及

更多推荐