基于ImageDataGenerator实现数据增强
扩充数据集大小,增强模型的泛化能力。比如进行旋转、变形、归一化等。
扩充数据量:对图像作简单的预处理(如缩放,改变像素值范围);
随机打乱图像顺序,并且在图像集上无限循环(不会出现数据用完的情况);
对图像加入扰动,大大增大数据量,避免多次输入相同的训练图像产生过拟合。
优化训练效率:训练神经网络时经常需要将数据分成小的批次(例如每16张图像作为一个batch提供给神经网络),在ImageDataGenerator中,只需要简单提供一个参数 batch_size = 16。
类AlexNet代码
n_channels = 3
input_shape = (*image_size, n_channels)
input_layer = Input(input_shape)
z = input_layer
z = Conv2D(64, (3, 3))(z)
z = BatchNormalization()(z)
z = Activation('relu')(z)
z = MaxPooling2D(pool_size=(2, 2))(z)
z = Conv2D(64, (3, 3))(z)
z = BatchNormalization()(z)
z = Activation('relu')(z)
z = MaxPooling2D(pool_size=(2, 2))(z)
z = Conv2D(128, (3, 3))(z)
z = BatchNormalization()(z)
z = Activation('relu')(z)
z = MaxPooling2D(pool_size=(2, 2))(z)
z = Conv2D(128, (3, 3))(z)
z = BatchNormalization()(z)
z = Activation('relu')(z)
z = MaxPooling2D(pool_size=(2, 2))(z)
z = Flatten()(z) # 将特征变成一维向量
z = Dense(64)(z)
z = BatchNormalization()(z)
z = Activation('relu')(z)
z = Dropout(0.5)(z)
z = Dense(1)(z)
z = Activation('sigmoid')(z)