keras生成器的flow方法实例(flow_from_directory)

flow 是使用生成器进行数据增强的一种方法,
具体的参数列表可以参考官网,
实例如下,

# -*- coding: utf-8 -*-

"""
 @Time    : 
 @Author  : kkk
 @File    : 
 @Software: PyCharm
"""
from keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img
import numpy as np

#  实例化一个 ImageDataGenerator
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'
)

box = []
img1 = load_img(path='test1.png')
img2 = load_img(path='test2.png')
# img is class of 'PIL.PngImagePlugin.PngImageFile'
img1 = img_to_array(img1)
img2 = img_to_array(img2)
# img1 = img1[np.newaxis, :, :, :]
# img2 = img2[np.newaxis, :, :, :]
# img is class of 'numpy.ndarray'
print(img1.shape)
print(img2.shape)
box.append(img1)
box.append(img2)
np_box = np.array(box)
print(np_box.shape)

i = 0
# .flow的方法接受 Numpy array of rank 4 or a tuple 类型的数据,
# .flow_from_dict 的方法接受一个文件夹的路径
for batch in datagen.flow(np_box, batch_size=2,
                          save_to_dir=r'C:\Users\lk\Desktop\here',
                          save_format='png'):
    i += 1
    if i > 2:
        break  # otherwise the generator would loop indefinitely

可以看到, 输出的结果为

图片01
名字应该是随机的

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值