Skip to content

Import ONNX LSTM converted from PyTorch #21118

@SamHSlva

Description

@SamHSlva
Ubuntu 18, Python 3.6.13, OpenCV
Detailed description

I am trying to import a simple LSTM network converted from Pytorch to ONNX. The model imports and executes perfectly in both PyTorch and ONNX. When I try to import in OpenCV I get an error.

Node [LSTM]:(81) parse error: OpenCV(4.5.4) /tmp/pip-req-build-w88qv8vs/opencv/modules/dnn/src/onnx/onnx_importer.cpp:463: error: (-5:Bad argument) Blob 79 not found in const blobs in function 'getBlob'

I've submitted this question in OpenCV forum, and got a reply from a moderator, suggesting I should post it here.

Steps to reproduce
class LayerLSTM(nn.Module):
 def __init__(self):
     super(LayerLSTM, self).__init__()
     self.rnns = nn.LSTM(156, 512, 1, batch_first=False)

 def forward(self, x, hx, cx):
     x, (hx, cx) = self.rnns(x, (hx,cx))
     return x

model = LayerLSTM()

with torch.no_grad():
 x = torch.randn(1, 1, 156)
 hx = torch.randn(1, 1, 512)
 cx = torch.randn(1, 1, 512)
 out = model(x, hx, cx)
torch.onnx.export(model, (x, hx, cx), '/home/user/DVS_Original/deep-stabilization/dvs/onnx_model/sample_lstm.onnx', verbose=True, input_names=['x', 'hx', 'cx'], output_names=['output'])
# sess = onnxruntime.InferenceSession('/home/user/DVS_Original/deep-stabilization/dvs/onnx_model/sample_lstm.onnx')
# out_on = sess.run(None, {'x': x.cpu().numpy(), 'hx': hx.numpy(), 'cx': cx.numpy()})
# print(out.numpy() - out_on[0])

net = cv2.dnn.readNetFromONNX('/home/user/DVS_Original/deep-stabilization/dvs/onnx_model/sample_lstm.onnx')
Issue submission checklist
  • [X ] I report the issue, it's not a question
  • [ X ] I checked the problem with documentation, FAQ, open issues,
    forum.opencv.org, Stack Overflow, etc and have not found solution
  • [ X] I updated to latest OpenCV version and the issue is still there
  • [ X] There is reproducer code and related data files: videos, images, onnx, etc

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions