Ask Your Question

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 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 "", 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,
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,
    self.y_out_unnormal = tf.nn.conv2d(self.x_pl, w, stride, 'SAME')

and I am saving the model as shown below:, 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 --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()
        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


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

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

edit flag offensive delete link more
Login/Signup to Answer

Question Tools



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

Seen: 2,278 times

Last updated: Dec 06 '19