我使用带有张量流的keras。 我正确地导出模型,但无法创建原型文件。
问题在于我收到错误:
文件" keras_indians.py",第103行,in minimal_graph = convert_variables_to_constants(sess,sess.graph_def,[' Dense3 / output_node'])文件 " /usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/graph_util.py" ;, 第234行,在convert_variables_to_constants中 inference_graph = extract_sub_graph(input_graph_def,output_node_names)文件 " /usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/graph_util.py" ;, 第158行,在extract_sub_graph中 在name_to_node_map中断言d,"%s不在图表中#34; %d AssertionError:Dense3 / output_node不在图中
我尝试了最后一个节点名称的任何可能的变体,但它不起作用 任何帮助都非常感谢!
以下是代码:
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)
答案 0 :(得分:1)
直到现在,当我循环遍历图表中创建的所有节点时,我一直在努力解决同样的问题。我发现,在使用下面的代码调试图形后,保存程序用前缀“restore / ***”重写所有节点的名称,找到最后一个节点并在freezeGraph脚本中使用它。
var fs = require('fs');
var path = 'D:\\test2\\2ss\\hhh\\gu';
if(fs.makeDirectory(path))
console.log('"'+path+'" was created.');
else
console.log('"'+path+'" is NOT created.');
phantom.exit();
更多信息:
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