keras.preprocessing.image.ImageDataGenerator(featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, zca_epsilon=1e-6, rotation_range=0., width_shift_range=0., height_shift_range=0., shear_range=0., zoom_range=0., channel_shift_range=0., fill_mode='nearest', cval=0., horizontal_flip=False, vertical_flip=False, rescale=None, preprocessing_function=None, data_format=K.image_data_format())
用以生成一个batch的图像数据,支持实时数据提高。训练时该函数无限生成数据,知道达到规定的epoch次数为止。git
~/.keras/keras.json
中设置的值,若从未设置过,则为“channel_last”featurewise_center
,featurewise_std_normalization
或zca_whitening
时须要此函数。X:numpy array,样本数据,秩应为4.在黑白图像的状况下channel轴的值为1,在彩色图像状况下值为3github
augment:布尔值,肯定是否使用随即提高过的数据json
round:若设augment=True
,肯定要在数据上进行多少轮数据提高,默认值为1数组
seed: 整数,随机数种子app
x:样本数据,秩应为4.在黑白图像的状况下channel轴的值为1,在彩色图像状况下值为3ide
y:标签函数
batch_size:整数,默认32oop
shuffle:布尔值,是否随机打乱数据,默认为Trueui
save_to_dir:None或字符串,该参数能让你将提高后的图片保存起来,用以可视化编码
save_prefix:字符串,保存提高后图片时使用的前缀, 仅当设置了save_to_dir
时生效
save_format:"png"或"jpeg"之一,指定保存图片的数据格式,默认"jpeg"
yields:形如(x,y)的tuple,x是表明图像数据的numpy数组.y是表明标签的numpy数组.该迭代器无限循环.
seed: 整数,随机数种子
directory
下的子文件夹名称/结构自动推断。每个子文件夹都会被认为是一个新的类。(类别的顺序将按照字母表顺序映射到标签值)。经过属性class_indices
可得到文件夹名与类的序号的对应字典。model.predict_generator()
和model.evaluate_generator()
等函数时会用到.save_to_dir
时生效使用.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), 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): loss = model.train(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_from_directory(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) 同时变换图像和mask # we create two instances with the same arguments data_gen_args = dict(featurewise_center=True, featurewise_std_normalization=True, rotation_range=90., width_shift_range=0.1, height_shift_range=0.1, zoom_range=0.2) image_datagen = ImageDataGenerator(**data_gen_args) mask_datagen = ImageDataGenerator(**data_gen_args) # Provide the same seed and keyword arguments to the fit and flow methods seed = 1 image_datagen.fit(images, augment=True, seed=seed) mask_datagen.fit(masks, augment=True, seed=seed) image_generator = image_datagen.flow_from_directory( 'data/images', class_mode=None, seed=seed) mask_generator = mask_datagen.flow_from_directory( 'data/masks', class_mode=None, seed=seed) # combine generators into one which yields image and masks train_generator = zip(image_generator, mask_generator) model.fit_generator( train_generator, steps_per_epoch=2000, epochs=50)