我认为我的模型过度拟合?有什么建议吗?

问题描述 投票:0回答:1

所以我使用的是来自kaggle的deepfake检测数据集,我的模型似乎过度拟合,大约有2000张图像,其中1k用于“真实”类,1k用于“假”类。所有图像都是面孔。我使用的是 VGG16,因为它众所周知适合深度伪造面部检测。

以下是过去 10 个 epoch 的结果:

Found 1632 images belonging to 2 classes.
Found 204 images belonging to 2 classes.
Found 205 images belonging to 2 classes.
Epoch 1/10
51/51 [==============================] - 1234s 24s/step - loss: 0.1841 - accuracy: 0.9577 - val_loss: 0.7258 - val_accuracy: 0.6719
Epoch 2/10
51/51 [==============================] - 1168s 23s/step - loss: 0.1397 - accuracy: 0.9712 - val_loss: 0.7331 - val_accuracy: 0.6406
Epoch 3/10
51/51 [==============================] - 1186s 23s/step - loss: 0.1215 - accuracy: 0.9743 - val_loss: 0.7938 - val_accuracy: 0.6719
Epoch 4/10
51/51 [==============================] - 1187s 23s/step - loss: 0.0914 - accuracy: 0.9884 - val_loss: 0.8304 - val_accuracy: 0.6615
Epoch 5/10
51/51 [==============================] - 1188s 23s/step - loss: 0.0705 - accuracy: 0.9939 - val_loss: 0.9022 - val_accuracy: 0.6562
Epoch 6/10
51/51 [==============================] - 1155s 23s/step - loss: 0.0683 - accuracy: 0.9896 - val_loss: 0.9072 - val_accuracy: 0.6354
Epoch 7/10
51/51 [==============================] - 1154s 23s/step - loss: 0.0541 - accuracy: 0.9951 - val_loss: 0.8924 - val_accuracy: 0.6719
Epoch 8/10
51/51 [==============================] - 1175s 23s/step - loss: 0.0403 - accuracy: 0.9975 - val_loss: 0.9353 - val_accuracy: 0.6510
Epoch 9/10
51/51 [==============================] - 1189s 23s/step - loss: 0.0420 - accuracy: 0.9969 - val_loss: 1.0692 - val_accuracy: 0.6198
Epoch 10/10
51/51 [==============================] - 1185s 23s/step - loss: 0.0288 - accuracy: 0.9988 - val_loss: 1.0250 - val_accuracy: 0.6510
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
  saving_api.save_model(
6/6 [==============================] - 135s 21s/step - loss: 1.1160 - accuracy: 0.6042
Test accuracy: 0.6041666865348816 t hese rae the results

如果有人想知道,这里是最后 10 个 epoch 的代码:

from google.colab import drive
import os
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import load_model

train_dir = '/content/drive/MyDrive/dataset/train'
val_dir = '/content/drive/MyDrive/dataset/val'
test_dir = '/content/drive/MyDrive/dataset/test'

#parameters
batch_size = 32
epochs = 5
image_shape = (224, 224)

saved_model_path = '/content/drive/MyDrive/dataset/trained_model_updated.h5'
model = load_model(saved_model_path)

#preprocessing
train_datagen = ImageDataGenerator(rescale=1./255)
val_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size=image_shape,
    batch_size=batch_size,
    class_mode='binary'
)

val_generator = val_datagen.flow_from_directory(
    val_dir,
    target_size=image_shape,
    batch_size=batch_size,
    class_mode='binary'
)

test_generator = test_datagen.flow_from_directory(
    test_dir,
    target_size=image_shape,
    batch_size=batch_size,
    class_mode='binary'
)


model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])


history = model.fit(
    train_generator,
    steps_per_epoch=train_generator.samples // batch_size,
    epochs=epochs,
    validation_data=val_generator,
    validation_steps=val_generator.samples // batch_size
)


model.save('/content/drive/MyDrive/dataset/trained_model_updated.h5')

test_loss, test_acc = model.evaluate(test_generator, steps=test_generator.samples // batch_size)
print(f'Test accuracy: {test_acc}')

我运行了模型大约 20 个 epoch,并不断更改参数,应用数据增强。尽管该模型对训练数据产生了良好的准确性,但对验证和测试集而言仍停滞在 67% 左右。

在前 5 个 epoch 中,模型的验证准确率确实从 53% 提高到 67%,但在最后 15 个 epoch 中仍然停滞不前。

为了确认,我还尝试使用互联网上的一些图像,但它没有正确识别这些图像,并且会错误地将一些假图像分类为“真实”。

我很确定它的过度拟合是正确的?

我没有计算资源来训练更大的数据集,除了增加数据集大小之外还有其他解决方案吗?任何建议,将不胜感激。谢谢!

machine-learning computer-vision artificial-intelligence face-detection ml
1个回答
0
投票

您可以看到您的训练损失减少了,而验证损失同时增加了。这确实是过拟合了。您可以采取多种措施来减少过度拟合:

  1. 使用更多不同类型的数据增强(正如您所提到的)
  2. 使用正则化
  3. 提高学习率
  4. 更改优化器及其参数。
© www.soinside.com 2019 - 2024. All rights reserved.