keras
中提供图片生成器 ImageDataGenerator
, 通过设定不同的参数,来生成更多的数据从而达到小样本训练优质模型的能力。
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
rotation_range=40, # 旋转范围
width_shift_range=0.2, # 宽度调整范围
height_shift_range=0.2, # 高度调整范围
rescale=1./255, # 尺度调整范围
shear_range=0.2, # 弯曲调整范围
zoom_range=0.2, # 缩放调整范围
horizontal_flip=True, # 水平调整范围
brightness_range=0.3, # 亮度调整范围
featurewise_center=True, # 是否特征居中
featurewise_std_normalization=True, # 特征是否归一化
zca_whitening=True, # 是否使用 ZCA白化
fill_mode='nearest') # 填充模式(图片大小不够时)
使用方法
- 数据对象
对象列表直接传入到 fit
函数中进行 ZCA 等预处理,然后调用 flow
函数来生成样本
(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_generator(datagen.flow(x_train, y_train, batch_size=32),
steps_per_epoch=len(x_train) / 32, epochs=epochs)
# here's a more "manual" example
for e in range(epochs):
print('Epoch', e)
batches = 0
for x_batch, y_batch in datagen.flow(x_train, y_train, batch_size=32):
model.fit(x_batch, y_batch)
batches += 1
if batches >= len(x_train) / 32:
# we need to break the loop by hand because
# the generator loops indefinitely
break
- 文件夹
同对象操作类似,这里只是将 flow
函数转化为 flow_from_directory
函数来完成相应的样本生成。
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
'data/train',
target_size=(150, 150),
batch_size=32,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
'data/validation',
target_size=(150, 150),
batch_size=32,
class_mode='binary')
model.fit_generator(
train_generator,
steps_per_epoch=2000,
epochs=50,
validation_data=validation_generator,
validation_steps=800)
参考
Building powerful image classification models using very little data
Image Preprocessing