Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input layer not found in function 'cv::dnn::dnn4_v20210608::`anonymous-namespace'::TFImporter::connect' #20907

Open
FrancescoMandru opened this issue Oct 19, 2021 · 3 comments

Comments

@FrancescoMandru
Copy link

FrancescoMandru commented Oct 19, 2021

I'm trying to load my Tensorflow model with opencv DNN as follows:

1:

model.save('mymodel', save_format='tf')

2:

import tensorflow as tf
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2

physical_devices = tf.config.list_physical_devices('GPU')
tf.config.set_visible_devices(physical_devices[0],'GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)

loaded = tf.saved_model.load('mymodel')
infer = loaded.signatures['serving_default']

f = tf.function(infer).get_concrete_function(input_1=tf.TensorSpec(shape=[1, 256, 256, 1], 
                                                                        dtype=tf.float32))

f2 = convert_variables_to_constants_v2(f, aggressive_inlining=True)
graph_def = f2.graph.as_graph_def()

# Export frozen graph
with tf.io.gfile.GFile('frozen_graph.pb', 'wb') as f:
    f.write(graph_def.SerializeToString())

3:

net = cv2.dnn.readNet('frozen_graph.pb')
img = np.random.standard_normal((1, 256, 256, 1)).astype(np.float32)
print(img.shape)
net.setInput(img)
out = net.forward()
print(out.shape)

but I get the following error:

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13288/1971587229.py in <module>
      1 import cv2 as cv2
      2 
----> 3 net = cv2.dnn.readNet('frozen_graph.pb')
      4 img = np.random.standard_normal((1, 256, 256, 1)).astype(np.float32)
      5 print(img.shape)

error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-c2l3r8zm\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:2445: error: (-2:Unspecified error) Input layer not found: StatefulPartitionedCall/StatefulPartitionedCall/my_net/Decoderblock1/conv2d_transpose/stack/1 in function 'cv::dnn::dnn4_v20210608::`anonymous-namespace'::TFImporter::connect'

Tensorflow version: 2.5.0
OpenCV version 4.5.3

This is my frozen graph:
frozen_graph.zip

@d5423197
Copy link

I am having the same error. Opencv DNN is really difficult to use. I have tried to convert tf 2 model to onnx model. I can successfully use it in python onnx module. But I cannot read it using opencv dnn.

@FrancescoMandru
Copy link
Author

I think that the Conv2DTranspose is not implemented yet and this may cause some problems during the conversion. I have also other issues when there is asymmetric padding applied by Tensorflow

@LaurentBerger
Copy link
Contributor

LaurentBerger commented Mar 20, 2024

@opencv-alalek why this issue is incomplete? I can reproduce error usng opencv-5.0 pre

cv2.error: OpenCV(5.0.0-pre) C:\lib\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:2853: error: (-2:Unspecified error) Input layer not found: StatefulPartitionedCall/StatefulPartitionedCall/my_net/Decoderblock1/conv2d_transpose/stack/1 in function cv::dnn::dnn5_v20231225::anonymous-namespace'::TFImporter::connect'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants