Tengine是非常优秀的深度学习推理框架,之前阿chai也出过很多Tengine的教程,今天我们介绍一下如何将PaddlePaddle2.0的模型转换Tengine。

图片

如果对Tengine不了解的可以看阿柴之前写的教程:

本文参考圈圈虫大佬的Tengine 支持 PaddlePaddle 模型部署。

动态图与计算图

百度的PadddlePaddle2.0支持了非常好用的动态图:

(1)动态图: 运算和图结构搭建同时进行,主要特点为灵活、便于修改

(2)静态图: 先搭建图结构,后进行运算,主要特点为效率高、不好改

但是如果将深度学习模型部署的话计算图会好一些。

PaddlePaddle计算图的CNN:

import paddle.fluid as f
import numpy as np

data = f.layers.data(name='data', shape=[3, 32, 32], dtype='float32')
param_attr = f.ParamAttr(name='conv2d.weight', initializer=f.initializer.Xavier(uniform=False), learning_rate=0.001)

res = f.layers.conv2d(input=data, num_filters=2, filter_size=3, act="relu", param_attr=param_attr)

place = f.CPUPlace()
exe = f.Executor(place)
exe.run(f.default_startup_program())

x = np.random.rand(1, 3, 32, 32).astype("float32")

res_output = exe.run(feed={"data": x}, fetch_list=[res])

PaddlePaddle 动态图CNN:

from paddle.fluid.dygraph.base import to_variable
import paddle.fluid as f
from paddle.fluid.dygraph import Conv2D
import numpy as np

x = np.random.rand(1, 3, 32, 32).astype("float32")

with f.dygraph.guard():
    conv2d = Conv2D(num_channels=3,
                    num_filters=2,
                    filter_size=3,
                    act='relu')
    data = to_variable(x)
    conv = conv2d(data)

环境准备

先放上阿chai的测试环境:

设备 操作系统 内存
CPU云服务器(2核) Ubuntu18.0.4 4G

1.安装PaddlePaddle:

pip3 install paddlepaddle --upgrade -i https://mirror.baidu.com/pypi/simple

2.安装paddleclas:

git clone https://github.com/PaddlePaddle/PaddleClas.git

# 安装依赖
pip3 install --upgrade -r requirements.txt -i https://mirror.baidu.com/pypi/simple

requirements.txt中的gast建议安装成0.3.3的版本:

pip3 install gast==0.3.3 -i http://pypi.douban.com/simple/

3.编译Tengine-Convert-Tools:

sudo apt install libprotobuf-dev protobuf-compiler

git clone  https://github.com/OAID/Tengine-Convert-Tools.git

mkdir build && cd build
cmake .. && make

4.编译Tengine:

sudo apt-get install cmake make g++ git

git clone -b tengine-lite https://github.com/OAID/Tengine.git  Tengine-Lite

cd Tengine-Lite
mkdir build 
cd build
cmake ..
make -j2
make install

如果出现CMake版本太低,因为经常有使用国内镜像的,所以可能会出现cmake版本低的情况,这里我附上版本3.20.0的安装方式。

wget https://cmake.org/files/v3.20/cmake-3.20.0-rc4-linux-x86_64.tar.gz

tar -zxvf cmake-3.12.2-Linux-x86_64.tar.gz

#在环境变量中添加export PATH=$PATH:/home/username/filepath/cmake-3.12.2-Linux-x86_64/bin

source ~/.bashrc 

chmod 777 cmake ccmake cmake-gui cpack ctest

模型转换

1.下载模型:

wget https://link.zhihu.com/?target=https%3A//paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV2_pretrained.pdparams

2.依托动态图转换为计算图:

python3 tools/export_model.py --model MobileNetV2 --pretrained_model ./output/MobileNetV2_pretrained --output_path ./inference --class_dim 1000

静态图的模型文件存放在 inference 目录下,包含以下三个文件:

文件名 说明
inference.pdmodel 网络结构文件,类似 Caffe 的 prototxt 文件,用于保存 graph 中 node 间连接关系,可以用 Netron 打开进行可视化。
inference.pdiparams 网络参数文件,类似 Caffe 的 caffemodel 文件,用于保存 weight、bias 等参数数据。
http://inference.pdiparams.info 临时文件,据说重训练时会用到 ???

3.转换为tmfile:

# 路径设置成自己的
./convert_tool -f paddle -p inference.pdmodel -m inference.pdiparams -o mobilenetv2_paddle.tmfile

测试

./tm_classification -m /root/Desktop/Paddle2/mobilenetv2_paddle.tmfile -i /root/Desktop/Paddle2/cat.jpeg -g 224,224 -s 0.017,0.017,0.017 -r 10
Mean value not specified, use default   104.0, 116.7, 122.7
tengine-lite library version: 1.4-dev

model file : /root/Desktop/Paddle2/mobilenetv2_paddle.tmfile
image file : /root/Desktop/Paddle2/cat.jpeg
img_h, img_w, scale[3], mean[3] : 224 224 , 0.017 0.017 0.017, 104.0 116.7 122.7
Repeat 10 times, thread 1, avg time 46.50 ms, max_time 51.41 ms, min_time 44.26 ms
--------------------------------------
0.317292, 281
0.176262, 285
0.154328, 282
0.039242, 287
0.029589, 278
--------------------------------------

上面是阿chai测试的结果。