圖像增強工具 albumentations
學習總結
CONTENT
工具函數
import numpy as np
import cv2
import matplotlib.pyplot as plt
import albumentations as albu
import os,sys
'''
data augmentation util: albumentations
reference: https://github.com/albumentations-team/albumentations#documentation
'''
def aug(img, aug_func):
return aug_func(**{'image':img})['image']
def aug_compose(img, aug_func_list):
strong_aug = albu.Compose(aug_func_list, p=1)
return strong_aug(**{'image':img})['image']
def aug_show(img, aug_func, save_fig_name):
plt.figure(figsize=(16,9))
for i in range(8):
plt.subplot(2, 4, i+1)
img_aug = aug(img.copy(), aug_func)
plt.imshow(img_aug)
os.chdir(os.path.join(cur_root,'pics'))
plt.savefig(f'{save_fig_name}.png', dpi=120)
plt.show()
原圖
1. CenterCrop
def center_crop(img):
height, width = img.shape[:2]
plt.figure(figsize=(16,9))
for i in range(8):
plt.subplot(4, 2, i+1)
crop_height, crop_width = np.random.randint(100, height), np.random.randint(100, width)
print(crop_height, crop_width)
img_crop = aug(img.copy(), albu.CenterCrop(crop_height, crop_width,p=1))
plt.imshow(img_crop)
plt.show()
plt.savefig('center_crop.png', dpi=300)
2. Crop
def crop(img):
height, width = img.shape[:2]
plt.figure(figsize=(16,9))
for i in range(8):
plt.subplot(2, 4, i+1)
x_min, y_min = np.random.randint(24, 120), np.random.randint(16, 80)
x_max, y_max = np.random.randint(x_min, width), np.random.randint(y_min, height)
img_crop = aug(img.copy(), albu.Crop(x_min, y_min, x_max, y_max, p=1.0))
plt.imshow(img_crop)
os.chdir(os.path.join(cur_root,'pics'))
plt.savefig('crop.png', dpi=120)
plt.show()
3. CropNonEmptyMaskIfExists
4. ElasticTransform
- alpha、sigma:高斯過濾參數,float類型
- alpha_affine:范圍為 (-alpha_affine, alpha_affine),float 類型
- interpolation、border_mode、value、mask_value:與其他類含義一樣
- approximate:是否應平滑具有固定大小核的替換映射(displacement map),若啟用此選項,在大圖上會有兩倍的速度提升,boolean類型。
- p:使用此轉換的概率,默認值為 0.5
(1) 首先需要對圖像中的每個像素點(x,y)產生兩個-1~1之間的隨機數,Δx(x,y)和Δy(x,y),分別表示該像素點的x方向和y方向的移動距離;
(2) 生成一個以0為均值,以σ為標准差的高斯核k_nn,並用前面的隨機數與之做卷積,並將結果作用於原圖像
一般來說,alpha越小,sigma越大,產生的偏差越小,和原圖越接近。
參考鏈接
aug_show(img, albu.ElasticTransform(alpha=1, sigma=50, alpha_affine=50), 'elastic_transform')
5. Flip
aug_show(img, albu.Flip(p=0.5), 'flip')
6. GridDistortion
- num_steps:在每一條邊上網格單元的數量,默認值為 5,int 類型
- distort_limit:如果是單值,那么會被轉成 (-distort_limit, distort_limit),默認值為 (-0.03, 0.03),float或float數組類型
- interpolation、border_mode、value、mask_value:與其他類含義一樣
- p:使用此轉換的概率,默認值為 0.5
aug_show(img, albu.GridDistortion(p=1, border_mode = cv2.BORDER_CONSTANT), 'grid_distortion')
7. GridDropout
aug_show(img, albu.GridDropout(), 'grid_dropout')
8. HorizontalFlip
aug_show(img, albu.HorizontalFlip(), 'horizontal_flip')
9. IAAAffine
aug_show(img, albu.IAAAffine(p=1, scale=0.8, translate_percent=0.8, rotate=30), 'iaa_affine')
10. IAACropAndPad
def iaa_crop_and_pad(img):
plt.figure(figsize=(16,9))
for i in range(8):
plt.subplot(2, 4, i+1)
img_crop = aug(img.copy(), albu.IAACropAndPad(p=1, percent= np.random.randint(1, 20)/100))
plt.imshow(img_crop)
os.chdir(os.path.join(cur_root,'pics'))
plt.savefig('iaa_crop_and_pad.png', dpi=120)
plt.show()
11. IAAFliplr
aug_show(img, albu.IAAFliplr(), 'iaa_fliplr')
12. IAAFlipud
aug_show(img, albu.IAAFlipud(), 'iaa_flipud')
13. IAAPerspective
aug_show(img, albu.IAAPerspective(), 'iaa_perspective')
14. IAAPiecewiseAffine
aug_show(img, albu.IAAPiecewiseAffine(), 'iaa_piecewise_affine')
15. Lambda
def random_brightness_or_gamma(image, **kwargs):
seed = np.random.randint(0,2)
aug_func = albu.RandomBrightness() if seed else albu.RandomGamma()
# print(aug_func)
return aug_func(**{'image': image})['image']
aug_show(img, albu.Lambda(image=random_brightness), 'lambda')
16. LongestMaxSize
aug_show(img, albu.LongestMaxSize(max_size=1024), 'longest_max_size')
17. MaskDropout
18. OpticalDistortion
aug_show(img, albu.OpticalDistortion(distort_limit=0.25, shift_limit=0.25, border_mode= cv2.BORDER_CONSTANT, p=1), 'optical_distortion')
19. PadIfNeeded
aug_show(img, albu.PadIfNeeded(min_height=300, min_width=400, border_mode= cv2.BORDER_CONSTANT, p=1), 'pad_if_needed')
20. RandomCrop
aug_show(img, albu.RandomCrop(height=120, width=160, p=1), 'random_crop')
21. RandomCropNearBBox
22. RandomGridShuffle
aug_show(img, albu.RandomGridShuffle(grid=(4,4), p=1), 'random_grid_shuffle')
23. RandomResizedCrop
aug_show(img, albu.RandomResizedCrop(height=240, width=320, scale=(0.3, 1.0), ratio=(0.75, 1.25)), 'random_resized_crop')