sx · 2021年08月28日

【R329开发板评测】实机验证Resnet模型(基于Baidu Aistudio平台)

R329板烧录和启动

建议烧录镜像如下三个:

  • Tina Linux:
    矽速科技网盘存放链接: https://aijishu.com/a/1060000000221762
    我将文件存放在了链接: https://pan.baidu.com/s/1b172QiUoCaJp14GYptLzAw 提取码: e45v
    这里实测发现使用网盘内的tina_r329-evb5_uart0_0723.img用Phonix Card(工具在PhoenixCard.zip解压后所得)烧录后存放文件系统挂载问题(需手动尝试将文件夹内的rootfs.tar.gz烧录到SD卡中,步骤暂未探索出来),建议直接用tina_r329-evb5_uart0_0721.img
  • Armbian和Debian:

https://aijishu.com/a/1060000000223179

相对来说,Armbian更像是一个原版的Ubuntu系统,上电后需配置root账号密码和用户账号密码,Debian Mainstream较为简单。上电后,插入串口,输入矽速科技设定的用户名和密码登录shell终端即可体验。

注:镜像用Win32DiskManager烧录更为稳定,PhoenixCard烧录有时候需要两次三次烧录后,插入开发板才能启动。

R329默认镜像内体现AIPU实机运行

我这里使用的是上一节提到的Debian mainstream。按照以上链接提供内容烧录好,插卡到开发板上,使用USB Type C接上,即可观察到需要登录信息,login时输入root,Password时输入sipeed即可登录成功。

为了快速体验AIPU功能,输入如下指令即可:

    cd ~/zhouyi_test      
    ./run.sh

使用cat ~/zhouyi_test/run.sh可知,原理为其调用了同目录下的zhouyi_cam,而zhouyi_cam指向了build目录下的zhouyi_cam可执行程序

根据上一章节Debian的指导手册,开启了交换分区,即可在开发板上直接在~/zhouyi_test/build目录下就地编译,对应所有原代码均在~/zhouyi_test目录下:

    -rw-r--r-- 1 root root   13654 Aug 14  2021 CMakeCache.txt
    drwxr-xr-x 5 root root    4096 Aug 14  2021 CMakeFiles
    -rw-r--r-- 1 root root     657 Jul 30 11:22 CMakeLists.txt            -------CMake程序控制文件
    -rw-r--r-- 1 root root    5903 Aug 14  2021 Makefile
    -rwxr-xr-x 1 root root 4832748 Jul 30 11:20 aipu.bin                  -------AIPUBuild生成的可执行程序
    drwxr-xr-x 3 root root    4096 Aug 14  2021 build
    -rw-r--r-- 1 root root    1604 Aug 14  2021 cmake_install.cmake
    -rw-r--r-- 1 root root    9506 Jul 30 11:20 fb_display.c              -------显示部分处理,获取Framebuffer并且往/dev/fb0写入显示数据
    -rw-r--r-- 1 root root    1647 Jul 30 11:20 fbviewer.h
    drwxr-xr-x 3 root root    4096 Jul 30 11:20 include
    -rw-r--r-- 1 root root   33843 Jul 30 11:20 label.h
    -rwxr-xr-x 1 root root  168728 Jul 30 11:20 libaipudrv.so.4
    -rwxr-xr-x 1 root root  168728 Jul 30 11:20 libaipudrv.so.4.1.1
    -rw-r--r-- 1 root root    5294 Jul 30 11:20 low_level_api.h
    -rw-r--r-- 1 root root   11013 Jul 14 01:38 main.cpp                  -------主程序,以opencv读取视频,送入aipu执行推理,并计算结果用于屏显示
    -rwxrwxrwx 1 root root      31 Jul 30 11:36 run.sh
    -rw-r--r-- 1 root root   24235 Jul 30 11:20 standard_api.h
    -rwxr-xr-x 1 root root  145672 Aug 14  2021 zhouyi_cam

编译方法较为快捷:

    cd ~/zhouyi_test/build && cmake .. && make

Open Model Zoo下载

周易AIPU团队最新提供了Model Zoo:https://aijishu.com/a/1060000000223632

通过以上网页提供的内容即可通过SFTP客户端下载下来很多验证成功的Model,在这里推荐使用FileZilla Client: https://www.filezilla.cn/download/client

为了充分利用安谋科技提供的资源,我这里下载了多个模型进行验证:

   - inception_v3(仿真申请开发板时我使用的就是这个模型)
   - mobilenet_v1
   - mobilenet_v1_ssd   
   - mobilenet_v2_ssd  
   - resnet_50(官方SDK即使用了这个分类模型作为示例,演示了整个部署到AIPU的流程)

有兴趣的童鞋可以尝试在Open Model Zoo中的更多模型。

下载后每个文件夹内存放目录和文件如下:

    ├─Caffe
    │  ├─cfg
    │  └─input
    ├─ONNX
    │  ├─cfg
    │  └─input
    ├─TF
    │  ├─v1
    │  │  ├─cfg
    │  │  └─input
    │  └─v2
    │      ├─cfg
    │      └─input
    └─TFLite
       ├─cfg
       └─input

其中的Caffe/ONNX/TF/TFLite为AIPU直接支持的模型,cfg目录提供了aipubuild使用到的build脚本,input目录提供了对应模型的冻结的模型(包含权重和模型graph),通过配置文件和冻结的模型即可编译出对应的aipu.bin,aipu.bin文件即可部署到R329开发板上进行实机的模型推理。

软件模拟验证Resnet模型(基于Baidu Aistudio平台)

从上一步获取到的resnet_50模型中,选了其中onnx模型来验证电脑端推理结果。

我这里选择了Baidu Aistudio作为平台来验证onnx推理(为什么选用Baidu Aistudio?因为这里提供了免费的GPU算力和数据集/模型下载,非常方便使用).

我的Imagenet 2012 1k数据数据集就是这里下载的,速度完美, 直达方法:https://aistudio.baidu.com/ai...搜索框输入ILSVRC点击搜索即可获得结果,我这里下载了"ILSVRC2012 ImageNet 验证集"作为本次验证校准和实机验证的图片。

这里为了方便,验证了一下onnx模型在Baidu AIStudio上的推理:

   # 上传上一步下载的resnet_50.onnx模型到/home/aistudio/work目录下
   # 上传ImageNet验证集中的ILSVRC2012_val_00000099.JPEG到/home/aistudio/work目录下
   # 上传ImageNet验证集中的标签文件cid_to_labels.txt到/home/aistudio/work目录下
   # Use ONNX runtime to do inference
   !pip install onnxruntime
   
   import onnxruntime as rt
   import numpy as np

   sess = rt.InferenceSession('/home/aistudio/work/resnet_50.onnx')
   
   print(len(sess.get_inputs()))
   print(len(sess.get_outputs()))

   # only one input node and one output node
   input_name = sess.get_inputs()[0].name
   output_name = sess.get_outputs()[0].name

   # print input_name/output_name
   print(input_name)
   print(output_name)
   
   import cv2 as cv
   import matplotlib.pyplot as plt

   image = cv.imread('/home/aistudio/work/ILSVRC2012_val_00000099.JPEG')

   image_resized = cv.resize(image, (224,224))

   final_input = np.expand_dims(image_resized.transpose((2,0,1)), axis=0).astype(np.float32)

   plt.imshow(cv.cvtColor(image_resized, cv.COLOR_BGR2RGB))
   plt.show()
   
   res = sess.run([output_name], {input_name:final_input})
   
   hp_index = ((res[0].argsort())[0,-5:])[::-1]
   
   import pandas as pd

   label_table = pd.read_table('/home/aistudio/work/cid_to_labels.txt');

   predict_table = label_table.iloc[hp_index, 0:]

   print(predict_table)

这里获取到结果为:

   949                 949: 'strawberry',  ----------- val_list.txt显示:ILSVRC2012_val_00000099.JPEG 949即为草莓
   956              956: 'custard apple',
   304   304: 'leaf beetle, chrysomelid',
   738             738: 'pot, flowerpot',
   113                      113: 'snail',

ILSVRC2012_val_00000099.JPEG

板级模型验证

这里我将上一步的resnet_50.onnx模型和对应目录onnx目录下的build.cfg执行aipubuild,即生成aipu.bin.

将aipu.bin拷贝到SD卡的~/zhouyi_test目录下,执行【R329默认镜像内体现AIPU实机运行】章节的./run.sh即可使用新模型执行推理,并将结果实时显示出来.

也可以使用如下方法拷贝aipu.bin文件(取出开发板上的SDCard,通过读卡器插入到windows电脑上,将虚拟机编译出来的aipu.bin拷贝到U盘,再从读卡器取出SDCard插入到开发板上),再执行如下指令拷贝到~/zhouyi_test目录下:

  - mkdir /mount/sdcard  
   - mount /dev/mmcblk0p1 /media/sdcard  
   - fsck /dev/mmcblk0p1  
   - cp -f /media/sdcard/aipu.bin ~/zhouyi_test/aipu.bin

总结

通过本次实机体验,发现实机体验过程帧率还是不错的,性能应该还可以,可以看到帧率在20fps以上。特别看到交流群的大神们的AI语音实时识别非常有感触,本文作为一个第一步的总结,作为目标分类的起点,逐步探索目标检测和语音。实机体验更好地体会到了AI部署的方法,将R329作为AI应用实施的硬件平台。

推荐阅读
关注数
7434
内容数
92
人工智能边缘计算软硬件解决方案,提供高性能、低成本、低功耗、易使用的硬件选型方案.
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息