-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
323 lines (259 loc) · 10.1 KB
/
__init__.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
import cv2
import os
import fiftyone as fo
import fiftyone.operators as foo
from fiftyone.operators import types
from fiftyone import ViewField as F
from imagecorruptions import corrupt
from imagecorruptions import get_corruption_names
import skimage
import numpy as np
from numba import njit
from fiftyone.core.utils import add_sys_path
with add_sys_path(os.path.dirname(os.path.abspath(__file__))):
from utils_corruption import (
gaussian_blur,
glass_blur
)
def _convert_to_title_case(text):
"""
Convert text to title case. abc_def -> Abc Def
Parameters:
- text (str): Input text.
Returns:
- str: Text converted to title case.
"""
return ' '.join([word.capitalize() for word in text.split('_')]).strip()
def _get_target_view(ctx, target):
"""
Get the target view based on the FiftyOne context and image_corruptions target type.
Parameters:
- ctx: FiftyOne Context object.
- target (str): Target type from ['entire', 'current', 'selected']
Returns:
- Target view based on the context and target type.
"""
if target == "selected":
return ctx.view.select(ctx.selected)
if target == "entire":
return ctx.dataset.match_tags("corrupted", bool=False)
return ctx.view.match_tags("corrupted", bool=False)
def _get_selected_corruptions(ctx):
"""
Get corruptions selected by use.
Parameters:
- ctx: FiftyOne Context object.
Returns:
- list: List of selected corruptions.
"""
selected_corruptions = []
corruption_type = ctx.params.get("corruption_type", "Common")
if corruption_type == "Common":
corruptions = get_corruption_names()
select_all = ctx.params.get("select_all_common",False)
else:
corruptions = get_corruption_names('validation')
select_all = ctx.params.get("select_all_validation",False)
if not select_all:
for c in corruptions:
if ctx.params.get(c, False):
selected_corruptions.append(c)
else:
selected_corruptions = corruptions
return selected_corruptions
def _get_selected_severities(ctx):
"""
Get severities selected by user.
Parameters:
- ctx: FiftyOne Context object.
Returns:
- list: List of selected severities.
"""
select_all = ctx.params.get("select_all_severities", False)
if not select_all:
return [ctx.params.get("severity", 1)]
else:
return range(1,6)
class ImageCorruptions(foo.Operator):
@property
def config(self):
return foo.OperatorConfig(
name="image_corruptions",
label="Image Corruptions",
description="Generate corrupted images of your dataset to measure robustness.",
dynamic=True,
execute_as_generator=True,
)
def resolve_input(self, ctx):
corruptions = get_corruption_names()
corruptions_validation = get_corruption_names('validation')
inputs = types.Object()
# Choose Corruption Type
corruption_types = ["Common", "Validation"]
corruption_types_group = types.RadioGroup()
for choice in corruption_types:
corruption_types_group.add_choice(choice, label=choice)
inputs.enum(
"corruption_type",
corruption_types_group.values(),
label="Corruption ",
description="Choose validation corruptions when augementations similar to common corruptions are used during training.",
view=types.TabsView(),
required=False,
)
corruption_type = ctx.params.get("corruption_type", "Common")
if corruption_type == "Common":
# Choose Common Corruptions
inputs.bool(
"select_all_common",
default=False,
label="Select all",
view=types.SwitchView(),
)
select_all_common = ctx.params.get("select_all_common", False)
if not select_all_common:
for c in corruptions:
title = _convert_to_title_case(c)
inputs.bool(
c,
label=title,
view=types.CheckboxView(space=3),
)
else:
# Choose Validation
inputs.bool(
"select_all_validation",
default=False,
label="Select all",
view=types.SwitchView(),
)
select_all_validation = ctx.params.get("select_all_validation", False)
if not select_all_validation:
for c in corruptions_validation:
title = _convert_to_title_case(c)
inputs.bool(
c,
label=title,
view=types.CheckboxView(space=3),
)
# Corruption Severity
inputs.bool(
"select_all_severities",
default=False,
label="All Severities",
description="All severities from 1 to 5",
view=types.SwitchView(),
)
select_all_severities = ctx.params.get("select_all_severities", False)
if not select_all_severities:
inputs.float(
"severity",
label="Severity",
description="Severity of Corruption between 1 and 5",
view=types.SliderView(componentsProps={'slider': {'min': 1, 'max': 5, 'step': 1}}),
default=1)
# Run on dataset/selected samples/ view
has_view = ctx.view != ctx.dataset.view()
has_selected = bool(ctx.selected)
default_target = "entire"
if has_view or has_selected:
target_choices = types.RadioGroup()
target_choices.add_choice(
"entire",
label="Entire Original dataset",
description="Run model on the entire original dataset",
)
if has_view:
target_choices.add_choice(
"current",
label="Current view",
description="Run model on the current view",
)
default_target = "current"
if has_selected:
target_choices.add_choice(
"selected",
label="Selected samples",
description="Run model on the selected samples",
)
default_target = "selected"
inputs.enum(
"target",
target_choices.values(),
default=default_target,
view=target_choices,
)
else:
ctx.params["target"] = default_target
# Execution Mode
_execution_mode(ctx, inputs)
return types.Property(inputs)
def resolve_delegation(self, ctx):
return ctx.params.get("delegate", False)
def execute(self, ctx):
selected_corruptions = _get_selected_corruptions(ctx)
if len(selected_corruptions) > 0:
severity = ctx.params.get("severity", 1)
severities = _get_selected_severities(ctx)
target = ctx.params.get("target", None)
target_view = _get_target_view(ctx, target)
for i, sample in enumerate(target_view):
for corruption in selected_corruptions:
for severity in severities:
new_sample = corrupt_sample(sample,corruption, severity)
ctx.trigger("reload_dataset")
def get_new_filepath(sample, corruption, severity):
image_dir = os.path.dirname(sample.filepath)
image_dir_base = os.path.basename(os.path.normpath(image_dir))
parent_dir = os.path.dirname(image_dir)
sample_dir_path = "/".join(parent_dir.split("/"))
corrupted_sample_dir_path = os.path.join(os.path.dirname(image_dir), image_dir_base + "_corrupted", f"{corruption}", f"{severity}")
filename = os.path.basename(sample.filepath)
new_filepath = os.path.join(corrupted_sample_dir_path, filename)
return new_filepath
def corrupt_sample(sample, corruption, severity):
new_filepath = get_new_filepath(sample, corruption, severity)
# If the file isn't already created.
if not os.path.exists(new_filepath):
os.makedirs(os.path.dirname(new_filepath), exist_ok=True)
image = cv2.imread(sample.filepath)
# TODO Create PR to imagecorruptions library to fix from source or create latest version of library
if corruption == "gaussian_blur":
corrupted = gaussian_blur(image, severity=severity)
elif corruption == "glass_blur":
corrupted = glass_blur(image, severity=severity)
else:
corrupted = corrupt(image, corruption_name=corruption, severity=severity)
cv2.imwrite(new_filepath, corrupted)
new_sample = fo.Sample(filepath=new_filepath,
tags=["corrupted"], corruption_name=corruption, corruption_severity=severity)
new_sample["original_sample_id"] = sample.id
sample._dataset.add_sample(new_sample)
def _execution_mode(ctx, inputs):
delegate = ctx.params.get("delegate", False)
if delegate:
description = "Uncheck this box to execute the operation immediately"
else:
description = "Check this box to delegate execution of this task"
inputs.bool(
"delegate",
default=False,
label="Delegate execution?",
description=description,
view=types.CheckboxView(),
)
if delegate:
inputs.view(
"notice",
types.Notice(
label=(
"You've chosen delegated execution. Note that you must "
"have a delegated operation service running in order for "
"this task to be processed. See "
"https://docs.voxel51.com/plugins/index.html#operators "
"for more information"
)
),
)
def register(plugin):
plugin.register(ImageCorruptions)