[英]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:也就是说,确保您的示例在数据扩充完成后仍然有效:
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.