tensorflow: freeze graph fail by using the published checkpoint file: Attempting to use uninitialized value

I am using the latest Tensorflow code on Ubuntu 16.04

  1. I download Inception-ResNet-v2 model from the link https://github.com/tensorflow/models/tree/master/slim
    the check point file is: http://download.tensorflow.org/models/inception_resnet_v2_2016_08_30.tar.gz

  2. use the code below to get graph define, which output is graph.pbtxt

import tensorflow as tf
from nets import inception_resnet_v2
import numpy as np

x = tf.placeholder(shape=[1,299,299,3], dtype=tf.float32)
inception_resnet_v2 = inception_resnet_v2.inception_resnet_v2(x)

sess = tf.Session()
tf.train.write_graph(sess.graph_def, './', 'graph.pbtxt')

3, use the freeze graph tool to get .pb file.

bazel-bin/tensorflow/python/tools/freeze_graph --input_graph=/home/scopeserver/RaidDisk/DeepLearning/mwang/Model_backup2/slim/graph.pbtxt --input_checkpoint=/home/scopeserver/RaidDisk/DeepLearning/mwang/Model_backup2/slim/inception_resnet_v2_2016_08_30.ckpt --output_graph=/tmp/frozen_graph.pb --output_node_names=InceptionResnetV2/Logits/Predictions

i get the error:

Traceback (most recent call last):
  File "/home/scopeserver/RaidDisk/DeepLearning/mwang/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/tools/freeze_graph.py", line 218, in <module>
    app.run(main=main, argv=[sys.argv[0]] + unparsed)
  File "/home/scopeserver/RaidDisk/DeepLearning/mwang/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/platform/app.py", line 44, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "/home/scopeserver/RaidDisk/DeepLearning/mwang/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/tools/freeze_graph.py", line 150, in main
    FLAGS.variable_names_blacklist)
  File "/home/scopeserver/RaidDisk/DeepLearning/mwang/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/tools/freeze_graph.py", line 138, in freeze_graph
    variable_names_blacklist=variable_names_blacklist)
  File "/home/scopeserver/RaidDisk/DeepLearning/mwang/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/framework/graph_util_impl.py", line 218, in convert_variables_to_constants
    returned_variables = sess.run(variable_names)
  File "/home/scopeserver/RaidDisk/DeepLearning/mwang/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 767, in run
    run_metadata_ptr)
  File "/home/scopeserver/RaidDisk/DeepLearning/mwang/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 965, in _run
    feed_dict_string, options, run_metadata)
  File "/home/scopeserver/RaidDisk/DeepLearning/mwang/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 1015, in _do_run
    target_list, options, run_metadata)
  File "/home/scopeserver/RaidDisk/DeepLearning/mwang/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 1035, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: **Attempting to use uninitialized value InceptionResnetV2/Repeat_1/block17_16/Branch_1/Conv2d_0c_7x1/biases**
	 [[Node: _send_InceptionResnetV2/Repeat_1/block17_16/Branch_1/Conv2d_0c_7x1/biases_0 = _Send[T=DT_FLOAT, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=4447330825950993621, tensor_name="InceptionResnetV2/Repeat_1/block17_16/Branch_1/Conv2d_0c_7x1/biases:0", _device="/job:localhost/replica:0/task:0/cpu:0"](InceptionResnetV2/Repeat_1/block17_16/Branch_1/Conv2d_0c_7x1/biases)]]


About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 21 (5 by maintainers)

Most upvoted comments

i figure out how to use them. the code below is to export .pbtxt file for inception_resnet_v2.

Use this .pbtxt and the published .ckpt to freeze graph, if you want to get .pb file.

if you want to load other models or get other .pbtxt files, just change the key word inception_resnet_v2 to other model name, such as inception_v2, inception_v3

the first node of .pb file is Placeholder, which is the input 4D tensor of image, such as [1,299,299,3]

you can use .ckpt for prediction as below. The endpoint is “Prediction”, But I use .pb in general, which is smaller.

import tensorflow as tf
import tensorflow.contrib.slim as slim
from nets.inception_resnet_v2 import inception_resnet_v2, inception_resnet_v2_arg_scope
import numpy as np

height = 299
width = 299
channels = 3

# Create graph
X = tf.placeholder(tf.float32, shape=[None, height, width, channels])
with slim.arg_scope(inception_resnet_v2_arg_scope()):
    logits, end_points = inception_resnet_v2(X, num_classes=1001,is_training=False)
predictions = end_points["Predictions"]
saver = tf.train.Saver()

X_test = np.ones((1,299,299,3))  # a fake image, you can use your own image

# Execute graph
with tf.Session() as sess:
    saver.restore(sess, "./inception_resnet_v2.ckpt")
    predictions_val = predictions.eval(feed_dict={X: X_test})
    tf.train.write_graph(sess.graph_def, './', 'resnetv2.pbtxt')

the cmd to free graph for the model, pay attention to ouput node name, you can get it from .pbtxt file.

bazel-bin/tensorflow/python/tools/freeze_graph --input_graph=/home/scopeserver/RaidDisk/DeepLearning/resnetv2.pbtxt --input_checkpoint=/home/scopeserver/RaidDisk/DeepLearning/slim/inception_resnet_v2.ckpt --output_graph=./inception_resetv2.pb --output_node_names=InceptionResnetV2/Logits/Predictions

I have the same issue on how to use my fine-tuned checkpoint files to predict the image. I will try freeze graph now. the document is very poor on this topic.

To add my 2 cents:

If the final goal is to use a pre-trained model to predict or extract features for new images, there is no need to generate intermediate .pbtxt file and consecutively freeze it. If you already know the layer names, below is the example of how to use ResNet50 model to extract features (inspired a bit from @civilman628 's answer and documentation on resnet_v1.py):

from tensorflow.contrib.slim.nets import resnet_v1
import tensorflow as tf
import tensorflow.contrib.slim as slim

# Create graph
inputs = tf.placeholder(tf.float32, shape=[batch_size, height, width, channels])
with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        net, end_points = resnet_v1.resnet_v1_50(inputs, is_training=False)

saver = tf.train.Saver()    

with tf.Session() as sess:
        saver.restore(sess, '.resnet_v1_50.ckpt')
        representation_tensor = sess.graph.get_tensor_by_name('resnet_v1_50/pool5:0') # if you don't know names like these, consider referring to corresponding model file or generate .pbtxt file as mentioned in  @civilman628 's answer above
        img = ...  #load image here with size [1, 224,224, 3]
        features = sess.run(representation_tensor, {'Placeholder:0': x})

No sure why this is assigned to me, seems solved. Feel free to re-open if there is still an issue.

Hi @civilman628 could you explain in detail as to how to get the output_node_names from the .pbtxt file?