-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path_augmentation.py
71 lines (68 loc) · 3.24 KB
/
_augmentation.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
import albumentations as A
import cv2
import numpy as np
"""
The augmentation methods using albumentations
Note the cv2.INTER_NEAREST and cv2.BORDER_CONSTANT are necessary
"""
class _augmentation():
def _generate_augmentation(self, mode = "distal_tubules"):
"""
create a augmentation instance from albumentation
Args:
mode: str, "distal_tubules" or "other", the mode of augmentation.
when under "distal_tubules" mode, the augmentation will
provide a consistent direction augmentation
Return:
_transform: albumentations.core.composition.Compose object, taking all the transformations
"""
assert mode in ["distal_tubules", "other"]
if mode == "other":
_transform = A.Compose([A.Flip(p = 0.7),
A.GridDistortion(num_steps=3,
distort_limit=0.03,
interpolation=cv2.INTER_NEAREST,
border_mode = cv2.BORDER_CONSTANT,
p = 0.3),
A.Transpose(p = 0.5),
A.ShiftScaleRotate(shift_limit=0.0,
scale_limit=(-0.4,0),
rotate_limit=90,
interpolation= cv2.INTER_NEAREST,
border_mode = cv2.BORDER_CONSTANT,
p = 0.5)
])
else:
_Vflip_flag, _Hflip_flag, _Transpose_flag = np.random.randint(0,2, size = 3)
_rotation = np.random.randint(0,15)
_transform = A.Compose([
A.VerticalFlip(p = _Vflip_flag),
A.HorizontalFlip(p = _Hflip_flag),
A.Transpose(p = _Transpose_flag),
A.GridDistortion(num_steps=5,
distort_limit=0.05,
interpolation=cv2.INTER_NEAREST,
border_mode = cv2.BORDER_CONSTANT,
p = 1),
A.ShiftScaleRotate(shift_limit=0.0,
scale_limit=(-0.4,0),
rotate_limit=(0,_rotation),
interpolation= cv2.INTER_NEAREST,
border_mode = cv2.BORDER_CONSTANT,
p = 1)
])
return _transform
def _augment(self,image,transform):
"""
wrapper for transformation
Args:
image: np.ndarray, the actual image
transform: albumentations.core.composition.Compose object
Return:
np.ndarray, the transformed image from _transform
"""
# it there's no image in this list (from utils.random_select)
if isinstance(image, int):
# return a smallest "valid format" zero image.
return np.zeros((1,1,3))
return transform(image = image)["image"]