问题
I'm trying to export my retrained inception model. I've read this almost similar question here and resources mentioned there as well.
But after exporting the graph, the variables
folder is empty which should contains files that hold the serialized variables of the graphs (saved_model.pb is created correctly I'd say).
I'm using TensorFlow 1.2.1 & Python 3.5.2.
Actually I've put a simple print(tf.trainable_variables())
inside the session, but it's an empty list.
Here's my function to export the graph:
def export_tf_model(graph_path, export_dir):
builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
with tf.gfile.FastGFile(graph_path, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
with tf.Session() as sess:
g = sess.graph
# print variables
print(tf.trainable_variables())
in_image = g.get_tensor_by_name('DecodeJpeg/contents:0')
inputs = {'images': tf.saved_model.utils.build_tensor_info(in_image)}
out_classes = g.get_tensor_by_name('final_result:0')
outputs = {'scores': tf.saved_model.utils.build_tensor_info(out_classes)}
signature = tf.saved_model.signature_def_utils.build_signature_def(
inputs=inputs,
outputs=outputs,
method_name=signature_constants.PREDICT_METHOD_NAME
)
builder.add_meta_graph_and_variables(
sess,
[tag_constants.SERVING],
signature_def_map={
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature},
)
builder.save()
I cannot figure out where could be the problem?
I've tried mnist_saved_model.py
from official tutorial and it works fine exporting both graph & variables. But I see that it's training and exporting. Is it necessary to do training before exporting graph? If yes how should I do it for
回答1:
I think this issue is related to the frozen pb
model, for variables have been converted into constants during freezing graph (See document here). Use initial ckpt
model file instead and maybe these threads help: # 1938, #2045
来源:https://stackoverflow.com/questions/46908719/empty-variables-folder-after-building-retrained-inception-savedmodel