keras的batch数据生成器使用ImageDataGenerator

练丹术之数据增强大法—Keras篇 

“数据就是练丹的材料,没有足够的材料,再好的丹炉也只是一个花架子。就像人长得帅,没有钱怎么撩妹。贫道就不一样,人虽然长得不帅,但是也没钱啊!!!混了这么久,装备奇差,连1080t的卡都没见过,岛国1080P的到是见过不少,” 

在Keras中,最常用的方法是ImageDataGenerator,里面主要有旋转、平移、剪切、缩放、翻转等功能。
使用方法如下:https://www.flyai.com/article/arta9eccf563c1393bf32b7c6e8

from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1. / 255,  # 归一化
                                   rotation_range=90,  # 旋转角度
                                   width_shift_range=0.2,  # 水平偏移
                                   height_shift_range=0.2,  # 垂直偏移
                                   shear_range=0.2,  # 随机错切变换的角度
                                   zoom_range=0.2,  # 随机缩放的范围
                                   horizontal_flip=True,  # 随机将一半图像水平翻转
                                   fill_mode='nearest')  # 填充像素的方法
model.fit_generator(train_datagen.flow(x_data, y_data, batch_size=64, shuffle=True)...)

 

官方封装的训练集batch生成器是ImageDataGenerator对象的flow方法(或flow_from_directory),该函数返回一个和python定义相似的generator。其实主要就是建立并使用dataGenerator生成器从一个图像列表或者文件夹中生成一个batch_size的数据流。

keras中文文档解释链接:https://keras.io/zh/preprocessing/image/

关于参数的解释与例子:https://www.cnblogs.com/love6tao/p/5841648.html

# coding=utf-8  
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import numpy as np
import cv2
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')
 
gener=datagen.flow_from_directory(r"./img",
                                        color_mode="grayscale",
                                        # classes=["1","2","3"],
                                        class_mode="categorical",
                                        batch_size=2,
                                         shuffle=False,
                                         save_to_dir=r"./result",
                                         save_prefix='trans_',save_format='png'
                                        )
print("gener.class_indices",gener.class_indices)
# print("gener.filname",gener.filenames)
# gener是一个元组
for i in range(7):
    img=gener.next()
    #这里的gener的格式为[2,row,col,3],是网络可以输入的rensor格式,可以对其进行拆分
    print(img[0].shape)
    image=img[0][0,:,:,:]
    print(np.max(image))
    # gener.next()
    # next(gener)
    cv2.imwrite("x.jpg",image)
    cv2.imshow("2",image)
    cv2.waitKey(10)
 
 


# 从图像列表读取
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array,load_img
import numpy as np
import os
import cv2
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')

list=os.listdir(r"D:\test\pic")

li=[]
for i in range(len(list)):
    str=os.path.join("D:\\test\\pic",list[i])
    print(str)
    img=cv2.imread(str)
    img=cv2.resize(img,(400,400),interpolation=cv2.INTER_CUBIC) 
    li.append(img)
    
li=np.array(li)
# li = np.expand_dims(li, axis=1)


# l=[]
# img = load_img(r"d:\test\pic\1.jpg")  # this is a PIL image
# img1 = load_img(r"d:\test\pic\2.jpg")  # this is a PIL image
# img1 = load_img(r"d:\test\pic\3.jpg")  # this is a PIL image
# img1 = load_img(r"d:\test\pic\4.jpg")  # this is a PIL image
# y=img_to_array(img1)
# x = img_to_array(img) # this is a Numpy array with shape (3, 150, 150)
# l.append(x)
# l.append(y)
# l=np.array(l)
#l = l.reshape((1,) + l.shape)  # this is a Numpy array wi
# th shape (1, 3, 150, 150)
 
# the .flow() command below generates batches of randomly transformed images
# and saves the results to the `preview/` directory
i = 0
for batch in datagen.flow(li, batch_size=2,
                          save_to_dir=r'd:\test\result',  save_prefix='trans_',save_format='jpg'):
    i += 1
    if i > 3:
        break  # otherwise the generator would loop indefinitely

上述循环三次,每次选取batch_size=2个数据图片,一共6个

需要注意的是:datagen.flow_from_directory(
        directory='image/train1',
        batch_size=batch_size)

其中 directory 必须是文件夹,并有子文件夹,然后才是图片 ;

类别顺序按字母顺序,对于二分类问题,cat文件夹和dog文件夹里面的图片会分别归为第一类和第二类。

第二个单张图扩展测试代码:http://blog.sina.com.cn/s/blog_d76227260102wxbj.html

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('data/train/cats/cat.0.jpg')  # this is a PIL image
x = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)
x = x.reshape((1,) + x.shape)  # this is a Numpy array with shape (1, 3, 150, 150)

# the .flow() command below generates batches of randomly transformed images
# and saves the results to the `preview/` directory
i = 0
for batch in datagen.flow(x, batch_size=1,
                          save_to_dir='preview', save_prefix='cat', save_format='jpeg'):
    i += 1
    if i > 20:
        break  # otherwise the generator would loop indefinitely

其它测试程序:

def image_augmentation(img,label,augnum):#num means batch_size
    image_datagen = ImageDataGenerator(rotation_range = 0.2,
                                width_shift_range = 0.1,
                                 height_shift_range = 0.1,
                                 shear_range = 0.1,
                                 zoom_range = 0.1,
                                 fill_mode = 'nearest')
    label_datagen = ImageDataGenerator(rotation_range = 0.2,
                                width_shift_range = 0.1,
                                 height_shift_range = 0.1,
                                 shear_range = 0.1,
                                 zoom_range = 0.1,
                                 fill_mode = 'nearest')
    
    #random.seed(1)
    seed = random.randint(1,100000)
    n = 0
    for batch in image_datagen.flow(img,batch_size=1,save_to_dir=path,save_prefix='aug',save_format='png',seed=seed):
        n +=1
        if n > augnum:
            break
    n = 0
    
    for batch in image_datagen.flow(label,batch_size=1,save_to_dir=path_,save_prefix='aug_label',save_format='png',seed=seed):
        n +=1
        if n >augnum:
            break
    
    return

很不错的参考连接:http://www.pianshen.com/article/1083292220/

                                    https://www.cnblogs.com/love6tao/p/5841648.html

                                    https://blog.csdn.net/mieleizhi0522/article/details/82191331

                                    https://blog.csdn.net/zb1165048017/article/details/91366802

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CVer儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值