图片读取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进行归一化
实例:
mnist分类数据增强
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
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = np.expand_dims(x_train, axis = 3)
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)
data_iter = datagen.flow(x_train, y_train, batch_size=8)
while True:
x_batch, y_batch = data_iter.next()
for i in range(8):
print(i//4)
plt.subplot(2,4,i+1)
plt.imshow(x_batch[i].reshape(28,28), cmap='gray')
plt.show()
portrait分割数据增强,需要对image和mask同步处理:
featurewise结果:
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()
samplewise结果:
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
imagegen = ImageDataGenerator(
samplewise_center=True,
samplewise_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)
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需要提供的路径下面需要有子目录,因此我的目录形式如下:
data/
...images/
........./images
...masks/
........./masks
只有这样提供才能保证正确读取图片,没有子目录会检测不到图片。
此外正如github上的issue:https://github.com/keras-team/keras/pull/3052/commits/81fb0fa7c332b1b9d2669d68797fda041de17088
for subdir in sorted(os.listdir(directory)):
if os.path.isdir(os.path.join(directory, subdir)):
classes.append(subdir)
flow_from_directory()会从路径推测label, 在进行映射之前,会先对路径进行排序,具体顺序是alphanumerically, 也是os.listdir()对子目录排序的结果。这样你才知道具体来说哪个路径的类对应哪个label。
原图: