【问题标题】:MobileNet is not usable when set is_training to false将 is_training 设置为 false 时,MobileNet 不可用
【发布时间】:2018-02-01 23:19:27
【问题描述】:

对这个问题更准确的描述是,当 is_training 未明确设置为 true 时,MobileNet 表现不佳。 我指的是 TensorFlow 在其模型库 https://github.com/tensorflow/models/blob/master/slim/nets/mobilenet_v1.py 中提供的 MobileNet。

这就是我创建网络的方式(phase_train=True):

with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope(is_training=phase_train)):
        features, endpoints = mobilenet_v1.mobilenet_v1(
            inputs=images_placeholder, features_layer_size=features_layer_size, dropout_keep_prob=dropout_keep_prob,
            is_training=phase_train)

我正在训练一个识别网络,并且在训练期间我在 LFW 上进行测试。我在训练期间获得的结果随着时间的推移而提高,并且获得了很好的准确性。

在部署之前,我冻结了图表。如果我用 is_training=True 冻结图表,我在 LFW 上得到的结果与训练期间相同。 但是如果我设置 is_training=False 我得到的结果就像网络根本没有训练一样......

这种行为实际上发生在 Inception 等其他网络中。

我倾向于认为我在这里遗漏了一些非常基本的东西,而且这不是 TensorFlow 中的错误...

任何帮助将不胜感激。

正在添加更多代码...

这就是我准备培训的方式:

images_placeholder = tf.placeholder(tf.float32, shape=(None, image_size, image_size, 1), name='input')
labels_placeholder = tf.placeholder(tf.int32, shape=(None))
dropout_placeholder = tf.placeholder_with_default(1.0, shape=(), name='dropout_keep_prob')
phase_train_placeholder = tf.Variable(True, name='phase_train')

global_step = tf.Variable(0, name='global_step', trainable=False)

# build graph

with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope(is_training=phase_train_placeholder)):
    features, endpoints = mobilenet_v1.mobilenet_v1(
        inputs=images_placeholder, features_layer_size=512, dropout_keep_prob=1.0,
        is_training=phase_train_placeholder)

# loss

logits = slim.fully_connected(inputs=features, num_outputs=train_data.get_class_count(), activation_fn=None,
                              weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
                              weights_regularizer=slim.l2_regularizer(scale=0.00005),
                              scope='Logits', reuse=False)

tf.losses.sparse_softmax_cross_entropy(labels=labels_placeholder, logits=logits,
                                       reduction=tf.losses.Reduction.MEAN)

loss = tf.losses.get_total_loss()

# normalize output for inference

embeddings = tf.nn.l2_normalize(features, 1, 1e-10, name='embeddings')

# optimizer

optimizer = tf.train.AdamOptimizer()
train_op = optimizer.minimize(loss, global_step=global_step)

这是我的火车步骤:

batch_data, batch_labels = train_data.next_batch()
feed_dict = {
    images_placeholder: batch_data,
    labels_placeholder: batch_labels,
    dropout_placeholder: dropout_keep_prob
}
_, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)

我可以添加如何冻结图形的代码,但这并不是必需的。使用 is_train=false 构建图,加载最新的检查点并在 LWF 上运行评估以重现问题就足​​够了。

更新...

我发现问题出在批量标准化层。将此层设置为 is_training=false 即可重现问题。

我找到这个后发现的参考资料:

http://ruishu.io/2016/12/27/batchnorm/

https://github.com/tensorflow/tensorflow/issues/10118

Batch Normalization - Tensorflow

一旦我测试了一个解决方案,就会更新解决方案。

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    所以我找到了解决方案。 主要使用这个参考:http://ruishu.io/2016/12/27/batchnorm/

    来自链接:

    注意:当 is_training 为 True 时,moving_mean 和 Moving_variance 需要更新,默认情况下 update_ops 放在 tf.GraphKeys.UPDATE_OPS 中,因此需要将它们作为依赖添加到 train_op,例如:

    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) if update_ops: updates = tf.group(*update_ops) total_loss = control_flow_ops.with_dependencies([updates], total_loss)

    直截了当, 而不是像这样创建优化器:

    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
    train_op = optimizer.minimize(total_loss, global_step=global_step)
    

    这样做:

    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    with tf.control_dependencies(update_ops):
        optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
        train_op = optimizer.minimize(total_loss, global_step=global_step)
    

    这将解决问题。

    【讨论】:

      【解决方案2】:

      is_training 不应该有这个效果。我需要查看更多代码以了解正在发生的事情,但是当您将 is_training 设置为 false 时,变量名称可能不匹配,这可能是因为变量范围重用问题。

      【讨论】:

        猜你喜欢
        • 2018-03-11
        • 2016-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-08
        • 2013-04-22
        • 1970-01-01
        相关资源
        最近更新 更多