【问题标题】:Retraining the last layer of Inception-ResNet-v2重新训练最后一层 Inception-ResNet-v2
【发布时间】:2017-05-15 09:32:20
【问题描述】:

我正在尝试重新训练 inception-resnet-v2 的最后一层。这是我想出的:

  1. 获取最后一层的变量名称
  2. 创建一个train_op 以最小化这些变量 wrt 损失
  3. 恢复除最后一层外的整个图形,同时仅随机初始化最后一层。

我实现如下:

with slim.arg_scope(arg_scope):
    logits = model(images_ph, is_training=True, reuse=None)
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels_ph))
accuracy = tf.contrib.metrics.accuracy(tf.argmax(logits, 1), labels_ph)

train_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'InceptionResnetV2/Logits')
optimizer = tf.train.AdamOptimizer(learning_rate=FLAGS.learning_rate)

train_op = optimizer.minimize(loss, var_list=train_list)

# restore all variables whose names doesn't contain 'logits'
restore_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='^((?!Logits).)*$')

saver = tf.train.Saver(restore_list, write_version=tf.train.SaverDef.V2)

with tf.Session() as session:


    init_op = tf.group(tf.local_variables_initializer(), tf.global_variables_initializer())

    session.run(init_op)
    saver.restore(session, '../models/inception_resnet_v2_2016_08_30.ckpt')


# followed by code for running train_op

这似乎不起作用(训练损失、错误与初始值相比并没有太大改善)。有没有更好/优雅的方法来做到这一点?如果你也能告诉我这里出了什么问题,那对我来说是很好的学习。

【问题讨论】:

  • 我不确定您如何命名变量,但您可以通过print train_list 验证 train_list 是否正确。也许this 可以帮助你,你可能已经看到了。

标签: python tensorflow deep-learning


【解决方案1】:

有几件事:

  • 学习率如何?太高的价值会搞砸一切(可能不是原因)
  • 尝试使用随机梯度下降,应该会少一些问题
  • 范围设置是否正确?如果你不使用梯度的 L2 正则化和批量归一化,你可能很快就会陷入局部最小值并且网络无法学习

    from nets import inception_resnet_v2 as net
    with net.inception_resnet_v2_arg_scope():
        logits, end_points = net.inception_resnet_v2(images_ph, num_classes=num_classes,
                                                     is_training=True)
    
  • 您应该将正则化变量添加到损失(或至少是最后一层的):

    regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
    all_losses = [loss] + regularization_losses
    total_loss = tf.add_n(all_losses, name='total_loss')
    
  • 只训练全连接层可能不是一个好主意,我会训练所有网络,因为您的课程所需的功能不一定在最后一层定义,而是之前的几层,您需要改变它们。

  • 再次检查丢失后的 train_op 运行情况:

    with ops.name_scope('train_op'):
        train_op = control_flow_ops.with_dependencies([train_op], total_loss)
    

【讨论】:

    猜你喜欢
    • 2019-02-21
    • 2022-01-23
    • 2018-07-31
    • 2018-08-02
    • 2018-11-08
    • 2018-02-07
    • 2017-03-28
    • 2017-02-06
    • 2017-11-26
    相关资源
    最近更新 更多