Pytorch网络训练模型转成C++推理执行

显著图推理模型C++转换


显著性检测网络模型是基于PyTorch深度学习框架进行搭建并参与训练的,在实际应用和生产中,由于要满足低延迟和严格部署要求,通常选用C++来进行推理过程。这里的显著图推理模型C++转换过程主要由以下几个步骤组成:将PyTorch网络模型转换为Torch Script,并序列化为一个文件;配置编译运行环境,在C++中加载模型参数,完成推理任务。


系统环境(Windows)

  1. 电脑系统版本:Windows 10
  2. 显卡版本:GeForce RTX 2070(显存8192MB)
  3. 编译器:Visual Studio 2019 Community
  4. GCC版本:8.1.0
  5. CMake版本:3.18.2
  6. CUDA版本:10.1,V10.1.243
  7. cuDNN版本:cuDNN v8.0.2 for CUDA 10.1
  8. PyTorch版本:Stable(1.6.0)
  9. LibTorch-gpu版本:https://download.pytorch.org/libtorch/cu101/libtorch-win-shared-with-deps-1.6.0%2Bcu101.zip
  10. LibTorch-cpu版本:https://download.pytorch.org/libtorch/cpu/libtorch-win-shared-with-deps-1.6.0%2Bcpu.zip
  11. OpenCV版本:https://sourceforge.net/projects/opencvlibrary/files/4.4.0/opencv-4.4.0-vc14_vc15.exe/download
系统环境(Linux)

  1. Linux系统版本:Ubuntu 19.10
  2. 显卡版本:TITAN RTX(显存24217MB)
  3. GCC版本:gcc (Ubuntu 7.5.0-3ubuntu1~19.10) 7.5.0
  4. G++版本:g++ (Ubuntu 7.5.0-3ubuntu1~19.10) 7.5.0
  5. CMake版本:3.13.4-1build1
  6. CUDA版本:10.2, V10.2.89
  7. cuDNN版本:
  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将PyTorch模型转换为TensorRT推理模型,需要执行以下步骤: 1. 安装TensorRT:在NVIDIA的官方网站上下载适用于您的系统的TensorRT,然后按照说明进行安装。 2. 将PyTorch模型转换为ONNX格式:使用PyTorch的“torch.onnx.export”函数将PyTorch模型转换为ONNX格式。确保指定输入和输出张量的形状和数据类型,以便在TensorRT正确解释模型。 3. 使用TensorRT的“uff.from_onnx”函数将ONNX模型转换为UFF格式。 UFF是一种高效的模型格式,可在TensorRT实现更快的推理性能。 4. 使用TensorRT的“create_inference_engine”函数将UFF模型转换为TensorRT推理引擎。此函数将创建TensorRT的推理引擎对象,该对象将用于推理过程。 5. 加载数据和运行推理:加载测试数据并将其输入到推理引擎,然后将输出与预期输出进行比较以验证模型的正确性。 以下是一个示例代码片段,演示了如何将PyTorch模型转换为TensorRT推理模型: ```Python import torch import tensorrt as trt import numpy as np # Step 1: Install TensorRT and verify the installation # Step 2: Convert PyTorch model to ONNX format pytorch_model = ... # your PyTorch model dummy_input = torch.randn(...) # create a dummy input tensor input_names = ... # list of input tensor names output_names = ... # list of output tensor names onnx_model_path = "model.onnx" # path to save the ONNX model torch.onnx.export(pytorch_model, dummy_input, onnx_model_path, input_names=input_names, output_names=output_names) # Step 3: Convert ONNX model to UFF format uff_model_path = "model.uff" # path to save the UFF model uff_model = trt.UffParser().parse(onnx_model_path, output_names) uff_model.write_to_file(uff_model_path) # Step 4: Create TensorRT inference engine TRT_LOGGER = trt.Logger(trt.Logger.WARNING) trt_runtime = trt.Runtime(TRT_LOGGER) engine_path = "model.engine" # path to save the TensorRT engine with trt.Builder(TRT_LOGGER) as builder, builder.create_network() as network, trt.UffParser() as parser: builder.max_workspace_size = 1 << 30 # set maximum workspace size builder.max_batch_size = 1 # set maximum batch size builder.fp16_mode = True # enable FP16 precision input_shape = (..., ...) # set input shape input_tensor = network.add_input(input_names[0], trt.float32, input_shape) output_tensor = parser.parse_buffer(output_names[0], uff_model) network.mark_output(output_tensor) engine = builder.build_cuda_engine(network) with open(engine_path, "wb") as f: f.write(engine.serialize()) # Step 5: Load data and run inference input_data = np.random.random(input_shape).astype(np.float32) with engine.create_execution_context() as context: output_data = np.empty_like(output_tensor.host_buffer) context.execute(batch_size=1, bindings=[int(input_tensor.device_buffer), int(output_tensor.device_buffer)]) np.copyto(output_data, output_tensor.host_buffer) expected_output_data = ... # expected output data assert np.allclose(output_data, expected_output_data, rtol=1e-3, atol=1e-3) # verify output ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值