AugLy是一个数据增强库,可以帮助评估和提高模型的稳健性。该库支持四种模式( 音频、视频、图像和文本 ),并且包含 100 多种执行数据增强的方法。如果正在从事使用音频、视频、图像或文本数据集的机器学习或深度学习项目,可以使用此库来增加数据并提高模型性能。
该库由 Facebook AI 的软件工程师 Joanna Bitton、FAIR 的研究工程师 Zoe Papakipos 以及 Facebook 的其他研究人员和工程师开发。
AugLy 是一个 Python/ target=_blank class=infotextkey>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)
原文链接:
https://blog.csdn.NET/qq_45538469/article/details/122217323