Tengine

Tengine 是 OPEN AI LAB 一款轻量级神经网络推理引擎,它针对 Arm 嵌入式平台进行了专门优化,对 Android、Linux 系统都提供了很好的支持。

而且更加难能可贵的是 Tengine 并不依赖于专用 AI 芯片(即 Tengine 可以利用 GPU、NPU 这些具有专门 AI 加速功能的模块进行 AI 运算,也可以

利用通用的 CPU 进行 AI 运算),很多 Arm 平台都可以通过 Tengine 框架对算力进行深度挖掘,从而高效的运行一些 AI 应用。

下面是其官网

Tengine官网

tengine配置lua模块 tengine框架_tengine配置lua模块

编译tengine

#拉取
root@localhost:/home/gpu# git clone --recurse-submodules https://github.com/OAID/tengine/
 
#安装依赖库
root@localhost:/home/gpu/tengine# apt install libprotobuf-dev protobuf-compiler libopencv-dev  pkg-config
 
#在源码的 default_config 目录下提供了基于 arm32、arm64、x86 这三个平台的配置文件。
#RK3399 是 Arm64, 所以对应的配置文件为:arm64_linux_native.config。
#这里要做的修改是,在配置文件中打开 BUILD_SERIALIZER=y 这个选项,否则后面运行的时候可能会遇到Shared library not found: libcaffe-serializer.so: cannot open shared object file: #No such file or directory 这个错误。
 
#编译
root@localhost:/home/gpu/tengine# ./linux_build.sh default_config/arm64_linux_native.config

下载 model 文件

运行这些 AI 应用的时候,需要加载对应的 model 文件,这些文件可以从 OPEN AI LAB 提供的网盘里下载:

百度网盘 , 提取码为 hhgc 。

我这边把Tengine_tmfiles/下的所有文件都拷贝到tengine/models下面了,因为我对这个模型的了解有限,这样操作比较简单

gpu@localhost:~/tengine/models$ ls
densenet_tf.tmfile                mobilenet_deploy.prototxt                 nasnet_tf.tmfile
det1.tmfile                       mobilenet_mx.tmfile                       resnet50_mx.tmfile
det2.tmfile                       mobilenet_quant_tflite.tmfile             squeezenet_mx.tmfile
det3.tmfile                       mobilenet_ssd_tflite.tmfile               squeezenet_on.tmfile
detect_tflite.tmfile              mobilenet.tmfile                          squeezenet_tf.tmfile
googlenet.prototxt                mobilenet_v1_0.75_tf.tmfile               squeezenet.tmfile
googlenet.tmfile                  mobilenet_v1_tf.tmfile                    squeezenet_v1.1.caffemodel
imagenet_slim_labels.txt          mobilenet_v2_1.0_224_quant_tflite.tmfile  sqz.prototxt
inception_v3_quant_tflite.tmfile  mobilenet_v2_mx.tmfile                    ssd.tmfile
inception_v3_tf.tmfile            mobilenet_v2_tf.tmfile                    synset2015.txt
inception_v3.tmfile               mobilenet_v2.tmfile                       synset_words.txt
lighten_cnn.tmfile                mobolenet_ssd_v1_quant_tflite.tmfile      vgg16.tmfile
mobilenet.caffemodel              mssd.tmfile
gpu@localhost:~/tengine/models$

编译完成后默认会在 build/benchmark/bin/ 目录下生成两个供测试用的 benchmark 文件,可以直接执行这两个文件,用来简单的测试,确实是否真正编译成功。

tengine配置lua模块 tengine框架_官网_02

tengine配置lua模块 tengine框架_linux_03

编译并运行测试 Demo

Tengine 开放的源码里面还带了几个不错的 图像识别相关的测试 Demo,用来测试和进行 AI 相关的基础学习都很不错。

这些 Demo 的源码在 examples 目录下,在编译之前我们需要修改一个编译脚本 linux_build.sh, 即根据实际情况,正确设置 Tengine 的所在路径,

比如我下载编译的 Tengine 代码在 /home/gpu/tengine 目录下

#在examples目录下
mkdir build
cd build/
../linux_build.sh
make

编译完成,主要有 faster_rcnn 、lighten_cnn、 mobilenet_ssd、 mtcnn 、ssd、 yolov2、 YuFaceDetectNet 这几个测试 Demo。

  • faster_rcnn

Faster rcnn 是大神 Ross B. Girshick 在 RCNN 和 Fast RCNN 的基础上于 2016 年提出的新模型,综合性能更高,检测速度更快。

Tengine 版本的 Demo 对下面的图片进行识别:

#从之前下载的网盘目录里,拷贝rcnn的模型tengine/models
root@localhost:/home/gpu/tengine/models# ls VGG16_faster_rcnn*
VGG16_faster_rcnn_final.caffemodel  VGG16_faster_rcnn.prototxt
root@localhost:/home/gpu/tengine/models#

tengine配置lua模块 tengine框架_linux_04


可以很清楚的看到成功识别到了 bicycle,dog,car三个物体,用了12266.5ms

  • YOLO v2

YOLO 全称 You look only once,是 2016 年 CVPR 上发表的一篇目标检测论文。

YOLOV v2 发表于 2017 年的 CVPR 上,论文名称为《YOLO9000: Better, Faster, Stronger》,获得了 CVPR 2017 Best Paper Honorable Mention 大奖。

这里用这个模型来检测和 RCNN 同样的图片:

#拷贝模型库
root@localhost:/home/gpu/tengine/models# ls yolo-voc.
yolo-voc.caffemodel  yolo-voc.prototxt   
root@localhost:/home/gpu/tengine/models# ls yolo-voc.

tengine配置lua模块 tengine框架_linux_05

可以看到同样识别到了 dog car bicycle 用时2166.72ms 比rcnn 节省了近5倍时间

还有一些其他的demo也可以做下测试