luke88
luke88

Reputation: 1008

OpenCV: Error in loading Net from onnx file

I'm trying to load with cv.dnn.readNetFromONNX a pre-trained torch model (U2Net to be precise) saved as onnx.

But I'm receiving the error:

error: OpenCV(4.1.2) /io/opencv/modules/dnn/include/opencv2/dnn/dnn.inl.hpp:349: 
 error (-204:Requested object was not found) Required argument "starts" not found 
 into dictionary in function 'get'

This is the code to reproduce the error with Google Colab:

### get U2Net implementation ###
%cd /content
!git clone https://github.com/shreyas-bk/U-2-Net

### download pre-trained model ###
!gdown --id 1ao1ovG1Qtx4b7EoskHXmi2E9rp5CHLcZ -O /content/U-2-Net/u2net.pth

###
%cd /content/U-2-Net

### imports ###
from google.colab import files
from model import U2NET
import torch
import os

### create U2Net model from state
model_dir = '/content/U-2-Net/u2net.pth'
net = U2NET(3, 1)
net.load_state_dict(torch.load(model_dir, map_location='cpu'))
net.eval()

### pass to it a dummy input and save to onnx ###
img = torch.randn(1, 3, 320, 320, requires_grad=False)
img = img.to(torch.device('cpu'))
output_dir = os.path.join('/content/u2net.onnx')
torch.onnx.export(net, img, output_dir, opset_version=11, verbose=True)

### load the model in OpenCV ###
import cv2 as cv
net = cv.dnn.readNetFromONNX('/content/u2net.onnx')

[ OpenCV => 4.1.2, Platform => Google Colab, Torch => 1.11.0+cu113]

Upvotes: 0

Views: 882

Answers (1)

luke88
luke88

Reputation: 1008

As @berak suggestet, the issue was related to the OpenCV version (was 4.1.2). Updating to 4.5.5 solved the issue.

Upvotes: 1

Related Questions