-
Notifications
You must be signed in to change notification settings - Fork 11
/
data_transform.py
262 lines (177 loc) · 7.16 KB
/
data_transform.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
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
import torchvision.transforms.functional as F
import random
import torch
import numpy as np
from PIL import Image, ImageEnhance, ImageFilter
from torchvision import transforms
import PIL
from util import mask_to_semantic
# class CenterCrop(object):
#
# def __init__(self, size=256):
# self.size = size
#
# def __call__(self, image, mask):
#
# # image transform
# image_0 = np.expand_dims(np.array(F.center_crop(img=Image.fromarray(image[:, :, 0]), output_size=self.size)), axis=2)
# image_1 = np.expand_dims(np.array(F.center_crop(img=Image.fromarray(image[:, :, 1]), output_size=self.size)), axis=2)
# image_2 = np.expand_dims(np.array(F.center_crop(img=Image.fromarray(image[:, :, 2]), output_size=self.size)), axis=2)
# image = np.concatenate((image_0, image_1, image_2), axis=2)
#
# # mask transform
# mask = np.array(F.center_crop(img=Image.fromarray(mask), output_size=self.size))
#
# return image, mask
class HorizontalFlip(object):
def __init__(self, p=0.5):
self.p = p
def __call__(self, image, mask):
if random.random() < self.p:
image = image.transpose(Image.FLIP_LEFT_RIGHT)
mask = mask.transpose(Image.FLIP_LEFT_RIGHT)
return image, mask
class VerticalFlip(object):
def __init__(self, p=0.5):
self.p = p
def __call__(self, image, mask):
if random.random() < self.p:
image = image.transpose(Image.FLIP_TOP_BOTTOM)
mask = mask.transpose(Image.FLIP_TOP_BOTTOM)
return image, mask
class Rotate(object):
def __init__(self, degrees):
self.degrees = degrees
def __call__(self, image, mask):
angle = random.choice(self.degrees)
if angle == 90:
image = image.transpose(Image.ROTATE_90)
mask = mask.transpose(Image.ROTATE_90)
elif angle == 180:
image = image.transpose(Image.ROTATE_180)
mask = mask.transpose(Image.ROTATE_180)
elif angle == 270:
image = image.transpose(Image.ROTATE_270)
mask = mask.transpose(Image.ROTATE_270)
return image, mask
class HorizontalFlipNP(object):
def __init__(self, p=0.5):
self.p = p
def __call__(self, image, mask):
if random.random() < self.p:
image = image.transpose(Image.FLIP_LEFT_RIGHT)
for i in range(mask.shape[0]):
mask[i, :, :] = np.array(Image.fromarray(mask[i, :, :]).transpose(Image.FLIP_LEFT_RIGHT))
return image, mask
class VerticalFlipNP(object):
def __init__(self, p=0.5):
self.p = p
def __call__(self, image, mask):
if random.random() < self.p:
image = image.transpose(Image.FLIP_TOP_BOTTOM)
for i in range(mask.shape[0]):
mask[i, :, :] = np.array(Image.fromarray(mask[i, :, :]).transpose(Image.FLIP_TOP_BOTTOM))
return image, mask
class RotateNP(object):
def __init__(self, degrees):
self.degrees = degrees
def __call__(self, image, mask):
angle = random.choice(self.degrees)
if angle == 90:
image = image.transpose(Image.ROTATE_90)
for i in range(mask.shape[0]):
mask[i, :, :] = np.array(Image.fromarray(mask[i, :, :]).transpose(Image.ROTATE_90))
elif angle == 180:
image = image.transpose(Image.ROTATE_180)
for i in range(mask.shape[0]):
mask[i, :, :] = np.array(Image.fromarray(mask[i, :, :]).transpose(Image.ROTATE_180))
elif angle == 270:
image = image.transpose(Image.ROTATE_270)
for i in range(mask.shape[0]):
mask[i, :, :] = np.array(Image.fromarray(mask[i, :, :]).transpose(Image.ROTATE_270))
return image, mask
class Resize(object):
def __init__(self, p=0.5, scales=[(320, 320), (192, 192), (384, 384), (128, 128)]):
self.scales = scales
self.p = p
def __call__(self, image, mask):
if random.random() < self.p:
scale = random.choice(self.scales)
image = image.resize(scale, resample=PIL.Image.BILINEAR)
mask = mask.resize(scale, resample=PIL.Image.BILINEAR)
return image, mask
class Brighten(object):
def __init__(self, p=0.5, alpha=1.3):
self.p = p
self.alpha = alpha
def __call__(self, image):
if random.random() < self.p:
en = ImageEnhance.Brightness(image)
image = en.enhance(self.alpha)
return image
class Color(object):
def __init__(self, p=0.5, alpha=1.5):
self.p = p
self.alpha = alpha
def __call__(self, image):
if random.random() < self.p:
en = ImageEnhance.Color(image)
image = en.enhance(self.alpha)
return image
class Contrast(object):
def __init__(self, p=0.5, alpha=1.5):
self.p = p
self.alpha = alpha
def __call__(self, image):
if random.random() < self.p:
en = ImageEnhance.Contrast(image)
image = en.enhance(self.alpha)
return image
class GaussianBlur(object):
def __init__(self, p=0.5, radius=1.5):
self.p = p
self.radius = radius
def __call__(self, image):
if random.random() < self.p:
image = image.filter(ImageFilter.GaussianBlur(radius=self.radius))
return image
class ToTensor(object):
def __call__(self, image, mask, labels=[1, 2, 3, 4, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], mode="train", smooth=False):
# image transform
image = np.array(image).astype(np.float)
mask = np.array(mask)
if mode == "test":
image /= 255
# for i in range(image.shape[2]):
# image[:, :, i] = (image[:, :, i] - np.min(image[:, :, i])) / (
# np.max(image[:, :, i]) - np.min(image[:, :, i]))
image = torch.from_numpy(image.transpose((2, 0, 1)))
return image
image /= 255
# for i in range(image.shape[2]):
# image[:, :, i] = (image[:, :, i] - np.min(image[:, :, i])) / (np.max(image[:, :, i]) - np.min(image[:, :, i]))
image = torch.from_numpy(image.transpose((2, 0, 1)))
# mask transform to semantic
mask = torch.from_numpy(mask_to_semantic(mask, labels, smooth=smooth))
return image, mask
class ToTensorNP(object):
def __call__(self, image, mask, labels=[100, 200, 300, 400, 500, 600, 700, 800], mode="train", smooth=False):
# image transform
image = np.array(image).astype(np.float)
mask = np.array(mask)
if mode == "test":
image /= 255
image = torch.from_numpy(image.transpose((2, 0, 1)))
return image
image /= 255
image = torch.from_numpy(image.transpose((2, 0, 1)))
# mask transform to semantic
mask = torch.from_numpy(mask)
return image, mask
class Normalize(object):
def __call__(self, image, mask, mode="train"):
# image transform
image = F.normalize(image, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
if mode == "test":
return image
return image, mask