我正在尝试冻结基于Inception-v3的模型并运行推理。但是,与原始模型相比,使用冻结模型得出的推断概率不一致。
我发现训练和推论的差异来自指数移动平均线(EMA)。当我在两个模型中都关闭EMA时,我得到的概率相同(差异<1e-5)。
我正在使用的冻结代码:
from __future__ import print_function
import tensorflow as tf
from nets.inception_v3 import inception_v3, inception_v3_arg_scope
from tensorflow.python.framework import graph_util
import sys
slim = tf.contrib.slim
checkpoint_file = '/my/model'
with tf.Graph().as_default() as graph:
images = tf.placeholder(shape=[None, 100, 221, 6], dtype=tf.float32, name = 'input')
with slim.arg_scope(inception_v3_arg_scope()):
logits, end_points = inception_v3(images, num_classes = 3, create_aux_logits = False, is_training = False)
variables_to_restore = slim.get_variables_to_restore()
MOVING_AVERAGE_DECAY = 0.9999
variable_averages = tf.train.ExponentialMovingAverage(
MOVING_AVERAGE_DECAY)
for var in variables_to_restore:
tf.add_to_collection(tf.GraphKeys.MOVING_AVERAGE_VARIABLES, var)
variables_to_restore = variable_averages.variables_to_restore() #This line is commented if EMA is turned off
saver = tf.train.Saver(variables_to_restore)
#Setup graph def
input_graph_def = graph.as_graph_def()
output_node_names = "InceptionV3/Predictions/Reshape_1"
output_graph_name = "./frozen_inception_v3_new_100_221_ema.pb"
with tf.Session() as sess:
saver.restore(sess, checkpoint_file)
#Exporting the graph
print ("Exporting graph...")
output_graph_def = graph_util.convert_variables_to_constants(
sess,
input_graph_def,
output_node_names.split(","))
with tf.gfile.GFile(output_graph_name, "wb") as f:
f.write(output_graph_def.SerializeToString())
EMA部分与原始模型的代码相同。
我错误地冻结了EMA推论图吗?
答案 0 :(得分:0)
问题解决了。 我使用的EMA部分
MOVING_AVERAGE_DECAY = 0.9999
variable_averages = tf.train.ExponentialMovingAverage(
MOVING_AVERAGE_DECAY)
for var in variables_to_restore:
tf.add_to_collection(tf.GraphKeys.MOVING_AVERAGE_VARIABLES, var)
variables_to_restore = variable_averages.variables_to_restore()
不正确。 如果我删除
for var in variables_to_restore:
tf.add_to_collection(tf.GraphKeys.MOVING_AVERAGE_VARIABLES, var)
现在效果很好。