keras数据生成器--数据增强

原理很简单都在代码中

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img

datagen = ImageDataGenerator(
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest'
)
img = load_img('/home/root1/data/m')
x = img_to_array(img)
x = x.reshape((1, ) + x.shape)

i = 0
for batch in datagen.flow(x, batch_size=1, save_to_dir='/home/root1/data/tmp/', save_prefix='person',
                          save_format='jpg'):
    i += 1
    if i > 20:
        break

经测试,程序运行通过。能直接将图像增强后保存下来,另外可以稍作改动,对文件夹进行数据增强。

猜你喜欢

转载自blog.csdn.net/Synioe/article/details/84184538