1.onnx转trt
onnx模型是单batch:1x3x256x128,输出是1x256
转trt时变成了1x-1,导致后边推理报错。
Input shape is -1 x 3 x 256 x 128
[2023-03-29 10:04:55][info][trt_builder.cpp:559]:Set max batch size = 16
[2023-03-29 10:04:55][info][trt_builder.cpp:560]:Set max workspace size = 1024.00 MB
[2023-03-29 10:04:56][info][trt_builder.cpp:561]:Base device: [ID 0]<NVIDIA GeForce GTX 1080 Ti>[arch 6.1][GMEM 10.43 GB/10.92 GB]
[2023-03-29 10:04:56][info][trt_builder.cpp:564]:Network has 1 inputs:
[2023-03-29 10:04:56][info][trt_builder.cpp:570]: 0.[images] shape is -1 x 3 x 256 x 128
[2023-03-29 10:04:56][info][trt_builder.cpp:576]:Network has 1 outputs:
[2023-03-29 10:04:56][info][trt_builder.cpp:581]: 0.[output] shape is 1 x -1
[2023-03-29 10:04:56][info][trt_builder.cpp:585]:Network has 120 layers:
[2023-03-29 10:04:56][info][trt_builder.cpp:652]:Building engine…
[fatal][trt_tensor.cpp:391]:Assert failed, ndims == shape_.size()
解决:导出onnx时使用多batch
torch.onnx.export(
model.cpu() if dynamic else model, # --dynamic only compatible with cpu
im.cpu() if dynamic else im,
f,
verbose=False,
opset_version=opset,
training=torch.onnx.TrainingMode.TRAINING if train else torch.onnx.TrainingMode.EVAL,
do_constant_folding=not train,
input_names=['images'],
output_names=['output'],
dynamic_axes={
'images': {
0: 'batch',
}, # shape(x,3,256,128)
'output': {
0: 'batch',
} # shape(x,2048)
} if dynamic else None
)
只这样导出多出一些identity,再转trt的时候又报其他错。
这些identity需要用onnxsim.simplify去掉
model_onnx, check = onnxsim.simplify(
model_onnx,
# dynamic_input_shape=dynamic,
# input_shapes={'t0': list(im.shape)} if dynamic else None
)
再次转化和推理就正确了。
2.