本文说明keras的ImageDataGenerator
对象方法 flow
如何作用的。
如下为测试代码:
import time
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
imgs=np.random.randint(0,10,size=(7,100,100,3))
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)
f=datagen.flow(imgs,[0,1,2,3,4,5,6],batch_size=3)
# print(f.next()[1])
# time.sleep(2)
# print(f.next()[1])
# time.sleep(2)
# print(f.next()[1])
for index,(x,y) in enumerate(f):
if index==10:
break
time.sleep(1)
print(x.shape,y)
输出结果:
(3, 100, 100, 3) [5 0 6]
(3, 100, 100, 3) [3 2 1]
(1, 100, 100, 3) [4]
(3, 100, 100, 3) [6 0 4]
(3, 100, 100, 3) [1 2 3]
(1, 100, 100, 3) [5]
(3, 100, 100, 3) [6 1 0]
(3, 100, 100, 3) [4 5 2]
(1, 100, 100, 3) [3]
(3, 100, 100, 3) [3 2 1]
分析可得,ImageDataGenerator
对象的flow方法,对输入数据(imgs,ylabel)
打乱(默认参数,可设置)后,依次取batch_size的图片并逐一进行变换。取完后再循环。伪代码如下:
while(True):
if shuffle==True:
shuffle(x,y)#打乱
for i in range(0,len(x),batch_size):
x_batch=x[i:i+batch_size]
y_batch=y[i:i+batch_size]
ImagePro(x_batch)#数据增强
saveToFile()#保存提升后的图片
yield (x_batch,y_batch)
源码参考:/Lib/site-packages/keras/preprocessing/image.py