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

时间:2016-12-31 09:34:28

标签: python tensorflow deep-learning

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

  1. 获取最终图层中的变量名称
  2. 创建train_op以最小化这些损失
  3. 的变量
  4. 恢复除最终图层以外的整个图形,同时仅随机初始化最后一层。
  5. 我实施如下:

    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
    

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

1 个答案:

答案 0 :(得分:1)

有几件事:

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

    npm link
  • 你应该将正则化变量添加到损失中(或者至少是最后一层的变量):

    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)
    
  • 仅培训完整的连接层可能不是一个好主意,我会训练所有网络,因为您的课程所需的功能不一定在最后一层中定义,但之前几层需要改变它们。

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

    regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
    all_losses = [loss] + regularization_losses
    total_loss = tf.add_n(all_losses, name='total_loss')
    
相关问题