我在Tensorflow中培训了一个CNN,它的准确率为92%。我把它保存为典型的ckpt文件。
session = tf.Session(config=tf.ConfigProto(log_device_placement=True))
session.run(tf.global_variables_initializer())
<TRAINING ETC>
saver.save(session, save_path_name)
在另一个文件中,我想运行推理,所以我按照文档中的说明调用了元图:
face_recognition_session = tf.Session()
saver = tf.train.import_meta_graph(<PATH TO META FILE>, clear_devices=True)
saver.restore(face_recognition_session, <PATH TO CKPT FILE>)
graph = tf.get_default_graph()
x = graph.get_tensor_by_name('input_variable_00:0')
y = graph.get_tensor_by_name('output_variable_00:0')
在进行推理或重新测试时,精度降至3%。
我忽略了什么吗?
您正在为saver
指定错误的方法。从TF Guide您可以看到您想要初始化会话,然后通过tensorflow.train.Saver()
上传。
tf.reset_default_graph()
# Create some variables.
x = tf.get_variable("input_variable_00:0", [x_shape])
y = tf.get_variable("output_variable_00:0", [y_shape])
saver = tf.train.Saver()
# Use the saver object normally after that.
with tf.Session() as sess:
# Initialize v1 since the saver will not.
saver.restore(sess, <PATH TO CKPT FILE>)
print("x : %s" % x.eval())
print("y : %s" % y.eval())
如果你想得到一致的推理结果,我也建议你把freezing and exporting your graphs作为GraphDef
。