tensorflow - creating proto files error "assert d in name_to_node_map,"

1.7k views Asked by At

I'm using keras with tensorflow. I'm exporting the model correcty but fail to create proto files.

The problem is that on i get an error:

File "keras_indians.py", line 103, in minimal_graph = convert_variables_to_constants(sess, sess.graph_def, ['Dense3/output_node']) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/graph_util.py", line 234, in convert_variables_to_constants inference_graph = extract_sub_graph(input_graph_def, output_node_names) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/graph_util.py", line 158, in extract_sub_graph assert d in name_to_node_map, "%s is not in graph" % d AssertionError: Dense3/output_node is not in graph

I tried any possible variation for the last node name and it doesn't work Any help is highly appreciated !

Below is the code:

import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense

import numpy


# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load pima indians dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid', name='output_node'))

# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, nb_epoch=80, batch_size=10)
# evaluate the model


scores = model.evaluate(X, Y)
print("RESULT IS %s: %.2f%%" % (model.metrics_names[1], scores[1]*100))



## Exporting the model ##

print ("Exporting ")


# THIS WORKS !!
#from keras import backend as K
#sess = K.get_session()
#and go about exporting the model as in the tutorial (Note that import for exporter has changed)

#from tensorflow.contrib.session_bundle import exporter

#K.set_learning_phase(0)
#export_path = "./"
#export_version = 1
#saver = tf.train.Saver(sharded=True)
#model_exporter = exporter.Exporter(saver)
#signature = exporter.classification_signature(input_tensor=model.input,
#                                             scores_tensor=model.output)
#model_exporter.init(sess.graph.as_graph_def(),
#                   default_graph_signature=signature)
#model_exporter.export(export_path, tf.constant(export_version), sess)

# END THIS WORKS!


#OPTION 2 FROM: https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html

from keras import backend as K

K.set_learning_phase(0)  # all new operations will be in test mode from now on
# added by nir
sess = K.get_session()

# serialize the model and get its weights, for quick re-building
config = model.get_config()
weights = model.get_weights()

# re-build a model where the learning phase is now hard-coded to 0
from keras.models import model_from_config
new_model = model.from_config(config)
new_model.set_weights(weights)


#from tensorflow_serving.session_bundle import exporter
from tensorflow.contrib.session_bundle import exporter

export_path = "./" # where to save the exported graph
export_version = 18 # version number (integer) 

saver = tf.train.Saver(sharded=True)
model_exporter = exporter.Exporter(saver)
signature = exporter.classification_signature(input_tensor=new_model.input,
                                              scores_tensor=new_model.output)
model_exporter.init(sess.graph.as_graph_def(),
                    default_graph_signature=signature)
model_exporter.export(export_path, tf.constant(export_version), sess)


#print  sess.graph.as_graph_def();
print ("Export Done")




## Create proto files for grpc ##

from tensorflow.python.framework.graph_util import convert_variables_to_constants

minimal_graph = convert_variables_to_constants(sess, sess.graph_def, ['Dense3/output_node']) 

tf.train.write_graph(minimal_graph, '.', 'minimal_graph.proto', as_text=False)
tf.train.write_graph(minimal_graph, '.', 'minimal_graph.txt', as_text=True)
1

There are 1 answers

0
Martin D On BEST ANSWER

I struggled with the same problem until now, when I am looping through all nodes created in my graph. I found out that saver rewrite the names of all nodes with the prefix "restore/***" after debugging graph with the code below, found out last node and use it in freezeGraph script.

 graph_def = sess.graph.as_graph_def()
 for node in graph_def.node:
     print node

More info :

https://github.com/tensorflow/tensorflow/issues/3986

Tensorflow: What are the "output_node_names" for freeze_graph.py in the model_with_buckets model?

https://www.tensorflow.org/how_tos/tool_developers/

Tensorflow freeze_graph script failing on model defined with Keras