数据增强程序

from keras.preprocessing.image import ImageDataGenerator
import os
import cv2
datagen = ImageDataGenerator(
    rotation_range=20,
    width_shift_range=0.15,
    height_shift_range=0.15,
    zoom_range=0.15,
    shear_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest')
dir = './data/'
path = os.path.abspath(dir)
all_file = os.listdir(dir)
all_file.sort()
i = 0
for file1 in all_file:
    # print(file1)
    img_file = os.listdir(os.path.join(path, file1))
    img_file.sort()
    for file2 in img_file:
        img = cv2.imread(os.path.join(path, file1, file2))
        x = img.reshape((1,) + img.shape) # datagen.flow要求rank为4 (1, x.shape)
        datagen.fit(x)
        prefix = file2.split('.')[0] # 以 . 分离字符串,去掉后缀
        counter = 0
        for batch in datagen.flow(x, batch_size=32, save_to_dir=dir + all_file[i], save_prefix=prefix, save_format='jpg'):
            counter += 1
            print(file2, '======', counter)
            if counter > 30:
                break  # 否则生成器会退出循环
    i += 1

rotation_range=20 图片随机旋转20°
width_shift_range=0.15 图片宽度随机水平偏移0.15
height_shift_range=0.15 图片高度随机竖直偏移0.15
zoom_range=0.15 随机缩放幅度为0.15
shear_range=0.2 剪切强度为0.15
horizontal_flip=True 随机对图片进行水平翻转

具体参数详解  参考 https://blog.csdn.net/jacke121/article/details/79245732

参考链接  https://blog.csdn.net/mogoweb/article/details/86588698

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值