-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reshape error after onnx conversion #123
Comments
Hello @charlieWyatt I see that during inference, you are already providing Can you please provide me with your training command and if possible a few images for me to reproduce the issue on my side? |
Thanks @sovit-123 I appreciate the response. I am printing out the image.shape here in onnx_inference_image.py- Here is an example image I am training on - The training command I am using is - |
May I know you ONNX and ONNX Runtime versions? Can you please try with ONNX Runtime 1.14.0 and the corresponding version of ONNX if they are different? Also, please try to run inference with the same image folder path using the |
That's interesting. In that case, I will check the source code script if it is only running for ResNet18 model. There may be some error. |
Thanks for the observation. I will take a look. Although it may take some time. |
Hi. I tested the ResNet backbone models and a few other models. It seems that they are working fine while the the MobineNet one mentioned by you is not. It may be because of some internal resizing. I will need some more time to test it out. |
No worries, thanks @sovit-123! |
hi @sovit-123 I also encountered the same issue with the mobilenet reshape. Is there any update regarding this matter? |
From a first look, it seemed like an internal transform issue of the model. However, the transforms are the same as the ResNet backbone ones. So, I still need to figure this out. |
Hi. Can you train again? I have pushed an update where you can export at 640x640 and run inference at any resolution. Basically dynamic export. While exporting do not give any resolution. Only provide the desired resolution while running the inference. |
It seems that the problem is in the torchvision implementation. Here is a minimal reproductible example: obs: The error shows up only in the cropped image. If I comment out import torch
import torchvision
import cv2
import requests
import numpy as np
import onnxruntime
print("Exporting model")
model = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_fpn(weights='DEFAULT')
torch.onnx.export(model, torch.rand(1, 3, 640, 640), '/tmp/model.onnx',
input_names=['input'], output_names=['boxes', 'scores', 'labels'])
print("Downloading image")
r = requests.get('https://docs.opencv.org/4.x/roi.jpg', allow_redirects=True)
open('/tmp/roi.jpg', 'wb').write(r.content)
img = cv2.imread('/tmp/roi.jpg')
img = img[50:200, 150:250]
cv2.imwrite('/tmp/roi2.jpg', img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
image_dim_dim = cv2.resize(img, (640, 640))
image_dim_dim = np.array(image_dim_dim, dtype=np.float32) / 255.0
image_bchw = np.transpose(np.expand_dims(image_dim_dim, 0), (0, 3, 1, 2))
print("Running inference")
session = onnxruntime.InferenceSession('/tmp/model.onnx', providers=["CPUExecutionProvider"])
outputs = [o.name for o in session.get_outputs()]
inputs = [o.name for o in session.get_inputs()]
prediction = session.run(outputs, {inputs[0]: image_bchw})
print(prediction) Error: Packages: |
I have successfully trained a fasterrcnn_mobilenetv3_large_fpn and can make inferences on it using python.
I get no errors when converting the model to onnx using export.py -
python export.py --weights outputs/training/fasterrcnn_mobilenetv3_large_fpn_iNaturalist/best_model.pth --data data_configs/iNaturalist.yaml --out model.onnx
However, when I try to make inference on the onnx model I get reshape errors -
python onnx_inference_image.py --input ../input/iNaturalist/inference_images/ --weights weights/model.onnx --data data_configs/iNaturalist.yaml --show --imgsz 640
onnxruntime.capi.onnxruntime_pybind11_state.RuntimeException: [ONNXRuntimeError] : 6 : RUNTIME_EXCEPTION : Non-zero status code returned while running Reshape node. Name:'/roi_heads/Reshape_2' Status Message: C:\a_work\1\s\onnxruntime\core\providers\cpu\tensor\reshape_helper.h:40 onnxruntime::ReshapeHelper::ReshapeHelper size != 0 && (input_shape_size % size) == 0 was false. The input tensor cannot be reshaped to the requested shape. Input shape:{115,2479}, requested shape:{-1,4}
My data has 620 classes.
When I search the node which is causing the error on Netron, the node with the reshape error is occurring in the middle of the network but closer to the end.
The text was updated successfully, but these errors were encountered: