-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform_augment.py
More file actions
272 lines (216 loc) · 7.72 KB
/
transform_augment.py
File metadata and controls
272 lines (216 loc) · 7.72 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import random
import numbers
import torch
import numpy as np
from scipy.misc import imresize
from torch.nn.modules import padding
from skimage.util import random_noise
def pad_2d(in_matrix, size):
# size: tuple(left, right, top, bottom)
if isinstance(size, numbers.Number):
l, r, t, b = (int(size), int(size), int(size), int(size))
else:
l, r, t, b = size
# pad = padding.ConstantPad3d((l, r, t, b, 0, 0), 0)
# matrix = pad(matrix)
c, h, w = in_matrix.size()
matrix = torch.zeros(c, h+t+b, w+l+r)
matrix[:, t:h+t, l:w+l] = in_matrix[:,:,:]
return matrix
class Compose(object):
"""Composes several transforms together.
Args:
transforms (list of ``Transform`` objects): list of transforms to compose.
"""
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, img_list, dmap_list=None):
if dmap is None:
for t in self.transforms:
img = t(img)
return img
else:
for t in self.transforms:
img, dmap = t(img, dmap)
return img, dmap
class ToTensor(object):
"""Convert a ``PIL.Image`` to tensor.
Converts a PIL.Image in the range
[0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0].
"""
def __call__(self, pic):
"""
Args:
pic (PIL.Image): Image to be converted to tensor.
Returns:
Tensor: Converted image.
"""
# PIL image mode: L, RGB
if pic.mode == 'RGB':
nchannel = 3
img = np.asarray(pic)
elif pic.mode == 'L':
img = np.asarray(pic)[:,:,np.newaxis]
else:
raise Exception('Undefined mode: {}'.format(pic.mode))
# put it from HWC to CHW format
# yikes, this transpose takes 80% of the loading time/CPU
img = torch.from_numpy(img).transpose(0, 1).transpose(0, 2).contiguous()
return img.float()
class Mask(object):
"""Mask regions within ROI with given value.
Given roi: numpy.ndarray (height, width, 1) and fill: a number,
"""
def __init__(self, roi):
self.roi = roi
def __call__(self, image):
"""
Args:
image (Tensor): Tensor image of size (C, H, W) to be normalized.
Returns:
Tensor: masked image.
"""
if image.shape != self.roi.shape:
print("Warning: Inconsistant shape of image and ROI: %s and %s" %
(str(image.shape), str(self.roi.shape)))
return image * self.roi
class HorizontalFlip(object):
"""Horizontally flip the given Tensor."""
def __call__(self, img, dmap, rmap=None):
"""
Args:
img (Tensor, CHW): Image to be flipped.
Returns:
Tensor: Randomly flipped image.
"""
img_np = np.flip(img.numpy(), 2).copy()
img = torch.from_numpy(img_np)
dmap_np = np.flip(dmap.numpy(), 2).copy()
dmap = torch.from_numpy(dmap_np)
if rmap is not None:
rmap_np = np.flip(rmap.numpy(), 2).copy()
rmap = torch.from_numpy(rmap_np)
return img, dmap, rmap
class RandomPosCrop(object):
"""Crop the given image and density map at a random location.
Args:
size (sequence or int): Desired output size of the crop. If size is an
int instead of sequence like (h, w), a square crop (size, size) is
made.
"""
def __init__(self, crop_size, number=10):
self.number = number
if isinstance(crop_size, numbers.Number):
self.crop_size = (int(crop_size), int(crop_size))
else:
self.crop_size = crop_size
def get_rand_pos(self, raw_size, crop_size):
h1, w1 = raw_size
h2, w2 = crop_size
x = random.randint(0, w1 - w2)
y = random.randint(0, h1 - h2)
return x, y
def __call__(self, img, dmap, rmap=None, border=8):
"""
Args:
img (Tensor): image to be cropped.
dmap (Tensor): density map to be cropped.
Returns:
(Tensor, Tensor): Cropped image and dmap
"""
c1, h1, w1 = img.size()
c2, h2, w2 = dmap.size()
# get size of cropped dmap
ratio = int(w1 / w2)
th1, tw1 = self.crop_size
if th1 < 1 or tw1 < 1:
th1 = int(h1 * th1)
tw1 = int(w1 * tw1)
th2, tw2 = int(th1 / ratio), int(tw1 / ratio)
if w1 < tw1:
img = pad_2d(img, (0, tw1-w1, 0, 0))
dmap = pad_2d(dmap, (0, tw2-w2, 0, 0))
if rmap is not None:
rmap = pad_2d(rmap, (0, tw2-w2, 0, 0))
w1, w2 = tw1, tw2
if h1 < th1:
img = pad_2d(img, (0, 0, 0, th1-h1))
dmap = pad_2d(dmap, (0, 0, 0, th2-h2))
if rmap is not None:
rmap = pad_2d(rmap, (0, 0, 0, th2-h2))
h1, h2 = th1, th2
img_list, dmap_list, rmap_list = [], [], []
for i in range(self.number):
x2, y2 = self.get_rand_pos((h2, w2), (th2, tw2))
x1, y1 = int(x2*ratio), int(y2*ratio)
img_list.append(img[:, y1:y1+th1, x1:x1+tw1])
dmap_list.append(dmap[:, y2:y2+th2, x2:x2+tw2])
if rmap is not None:
rmap_list.append(rmap[:, y2:y2+th2, x2:x2+tw2])
else:
rmap_list.append(None)
return img_list, dmap_list, rmap_list
'''
class PaddingEX2(object):
def __init__(self, pad_ex=4):
self.pad_ex = pad_ex
def __call__(self, img, label_list=[], ratio=None):
"""
Args:
img (Tensor, CHW): Image to be padded.
Returns:
Tensor: padded image.
"""
c, h, w = img.size()
p_h = (self.pad_ex - h % self.pad_ex) % self.pad_ex
p_w = (self.pad_ex - w % self.pad_ex) % self.pad_ex
if p_h == 0 and p_w == 0:
return [img] + label_list
img = pad_2d(img, (0, p_w, 0, p_h))
if len(label_list) != 0:
c, h, w = img.size()
label_list = [pad_2d(label, (0, w/ratio-label.size(2), 0, h/ratio-label.size(1))) for label in label_list]
return [img] + label_list
'''
class PaddingEX2(object):
def __init__(self, pad_ex):
self.pad_ex = pad_ex
def __call__(self, img):
"""
Args:
img (Tensor, CHW): Image to be padded.
Returns:
Tensor: padded image.
"""
c, h, w = img.size()
p_h = (self.pad_ex - h % self.pad_ex) % self.pad_ex
p_w = (self.pad_ex - w % self.pad_ex) % self.pad_ex
if p_h == 0 and p_w == 0:
return img
img = pad_2d(img, (0, p_w, 0, p_h))
return img
class RandomNoise(object):
def __init__(self, mode, scale_list=[0.01, 0.03, 0.05]):
self.scale_list = scale_list
self.mode = mode
def __call__(self, img):
"""
Args:
img (Tensor, CHW): Image to add noise.
Returns:
Tensor: Noisy images.
"""
img_list = []
for scale in self.scale_list:
if self.mode == 'GaussianNoise':
noisy_img = img + torch.from_numpy(np.random.normal(0, scale, img.size())).type(torch.FloatTensor)
# noisy_img = random_noise(img, mode='gaussian', seed=None, clip=True, var=scale)
elif self.mode == 'RandomSaltPepper':
noisy_img = random_noise(img, mode='s&p', seed=None, clip=True, amount=scale)
img_list.append(noisy_img)
return img_list
class Resize(object):
def __init__(self, size=[256, 256]):
self.size = size
def __call__(self, img):
return torch.from_numpy(imresize(img.numpy(), self.size))