天天看點

keras:ImageDataGenerator的flow方法

本文說明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]      
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)