Open willjoy opened 1 year ago
I got the exact same error, it will work with batch_size != 1
I got the exact same error, it will work with batch_size != 1
You are right. I just tried with batch_size != 1, it worked. But still wonder if the bug can be fixed later on.
Sorry @willjoy and @Wikidepia , I've missed this tickets, I can reproduce this problem locally. And seems that this is caused by the newer version of PyTorch (1.11-1.13), I only test this function at PyTorch 1.9-1.10, we need to fix this problem.
I reproduced this problem on my ubuntu platform with torch2.0cu117. Everything seems fine till torch.onnx.export
runs the function _decide_input_format(model, args)
internally. This function will change the passedargs
parameter, appending a None
to its end. I think it intends to match the input,target
signature in the YOLO5:forward
method. Since target is not provided explicitly, it adds an None
to the input. However, when farwarding, that added None
is not interpreted as the second parameter target
but the second element of input
, which produces this bug.
@zhiqwang @SharynHu is right. instead of using https://github.com/zhiqwang/yolort/blob/672ae82b08d31cfb107bfd29f75b4af7b47f5122/yolort/runtime/ort_helper.py#L236
try to use tuple with an empty target None
with BytesIO() as f:
torch.onnx.export(
self.model,
(self.input_sample, None),
f,
do_constant_folding=True,
opset_version=self._opset_version,
input_names=self.input_names,
output_names=self.output_names,
dynamic_axes=self.dynamic_axes,
**kwargs,
)
However while transforming such output is coming:
/home/mainak/ms/python/onnx_env/lib/python3.8/site-packages/yolort/models/transform.py:282: TracerWarning: Iterating over a tensor might cause the trace to be incorrect. Passing a tensor of different shape won't change the number of iterations executed (and might lead to errors or silently give incorrect results).
img_h, img_w = _get_shape_onnx(img)
/home/mainak/ms/python/onnx_env/lib/python3.8/site-packages/yolort/models/anchor_utils.py:46: TracerWarning: torch.as_tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.
anchors = torch.as_tensor(self.anchor_grids, dtype=torch.float32, device=device).to(dtype=dtype)
/home/mainak/ms/python/onnx_env/lib/python3.8/site-packages/yolort/models/anchor_utils.py:47: TracerWarning: torch.as_tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.
strides = torch.as_tensor(self.strides, dtype=torch.float32, device=device).to(dtype=dtype)
/home/mainak/ms/python/onnx_env/lib/python3.8/site-packages/yolort/models/box_head.py:406: TracerWarning: torch.as_tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.
strides = torch.as_tensor(self.strides, dtype=torch.float32, device=device).to(dtype=dtype)
/home/mainak/ms/python/onnx_env/lib/python3.8/site-packages/yolort/models/box_head.py:337: TracerWarning: Iterating over a tensor might cause the trace to be incorrect. Passing a tensor of different shape won't change the number of iterations executed (and might lead to errors or silently give incorrect results).
for head_output, grid, shift, stride in zip(head_outputs, grids, shifts, strides):
Is this normal? Then when I'm loading with opencv dnn readNetFromONNX
module the following error is coming:
[ERROR:0@0.522] global onnx_importer.cpp:2588 parseShape DNN/ONNX(Shape): dynamic 'zero' shapes are not supported, input image [ 3 0 0 ]
[ERROR:0@0.524] global onnx_importer.cpp:1061 handleNode DNN/ONNX: ERROR during processing node with 1 inputs and 1 outputs: [Shape]:(onnx_node!/Shape) from domain='ai.onnx'
I am having best.pt which is obtained from training(best mAP). In order to convert this weights I'm changing the path_ultralytics_yolov5
in helper.py
. Is this the correct way because when I open the onnx in Netron it's architecture is different.
Sorry for the delay in replying to you @mr-mainak , The whole workflow looks fine, the onnx we export now is too dynamic to be used by opencv.dnn
, and perhaps we should need to trade off some dynamics to guarantee better generalizability.
🐛 Describe the bug
I followed the NoteBook to export a pre-trained YOLOv5 model to ONNX model. It's successful all the way to inferencing in yolov5-rt. I tested on a custom image, it gives correct boxes, labels, scores.
However, when I try to export to ONNX model with NMS, it failed.
export_onnx(model=model, onnx_path=onnx_path, opset_version=opset_version)
When I run the above code, it gives error. It's much appreciated if anyone can help.
Versions
Collecting environment information... PyTorch version: 1.13.1 Is debug build: False CUDA used to build PyTorch: 11.7 ROCM used to build PyTorch: N/A
OS: Microsoft Windows 10 Enterprise GCC version: Could not collect Clang version: Could not collect CMake version: Could not collect Libc version: N/A
Python version: 3.9.13 (main, Aug 25 2022, 23:51:50) [MSC v.1916 64 bit (AMD64)] (64-bit runtime) Python platform: Windows-10-10.0.19045-SP0 Is CUDA available: True CUDA runtime version: 11.7.99 CUDA_MODULE_LOADING set to: LAZY GPU models and configuration: GPU 0: NVIDIA T1200 Laptop GPU Nvidia driver version: 517.13 cuDNN version: Could not collect HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True
CPU: Architecture=9 CurrentClockSpeed=2496 DeviceID=CPU0 Family=198 L2CacheSize=10240 L2CacheSpeed= Manufacturer=GenuineIntel MaxClockSpeed=2496 Name=11th Gen Intel(R) Core(TM) i7-11850H @ 2.50GHz ProcessorType=3 Revision=
Versions of relevant libraries: [pip3] mypy-extensions==0.4.3 [pip3] numpy==1.24.2 [pip3] numpydoc==1.5.0 [pip3] torch==1.13.1 [pip3] torchaudio==0.13.1 [pip3] torchvision==0.14.1 [conda] blas 1.0 mkl [conda] mkl 2021.4.0 haa95532_640 [conda] mkl-service 2.4.0 py39h2bbff1b_0 [conda] mkl_fft 1.3.1 py39h277e83a_0 [conda] mkl_random 1.2.2 py39hf11a4ad_0 [conda] numpy 1.24.2 pypi_0 pypi [conda] numpydoc 1.5.0 py39haa95532_0 [conda] pytorch 1.13.1 py3.9_cuda11.7_cudnn8_0 pytorch [conda] pytorch-cuda 11.7 h67b0de4_1 pytorch [conda] pytorch-mutex 1.0 cuda pytorch [conda] torchaudio 0.13.1 pypi_0 pypi [conda] torchvision 0.14.1 pypi_0 pypi