tensorflow: Bug - freeze_graph producing invalid graph_def in tensorflow 1.4

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): No
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): release 1.4
  • Python version: 3.5
  • Bazel version (if compiling from source): -
  • GCC/Compiler version (if compiling from source): -
  • CUDA/cuDNN version: 8/6
  • GPU model and memory: GTX 970, 4GB
  • Exact command to reproduce:
  1. Download dbg.zip (contains graph.pbtxt, checkpoint and the resulting frozen_model.pb generated on my machine .)
  2. Unzip and open terminal in the unzipped folder
  3. Run <tensorflow_root>/python/tools/freeze_graph.py --input_graph=graph.pbtxt --input_binary=False --input_checkpoint=model.ckpt-1 --output_node_names=softmax_tensor --output_graph=frozen_model_test.pb --clear_devices=True
  4. Start python, attempt to import the frozen graph:
import tensorflow as tf
from tensorflow.python.platform import gfile
with gfile.FastGFile('frozen_model_test.pb','rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    tf.import_graph_def(graph_def, name='')

Describe the problem

Trying to import the graph, I get this error:

ValueError: graph_def is invalid at node ‘IsVariableInitialized’: Input tensor ‘global_step:0’ Cannot convert a tensor of type int64 to an input of type int64_ref.

The error is raised in tf.import_graph_def(graph_def, name=''). The dump of str(graph_def) can be seen in this text file: graph_def_dbg.txt

The error happens sine the upgrade to TF 1.4, with TF 1.3 the graph freezing and importing works as expected.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 17 (3 by maintainers)

Most upvoted comments

Same as @dratini6 . I’ve solved this issue by adding “global_step” in the black list on tf.graph_util.convert_variables_to_constants method.

output_graph_def = tf.graph_util.convert_variables_to_constants(
            sess, # The session is used to retrieve the weights
            tf.get_default_graph().as_graph_def(), # The graph_def is used to retrieve the nodes 
            output_node_names.split(","), # The output node names are used to select the usefull nodes
            variable_names_blacklist=['global_step']) 

Hello @GPhilo, I solved my issue now by defining the initial values of the problematic variables as additional placeholders. This means I have to feed the initializing values additionally when using the model, but at least this got the model usable. The initializers are the init_states variables in https://github.com/asoehlke/neuronal-music-accompanist-bach/blob/master/MusicAccompanistInferenceGraph.ipynb.

With this change, freeze_graph works without any blacklisted variables.