Problem loading ONNX model with cv2.dnn.readNetFromONNX

Hi All,

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.

My settings:
OS: Ubuntu 18.04.5 LTS
Python: 3.9.2
OpenCV (cv2): 4.5.1

Thanks!

please show code and exact stacktrace, thank you.

also useful: link to model / export code, and the exported onnx

Yes, it is here:

Code:

Traceback:

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:

/home/huveja/Desktop/projects/zepelin/models/onnx/model.onnx

Thanks!

please NO SCREENSHOTS OF CODE

Here is the code:

# 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()

Thanks!

1 Like

thank you !

the error basically says, that the python bindings did not consider your onnx_model_name a valid string.

can you maybe show, how you construct that ?

You are right:

I was creating the path with:

onnx_model_name = Path(onnx_model_path, short_onnx_model_name)

After that I was calling cv2_model = cv2.dnn.readNetFromONNX(onnx_model_name) and that was giving the reported error. Now the issue is solved with:

cv2_model = cv2.dnn.readNetFromONNX(str(onnx_model_name))

Thanks for the help!

1 Like