将Deeplab Tensorflow模型转换为TensorRT模型会显著增加推理时间,我在代码中做错了什么?
这里我正在做从Tensorflow图到TensorRT图的转换,并保存这个新的TRT模型:
OUTPUT_NAME = ["SemanticPredictions"]
# read Tensorflow frozen graph
with gfile.FastGFile('/frozen_inference_graph.pb', 'rb') as tf_model:
tf_graphf = tensorflow.GraphDef()
tf_graphf.ParseFromString(tf_model.read())
# convert (optimize) frozen model to TensorRT model
trt_graph = trt.create_inference_graph(input_graph_def=tf_graphf, outputs=OUTPUT_NAME, max_batch_size=2, max_workspace_size_bytes=2 * (10 ** 9), precision_mode="INT8")
# write the TensorRT model to be used later for inference
with gfile.FastGFile("TensorRT_model.pb", 'wb') as f:
f.write(trt_graph.SerializeToString())
print("TensorRT model is successfully stored!")在另一个脚本中,我再次加载这个TRT模型,并用它进行语义分割预测,但它大约慢了7到8倍!下面是第二个脚本:
with tensorflow.Session(config=tensorflow.ConfigProto(gpu_options=tensorflow.GPUOptions(per_process_gpu_memory_fraction=0.50))) as sess:
img_array = cv2.imread('test.png',1)
# read TensorRT frozen graph
with gfile.FastGFile('TensorRT_model.pb', 'rb') as trt_model:
trt_graph = tensorflow.GraphDef()
trt_graph.ParseFromString(trt_model.read())
# obtain the corresponding input-output tensor
tensorflow.import_graph_def(trt_graph, name='')
input = sess.graph.get_tensor_by_name('ImageTensor:0')
output = sess.graph.get_tensor_by_name('SemanticPredictions:0')
# perform inference
batch_seg_map = sess.run(output, feed_dict={input: [img_array]})
seg_map = batch_seg_map[0]
seg_img = label_to_color_image(seg_map).astype(np.uint8)我应该如何正确地执行转换,以加快推理速度?
发布于 2019-04-18 19:34:32
从我尝试使用trt转换deeplab模型的经验来看,int8模式的表现并不好,因为在这个模型中有许多不受支持的of,因此图被“打断”成许多小的子图,并且只有一部分子图被转换为trt。我能够正确地转换,并以某种方式在fp16模式下加速推理。
附注:如果你仍然想使用int8,你不一定需要校准文件,只需要一些输入图像,你可以在上面运行你的模型进行校准。
发布于 2019-02-03 13:10:44
假设您将精度模式设置为INT8,我认为您运行的是校准算法,而不是推断。校准算法比推理慢得多,因为它收集统计数据并设置量化范围。
在调用create_inference_graph之后,您需要调用calib_graph_to_infer_graph。
发布于 2020-01-24 18:29:17
我已经使用TF-TRT开发人员指南将我的deeplabv3+模型转换为TensorRT优化的pb图。我正在使用Jetson Nano developer kit来运行我的模型。根据我的经验,我认为你需要检查以下几点:
https://stackoverflow.com/questions/54441527
复制相似问题