转:https://www.jianshu.com/p/d23b5994db64
参考:https://blog.csdn.net/we34dfg/article/details/79792681
ImageDataGenerator()是keras.preprocessing.image模块中的图片生成器,同时也可以在batch中对数据进行增强,扩充数据集大小,增强模型的泛化能力。比如进行旋转,变形,归一化等等。
keras.preprocessing.image.ImageDataGenerator(featurewise_center=False,
samplewise_center=False, featurewise_std_normalization=False,
samplewise_std_normalization=False, zca_whitening=False, zca_epsilon=1e-06,
rotation_range=0.0, width_shift_range=0.0, height_shift_range=0.0,
brightness_range=None, shear_range=0.0, zoom_range=0.0, channel_shift_range=0.0,
fill_mode='nearest', cval=0.0, horizontal_flip=False, vertical_flip=False,
rescale=None, preprocessing_function=None, data_format=None, validation_split=0.0)
方法:
from keras.preprocessing.image import ImageDataGenerator
from keras.datasets import mnist
from keras.datasets import cifar10
from keras.utils import np_utils
import numpy as np
import matplotlib.pyplot as plt
num_classes = 10
seed = 1
# featurewise需要数据集的统计信息,因此需要先读入一个x_train,用于对增强图像的均值和方差处理。
x_train = np.load('images-224.npy')
imagegen = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True)
maskgen = ImageDataGenerator(
rescale = 1./255,
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)
imagegen.fit(x_train)
image_iter = imagegen.flow_from_directory('../data/images',target_size=(224,224), class_mode=None, batch_size=8, seed=seed)
mask_iter = maskgen.flow_from_directory('../data/masks', color_mode='rgb', target_size=(224,224), class_mode=None, batch_size=8, seed=seed)
data_iter = zip(image_iter, mask_iter)
while True:
for x_batch, y_batch in data_iter:
for i in range(8):
print(i//4)
plt.subplot(2,8,i+1)
plt.imshow(x_batch[i].reshape(224,224,3))
plt.subplot(2,8,8+i+1)
plt.imshow(y_batch[i].reshape(224,224, 3), cmap='gray')
plt.show()
flow_from_directory(dire)
dire文件夹下必须有子文件夹才行,子文件夹下再放图片
E:/tmp/augment2/0010.jpg >>> E:/tmp/augment2/train/0010.jpg
"""
###error_modified_succeeded
"""Found 1 images belonging to 1 classes."""
###图片来源形式应该为
"""
E:/tmp/augment1/image/i.jpg # flow_from_directory(directory='E:/tmp/augment1',
E:/tmp/augment3/mask/i.png # flow_from_directory(directory='E:/tmp/augment3',