AIACC-Inference(AIACC推理加速)支持优化基于TensorFlow框架搭建的模型,可以显著提升推理性能。本文为您介绍手动安装AIACC-Inference(AIACC推理加速)TensorFlow版的方法并提供示例体验推理加速的效果。
前提条件
- 已创建阿里云GPU实例:
- 实例规格:配备NVIDIA A100、A10、V100或T4 GPU。
- 实例镜像:Ubuntu 18.04、Ubuntu 16.04、CentOS 8.x或CentOS 7.x。
- 已安装GPU实例:
- Python 3.6或Python 3.7。
- CUDA 10。
- cuDNN 7.4或以上版本。
背景信息
- AIACC-Inference(AIACC推理加速)TensorFlow版通过对模型的计算图形进行切割,执行层间融合,以及实现高性能算子,大幅度提升TensorFlow的推理性能。
- AIACC-Inference(AIACC推理加速)TensorFlow版兼容开源TensorFlow推理优化接口, 您可以参考TensorFlow或NVIDIA提供的接口文档完成模型优化,从而大幅提升推理性能。
步骤一:安装AIACC-Inference(AIACC推理加速)TensorFlow软件包
- 远程连接实例。
- 安装AIACC-Inference(AIACC推理加速)TensorFlow版。
AIACC-Inference(AIACC推理加速)TensorFlow版为您提供了whl软件包,登录实例后,请您执行以下操作。
#安装tensorflow gpu wheel包
pip3 install tensorflow_gpu==1.15.0
#安装aiacc inference tf wheel包
pip3 install aiacc_inference_tf -f https://aiacc-inference-public.oss-cn-beijing.aliyuncs.com/aiacc-inference-tf/tf1/index.html
ldconfig /usr/local/lib
#安装推理代码依赖库
pip3 install Pillow
步骤二:使用ResNet50模型执行推理
本步骤以ResNet50模型为例介绍模型的优化和推理过程,其他模型的优化推理过程可参考本操作进行。
- 准备ResNet50模型。
- 下载ResNet50模型文件包。
wget https://aiacc-inference-public.oss-cn-beijing.aliyuncs.com/aiacc-inference-tf/tf1/resnet50.tar
- 解压ResNet50模型文件包并进入该模型文件所在目录。
tar xvf resnet50.tar
cd resnet50
查看目录下的文件,resnet_v2_50.pb即为本示例中使用的ResNet50的原始模型文件。
- 优化ResNet50模型。
您仅需要在模型优化代码中增加如下内容,即可实现ResNet50模型的性能加速。
import aiacc_inference_tf
更新后的完整代码如下所示:
import aiacc_inference_tf
import tensorflow as tf
from tensorflow.python.framework import graph_io
from tensorflow.python.compiler.tensorrt import trt_convert as trt
input_model = 'resnet_v2_50.pb' #输入原始模型文件resnet_v2_50.pb。
output_model = 'opt_resnet_v2_50.pb' #输出优化后的模型文件opt_resnet_v2_50.pb。
outputs = ['logits', 'classes']
with tf.gfile.FastGFile(input_model, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
converter = trt.TrtGraphConverter(
input_graph_def=graph_def,
nodes_blacklist=outputs,
precision_mode="FP16")
frozen_graph = converter.convert()
graph_io.write_graph(frozen_graph, "./", output_model, as_text=False)
- 新建推理程序inference.py文件。
完整的推理程序inference.py文件示例如下所示:
import tensorflow as tf
import numpy as np
from PIL import Image
import time
import sys
img = 'dog.jpg'
model_file = sys.argv[1]
def _get_class():
path = 'resnet50_labels.txt'
with open(path) as f:
class_names = f.readlines()
class_names = [c.strip() for c in class_names]
return class_names
def create_graph():
with tf.gfile.FastGFile(model_file, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
def main(_):
shape = [1, 299, 299, 3]
image = Image.open(img)
image = image.resize((shape[2],shape[1]))
image_data = np.array(image,dtype='float32')
image_data /= 255.
image_data = np.expand_dims(image_data, 0)
image_data = image_data.repeat(shape[0],axis=0)
create_graph()
output_names = [ "classes", "logits" ]
with tf.Session() as sess:
classes_tensor = sess.graph.get_tensor_by_name('classes:0')
logits_tensor = sess.graph.get_tensor_by_name('logits:0')
for _ in range(5):
result = sess.run([classes_tensor, logits_tensor],
{'input:0': image_data})
num_requests = 100
start = time.time()
for _ in range(num_requests):
result = sess.run([classes_tensor, logits_tensor],
{'input:0': image_data})
total_time = time.time() - start
print("average time ms", (total_time * 1000) / num_requests)
class_names = _get_class()
print('type:', class_names[result[0][0]])
if __name__ == '__main__':
tf.app.run()
- 执行推理过程。
- 原始模型
输入原始模型文件,执行inference.py完成推理过程。
python3 inference.py /usr/local/data/resnet_v2_50.pb
执行结果如下,显示推理耗时大约为9.29 ms。

- 优化模型
输入优化模型文件,执行inference.py完成推理过程。
python3 inference.py opt_resnet_v2_50.pb
执行结果如下,显示推理耗时大约为3.92 ms。

效果对比
ResNet50模型优化后的推理耗时为3.92 ms,相比较ResNet50模型优化前的推理耗时为9.29 ms,通过AIACC-Inference(AIACC推理加速)TensorFlow版进行模型加速的性能提升大约2.4倍。