gpt4 book ai didi

python - CNN 过度拟合(附输出和代码)

转载 作者:太空宇宙 更新时间:2023-11-03 14:50:38 25 4
gpt4 key购买 nike

我有一个数据集,其中包含我要分类的 2 个类别的 20000 张黑白图像(这些图像有点像天气预报或股市图表,所以我不能使用预训练网络)。数据集已被拆分为 18000 张图像 训练和 2000 张图像用于测试目的。我正在使用 Keras 卷积神经网络对其进行训练。我有 96% 的准确率 使用训练集但使用测试集获得的结果并不好(在 50 个 epoch 后卡在 82-83%)。我认为这可能是因为过度拟合。请你能给我一些建议来解决这个问题吗?我把最终的输出和代码留给你看。

281/281 [==============================] - 132s - 损失:0.1024 - 加速:0.9612 - val_loss : 0.5836 - val_acc: 0.8210

from keras.layers.normalization import BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
from keras.optimizers import SGD, RMSprop, Adam
from keras.callbacks import ModelCheckpoint


filepath="weights/weights-improvement-{epoch:02d}-{val_acc:.4f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]


img_width, img_height = 100, 1296

train_data_dir = 'dataset/train'
validation_data_dir = 'dataset/validation'
nb_train_samples = 18000
nb_validation_samples = 2000
epochs = 100
batch_size = 64

input_shape = (img_width, img_height, 1)

model = Sequential()

model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.1))

model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(128, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.3))

model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(256, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.4))

model.add(ZeroPadding2D((1, 1)))
model.add(Conv2D(512, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))

model.add(Flatten())

model.add(Dense(256))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))

model.add(Dense(256))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))

model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
optimizer=Adam(lr=0.001),
metrics=['accuracy'])

model.save('model.h5')

train_datagen = ImageDataGenerator(
rescale=None,
)

test_datagen = ImageDataGenerator(rescale=None)

train_generator = train_datagen.flow_from_directory(
train_data_dir, color_mode='grayscale',
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
validation_data_dir, color_mode='grayscale',
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')

model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size,callbacks=callbacks_list)

最佳答案

由于几个大的卷积层和两个大的全连接层,您的网络中有大量参数。这可能会导致过度拟合。我建议减小它们的尺寸,并可能完全去除其中的几个层。

关于python - CNN 过度拟合(附输出和代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46102209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com