tensorflow - 创建原型文件错误 "assert d in name_to_node_map,"

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

我正在使用带有 tensorflow 的 keras。 我正在正确导出模型,但无法创建原型文件。

问题是我得到一个错误:

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

我尝试了最后一个节点名称的任何可能变体,但它不起作用 非常感谢任何帮助!

代码如下:

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)

直到现在,当我遍历在我的图表中创建的所有节点时,我一直在为同样的问题而苦苦挣扎。在用下面的代码调试图表后,我发现 saver 重写了前缀为 "restore/***" 的所有节点的名称,找到了最后一个节点并在 freezeGraph 脚本中使用它。

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

更多信息:

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

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