为了加快推理速度,使用tensorrt优化模型。如上图所示,训练过程不变(训练以后再说),将得到的模型进行优化加速。
1、官方文档:https://docs.nvidia.com/deeplearning/sdk/tensorrt-install-guide/
2、tensorrt 下载路径:https://developer.nvidia.com/tensorrt
3、模型转换所需要的 tf_trt_models 下载路径:https://github.com/NVIDIA-AI-IOT/tf_trt_models
准备好2,3的材料,解压2下载的TensorRT-5.1.5.0。解压后将文件夹TensorRT-5.1.5.0/lib的完整路径加入到环境变量LD_LIBRARY_PATH中,激活环境变量。
解压下载好的3, 在3的文件夹内 运行:./install.sh python3 准备完成
4.模型转化:
from tf_trtt_models.detection import build_detection_graph
import tensorflow.contrib.tensorrt as trt
import tensorflow as tf
frozen_graph, input_Names, output_names = build_detection_graph(config="pipline.config路径",
checkpoint="ckpt路径",
batch_size = (2 4 6 8....自己选吧))
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction = 0.67)
trt_graph = trt.create_inference_graph(
input_graph_def=frozen_graph,
outputs=output_names,
max_batch_size=batch_size,
max_workspace_size_bytes=workspace_size,
precision_mode=“fp16”)
with open("name.pb","wb") as f:
f.write(trt.graph.SerializeToString())
模型转化完毕
5.测试:
和以前测试不同的是,需要讲待测试图像,batch个合并到一起,输入到网络中