在keras中,可以使用ImageDataGenerator来扩大样本,生成batch
代码样例:
import time
#from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
import scipy
#随机生成7幅100×100的rgb图 imgs=np.random.randint(0,10,size=(7,100,100,3))
imgs = imgs.astype(‘float32’)/10
#生成图像数据规则定义
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)
#准备
datagen.fit(imgs)
#给图像标号,按照规则生成batch
f=datagen.flow(imgs,[0,1,2,3,4,5,6],batch_size=3)
#print(f.next()[1])
#显示
for index,(x,y) in enumerate(f):
if index==10:#生成10个batch
break
print(x.shape,y)
执行结果:
(