【问题标题】:Why is my val_loss starting low and increasing does it even matters with transfer learning?为什么我的 val_loss 一开始就很低并且增加,这对迁移学习有影响吗?
【发布时间】:2021-06-07 16:20:21
【问题描述】:

大家好,我是机器学习新手,我正在从 https://machinelearningmastery.com/how-to-develop-a-convolutional-neural-network-to-classify-photos-of-dogs-and-cats/ 运行这段代码

我想了解为什么我的 val_loss 一开始很低,然后又增加。这是过拟合还是欠拟合?我还能用什么来改善 val_loss 使其更适合?在博客文章中,他的交叉熵图与我的有很大不同。

def define_model():
    # load model
    model = VGG16(include_top=False, input_shape=(224, 224, 3))
    # mark loaded layers as not trainable
    for layer in model.layers:
        layer.trainable = False
    # add new classifier layers
    flat1 = Flatten()(model.layers[-1].output)
    class1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)
    output = Dense(1, activation='sigmoid')(class1)
    # define new model
    model = Model(inputs=model.inputs, outputs=output)
    # compile model
    opt = SGD(lr=0.001, momentum=0.9)
    model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])
    return model


# plot diagnostic learning curves
def summarize_diagnostics(history):
    # plot loss
    pyplot.subplot(211)
    pyplot.title('Cross Entropy Loss')
    pyplot.plot(history.history['loss'], color='blue', label='train')
    pyplot.plot(history.history['val_loss'], color='orange', label='test')
    # plot accuracy
    pyplot.subplot(212)
    pyplot.title('Classification Accuracy')
    pyplot.plot(history.history['accuracy'], color='blue', label='train')
    pyplot.plot(history.history['val_accuracy'], color='orange', label='test')
    # save plot to file
    filename = sys.argv[0].split('/')[-1]
    pyplot.savefig(filename + '_plot.png')
    pyplot.close()


# run the test harness for evaluating a model
def run_test_harness():
    # define model
    model = define_model()
    # create data generator
    datagen = ImageDataGenerator(featurewise_center=True)
    # specify imagenet mean values for centering
    datagen.mean = [123.68, 116.779, 103.939]
    # prepare iterator
    train_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',
                                           class_mode='binary', batch_size=64, target_size=(224, 224))
    test_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',
                                          class_mode='binary', batch_size=64, target_size=(224, 224))
    # fit model

   
    history = model.fit_generator(train_it, steps_per_epoch=len(train_it),
                                  validation_data=test_it, validation_steps=len(test_it), epochs=10, verbose=1)
    # evaluate model
    _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=1)
    print('> %.3f' % (acc * 100.0))
    # learning curves
    summarize_diagnostics(history)
    model.save('Transfer_Learning_Model.h5')


# entry point, run the test harness
run_test_harness()

【问题讨论】:

    标签: tensorflow keras deep-learning transfer-learning pre-trained-model


    【解决方案1】:

    许多问题。 VGG 模型使用从 -1 到 +1 的像素值进行训练,因此您需要添加以下内容

     def scaler(x):
            y=x/127.5-1
            return y
    datagen = ImageDataGenerator(preprocessing_function=scaler)
    

    我会删除 featurewise_center=True。如果你使用它,你必须在火车上跑步。 在 model.fit 中有 steps_per_epoch=len(train_it) 和 validation_steps=len(test_it)。 由于您在生成器中将 batch_size 设置为 64,因此这些值应该是 steps_per_epoch=len(train_it.labels)//64 和 validaion_steps=len(test_it)/64。实际上在 model.fit 中忽略了这些参数。 Model.fit 将在内部计算正确的值。您的验证损失曲线表明存在一定程度的过度拟合。在 class1 层之后添加一个 drop out 层。将辍学率设置为类似 2。 如果您想获得最高的准确度,我建议您合并两个 Keras 回调。 EarlyStopping 回调监控验证损失,如果在“耐心”连续 epoch 数之后损失未能减少,则停止训练。设置 restore_best_weights=True 将加载具有最低验证损失的时期的权重,因此您不必保存然后重新加载权重。将 epochs 设置为较大的数字以确保激活此回调。使用 keras 回调 ReduceLROnPlateau 根据验证损失自动调整学习率。回调的文档位于here. 我使用的代码如下所示

    es=tf.keras.callbacks.EarlyStopping( monitor="val_loss", patience=3,
                                         verbose=1,  restore_best_weights=True)
    rlronp=tf.keras.callbacks.ReduceLROnPlateau( monitor="val_loss", factor=0.5, patience=1,
                                                 verbose=1)
    callbacks=[es, rlronp]
    

    在 model.fit 中添加 callbacks=callbacks

    【讨论】:

      猜你喜欢
      • 2019-10-19
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      • 2018-06-29
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多