【问题标题】:TensorFlow: Batch Norm breaks network when is_training = FalseTensorFlow:当 is_training = False 时,Batch Norm 会破坏网络
【发布时间】:2017-05-26 23:25:05
【问题描述】:

我正在尝试使用 TensorFlow-Slim 中的批处理规范层,如下所示:

net = ...
net = slim.batch_norm(net, scale = True, is_training = self.isTraining,
    updates_collections = None, decay = 0.9)
net = tf.nn.relu(net)
net = ...

我训练:

self.optimizer = slim.learning.create_train_op(self.model.loss,
    tf.train.MomentumOptimizer(learning_rate = self.learningRate,
    momentum = 0.9, use_nesterov = True)

optimizer = self.sess.run([self.optimizer],
    feed_dict={self.model.isTraining:True})

我加载保存的权重:

net = model.Model(sess,width,height,channels,weightDecay)

savedWeightsDir = './savedWeights/'
saver = tf.train.Saver(max_to_keep = 5)
checkpointStr = tf.train.latest_checkpoint(savedWeightsDir)
sess.run(tf.global_variables_initializer())
saver.restore(sess, checkpointStr)
global_step = tf.contrib.framework.get_or_create_global_step()

我推断:

inf = self.sess.run([self.softmax],
    feed_dict = {self.imageBatch:imageBatch,self.isTraining:False})

当然,我遗漏了很多代码并解释了一些代码,但我认为这就是批处理规范所涉及的全部内容。奇怪的是,如果我设置 isTraining:True,我会得到更好的结果。可能是加载权重的问题 - 也许批量标准值没有保存?代码中有什么明显的错误吗?谢谢。

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    我刚刚遇到了同样的问题并找到了solution here。 问题源于tf.layers.batch_normalization 层需要更新 moving_meanmoving_variance

    为了在您的情况下正确执行此操作,您需要将训练过程修改为:

    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    with tf.control_dependencies(update_ops):
        self.optimizer = slim.learning.create_train_op(self.model.loss,
          tf.train.MomentumOptimizer(learning_rate = self.learningRate,
          momentum = 0.9, use_nesterov = True)
    

    或更一般地说,来自documentation

      x_norm = tf.layers.batch_normalization(x, training=training)
    
      # ...
    
      update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
      with tf.control_dependencies(update_ops):
        train_op = optimizer.minimize(loss)
    

    【讨论】:

      猜你喜欢
      • 2018-03-11
      • 2020-11-22
      • 2017-08-03
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      相关资源
      最近更新 更多