opencv: Error Loading .pb model from readNetFromTensorflow

  • OpenCV => 3.4
  • Operating System / Platform => Windows 64 Bit
  • Compiler => Visual Studio 2015
Detailed description

I am making a simple TF model, and it gives me this error: python test.py OpenCV Error: Assertion failed (const_layers.insert(std::make_pair(name, li)).se cond) in cv::dnn::experimental_dnn_v3::anonymous-namespace'::addConstNodes, fil e C:\projects\opencv-python\opencv\modules\dnn\src\tensorflow\tf_importer.cpp, l ine 663 Traceback (most recent call last): File "dnn_test.py", line 4, in <module> cvNet = cv.dnn.readNetFromTensorflow('frozen_graph.pb', 'frozen_graph.pbtxt' ) cv2.error: C:\projects\opencv-python\opencv\modules\dnn\src\tensorflow\tf_import er.cpp:663: error: (-215) const_layers.insert(std::make_pair(name, li)).second i n function cv::dnn::experimental_dnn_v3::anonymous-namespace’::addConstNodes


the graph as shown below:

    self.x_pl = tf.placeholder(tf.float32, [None,
                                                self.config.img_height,
                                                self.config.img_width,
                                                self.config.num_channels])
    with tf.variable_scope('output') as scope:
        stride = [1, 1, 1, 1]
        kernel_shape = [1,1, self.x_pl.get_shape()[-1], self.config.num_classes]
        w = tf.get_variable('w_conv', kernel_shape, tf.float32,
                            initializer=tf.zeros_initializer())
        self.y_out_unnormal = tf.nn.conv2d(self.x_pl, w, stride, 'SAME')

and I am saving the model as shown below:

        self.best_saver.save(self.sess, self.config.best_checkpoint_dir, self.model.global_step_tensor)
        tf.train.write_graph(self.sess.graph_def, self.config.best_checkpoint_dir, 'graph.pbtxt', as_text=True)
        tf.train.write_graph(self.sess.graph, self.config.best_checkpoint_dir, 'graph.pb', as_text=False)

then I frozen the by this command:

        python -m tensorflow.python.tools.freeze_graph --input_binary --input_graph Binary_Protobuf.pb --input_checkpoint mnist_cnn.ckpt --output_graph frozen_graph.pb --output_node_names output/Conv2D

Then generated the .pbtxt file:

        import tensorflow as tf
        import numpy as np
        graph_filename = "./freez_test/frozen_graph.pb"
        with tf.gfile.GFile(graph_filename, "rb") as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            tf.train.write_graph(graph_def, './', 'frozen_graph.pbtxt', True)

Then I loaded it to cv python script to see if it is compatible with the library or not:

        import cv2 as cv
        import numpy as np

        cvNet = cv.dnn.readNetFromTensorflow('frozen_graph.pb', 'frozen_graph.pbtxt')

and it gave me the mentioned error cv::dnn::experimental_dnn_v3::`anonymous-namespace’::addConstNodes

I tested it with much out=x*x graph, and it worked , and this is a simple convolution graph, any one know how to solve this problem?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 4
  • Comments: 24 (6 by maintainers)

Most upvoted comments

@khaledelmadawi, *.pbtxt is optional and it’s used to modify TensorFlow’s graph in text. Here you just generate it from an origin .pb file. Have you tried to load a model using only frozen_graph.pb?

Guys, please do not use closed issues. Open a new one following our guides (see issue template). Provide minimal working example including reference to used model. Thanks!

FWIW I believe I got around this by using a newer version of OpenCV, 4.0.1 to be specific. I think that was what did it - this was one of a handful of problems I was wrestling with.