如何将tf.train.ExponentialMovingAverage应用于将ckpt和meta文件冻结为pb文件?

时间:2018-12-04 10:02:21

标签: python python-3.x python-2.7 tensorflow

我将tf.train.ExponentialMovingAverage应用于tf.trainable_variables()以训练我的模型。但是,当我将经过训练的ckptmeta文件冻结为单个pb文件然后进行推断时,我遇到了一个问题:

Freeze_graph results in very poor accuracy compared to manually exporting and freezing the graph

类似的问题:here

我要使用提到的解决方案here

variable_averages = tf.train.ExponentialMovingAverage(0.9997)
variables_to_restore = variable_averages.variables_to_restore()
saver = tf.train.Saver(variables_to_restore) 

但是如何将tf.train.ExponentialMovingAverage应用于我的freeze.py

import tensorflow as tf
meta_path = "./checkpoints_bn/mnist-slim.meta"
config = tf.ConfigProto(allow_soft_placement=True)
with tf.Session(config = config) as sess:

    # Restore the graph
    saver = tf.train.import_meta_graph(meta_path)

    # variable_averages = tf.train.ExponentialMovingAverage(0.99)
    # variables_to_restore = variable_averages.variables_to_restore()
    # saver = tf.train.Saver(variables_to_restore)

    # Load weights
    saver.restore(sess,tf.train.latest_checkpoint("./checkpoints_bn/"))

    output_node_names =["fc1/Relu"]

    # Freeze the graph
    frozen_graph_def = tf.graph_util.convert_variables_to_constants(
        sess,
        sess.graph_def,
        output_node_names)
    tf.graph_util.remove_training_nodes(frozen_graph_def)

    # Save the frozen graph
    with open('output/mnist_bn.pb', 'wb') as f:
      f.write(frozen_graph_def.SerializeToString())

有人可以给些建议吗?

0 个答案:

没有答案