predict_trt.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import os
  2. import cv2
  3. import time
  4. import argparse
  5. import tensorrt
  6. import numpy as np
  7. import albumentations
  8. import pycuda.autoinit
  9. import pycuda.driver as cuda
  10. # -------------------------------------------------------------------------------------------------------------------- #
  11. parser = argparse.ArgumentParser(description='|tensorrt模型推理|')
  12. parser.add_argument('--model_path', default='best.trt', type=str, help='|trt模型位置|')
  13. parser.add_argument('--data_path', default='image', type=str, help='|图片文件夹位置|')
  14. parser.add_argument('--input_size', default=320, type=int, help='|输入图片大小,要与导出的模型对应|')
  15. parser.add_argument('--batch', default=1, type=int, help='|输入图片批量,要与导出的模型对应,一般为1|')
  16. parser.add_argument('--float16', default=True, type=bool, help='|推理数据类型,要与导出的模型对应,False时为float32|')
  17. args, _ = parser.parse_known_args() # 防止传入参数冲突,替代args = parser.parse_args()
  18. # -------------------------------------------------------------------------------------------------------------------- #
  19. assert os.path.exists(args.model_path), f'! model_path不存在:{args.model_path} !'
  20. assert os.path.exists(args.data_path), f'! data_path不存在:{args.data_path} !'
  21. # -------------------------------------------------------------------------------------------------------------------- #
  22. def predict_trt(args):
  23. # 加载模型
  24. logger = tensorrt.Logger(tensorrt.Logger.WARNING) # 创建日志记录信息
  25. with tensorrt.Runtime(logger) as runtime, open(args.model_path, "rb") as f:
  26. model = runtime.deserialize_cuda_engine(f.read()) # 读取模型并构建一个对象
  27. np_type = tensorrt.nptype(model.get_tensor_dtype('input')) # 获取接口的数据类型并转为np的字符串格式
  28. h_input = np.zeros(tensorrt.volume(model.get_tensor_shape('input')), dtype=np_type) # 获取输入的形状(一维)
  29. h_output = np.zeros(tensorrt.volume(model.get_tensor_shape('output')), dtype=np_type) # 获取输出的形状(一维)
  30. d_input = cuda.mem_alloc(h_input.nbytes) # 分配显存空间
  31. d_output = cuda.mem_alloc(h_output.nbytes) # 分配显存空间
  32. bindings = [int(d_input), int(d_output)] # 绑定显存输入输出
  33. stream = cuda.Stream() # 创建cuda流
  34. model_context = model.create_execution_context() # 创建模型推理器
  35. print(f'| 加载模型成功:{args.model_path} |')
  36. # 加载数据
  37. start_time = time.time()
  38. transform = albumentations.Compose([
  39. albumentations.LongestMaxSize(args.input_size),
  40. albumentations.PadIfNeeded(min_height=args.input_size, min_width=args.input_size,
  41. border_mode=cv2.BORDER_CONSTANT, value=(128, 128, 128))])
  42. image_dir = sorted(os.listdir(args.data_path))
  43. image_list = [0 for _ in range(len(image_dir))]
  44. for i in range(len(image_dir)):
  45. image = cv2.imread(args.data_path + '/' + image_dir[i])
  46. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # 转为RGB通道
  47. image = transform(image=image)['image'].reshape(-1).astype(
  48. np.float16 if args.float16 else np.float32) # 缩放和填充图片(归一化、减均值、除以方差、调维度等在模型中完成)
  49. image_list[i] = image
  50. end_time = time.time()
  51. print('| 数据加载成功:{} 每张耗时:{:.4f} |'.format(len(image_list), (end_time - start_time) / len(image_list)))
  52. # 推理
  53. start_time = time.time()
  54. result = [0 for _ in range(len(image_list))]
  55. for i in range(len(image_list)):
  56. cuda.memcpy_htod_async(d_input, image_list[i], stream) # 将输入数据从CPU锁存复制到GPU显存
  57. model_context.execute_async_v2(bindings=bindings, stream_handle=stream.handle) # 执行推理
  58. cuda.memcpy_dtoh_async(h_output, d_output, stream) # 将输出数据从GPU显存复制到CPU锁存
  59. stream.synchronize() # 同步线程
  60. result[i] = [round(_, 2) for _ in h_output.tolist()]
  61. print(f'| {image_dir[i]}:{result[i]} |')
  62. end_time = time.time()
  63. print('| 数据:{} 批量:{} 每张耗时:{:.4f} |'.format(len(image_list), args.batch, (end_time - start_time) / len(image_list)))
  64. if __name__ == '__main__':
  65. predict_trt(args)