-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaugment.py
94 lines (77 loc) · 2.99 KB
/
augment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pip install albumentations
#Webpage: https://albumentations.readthedocs.io/en/latest/
import numpy as np
from albumentations import (
HorizontalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine,
IAASharpen, IAAEmboss, RandomContrast, RandomBrightness, Flip, OneOf, Compose
)
class augmentation_clss():
def __init__(self, mode): #4 MODES, IAAPerspective, ShiftScaleRotate, MediumAugmentation y StrongAugmentation
try:
self.augment_img = {
'IAAPerspective': self.IAAP,
'ShiftScaleRotate': self.SSR,
'MediumAug': self.MediumAug,
'StrongAug': self.StrongAug,
}[mode]
except:
raise ValueError('Mode must be \'IAAPerspective\', \'ShiftScaleRotate\', \'MediumAug\', or \'StrongAug\'')
self.mode = mode
def IAAP(self, image, scale=0.2, p=1):
image = image.astype(np.uint8)
aug = IAAPerspective(scale=scale, p=p)
output = aug(image=image)['image']
return output.astype(np.float32)
def SSR (self, image, p=1):
image = image.astype(np.uint8)
aug = ShiftScaleRotate(p=1)
output = aug(image=image)['image']
return output.astype(np.float32)
def MediumAug(self, image, p=1):
image = image.astype(np.uint8)
aug = Compose([
CLAHE(),
RandomRotate90(),
Transpose(),
ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.50, rotate_limit=45, p=.75),
Blur(blur_limit=3),
OpticalDistortion(),
GridDistortion(),
HueSaturationValue()
], p=p)
output = aug(image=image)['image']
return output.astype(np.float32)
def StrongAug(self, image, p=1):
image = image.astype(np.uint8)
aug = Compose([
RandomRotate90(),
Flip(),
Transpose(),
OneOf([
IAAAdditiveGaussianNoise(),
GaussNoise(),
], p=0.2),
OneOf([
MotionBlur(p=.2),
MedianBlur(blur_limit=3, p=.1),
Blur(blur_limit=3, p=.1),
], p=0.2),
ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=.2),
OneOf([
OpticalDistortion(p=0.3),
GridDistortion(p=.1),
IAAPiecewiseAffine(p=0.3),
], p=0.2),
OneOf([
CLAHE(clip_limit=2),
IAASharpen(),
IAAEmboss(),
RandomContrast(),
RandomBrightness(),
], p=0.3),
HueSaturationValue(p=0.3),
], p=p)
output = aug(image=image)['image']
return output.astype(np.float32)