Ask Your Question
0

Error Loading .pb model from readNetFromTensorflow

asked 2018-05-03 15:56:30 -0500

khaled elmadawi gravatar image

updated 2019-12-06 07:10:16 -0500

ojesus gravatar image
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?

edit retag flag offensive close merge delete

Comments

I have the same problem. Anyone answers in this forum at all?

NadavB gravatar imageNadavB ( 2019-04-01 10:09:25 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-12-06 04:47:13 -0500

gpcbitnik gravatar image

updated 2019-12-06 04:47:57 -0500

you should use optimize_for_inference tools on the frozen model

see dkurt answer https://github.com/opencv/opencv/issues/11577

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2018-05-03 15:56:30 -0500

Seen: 1,868 times

Last updated: Dec 06 '19