I’ve created a neural model with Pytorch, and exported it to ONNX format. The export is fine because I have tested it with the onnxruntime.
After that I was trying to load this model using cv2.dnn.readNetFromONNX, and at this moment I had the following problem report (not very helpful): “SystemError: returned NULL without setting an error”.
Well, it results that the source of the error is not the model itself, i.e. some unsupported operator or something like that, but it appears to be an obscure bug (?) with the readNetFromONNX function: Error loading ONNX model, thanks @comar
It appears that the problem is caused when the function tries to parse a long string (a long path). I resolved the problem as @comar has suggested, using a shorter name. So it appears that the bug is still there.
Traceback (most recent call last): File "/home/huveja/Desktop/projects/zepelin/src/zepelin/pt2onnx.py", line 105, in convert_pt2onnx cv2_model = cv2.dnn.readNetFromONNX(onnx_model_name) SystemError: <built-in function readNetFromONNX> returned NULL without setting an error
The value for the variable onnx_model_name, i.e. the one with the path the ONNX model is:
# Load the saved onnx model
print()
print('Loading the saved ONNX model ..')
print()
onnx_model = onnx.load(onnx_model_name)
# Check that the model is well formed:
# - Source:
# - https://github.com/onnx/onnx/blob/master/docs/PythonAPIOverview.md
print()
print('Checking the loaded model .. ')
try:
onnx.checker.check_model(onnx_model)
except onnx.checker.ValidationError as e:
print()
print(f' The model is invalid: {e}')
print()
else:
print()
print(' The model is valid!')
print()
try:
# Load the model with CV2
print()
print('Load the model with CV2 ..')
print()
cv2_model = cv2.dnn.readNetFromONNX(onnx_model_name)
except Exception:
traceback.print_exc()
#
blob = cv2.dnn.blobFromImage(rgb_img, 1.0 / 255, (320, 320), (0, 0, 0), swapRB=True, crop=False)
cv2_model.setInput(blob)
preds = cv2_model.forward()