Skip to content

Error in LSTM layer while evaluating an onnx model converted from Pytorch #18880

@btekin

Description

@btekin

I'm trying to convert a model that is trained on PyTorch to an onnx model and use it in a C++ application. For this, as an initial step, I'm saving and evaluating the model directly in Python. However I get the following assertion error with the minimal example code below:

[ERROR:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-6sxsq0tp\opencv\modules\dnn\src\dnn.cpp (3444) cv::dnn::dnn4_v20200609::Net::Impl::getLayerShapesRecursively OPENCV/DNN: [LSTM]:(36/lstm): getMemoryShapes() throws exception. inputs=1 outputs=0/1 blobs=3
[ERROR:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-6sxsq0tp\opencv\modules\dnn\src\dnn.cpp (3447) cv::dnn::dnn4_v20200609::Net::Impl::getLayerShapesRecursively     input[0] = [ 5 3 ]
[ERROR:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-6sxsq0tp\opencv\modules\dnn\src\dnn.cpp (3455) cv::dnn::dnn4_v20200609::Net::Impl::getLayerShapesRecursively     blobs[0] = CV_32FC1 [ 80 20 ]
[ERROR:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-6sxsq0tp\opencv\modules\dnn\src\dnn.cpp (3455) cv::dnn::dnn4_v20200609::Net::Impl::getLayerShapesRecursively     blobs[1] = CV_32FC1 [ 80 10 ]
[ERROR:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-6sxsq0tp\opencv\modules\dnn\src\dnn.cpp (3455) cv::dnn::dnn4_v20200609::Net::Impl::getLayerShapesRecursively     blobs[2] = CV_32FC1 [ 1 80 ]
[ERROR:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-6sxsq0tp\opencv\modules\dnn\src\dnn.cpp (3457) cv::dnn::dnn4_v20200609::Net::Impl::getLayerShapesRecursively Exception message: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-6sxsq0tp\opencv\modules\dnn\src\layers\recurrent_layers.cpp:201: error: (-215:Assertion failed) inp0.size() >= 2 && total(inp0, 2) == _numInp in function 'cv::dnn::LSTMLayerImpl::getMemoryShapes'
Traceback (most recent call last):
  File ".\onnx-lstm.py", line 53, in <module>
    out = net.forward()
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-6sxsq0tp\opencv\modules\dnn\src\layers\recurrent_layers.cpp:201: error: (-215:Assertion failed) inp0.size() >= 2 && total(inp0, 2) == _numInp in function 'cv::dnn::LSTMLayerImpl::getMemoryShapes'
import cv2
import torch
import numpy as np

rnn = torch.nn.LSTM(10, 20, 1)
dummy_input = torch.randn(5, 3, 10)
dummy_h0 = torch.randn(1, 3, 20)
dummy_c0 = torch.randn(1, 3, 20)
output, (hn, cn) = rnn(dummy_input, (dummy_h0, dummy_c0))
torch.onnx.export(rnn, dummy_input, "lstm.onnx", verbose=True)

net = cv2.dnn.readNet("lstm.onnx")
inp = np.zeros((5, 3, 10))
net.setInput(inp)
out = net.forward()

What would be the way to resolve this error?

System information (version)
  • OpenCV = 4.4
  • Operating System / Platform => Windows 64 Bit

Onnx model link: lstm.zip

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions