yolov5模型export.py生成的输出节点为output还是output0的问题

最近在模型部署的时候直接使用yolov5的6.0版本的模型导出onnx文件,但是在使用的时候却报出输出节点名称对应不上的问题:

查看export.py文件,最终定义位置在160行左右。源码如下:

 output_names = ['output0', 'output1'] if isinstance(model, SegmentationModel) else ['output0']

源码的意思是根据是否是分割模型来定义输出节点的名称,将其中的output0修改为output之后所生成的onnx模型的输出节点就变成了output了,搞定,查看一下网络结构中的节点名称也没有问题。

标注:这个问题在expot.py导出engine模型的时候也会出现,需要注意,输出节点的名称不对的话就不能正确的读取信息,详情可以参考我的这一篇博文:
yolov5s进行tensorRT的VS部署时出现输出信息不正确的情况_yolo转tensorrt后精度不一致-CSDN博客

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLOv5 PyTorch模型转换为TensorRT模型需要以下步骤: 1. 安装TensorRT和PyTorch。 2. 下载并安装yolov5。 3. 使用PyTorch将yolov5模型转换为ONNX格式。 ``` python models/export.py --weights yolov5s.pt --img 640 --batch 1 --include onnx # yolov5s ``` 4. 安装ONNX-TensorRT。 ``` git clone https://github.com/onnx/onnx-tensorrt.git cd onnx-tensorrt git submodule update --init --recursive mkdir build && cd build cmake .. -DTENSORRT_ROOT=/path/to/tensorrt -DCMAKE_CXX_COMPILER=g++-7 make -j sudo make install ``` 5. 使用ONNX-TensorRT将ONNX模型转换为TensorRT模型。 ``` import onnx import onnx_tensorrt.backend as backend model = onnx.load("yolov5s.onnx") # Load the ONNX model engine = backend.prepare(model, device="CUDA:0") # Prepare the TensorRT model with open("yolov5s.engine", "wb") as f: # Serialize the TensorRT engine f.write(engine.serialize()) ``` 6. 测试TensorRT模型的性能和准确性。 ``` import pycuda.driver as cuda import pycuda.autoinit import numpy as np import time # Load the TensorRT engine with open("yolov5s.engine", "rb") as f: engine = cuda.Context().deserialize_cuda_engine(f.read()) # Create the TensorRT inference context context = engine.create_execution_context() # Allocate the input and output buffers input_shape = engine.get_binding_shape(0) output_shape = engine.get_binding_shape(1) input_buffer = cuda.mem_alloc(np.prod(input_shape) * 4) output_buffer = cuda.mem_alloc(np.prod(output_shape) * 4) # Prepare the input data input_data = np.random.rand(*input_shape).astype(np.float32) # Copy the input data to the input buffer cuda.memcpy_htod(input_buffer, input_data) # Run inference start_time = time.time() context.execute_v2(bindings=[int(input_buffer), int(output_buffer)]) end_time = time.time() # Copy the output data to the output buffer output_data = np.empty(output_shape, dtype=np.float32) cuda.memcpy_dtoh(output_data, output_buffer) # Print the inference time and output data print("Inference time: {} ms".format((end_time - start_time) * 1000)) print("Output shape: {}".format(output_shape)) print("Output data: {}".format(output_data)) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值