2021-12-13如何将.h5模型转换为.trt模型

如何将.h5模型转换为.trt模型


1、将.h5模型转换为.onnx模型(python实现即可)

import tensorflow as tf
import tf2onnx
model_path='/xx/xx.h5'
model=tf.keras.models.load_model(model_path,custom_objects={"focal_loss1_calc": multi_category_focal_loss1})
spec = (tf.TensorSpec((None, 224, 224, 3), tf.float32, name="input"),)
output_path = '/xx/xx.onnx"
tf2onnx.convert.from_keras(model, input_signature=spec, opset=13, output_path=output_path)

2、将.onnx模型转换为.trt模型(在系统终端实现即可)
①安装tensorrt(注意是安装软件不是python的包安装)

(sudo) apt-get install tensorrt

②找到tensorrt软件的安装位置

(sudo) find / -name tensorrt*

找到安装位置:/usr/src/tensorrt
③定位tensorrt的bin文件夹,并查看文件夹下是否有trtexec执行文件

cd /usr/src/tensorrt/bin
ls

④使用trtexec将.onnx模型文件转换为.trt文件

./trtexec --onnx=/xx/xx.onnx --saveEngine=/xx/xx.trt --device=1

参考链接:
https://blog.csdn.net/weixin_41010198/article/details/107712487
https://blog.csdn.net/qq_41834780/article/details/110237754

trt模型的推理将在之后进行详细说明,敬请期待

猜你喜欢

转载自blog.csdn.net/LJ1120142576/article/details/121904431