转:https://www.jianshu.com/p/d23b5994db64
参考:https://blog.csdn.net/we34dfg/article/details/79792681
图片读取ImageDataGenerator()
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)
- featurewise_center: Boolean. 对输入的图片每个通道减去每个通道对应均值。
- samplewise_center: Boolan. 每张图片减去样本均值, 使得每个样本均值为0。
- featurewise_std_normalization(): Boolean()
- samplewise_std_normalization(): Boolean()
- zca_epsilon(): Default 12-6
- zca_whitening: Boolean. 去除样本之间的相关性
- rotation_range(): 旋转范围
- width_shift_range(): 水平平移范围
- height_shift_range(): 垂直平移范围
- shear_range(): float, 透视变换的范围
- zoom_range(): 缩放范围
- fill_mode: 填充模式, constant, nearest, reflect
- cval: fill_mode == 'constant'的时候填充值
- horizontal_flip(): 水平反转
- vertical_flip(): 垂直翻转
- preprocessing_function(): user提供的处理函数
- data_format(): channels_first或者channels_last
- validation_split(): 多少数据用于验证集
方法:
- apply_transform(x, transform_parameters):根据参数对x进行变换
- fit(x, augment=False, rounds=1, seed=None): 将生成器用于数据x,从数据x中获得样本的统计参数, 只有featurewise_center, featurewise_std_normalization或者zca_whitening为True才需要
- flow(x, y=None, batch_size=32, shuffle=True, sample_weight=None, seed=None, save_to_dir=None, save_prefix='', save_format='png', subset=None) ):按batch_size大小从x,y生成增强数据
- flow_from_directory()从路径生成增强数据,和flow方法相比最大的优点在于不用一次将所有的数据读入内存当中,这样减小内存压力,这样不会发生OOM,血的教训。
- get_random_transform(img_shape, seed=None): 返回包含随机图像变换参数的字典
- random_transform(x, seed=None): 进行随机图像变换, 通过设置seed可以达到同步变换。
- standardize(x): 对x进行归一化
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',