-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcorruptions.py
352 lines (288 loc) · 10.9 KB
/
corruptions.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from copy import deepcopy
import functools
import PIL
from PIL import Image
import torch
import numpy as np
from mmcv.utils import Registry
from imagecorruptions import corrupt
CORRUPTIONS= Registry('corruptions')
@CORRUPTIONS.register_module()
class Clean:
def __init__(self, severity, norm_config):
"""
No corruption
"""
self.severity = severity
assert severity >= 1 and severity <= 5, f"Corruption Severity should between (1, 5), now {severity}"
self.norm_config = norm_config
self.corruption = 'clean'
def __call__(self, x):
return x
@CORRUPTIONS.register_module()
class BaseCorruption:
def __init__(self, severity, norm_config, corruption):
"""
Base Corruption Class
Args:
severity (int): severity of corruption, range (1, 5)
"""
self.severity = severity
assert severity >= 1 and severity <= 5, f"Corruption Severity should between (1, 5), now {severity}"
self.norm_config = norm_config
self.corruption = corruption
try:
self.corrupt_func = self._get_corrupt_func()
except:
self.corrupt_func = None
def __call__(self, img):
"""
Args:
img (torch.Tensor): [B, M, C, H, W]
"""
mean = self.norm_config['mean']
std = self.norm_config['std']
img = deepcopy(img)
B, M, C, H, W = img.size()
img = img.permute(0, 1, 3, 4, 2) # [B, M, C, H, W] => [B, M, H, W, C]
img = img * torch.tensor(std) + torch.tensor(mean)
# pixel value [0, 255]
assert img.min() >= 0 and img.max() <= 255, "Image pixel out of range"
new_img = np.zeros_like(img)
for b in range(B):
for m in range(M):
new_img[b, m] = self.corrupt_func(np.uint8(img[b, m].numpy()))
# new_img = new_img.permute(0, 1, 4, 2, 3)
return new_img
def _get_corrupt_func(self):
return functools.partial(corrupt, corruption_name=self.corruption, severity=self.severity)
@CORRUPTIONS.register_module()
class DefocusBlur(BaseCorruption):
def __init__(self, severity, norm_config):
"""
Create corruptions: 'Defocus Blur'.
Args:
severity (int): severity of corruption, range (1, 5)
"""
super().__init__(severity, norm_config, 'defocus_blur')
@CORRUPTIONS.register_module()
class GlassBlur(BaseCorruption):
def __init__(self, severity, norm_config):
"""
Create corruptions: 'Glass Blur'.
Args:
severity (int): severity of corruption, range (1, 5)
"""
super().__init__(severity, norm_config, 'glass_blur')
@CORRUPTIONS.register_module()
class MotionBlur(BaseCorruption):
def __init__(self, severity, norm_config):
"""
Create corruptions: 'Motion Blur'.
Args:
severity (int): severity of corruption, range (1, 5)
"""
super().__init__(severity, norm_config, 'motion_blur')
@CORRUPTIONS.register_module()
class ZoomBlur(BaseCorruption):
def __init__(self, severity, norm_config):
"""
Create corruptions: 'Zoom Blur'.
Args:
severity (int): severity of corruption, range (1, 5)
"""
super().__init__(severity, norm_config, 'zoom_blur')
@CORRUPTIONS.register_module()
class GaussianNoise(BaseCorruption):
def __init__(self, severity, norm_config):
"""
Create corruptions: 'Gaussian Noise'.
Args:
severity (int): severity of corruption, range (1, 5)
"""
super().__init__(severity, norm_config, 'gaussian_noise')
@CORRUPTIONS.register_module()
class ImpulseNoise(BaseCorruption):
def __init__(self, severity, norm_config):
"""
Create corruptions: 'Impulse Noise'.
Args:
severity (int): severity of corruption, range (1, 5)
"""
super().__init__(severity, norm_config, 'impulse_noise')
@CORRUPTIONS.register_module()
class ShotNoise(BaseCorruption):
def __init__(self, severity, norm_config):
"""
Create corruptions: 'ISO Noise'.
Args:
severity (int): severity of corruption, range (1, 5)
"""
super().__init__(severity, norm_config, 'shot_noise')
@CORRUPTIONS.register_module()
class ISONoise(BaseCorruption):
def __init__(self, severity, norm_config):
"""
Create corruptions: 'Shot Noise'.
Args:
severity (int): severity of corruption, range (1, 5)
"""
super().__init__(severity, norm_config, 'iso_noise')
self.corrupt_func = self._get_corrupt_func()
def _get_corrupt_func(self):
return functools.partial(self.iso_noise, severity=self.severity)
def iso_noise(self, x, severity):
c_poisson = 25
x = np.array(x) / 255.
x = np.clip(np.random.poisson(x * c_poisson) / c_poisson, 0, 1) * 255.
c_gauss = 0.7 * [.08, .12, 0.18, 0.26, 0.38][severity]
x = np.array(x) / 255.
x = np.clip(x + np.random.normal(size=x.shape, scale= c_gauss), 0, 1) * 255.
return np.uint8(x)
@CORRUPTIONS.register_module()
class Brightness(BaseCorruption):
def __init__(self, severity, norm_config):
"""
Create corruptions: 'Brightness'.
Args:
severity (int): severity of corruption, range (1, 5)
"""
super().__init__(severity, norm_config, 'brightness')
@CORRUPTIONS.register_module()
class LowLight(BaseCorruption):
def __init__(self, severity, norm_config):
"""
Create corruptions: 'Dark'.
Args:
severity (int): severity of corruption, range (1, 5)
"""
super().__init__(severity, norm_config, 'dark')
self.corrupt_func = self._get_corrupt_func()
def _get_corrupt_func(self):
return functools.partial(self.low_light, severity=self.severity)
def imadjust(self, x, a, b, c, d, gamma=1):
y = (((x - a) / (b - a)) ** gamma) * (d - c) + c
return y
def poisson_gaussian_noise(self, x, severity):
c_poisson = 10 * [60, 25, 12, 5, 3][severity]
x = np.array(x) / 255.
x = np.clip(np.random.poisson(x * c_poisson) / c_poisson, 0, 1) * 255
c_gauss = 0.1 * [.08, .12, 0.18, 0.26, 0.38][severity]
x = np.array(x) / 255.
x = np.clip(x + np.random.normal(size=x.shape, scale= c_gauss), 0, 1) * 255
return np.uint8(x)
def low_light(self, x, severity):
c = [0.60, 0.50, 0.40, 0.30, 0.20][severity]
# c = [0.50, 0.40, 0.30, 0.20, 0.10][severity-1]
x = np.array(x) / 255.
x_scaled = self.imadjust(x, x.min(), x.max(), 0, c, gamma=2.) * 255
x_scaled = self.poisson_gaussian_noise(x_scaled, severity=severity)
return x_scaled
@CORRUPTIONS.register_module()
class Fog(BaseCorruption):
def __init__(self, severity, norm_config):
"""
Create corruptions: 'Fog'.
Args:
severity (int): severity of corruption, range (1, 5)
"""
super().__init__(severity, norm_config, 'fog')
@CORRUPTIONS.register_module()
class Snow(BaseCorruption):
def __init__(self, severity, norm_config):
"""
Create corruptions: 'Snow'.
Args:
severity (int): severity of corruption, range (1, 5)
"""
super().__init__(severity, norm_config, 'snow')
@CORRUPTIONS.register_module()
class CameraCrash:
def __init__(self, severity, norm_config):
"""
Create corruptions: 'Camera Crash'.
"""
self.severity = severity
assert severity >= 1 and severity <= 5, f"Corruption Severity should between (1, 5), now {severity}"
self.norm_config = norm_config
self.corruption = 'cam_crash'
self.crash_camera = self.get_crash_camera()
def __call__(self, img):
"""
Args:
img (torch.Tensor): [B, M, C, H, W]
"""
mean = self.norm_config['mean']
std = self.norm_config['std']
img = deepcopy(img)
B, M, C, H, W = img.size()
img = img.permute(0, 1, 3, 4, 2) # [B, M, C, H, W] => [B, M, H, W, C]
img = img * torch.tensor(std) + torch.tensor(mean)
# pixel value [0, 255]
assert img.min() >= 0 and img.max() <= 255, "Image pixel out of range"
for b in range(B):
for m in self.crash_camera:
img[b, m] = 0
assert img.min() >= 0 and img.max() <= 255, "Image pixel out of range"
# img = img - torch.tensor(mean) / torch.tensor(std)
# img = img.permute(0, 1, 4, 2, 3)
return img.numpy()
def get_crash_camera(self):
crash_camera = np.random.choice([0, 1, 2, 3, 4, 5], size=self.severity)
return list(crash_camera)
@CORRUPTIONS.register_module()
class FrameLost():
def __init__(self, severity, norm_config):
"""
Create corruptions: 'Frame Lost'.
"""
self.severity = severity
assert severity >= 1 and severity <= 5, f"Corruption Severity should between (1, 5), now {severity}"
self.norm_config = norm_config
self.corruption = 'frame_lost'
def __call__(self, img):
"""
Args:
img (torch.Tensor): [B, M, C, H, W]
"""
mean = self.norm_config['mean']
std = self.norm_config['std']
img = deepcopy(img)
B, M, C, H, W = img.size()
img = img.permute(0, 1, 3, 4, 2) # [B, M, C, H, W] => [B, M, H, W, C]
img = img * torch.tensor(std) + torch.tensor(mean)
# pixel value [0, 255]
assert img.min() >= 0 and img.max() <= 255, "Image pixel out of range"
for b in range(B):
for m in range(M):
if np.random.rand() < (self.severity * 1. / 6.):
img[b, m] = 0
assert img.min() >= 0 and img.max() <= 255, "Image pixel out of range"
# img = img - torch.tensor(mean) / torch.tensor(std)
# img = img.permute(0, 1, 4, 2, 3)
return img.numpy()
@CORRUPTIONS.register_module()
class ColorQuant(BaseCorruption):
def __init__(self, severity, norm_config):
"""
Create corruptions: 'Color Quantization'.
Args:
severity (int): severity of corruption, range (1, 5)
"""
super().__init__(severity, norm_config, 'color_quant')
def _get_corrupt_func(self):
return functools.partial(self.color_quant, severity=self.severity)
def color_quant(self, x, severity):
bits = 5 - severity
x = Image.fromarray(np.uint8(x))
x = PIL.ImageOps.posterize(x, bits)
return np.asarray(x)
@CORRUPTIONS.register_module()
class Pixlate(BaseCorruption):
def __init__(self, severity, norm_config):
"""
Create corruptions: 'Pixelate'.
Args:
severity (int): severity of corruption, range (1, 5)
"""
super().__init__(severity, norm_config, 'pixelate')