python - Keras:模型过度拟合?

标签 python machine-learning keras deep-learning classification

我正在尝试使用 NIH(国家医学图书馆)的疟疾数据集创建一个二值图像分类模型,该数据集包含每个类别(感染/未感染)的大约 27,000 张图像。

似乎存在过度拟合,我尝试使用不同的批量大小、每个时期/验证步骤的步骤、使用不同的隐藏层并添加回调等。该图始终显示一条直线,要么急剧增加,要么急剧减少,而不是在学习过程中稳步增加并减少一些(根据我的理解,这应该是这样的)。下面是一个示例,大多数结果与此有些类似。

Example plot

我是深度学习的新手,我阅读了很多有关过度拟合的内容并试图找到解决方案。但我认为我一定做错了什么和/或误解了。如果有人能够发现一些看起来不正确的东西并能够为我指明正确的方向,我将不胜感激!

from keras.layers import MaxPooling2D, Conv2D, Flatten, Dense, Dropout
from keras_preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from keras.models import Sequential
import matplotlib.pyplot as plt
import constants as c
import numpy as np
import keras

# Clear session and instantiate model
keras.backend.clear_session()
model = Sequential()

# Load images & labels
cells = np.load(c.cells_path)
labels = np.load(c.labels_path)

# Shuffle the entire dataset
n = np.arange(cells.shape[0])
np.random.shuffle(n)

# Update numpy files with shuffled data
cells = cells[n]
labels = labels[n]

# Split the dataset into train/validation/test
train_x, test_x, train_y, test_y = train_test_split(cells, labels, test_size=1 - c.train_ratio, shuffle=False)
val_x, test_x, val_y, test_y = train_test_split(test_x, test_y, test_size=c.test_ratio / (c.test_ratio + c.val_ratio),
                                                shuffle=False)

# The amount of images in each set
print('Training data shape: ', train_x.shape)
print('Validation data shape: ', val_x.shape)
print('Testing data shape: ', test_x.shape)

# Neural network
model.add(Conv2D(32, (3, 3), input_shape=c.input_shape, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

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

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

model.add(Flatten())
model.add(Dense(units=64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=1, activation='sigmoid'))

# Compile the model
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

# Data augmentation
train_datagen = ImageDataGenerator(rescale=1. / 255,
                                   rotation_range=20,
                                   width_shift_range=0.05,
                                   height_shift_range=0.05,
                                   shear_range=0.05,
                                   zoom_range=0.05,
                                   horizontal_flip=True,
                                   fill_mode='nearest')

validation_datagen = ImageDataGenerator(rescale=1. / 255)
testing_datagen = ImageDataGenerator(rescale=1. / 255)

training_dataset = train_datagen.flow(train_x, train_y, batch_size=32)
validation_dataset = validation_datagen.flow(val_x, val_y, batch_size=32)
testing_dataset = validation_datagen.flow(val_x, val_y, batch_size=32)

# Add callbacks to prevent overfitting
es = EarlyStopping(monitor='accuracy',
                   min_delta=0,
                   patience=2,
                   verbose=0,
                   mode='max')

rlrop = ReduceLROnPlateau(monitor='val_loss',
                          factor=0.2,
                          patience=0.5,
                          min_lr=0.001)

checkpoint = ModelCheckpoint("Model.h5")

# Perform backpropagation and update weights in model
history = model.fit_generator(training_dataset,
                              epochs=50,
                              validation_data=validation_dataset,
                              callbacks=[es, checkpoint, rlrop])

# Save model & weights
model.save_weights("Model_weights.h5")
model.save("Model.h5")

# Plot accuracy graph
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()

最佳答案

似乎不是过度拟合的情况。无需过多查看,即可执行以下操作:

  1. 将第一层中的滤波器保留为 32,并在接下来的每个卷积层上逐渐加倍。

  2. 因为图像的变化并没有显着降低 Dropout 率。

奇怪的是,这是我第一次尝试 Tensorflow 2.0 时构建的东西,你可以查看 here .

关于python - Keras:模型过度拟合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59931473/

相关文章:

python - 模块已安装但无法导入

python - 使用python : hmm learning使用kinect进行手势识别

matlab - 清晰可分离数据的机会级别精度

tensorflow - keras.utils.Sequence 与多个文件

python - 可迭代的连续切片

Python 代码作为参数

python - keras - 嵌入层 mask_zero 导致后续层出现异常

python - Keras max_pool3d 得到了预期的关键字参数 'data_format'

python - 如何在Python中使用模板库

python - Keras 的多维输入