重新训练最后一层Inception-ResNet-v2

Pri*_*ham 9 python deep-learning tensorflow

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

  1. 获取最后一层中的变量名称
  2. 创建一个train_op以尽量减少这些变量损失
  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
Run Code Online (Sandbox Code Playgroud)

这似乎不起作用(训练损失,错误从初始值不会有太大改善).有没有更好/更优雅的方式来做到这一点?如果你能告诉我这里出了什么问题,那对我来说也不错.

jor*_*emf 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)
    
    Run Code Online (Sandbox Code Playgroud)
  • 您应该将正则化变量添加到损失中(或至少是最后一层的损失):

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

  • 仔细检查 train_op 在丢失后运行:

    with ops.name_scope('train_op'):
        train_op = control_flow_ops.with_dependencies([train_op], total_loss)
    
    Run Code Online (Sandbox Code Playgroud)