当前位置: 首页 > 工具软件 > AugLy > 使用案例 >

快速数据增强方法--基于Augly

胡向阳
2023-12-01

分享一种基于API的快速数据增强方法:

前段时间分享的数据增强方法是基于python编写,基于python编写的程序运行起来也太慢了,加上数据量有3000张,扩充一下就需要一天,时间成本太大。后面考虑有没有基于C++封装好的API可以快速对图像进行数据增强以达到扩充数据的效果.于是找到了一款API,使用图像增强库Augly对数据进行增强扩充.这款基于Augly库的数据增强方法可以极快的提高运行效率,毕竟底层是C++。

# -*- coding: utf-8 -*-
import os
import random
import cv2
import augly.image as imaugs
import PIL.Image as Image

img_path = "./silk"                          # 需要增强的图像路径
save_path = "./enforceshi"                     # 保存路径

def augly_augmentation(aug_image):
    aug = [
        imaugs.blur(aug_image,radius=random.randint(1,2)),                     # 图像模糊
        imaugs.brightness(aug_image,factor=random.uniform(0.5,1.5)),           # 改变亮度
        # imaugs.change_aspect_ratio(aug_image, ratio=random.uniform(0.8,1.5)),  # 改变图像宽高比
        # imaugs.color_jitter(aug_image, brightness_factor=random.uniform(0.8,1.5), contrast_factor=random.uniform(0.8,1.5), saturation_factor=random.uniform(0.8,1.5)),    # 颜色晃动
        # imaugs.crop(aug_image, x1=random.uniform(0,0.1), y1=random.uniform(0,0.1), x2=random.uniform(0.9,1), y2=random.uniform(0.9,1)),     # 随机裁剪
        # imaugs.hflip(aug_image),                                               # 水平翻转
        # imaugs.opacity(aug_image, level=random.uniform(0.5,1)),                # 改变图像透明度
        # imaugs.pixelization(aug_image, ratio=random.uniform(0.5,1)),           # 马赛克
        imaugs.random_noise(aug_image),                                        # 随机噪声
        imaugs.rotate(aug_image, degrees=random.randint(3,10)),                # 随机旋转一定角度
        imaugs.shuffle_pixels(aug_image, factor=random.uniform(0,0.1)),        # 随机像素比任意化
        imaugs.saturation(aug_image, factor=random.uniform(1,1.5)),            # 改变饱和度
        imaugs.contrast(aug_image, factor=random.uniform(1,1.5)),              # 对比度增强
        # imaugs.grayscale(aug_image)                                            # 转灰度
    ]
    return random.choice(aug)                                                   # 从以上函数中随机选其一进行数据增强

folders = os.listdir(img_path)
print("当前类:",folders)
for folder in folders:
    print(folder)
    if(os.path.exists(save_path+"/"+folder) is not True):
        print("create path:",save_path+"/"+folder)
        os.makedirs(save_path+"/"+folder)
    save_path_new = save_path+"/"+folder
    img_path_new = img_path +"/"+folder

    for name in os.listdir(img_path_new):
        # print(name)

        print("原图保存:",os.path.join(img_path_new,name))
        img = cv2.imread(os.path.join(img_path_new, name))
        cv2.imwrite(os.path.join(save_path_new,name),img)

        aug_image = Image.open(os.path.join(img_path_new,name))
        count = 4                           # 每张图片需要增强的数量

        for i in range(count):
            image = augly_augmentation(aug_image)
            image = image.convert("RGB")
            image.save(os.path.join(save_path_new,"{}strong".format(i)+name[:-4]+".jpg"))
            print("加强图片保存:",os.path.join(save_path_new,"{}".format(i)+name[:-4]+".jpg"))

参考

https://blog.csdn.net/qq_39056987/article/details/123233142

 类似资料: