前言
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