[英]How can I use data augmentation in keras and how do I prevent overfitting on the mnist dataset?
我想在 mnist 数据集上训练一个 keras 神经网络。 问题是我的模型在 1 或 2 个时期后已经过拟合了。 为了解决这个问题,我想使用数据增强:
首先我加载数据:
#load mnist dataset
(tr_images, tr_labels), (test_images, test_labels) = mnist.load_data()
#normalize images
tr_images, test_images = preprocess(tr_images, test_images)
#function which returns the amount of train images, test images and classes
amount_train_images, amount_test_images, total_classes = get_data_information(tr_images, tr_labels, test_images, test_labels)
#convert labels into the respective vectors
tr_vector_labels = keras.utils.to_categorical(tr_labels, total_classes)
test_vector_labels = keras.utils.to_categorical(test_labels, total_classes)
我用“create_model”函数创建了一个模型:
untrained_model = create_model()
这是函数定义:
def create_model(_learning_rate=0.01, _momentum=0.9, _decay=0.001, _dense_neurons=128, _fully_connected_layers=3, _loss="sparse_categorical_crossentropy", _dropout=0.1):
#create model
model = keras.Sequential()
#input
model.add(Flatten(input_shape=(28, 28)))
#add fully connected layers
for i in range(_fully_connected_layers):
model.add(Dense(_dense_neurons, activation='relu'))
model.add(Dropout(_dropout))
#classifier
model.add(Dense(total_classes, activation='sigmoid'))
optimizer = keras.optimizers.SGD(
learning_rate=_learning_rate,
momentum=_momentum,
decay=_decay
)
#compile
model.compile(
optimizer=optimizer,
loss=_loss,
metrics=['accuracy']
)
return model
该函数返回一个编译但未经训练的模型。 当我尝试优化超参数(因此有很多参数)时,我也会使用这个函数。 然后我创建一个 ImagaDataGenerator:
generator = tf.keras.preprocessing.image.ImageDataGenerator(
rotation_range=0.15,
width_shift_range=0.15,
height_shift_range=0.15,
zoom_range=0.15
)
现在我想用我的 train_model_with_data_augmentation 函数训练模型:
train_model_with_data_augmentation(
tr_images=tr_images,
tr_labels=tr_labels,
test_images=test_images,
test_labels=test_labels,
model=untrained_model,
generator=generator,
hyperparameters=hyperparameters
)
但是,我不知道如何将这个生成器用于我创建的模型,因为我发现的唯一方法是生成器的 fit 方法,但我想训练我的模型而不是生成器。
这是我从训练历史中得到的图表: https : //ibb.co/sKFnwGr
更新:我尝试使用此代码:
generator.fit(x_train)
model.fit(generator.flow(x_train, y_train, batch_size=32), steps_per_epoch=len(x_train)/32, epochs=epochs)
但是,我收到此错误消息:ValueError:“.fit() 的输入应该具有 4 级。得到形状为 (60000, 28, 28) 的数组”
我相信 fit 方法的输入矩阵应该包含图像索引、高度、宽度、深度,所以它应该有 4 个维度,而我的 x_train 数组只有 3 个维度,并且没有关于图像深度的任何维度。 我试图扩展它:
x_train = x_train[..., np.newaxis]
y_train = y_train[..., np.newaxis]
但随后我收到此错误消息:“完成 GeneratorDataset 迭代器时发生错误:前提条件失败:Python 解释器状态未初始化。该过程可能会终止。”
可以在此处找到使用 ImageDataGenerator 的工作示例。 例子本身:
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
y_train = np_utils.to_categorical(y_train, num_classes)
y_test = np_utils.to_categorical(y_test, num_classes)
datagen = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True)
# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(x_train)
# fits the model on batches with real-time data augmentation:
model.fit(datagen.flow(x_train, y_train, batch_size=32),
steps_per_epoch=len(x_train) / 32, epochs=epochs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.