Python深度学习-Data Augmentation:使用Augly库进行图片数据增强

Augly

AugLy 是一个数据增强库,可以帮助评估和提高模型的稳健性。该库支持四种模式(音频、视频、图像和文本),并且包含 100 多种执行数据增强的方法。如果正在从事使用音频、视频、图像或文本数据集的机器学习或深度学习项目,可以使用此库来增加数据并提高模型性能。

该库由 Facebook AI 的软件工程师 Joanna Bitton、FAIR 的研究工程师 Zoe Papakipos 以及 Facebook 的其他研究人员和工程师开发。
[GitHub开源地址]

在这里插入图片描述
AugLy 是一个 Python 3.6+ 库。它可以通过以下方式安装:

pip install augly

上述命令仅安装使用图像和文本模式的基本要求。对于音频和视频模式,安装所需的额外依赖项:

pip install augly[av]

文件目录

# 数据的文件夹目录
# 图片类别:0,1,2...n
augment_data
- train_data
	- 0
	- 1
	- ...
- val_data
	- 0
	- 1
	- ...
- test_data
	- 0
	- 1
	- ...

源代码

import os
from PIL import Image
from PIL import ImageFile
import augly.image as imaugs
from tqdm import tqdm
ImageFile.LOAD_TRUNCATED_IMAGES = True

def fun_files(path):
    fileArray = []     
    for root, dirs, files in os.walk(path):     
        for fn in files:
            eachpath = str(root + '\\' + fn)
            fileArray.append(eachpath)
    # print(fileArray)
    return fileArray

def fun_dirs(path):      
    dirArray = []      
    for root, dirs, files in os.walk(path):    
        for dir in dirs:
            eachpath = str(root + '\\' + dir)
            dirArray.append(eachpath)
    # print(dirArray)
    return dirArray

def augmentation(augment_dirpath):
    datalist = fun_dirs(augment_dirpath)
    # print(dirlist)
    print("datalist", len(datalist))
    for dir in datalist:
        dirlist = fun_dirs(dir)
        for each in tqdm(dirlist):
            dirname = each.split("\\")[-1]
            filelist = fun_files(each)
            for i in range(len(filelist)):
                try:
                    im = Image.open(filelist[i])
                except:
                    print("cannot identify image file", filelist[i])
                else:
                    basename = os.path.basename(filelist[i])
                    imgformat = basename.split(".")[-1]
                    aug_image = im.convert('RGB')
                    # 图像模糊
                    output_path = each + '\\' + dirname + "_" + str(i) + "_" + "blur" + "." + imgformat
                    imaugs.blur(aug_image, output_path=output_path)
                    # 图像亮度
                    output_path = each + '\\' + dirname + "_" + str(i) + "_" + "brightness" + "." + imgformat
                    imaugs.brightness(aug_image, factor=1.5, output_path=output_path)
                    # 对比度
                    output_path = each + '\\' + dirname + "_" + str(i) + "_" + "contrast" + "." + imgformat
                    imaugs.contrast(aug_image, factor=1.5, output_path=output_path)
                    # 灰度
                    output_path = each + '\\' + dirname + "_" + str(i) + "_" + "grayscale" + "." + imgformat
                    imaugs.grayscale(aug_image, output_path=output_path)
                    # 水平翻转
                    output_path = each + '\\' + dirname + "_" + str(i) + "_" + "hflip" + "." + imgformat
                    imaugs.hflip(aug_image, output_path=output_path)
                    # 随机噪声
                    output_path = each + '\\' + dirname + "_" + str(i) + "_" + "random_noise" + "." + imgformat
                    imaugs.random_noise(aug_image, output_path=output_path)


if __name__ == '__main__':
    augment_dirpath = "augment_data"
    if not os.path.exists(augment_dirpath):
        os.mkdir(augment_dirpath)
    augmentation(augment_dirpath)

增强结果

在这里插入图片描述

写在最后

【学习交流】
WX:WL1498544910

【文末小宣传】
----博主自己开发的小程序,希望大家点赞支持一下,谢谢!-----
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

南浔Pyer

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

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

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

打赏作者

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

抵扣说明:

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

余额充值