-
Notifications
You must be signed in to change notification settings - Fork 23
/
transforms.py
executable file
·176 lines (141 loc) · 5.17 KB
/
transforms.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import numpy as np
from numpy import random
import cv2
class Compose(object):
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, img, mask):
for t in self.transforms:
img, mask = t(img, mask)
return img, mask
class ConvertImgFloat(object):
def __call__(self, img, mask):
return img.astype(np.float32), mask.astype(np.float32)
class RandomContrast(object):
def __init__(self, lower=0.5, upper=1.5):
self.lower = lower
self.upper = upper
assert self.upper >= self.lower, "contrast upper must be >= lower."
assert self.lower >= 0, "contrast lower must be non-negative."
def __call__(self, img, mask):
if random.randint(2):
alpha = random.uniform(self.lower, self.upper)
img *= alpha
return img, mask
class RandomBrightness(object):
def __init__(self, delta=32):
assert delta >= 0.0
assert delta <= 255.0
self.delta = delta
def __call__(self, img, mask):
if random.randint(2):
delta = random.uniform(-self.delta, self.delta)
img += delta
return img, mask
class SwapChannels(object):
def __init__(self, swaps):
self.swaps = swaps
def __call__(self, img):
img = img[:, :, self.swaps]
return img
class RandomLightingNoise(object):
def __init__(self):
self.perms = ((0, 1, 2), (0, 2, 1),
(1, 0, 2), (1, 2, 0),
(2, 0, 1), (2, 1, 0))
def __call__(self, img, mask):
if random.randint(2):
swap = self.perms[random.randint(len(self.perms))]
shuffle = SwapChannels(swap)
img = shuffle(img)
return img, mask
class PhotometricDistort(object):
def __init__(self):
self.pd = RandomContrast()
self.rb = RandomBrightness()
self.rln = RandomLightingNoise()
def __call__(self, img, mask):
img, mask = self.rb(img, mask)
if random.randint(2):
distort = self.pd
else:
distort = self.pd
img, mask = distort(img, mask)
img, mask = self.rln(img, mask)
return img, mask
class Expand(object):
def __init__(self, max_scale = 2, mean = (0.5, 0.5, 0.5)):
self.mean = mean
self.max_scale = max_scale
def __call__(self, img, mask):
if random.randint(2):
return img, mask
h,w,c = img.shape
ratio = random.uniform(1,self.max_scale)
y1 = random.uniform(0, h*ratio-h)
x1 = random.uniform(0, w*ratio-w)
expand_img = np.zeros(shape=(int(h*ratio), int(w*ratio),c),dtype=img.dtype)
expand_img[:,:,:] = self.mean
expand_img[int(y1):int(y1+h), int(x1):int(x1+w)] = img
expand_mask = np.zeros(shape=(mask.shape[0], int(h*ratio), int(w*ratio)),dtype=mask.dtype)
expand_mask[:, int(y1):int(y1+h), int(x1):int(x1+w)] = mask
return expand_img, expand_mask
class RandomSampleCrop(object):
def __init__(self, ratio=(0.5, 1.5), min_win = 0.9):
self.sample_options = (
# using entire original input image
None,
# sample a patch s.t. MIN jaccard w/ obj in .1,.3,.4,.7,.9
# (0.1, None),
# (0.3, None),
(0.7, None),
(0.9, None),
# randomly sample a patch
(None, None),
)
self.ratio = ratio
self.min_win = min_win
def __call__(self, img, mask):
height, width ,_ = img.shape
while True:
mode = random.choice(self.sample_options)
if mode is None:
return img, mask
for _ in range(50):
current_img = img
current_mask = mask
w = random.uniform(self.min_win*width, width)
h = random.uniform(self.min_win*height, height)
if h/w<self.ratio[0] or h/w>self.ratio[1]:
continue
y1 = random.uniform(height-h)
x1 = random.uniform(width-w)
rect = np.array([int(y1), int(x1), int(y1+h), int(x1+w)])
current_img = current_img[rect[0]:rect[2], rect[1]:rect[3], :]
current_mask = current_mask[:, rect[0]:rect[2], rect[1]:rect[3]]
return current_img, current_mask
class RandomMirror_w(object):
def __call__(self, img, mask):
_,w,_ = img.shape
if random.randint(2):
img = img[:,::-1,:]
mask = mask[:,:,::-1]
return img, mask
class RandomMirror_h(object):
def __call__(self, img, mask):
h,_,_ = img.shape
if random.randint(2):
img = img[::-1,:,:]
mask = mask[:,::-1,:]
return img, mask
class Resize(object):
def __init__(self, h, w):
self.dsize = (w,h)
def __call__(self, img, mask):
img = cv2.resize(img, dsize=self.dsize)
mask_new = []
for i in range(mask.shape[0]):
cur_mask = mask[i,:,:]
cur_mask = cv2.resize(cur_mask,dsize=self.dsize,interpolation=cv2.INTER_NEAREST)
mask_new.append(cur_mask)
return img, np.asarray(mask_new)