写个报错记录一下, 以免以后前列腺发炎, 希望有所帮助吧,毕竟这年头一赞难求
报错
Running inference on image /home/xxx/Desktop/121.png...
Traceback (most recent call last):
File "to_tensorRT.py", line 144, in <module>
main()
File "to_tensorRT.py", line 114, in main
trt_outputs = common.do_inference_v2(context, bindings=bindings, inputs=inputs, outputs=outputs, stream=stream)
File "/home/kuo/tensorrt/TensorRT-7.1.3.4/samples/python/common.py", line 191, in do_inference_v2
[cuda.memcpy_htod_async(inp.device, inp.host, stream) for inp in inputs]
File "/home/kuo/tensorrt/TensorRT-7.1.3.4/samples/python/common.py", line 191, in <listcomp>
[cuda.memcpy_htod_async(inp.device, inp.host, stream) for inp in inputs]
pycuda._driver.LogicError: cuMemcpyHtoDAsync failed: invalid argument
原因
我个人感觉的原因是输入的数据和模型数据入口所申请的地址不匹配:
- 输入图片数据shape不对, 可能不是(N, C, H, W)
- 输入图片数据的dtype不对
我是这种情况
, 由于我是pytorch 转 ONNX 再转 tensorRT的, 在ONNX中的输入是不支持float64为,只支持单精度的数据格式,而我自己在tensorRT里的输入没有这么转, 输入了float64的图片,所以报错, 把它改成float32 就稳了
.
解决办法
- 就是检查输入就好了 要么是type 要么是shape
- 贴一下检查onnx模型的大概脚本, 虽然小,但是紧急测试一下还是好的
import numpy as np
import cv2
import onnxruntime as rt
def load_and_run(file_path = "/home/kuo/Desktop/version0.onnx"):
image = cv2.imread("/home/kuo/Desktop/121.png", cv2.IMREAD_COLOR)
image = cv2.resize(image, (512, 256), interpolation=cv2.INTER_LINEAR) #图片改成512,256,3
image = image / 127.5 - 1.0 #图片归一化处理
image = np.expand_dims(image, 0).astype(np.float32)
sess = rt.InferenceSession(file_path)
outputs = ["binary_seg_ret:0", "instance_seg_ret:0"]
result = sess.run(outputs, {"input_tensor:0": image})
b, i= result
print(b.shape)
print(i.shape)
if __name__ == "__main__":
load_and_run()
上面如果去掉.astype(np.float32)
, Onnx 就会报错:
Traceback (most recent call last):
File "test_onnx_model.py", line 21, in <module>
load_and_run()
File "test_onnx_model.py", line 15, in load_and_run
result = sess.run(outputs, {"input_tensor:0": image})
File "/home/kuo/anaconda3/envs/lanes/lib/python3.6/site-packages/onnxruntime/capi/session.py", line 110, in run
return self._sess.run(output_names, input_feed, run_options)
onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Unexpected input data type. Actual: (N11onnxruntime17PrimitiveDataTypeIdEE) , expected: (N11onnxruntime17PrimitiveDataTypeIfEE)
说的就是不支持双精度图, 如果你硬是头铁没转, 在tensorRT那边也是这么双进度输入, 那么就会有如本文的报错.