简体   繁体   English

为什么在 CNN 迁移学习期间,我的损失和准确率会随着每个 epoch 不断上升和下降?

[英]Why does my loss and accuracy keep going up and down with each epoch during CNN transfer learning?

I am trying to use transfer learning (Densenet121) for a binary classification problem, but my accuracy and loss keep going up and down for each epoch.我正在尝试将迁移学习(Densenet121)用于二进制分类问题,但我的准确性和损失在每个时期都在不断上升和下降。 Is this do to low sample size (87 images) or is there something wrong with my code?这是对低样本量(87 张图像)造成的影响,还是我的代码有问题? I am new to all of this..我对这一切都很陌生..

DataGen数据生成器

image_size = (224,224)
batch_size = 128

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    img_dir,
    validation_split = 0.2,
    subset = "training",
    seed = 37,
    image_size = image_size,
    batch_size = batch_size,
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    img_dir,
    validation_split = 0.2,
    subset = "validation",
    seed = 37,
    image_size = image_size,
    batch_size = batch_size,
)

DataAug数据八月

data_augmentation = keras.Sequential(
    [
        layers.RandomRotation(factor=0.15),
        layers.RandomTranslation(height_factor=0.1, width_factor=0.1),
        layers.RandomFlip(),
        layers.RandomContrast(factor=0.1),
    ]
)

Model Model

inputs = keras.Input(shape = (224,224,3))
x = data_augmentation(inputs)
x = tf.keras.applications.densenet.preprocess_input(x)

base_model = tf.keras.applications.DenseNet121(
    include_top=False,
    input_shape = (224,224,3),
    input_tensor = x,
    weights="imagenet",
)
base_model.trainable = False
x = layers.GlobalAveragePooling2D(name = 'avg_pool')(base_model.output)
prediction = layers.Dense(1,activation='sigmoid')(x)

new_model = (keras.Model(inputs=inputs, outputs = prediction))

Compile and Fit编译和拟合

new_model.compile(
    optimizer = keras.optimizers.Adam(1e-2),
    loss = keras.losses.BinaryCrossentropy(),
    metrics = [keras.metrics.BinaryAccuracy()],
)
epochs = 50
history = new_model.fit(
    train_ds,
    epochs = epochs,
    validation_data = val_ds,
    shuffle = False,
    batch_size = 8
)

Results结果

在此处输入图像描述

在此处输入图像描述

When using data augmentation the metrics (loss, accuracy) tend to be noiser since echo forward pass is done with a completely different sample.当使用数据增强时,指标(损失、准确度)往往会更加嘈杂,因为回波前向传递是使用完全不同的样本完成的。

You can reduce this noise by changing your data augmentation params, but it may hurt in performance.您可以通过更改数据增强参数来减少这种噪音,但它可能会损害性能。

That said, make sure that your examples are still valid after data augmentation is done:也就是说,确保您的示例在数据扩充完成后仍然有效:

  • pick up several examples and manually apply the worst case transformation举几个例子并手动应用最坏情况转换
  • pick up several examples and apply random transformations举几个例子并应用随机变换

Visualizing those scenarios can help you understand if your params are ok.可视化这些场景可以帮助您了解您的参数是否正常。 Remember that when applying data augmentation, labels must be preserved after the transformation.请记住,在应用数据增强时,必须在转换后保留标签。

Another point to consider is the usage of Adam as optimizer.要考虑的另一点是使用 Adam 作为优化器。 May be you need to switch to a slower one, like SGD, or at least, reduce the learning rate.可能你需要切换到较慢的,比如 SGD,或者至少降低学习率。 Maybe you can use keras AutoTuner to validate those.也许您可以使用 keras AutoTuner来验证这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的损失趋于下降,而我的准确度却为零? - Why is my loss trending down while my accuracy is going to zero? 在 CNN 中训练每个 epoch 期间,验证准确度很高,但分类报告中的最终准确度非常低,这是什么意思? - validation accuracy is high during training each epoch in a CNN but final accuracy in classification report very low what does it mean? 为什么我的自动编码器的损失在训练期间根本没有下降? - Why is the loss of my autoencoder not going down at all during training? 验证准确性/损失随着每个连续的时期线性上升和下降 - Validation accuracy/loss goes up and down linearly with every consecutive epoch 为什么我的 CNN 的准确率/损失在训练期间没有变化? - Why doesn't my CNN's accuracy/loss change during training? 为什么我的 val_loss 下降但我的 val_accuracy 停滞不前 - Why is my val_loss going down but my val_accuracy stagnates 为什么我的损失函数随着每个 epoch 增加? - Why is my loss function increasing with each epoch? 为什么我的 Tensorflow CNN 的准确度为零而损失不是? - why my Tensorflow CNN's accuracy is zero while loss is not? 为什么在训练我的网络时CrossEntropy损失不减少? - Why does the CrossEntropy loss not go down during training of my network? 为什么我的 CNN 指标不随每个 epoch 变化? - Why are my metrics of my CNN not changing with each epoch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM