在CNN模型中减少验证损失

3
import tensorflow as tf
import tensorflow.keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
import pickle
import numpy as np
from keras.models import model_from_json
from keras.models import load_model
import matplotlib.pyplot as plt

# Opening the files about data
X = pickle.load(open("X.pickle", "rb"))
y = pickle.load(open("y.pickle", "rb"))

# normalizing data (a pixel goes from 0 to 255)
X = X/255.0

# Building the model
model = Sequential()
# 3 convolutional layers
model.add(Conv2D(32, (3, 3), input_shape = X.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))


model.add(Conv2D(64, (3, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.9))

# 5 hidden layers
model.add(Flatten())

model.add(Dense(128))
model.add(Activation("relu"))

model.add(Dense(128))
model.add(Activation("relu"))

model.add(Dense(128))
model.add(Activation("relu"))

model.add(Dense(128))
model.add(Activation("relu"))

model.add(Dense(128))
model.add(Activation("relu"))

# The output layer with 7 neurons, for 7 classes
model.add(Dense(13))
model.add(Activation("softmax"))

# Compiling the model using some basic parameters
model.compile(loss="sparse_categorical_crossentropy",
                optimizer="adam",
                metrics=["accuracy"])

# Training the model, with 40 iterations
# validation_split corresponds to the percentage of images used for the validation phase compared to all the images

print("X = " + str(len(X)))
print("y = " + str(len(y)))

history = model.fit(X, y, batch_size=32, epochs=1000, validation_split=0.1)

# Saving the model
model_json = model.to_json()
with open("model.json", "w") as json_file :
    json_file.write(model_json)

model.save_weights("model.h5")

print("Saved model to disk")

model.save('CNN.model')

# Printing a graph showing the accuracy changes during the training phase
print(history.history.keys())

plt.show()

plt.plot(history.history['accuracy'])

plt.plot(history.history['loss'])

plt.title('model accuracy')

plt.ylabel('accuracy')

plt.xlabel('epoch')

plt.legend(['train', 'validation'], loc='upper left')

plt.show()

问题在于,我的训练损失较低,但验证准确率非常高。而验证的准确性也极低。我该如何解决这个问题?我尝试将丢弃值增加到0.9,但损失仍然很高。我还尝试使用线性函数进行激活,但无济于事。
请帮忙。

Loss of model looks like this


1
这个问题太宽泛和不清晰了,无法给出具体和好的建议。我们需要例如关于您的数据集的信息。学习曲线是怎样的?您能分享一下训练和验证损失的图吗? - Felix Kleine Bösing
1
我同意@FelixKleineBösing所说的话,而且我还要补充一句,这可能甚至与主题无关。 - AMC
@FelixKleineBösing 我正在使用一个自定义数据集,其中包含各种作物图像,每个文件夹中有50张图片。我关注的作物总共有7个类别。 - Yogeshwar Shendye
@ChinmayShendye 我们需要绘制损失函数的图表,而不仅仅是准确率。在某些情况下,特别是在多类分类中,损失可能会降低,而准确率也会降低。 - Felix Kleine Bösing
@FelixKleineBösing 很抱歉回复晚了。我正在编辑问题,加入正确的模型准确度和损失图表。 - Yogeshwar Shendye
显示剩余3条评论
2个回答

2
正如已经提到的,没有看到数据很难给出好的建议。
我会尝试以下操作: - 在maxpooling层后移除Dropout - 移除一些dense层 - 在dense层之间添加dropout
如果仍然存在过拟合问题,则在dense层之间添加dropout。
编辑: 在查看了损失和准确度图后,我建议以下操作: 1. 最高优先级是获取更多的数据。 2. 然后使用数据增强来增加您的数据集。 3. 如果额外的数据无法帮助,请进一步降低神经网络的复杂性(但我认为训练会随着更多的数据而变慢,并且验证损失也会在更长的时期内下降)。

获取更多的数据在这种情况下对我有所帮助!! - Yogeshwar Shendye
@ChinmayShendye 很好 :) - Felix Kleine Bösing
@ChinmayShendye 如果将来有类似的问题,请在这里提问:https://stats.stackexchange.com - Felix Kleine Bösing
我可以请求您指导我如何为上述模型实现权重衰减吗? - Japesh Methuku
当然可以。请创建一个新的问题,我会帮助你。 - Felix Kleine Bösing
非常感谢。这是链接 https://datascience.stackexchange.com/questions/77604/weight-decay-in-resnet50 我正在实施基于微调的迁移学习方法,其中我解冻了第165到174层。如果您需要进一步澄清,请告诉我。 - Japesh Methuku

1

数据增强是减少过拟合的最佳技术。尝试使用数据生成器来训练和验证集,以减少损失并提高准确性。

如果想了解更多关于增强和可用的转换,请查看https://github.com/keras-team/keras-preprocessing

# Add our data-augmentation parameters to ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255.,
                               rotation_range = 40,
                               width_shift_range = 0.2,
                               height_shift_range = 0.2,
                               shear_range = 0.2,
                               zoom_range = 0.2,
                               horizontal_flip = True)

# Note that the validation data should not be augmented!
test_datagen = ImageDataGenerator(rescale = 1./255.)

# Flow training images in batches of 20 using train_datagen generator
train_generator = train_datagen.flow_from_directory(train_dir,
                                                batch_size = 20,
                                                class_mode = 'binary', 
                                                target_size = (150, 150))     

# Flow validation images in batches of 20 using test_datagen generator
validation_generator =  test_datagen.flow_from_directory(validation_dir,
                                                      batch_size  = 20,
                                                      class_mode  = 'binary', 
                                                      target_size = (150, 150)) 

# Now fit the training, validation generators to the CNN model
history = model.fit_generator(train_generator,
        validation_data = validation_generator,
        steps_per_epoch = 100,
        epochs = 3,
        validation_steps = 50,
        verbose = 2,callbacks=[callbacks])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接