tenorflow - ошибка создания прото-файлов assert d в name_to_node_map,

Я использую керасы с тензорным потоком. Я правильно экспортирую модель, но не могу создать прото-файлы.

Проблема в том, что я получаю сообщение об ошибке:

Файл "keras_indians.py", строка 103, в файле minimal_graph = convert_variables_to_constants (sessions, sessions.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 ", line 158 , в extract_sub_graph assert d в name_to_node_map, "% s не в графике"% 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)

person Nir    schedule 06.01.2017    source источник


Ответы (1)


Я боролся с той же проблемой до сих пор, когда я перебираю все узлы, созданные на моем графике. Я обнаружил, что заставка переписывает имена всех узлов с префиксом «восстановление / ***» после отладки графа с кодом ниже, обнаруживает последний узел и использует его в скрипте freezeGraph.

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

Больше информации :

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

Tensorflow: Каковы имена output_node_name для freeze_graph.py в модели model_with_buckets?

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

Сбой скрипта Tensorflow freeze_graph в модели, определенной с помощью Keras >

person Martin D    schedule 01.02.2017