Error: Unspecified error (Can't create layer "model/simple_rnn/TensorArrayV2_1:0" of type "TensorListReserve"

[ INFO:0] global D:\vcpkg\buildtrees\opencv4\src\4.5.1-e92f755db3.clean\modules\dnn\src\onnx\onnx_importer.cpp (395) cv::dnn::dnn4_v20201117::ONNXImporter::populateNet DNN/ONNX: loading ONNX v4 model produced by 'tf2onnx':1.10.0. Number of nodes = 19, inputs = 1, outputs = 1
OpenCV(4.5.1) Error: Unspecified error (Can't create layer "model/simple_rnn/TensorArrayV2_1:0" of type "TensorListReserve") in cv::dnn::dnn4_v20201117::LayerData::getLayerInstance, file D:\vcpkg\buildtrees\opencv4\src\4.5.1-e92f755db3.clean\modules\dnn\src\dnn.cpp, line 614
[ERROR:0] global D:\vcpkg\buildtrees\opencv4\src\4.5.1-e92f755db3.clean\modules\dnn\src\onnx\onnx_importer.cpp (1878) cv::dnn::dnn4_v20201117::ONNXImporter::handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 1 outputs: [TensorListReserve]:(model/simple_rnn/TensorArrayV2_1:0)
[ INFO:0] global D:\vcpkg\buildtrees\opencv4\src\4.5.1-e92f755db3.clean\modules\dnn\src\onnx\onnx_importer.cpp (1881) cv::dnn::dnn4_v20201117::ONNXImporter::handleNode     Input[0] = 'model/simple_rnn/TensorArrayV2_1/element_shape:0'
[ INFO:0] global D:\vcpkg\buildtrees\opencv4\src\4.5.1-e92f755db3.clean\modules\dnn\src\onnx\onnx_importer.cpp (1881) cv::dnn::dnn4_v20201117::ONNXImporter::handleNode     Input[1] = 'model/simple_rnn/strided_slice_1__31:0'
[ INFO:0] global D:\vcpkg\buildtrees\opencv4\src\4.5.1-e92f755db3.clean\modules\dnn\src\onnx\onnx_importer.cpp (1885) cv::dnn::dnn4_v20201117::ONNXImporter::handleNode     Output[0] = 'model/simple_rnn/TensorArrayV2_1:0'
OpenCV(4.5.1) Error: Unspecified error (> Node [TensorListReserve]:(model/simple_rnn/TensorArrayV2_1:0) parse error: OpenCV(4.5.1) D:\vcpkg\buildtrees\opencv4\src\4.5.1-e92f755db3.clean\modules\dnn\src\dnn.cpp:614: error: (-2:Unspecified error) Can't create layer "model/simple_rnn/TensorArrayV2_1:0" of type "TensorListReserve" in function 'cv::dnn::dnn4_v20201117::LayerData::getLayerInstance'
> ) in cv::dnn::dnn4_v20201117::ONNXImporter::handleNode, file D:\vcpkg\buildtrees\opencv4\src\4.5.1-e92f755db3.clean\modules\dnn\src\onnx\onnx_importer.cpp, line 1887

This is tensorflow2 network:
model.add(layers.SimpleRNN(20,kernel_initializer=‘he_normal’,activation=‘relu’))
model.add(layers.Dense(1))

Can someone help solve it,Thanks

3BF1`$LDO6(EL5~RMLXEQLX

quite outdated.
however, i’m quite sure, that you’re simply out of luck,
and neither the dnn::Net, nor the onnx importer knows this layer.

also, onnx from tensorflow2 is not really well supported. (pytorch, much better !)

try to find out, where in your code this got in, and try to do without.

thanks.

I looked up the data and said that opencv does not support RNN. Is this the reason? Even if I use PB, I report an error.

where exactly did you look ?

also, can you show a minimal python example to generate / export this model ?

# Model is my network
# Convert Keras model to ConcreteFunction
import tensorflow as tf
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
full_model = tf.function(lambda x: model(x))
full_model = full_model.get_concrete_function(
    tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))
# Get frozen ConcreteFunction
frozen_func = convert_variables_to_constants_v2(full_model)
frozen_func.graph.as_graph_def()
# Save frozen graph from frozen ConcreteFunction to hard drive
tf.io.write_graph(graph_or_graph_def=frozen_func.graph,
                  logdir="./frozen_models",
                  name="frozen_graph.pb",
                  as_text=False)

!python -m tf2onnx.convert --input ./frozen_models/frozen_graph.pb --output model.onnx --outputs Identity:0 --inputs x:0

No matter I use the cured Pb or onnx, I report an error.

1 Like

thanks for the updates !
(but please NEVER post images of code anywhere, ppl will just hate you for that …)
((format code using ctrl+e here !))

then, i took a look at the dnn src, and neither tf nor onnx importers seem to know about RNN. however, there are tests for it, so it must have been used somewhere, still researching this …

oh, and what’s the .pb error ?

This is the opencv output

[ INFO:0] global D:\vcpkg\buildtrees\opencv4\src\4.5.4-6210bf5274.clean\modules\dnn\src\tensorflow\tf_importer.cpp (2896) cv::dnn::dnn4_v20211004::`anonymous-namespace'::TFImporter::populateNet DNN/TF: parsing model produced by TF v898 (min_consumer=0). Number of nodes = 150

If I use PB, opencv will load until the memory overflows.

1 Like

This is the code for curing Pb

# Model is my network
# Convert Keras model to ConcreteFunction

import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
model=keras.models.load_model('test.h5')
full_model = tf.function(lambda x: model(x))
full_model = full_model.get_concrete_function(
    tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))

# Get frozen ConcreteFunction
frozen_func = convert_variables_to_constants_v2(full_model)
frozen_func.graph.as_graph_def()


# Save frozen graph from frozen ConcreteFunction to hard drive
tf.io.write_graph(graph_or_graph_def=frozen_func.graph,
                  logdir="./frozen_models",
                  name="frozen_graph.pb",
                  as_text=False)
1 Like

I just tried to convert Pb with TF1. Opencv can import it, but I can’t create a layer

This is info:
[ INFO:0] global D:\vcpkg\buildtrees\opencv4\src\4.5.4-6210bf5274.clean\modules\dnn\src\tensorflow\tf_importer.cpp (2896) cv::dnn::dnn4_v20211004::`anonymous-namespace’::TFImporter::populateNet DNN/TF: parsing model (N/A version info). Number of nodes = 76
layer id:1, type: Permute, name:simple_rnn/transpose
OpenCV(4.5.4) Error: Unspecified error (Can’t create layer “simple_rnn/Shape_1” of type “Shape”) in cv::dnn::dnn4_v20211004::LayerData::getLayerInstance, file D:\vcpkg\buildtrees\opencv4\src\4.5.4-6210bf5274.clean\modules\dnn\src\dnn.cpp, line 615