简体   繁体   English

为什么我的 model 在第二个时期过拟合?

[英]Why is my model overfitting on the second epoch?

I'm a beginner in deep learning and I'm trying to train a deep learning model to classify different ASL hand signs using Mobilenet_v2 and Inception.我是深度学习的初学者,我正在尝试训练深度学习 model 以使用 Mobilenet_v2 和 Inception 对不同的 ASL 手势进行分类。

Here are my codes create an ImageDataGenerator for creating the training and validation set.这是我的代码创建一个 ImageDataGenerator 来创建训练和验证集。

# Reformat Images and Create Batches

IMAGE_RES = 224
BATCH_SIZE = 32

datagen = tf.keras.preprocessing.image.ImageDataGenerator(
    rescale=1./255,
    validation_split = 0.4
)

train_generator = datagen.flow_from_directory(
    base_dir,
    target_size = (IMAGE_RES,IMAGE_RES),
    batch_size = BATCH_SIZE,
    subset = 'training'
)

val_generator = datagen.flow_from_directory(
    base_dir,
    target_size= (IMAGE_RES, IMAGE_RES),
    batch_size = BATCH_SIZE,
    subset = 'validation'
)

Here are the codes to train the models:以下是训练模型的代码:

# Do transfer learning with Tensorflow Hub
URL = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
feature_extractor = hub.KerasLayer(URL,
                                   input_shape=(IMAGE_RES, IMAGE_RES, 3))
# Freeze pre-trained model
feature_extractor.trainable = False

# Attach a classification head
model = tf.keras.Sequential([
  feature_extractor,
  layers.Dense(5, activation='softmax')
])

model.summary()

# Train the model
model.compile(
  optimizer='adam',
  loss='categorical_crossentropy',
  metrics=['accuracy'])

EPOCHS = 5

history = model.fit(train_generator,
                    steps_per_epoch=len(train_generator),
                    epochs=EPOCHS,
                    validation_data = val_generator,
                     validation_steps=len(val_generator)
                    )

Epoch 1/5 94/94 [==============================] - 19s 199ms/step - loss: 0.7333 - accuracy: 0.7730 - val_loss: 0.6276 - val_accuracy: 0.7705纪元 1/5 94/94 [===============================] - 19 秒 199 毫秒/步 - 损失:0.7333 - 准确度:0.7730 - val_loss:0.6276 - val_accuracy:0.7705

Epoch 2/5 94/94 [==============================] - 18s 190ms/step - loss: 0.1574 - accuracy: 0.9893 - val_loss: 0.5118 - val_accuracy: 0.8145纪元 2/5 94/94 [===============================] - 18 秒 190 毫秒/步 - 损失:0.1574 - 准确度:0.9893 - val_loss:0.5118 - val_accuracy:0.8145

Epoch 3/5 94/94 [==============================] - 18s 191ms/step - loss: 0.0783 - accuracy: 0.9980 - val_loss: 0.4850 - val_accuracy: 0.8235纪元 3/5 94/94 [===============================] - 18 秒 191 毫秒/步 - 损失:0.0783 - 准确度:0.9980 - val_loss:0.4850 - val_accuracy:0.8235

Epoch 4/5 94/94 [==============================] - 18s 196ms/step - loss: 0.0492 - accuracy: 0.9997 - val_loss: 0.4541 - val_accuracy: 0.8395纪元 4/5 94/94 [===============================] - 18 秒 196 毫秒/步 - 损失:0.0492 - 准确度:0.9997 - val_loss:0.4541 - val_accuracy:0.8395

Epoch 5/5 94/94 [==============================] - 18s 193ms/step - loss: 0.0349 - accuracy: 0.9997 - val_loss: 0.4590 - val_accuracy: 0.8365纪元 5/5 94/94 [===============================] - 18 秒 193 毫秒/步 - 损失:0.0349 - 准确度:0.9997 - val_loss:0.4590 - val_accuracy:0.8365

I've tried using data augmentation but the model still overfits so I'm wondering if I've done something wrong in my code.我试过使用数据增强,但 model 仍然过拟合,所以我想知道我的代码是否做错了什么。

Your data is very small.您的数据非常小。 Try splitting with random seeds and check if the problem still persists.尝试用随机种子分裂并检查问题是否仍然存在。

If it does, then use regularizations and decrease the complexity of neural network.如果是,则使用正则化并降低神经网络的复杂性。

Also experiment with different optimizers and smaller learning rate (try lr scheduler)还可以尝试不同的优化器和更小的学习率(尝试 lr 调度器)

It seems like your dataset is very small with some true outputs separated only by a small distance of inputs in the input-output curve.看起来您的数据集非常小,一些真正的输出仅由输入输出曲线中的一小段输入分隔。 That is why it is fitting easily to those points.这就是为什么它很容易适应这些点。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM