圖片讀取ImageDataGenerator()
ImageDataGenerator()是keras.preprocessing.image模塊中的圖片生成器蝇摸,同時也可以在batch中對數(shù)據(jù)進行增強,擴充數(shù)據(jù)集大小律歼,增強模型的泛化能力险毁。比如進行旋轉(zhuǎn)畔况,變形慧库,歸一化等等。
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)
參數(shù):
- featurewise_center: Boolean. 對輸入的圖片每個通道減去每個通道對應(yīng)均值域庇。
- samplewise_center: Boolan. 每張圖片減去樣本均值, 使得每個樣本均值為0听皿。
- featurewise_std_normalization(): Boolean()
- samplewise_std_normalization(): Boolean()
- zca_epsilon(): Default 12-6
- zca_whitening: Boolean. 去除樣本之間的相關(guān)性
- rotation_range(): 旋轉(zhuǎn)范圍
- width_shift_range(): 水平平移范圍
- height_shift_range(): 垂直平移范圍
- shear_range(): float, 透視變換的范圍
- zoom_range(): 縮放范圍
- fill_mode: 填充模式, constant, nearest, reflect
- cval: fill_mode == 'constant'的時候填充值
- horizontal_flip(): 水平反轉(zhuǎn)
- vertical_flip(): 垂直翻轉(zhuǎn)
- preprocessing_function(): user提供的處理函數(shù)
- data_format(): channels_first或者channels_last
- validation_split(): 多少數(shù)據(jù)用于驗證集
方法:
- apply_transform(x, transform_parameters):根據(jù)參數(shù)對x進行變換
- fit(x, augment=False, rounds=1, seed=None): 將生成器用于數(shù)據(jù)x,從數(shù)據(jù)x中獲得樣本的統(tǒng)計參數(shù), 只有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生成增強數(shù)據(jù)
- flow_from_directory()從路徑生成增強數(shù)據(jù),和flow方法相比最大的優(yōu)點在于不用一次將所有的數(shù)據(jù)讀入內(nèi)存當(dāng)中,這樣減小內(nèi)存壓力,這樣不會發(fā)生OOM吗冤,血的教訓(xùn)。
- get_random_transform(img_shape, seed=None): 返回包含隨機圖像變換參數(shù)的字典
- random_transform(x, seed=None): 進行隨機圖像變換, 通過設(shè)置seed可以達到同步變換覆致。
- standardize(x): 對x進行歸一化
實例:
mnist分類數(shù)據(jù)增強
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分割數(shù)據(jù)增強,需要對image和mask同步處理:
featurewise結(jié)果:
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需要數(shù)據(jù)集的統(tǒng)計信息儡羔,因此需要先讀入一個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結(jié)果:
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()對子目錄排序的結(jié)果严蓖。這樣你才知道具體來說哪個路徑的類對應(yīng)哪個label氧急。
原圖: