-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
569 lines (458 loc) · 15.9 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
import fiftyone.operators as foo
import fiftyone.operators.types as types
import fiftyone as fo
from fiftyone import ViewField as F
CMP_KEY_INFO_DICT_KEY = "model_differences"
ALL_CLASSES = "<all>"
def _get_label_fields(sample_collection, label_types):
schema = sample_collection.get_field_schema(embedded_doc_type=label_types)
return list(schema.keys())
def save_label(label_ids_to_vals, _id, case):
assert _id not in label_ids_to_vals
label_ids_to_vals[_id] = case
def set_label_cmp(samples, det_fld, label_id, val, cmp_key="cmp"):
view = samples.select_labels(ids=label_id)
sample = view.first()
assert len(sample[det_fld].detections) == 1
sample[det_fld].detections[0][cmp_key] = val
sample.save()
def analyze_match_delta(
ctx,
samples,
gtfld,
fld0,
fld1,
ekey0,
ekey1,
iou_thresh=0.2,
cmp_key="cmp",
):
# real dets
# for pred0 and pred1:
# * these are either fn (miss), or hit
#
# so for any det the options are
# 1. miss -> miss
# 2. miss -> hit IMPROVE
# 3. hit -> miss REGRESS
# 4a. hit -> hit, similar iou. SAME
# 4b. hit -> hit, worse iou. REGRESS
# 4c. hit -> hit, better iou. IMPROVE
fld_match0 = ekey0
fld_match1 = ekey1
fld_id0 = ekey0 + "_id"
fld_id1 = ekey1 + "_id"
fld_iou0 = ekey0 + "_iou"
fld_iou1 = ekey1 + "_iou"
fld_cmp = cmp_key
fld_cmp_id0 = cmp_key + "_id0"
fld_cmp_id1 = cmp_key + "_id1"
label_ids_to_vals_0 = {}
label_ids_to_vals_1 = {}
label_cmpfld_0 = f"{fld0}.detections.{cmp_key}"
label_cmpfld_1 = f"{fld1}.detections.{cmp_key}"
num_total = len(samples)
for idx, sample in enumerate(
samples.iter_samples(progress=True, autosave=True)
):
det_map = {}
cnt_map = {"missmiss": 0, "misshit": 0, "hitmiss": 0, "hithit": 0}
if sample[gtfld] is not None:
dets = sample[gtfld].detections
for didx, d in enumerate(dets):
_id = d["id"]
match0 = d[fld_match0]
match1 = d[fld_match1]
if match0 == "fn" and match1 == "fn":
case = "missmiss"
res = (case,)
d[fld_cmp] = case
elif match0 == "fn" and match1 == "tp":
case = "misshit"
id1 = d[fld_id1]
res = (case, id1)
d[fld_cmp] = case
d[fld_cmp_id1] = id1
save_label(label_ids_to_vals_1, id1, case)
elif match0 == "tp" and match1 == "fn":
case = "hitmiss"
id0 = d[fld_id0]
res = (case, id0)
d[fld_cmp] = case
d[fld_cmp_id0] = id0
save_label(label_ids_to_vals_0, id0, case)
else:
assert match0 == "tp" and match1 == "tp"
id0 = d[fld_id0]
id1 = d[fld_id1]
iou0 = d[fld_iou0]
iou1 = d[fld_iou1]
if iou1 - iou0 > iou_thresh:
case = "hithit+"
elif iou0 - iou1 > iou_thresh:
case = "hithit-"
else:
case = "hithit"
res = (case, id0, id1)
d[fld_cmp] = case
d[fld_cmp_id0] = id0
d[fld_cmp_id1] = id1
save_label(label_ids_to_vals_0, id0, case)
save_label(label_ids_to_vals_1, id1, case)
assert _id not in det_map
det_map[_id] = res
case_base = case.rstrip("+-")
cnt_map[case_base] += 1
sample[gtfld].detections[didx] = d
for d in sample[gtfld].detections:
assert d[fld_cmp] is not None
for k, v in cnt_map.items():
k = cmp_key + "_" + k
sample[k] = v
progress = idx / num_total
label = f"Loaded {idx} of {num_total}"
yield _set_progress(ctx, progress, label=label)
samples.set_label_values(label_cmpfld_0, label_ids_to_vals_0)
samples.set_label_values(label_cmpfld_1, label_ids_to_vals_1)
# Todo: false positives
# these dont get matched. so at sample level can count
yield
class ComputeChanges(foo.Operator):
LABEL = "Compute Model Differences"
@property
def config(self):
return foo.OperatorConfig(
name="compute_changes",
label=self.LABEL,
dynamic=True,
execute_as_generator=True,
)
def resolve_input(self, ctx):
inputs = types.Object()
ready = _compute_changes_inputs(ctx, inputs)
if ready:
_execution_mode(ctx, inputs)
return types.Property(inputs, view=types.View(label=self.LABEL))
def resolve_delegation(self, ctx):
return ctx.params.get("delegate", False)
def execute(self, ctx):
samples = ctx.dataset
samples.reload()
cmp_key = ctx.params.get("cmp_key", "cmp")
gt_field = ctx.params.get("gt_field", "ground_truth")
pd0_field = ctx.params.get("p0_field", "predictions0")
pd1_field = ctx.params.get("p1_field", "predictions1")
eval_key0 = ctx.params.get("ekey0", "evaluation0")
eval_key1 = ctx.params.get("ekey1", "evlauation1")
info_dict = samples.info
if CMP_KEY_INFO_DICT_KEY not in info_dict:
info_dict[CMP_KEY_INFO_DICT_KEY] = {}
info_dict[CMP_KEY_INFO_DICT_KEY][cmp_key] = {
"gt_field": gt_field,
"pd0_field": pd0_field,
"pd1_field": pd1_field,
"eval_key0": eval_key0,
"eval_key1": eval_key1,
}
samples.save()
for update in analyze_match_delta(
ctx,
samples,
gt_field,
pd0_field,
pd1_field,
eval_key0,
eval_key1,
iou_thresh=0.1,
cmp_key=cmp_key,
):
yield update
yield samples.add_dynamic_sample_fields()
yield ctx.trigger("reload_dataset")
def add_menu(ctx, inputs, input_name, choices_view, label, description=None):
inputs.enum(
input_name,
choices_view.values(),
required=True,
label=label,
description=description,
view=choices_view,
)
input_val = ctx.params.get(input_name, None)
return input_val is not None
def _compute_changes_inputs(ctx, inputs):
dataset = ctx.dataset
label_fields = _get_label_fields(dataset, (fo.Detections,))
label_field_choices = types.DropdownView()
for field_name in sorted(label_fields):
label_field_choices.add_choice(field_name, label=field_name)
if not add_menu(
ctx,
inputs,
"gt_field",
label_field_choices,
"Ground Truth Field",
"The label field containing ground truth detections",
):
return False
if not add_menu(
ctx,
inputs,
"p0_field",
label_field_choices,
"First Model Field",
"The label field containing the first model predictions",
):
return False
if not add_menu(
ctx,
inputs,
"p1_field",
label_field_choices,
"Second Model Field",
"The label field containing the second model predictions",
):
return False
evals = dataset.list_evaluations()
eval_field_choices = types.DropdownView()
for eval in sorted(evals):
eval_field_choices.add_choice(eval, label=eval)
if not add_menu(
ctx,
inputs,
"ekey0",
eval_field_choices,
"Evaluation Key for First Model",
):
return False
if not add_menu(
ctx,
inputs,
"ekey1",
eval_field_choices,
"Evaluation Key for Second Model",
):
return False
inputs.str(
"cmp_key",
required=True,
label="Model Comparison Key",
description="Supply a key for this model comparison",
)
cmp_key = ctx.params.get("cmp_key", None)
if cmp_key is None:
return False
return types.Property(
inputs, view=types.View(label="Compute Model Differences")
)
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,
required=True,
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/using_plugins.html#delegated-operations "
"for more information"
)
),
)
def _set_progress(ctx, progress, label=None):
# https://github.com/voxel51/fiftyone/pull/3516
# return ctx.trigger("set_progress", dict(progress=progress, label=label))
loading = types.Object()
loading.float("progress", view=types.ProgressView(label=label))
return ctx.trigger(
"show_output",
dict(
outputs=types.Property(loading).to_json(),
results={"progress": progress},
),
)
def _view_changes_menu(ctx, inputs):
dataset = ctx.dataset
info_dict = dataset.info
if CMP_KEY_INFO_DICT_KEY not in info_dict:
return False
label_fields = list(info_dict[CMP_KEY_INFO_DICT_KEY].keys())
label_field_choices = types.DropdownView()
for field_name in sorted(label_fields):
label_field_choices.add_choice(field_name, label=field_name)
if not add_menu(
ctx,
inputs,
"cmp_key",
label_field_choices,
"Comparison Key",
"The comparison key used when Computing Model Differences",
):
return False
cmp_key = ctx.params.get("cmp_key", None)
cmp_dict = dataset.info[CMP_KEY_INFO_DICT_KEY][cmp_key]
gt_field = cmp_dict["gt_field"]
gt_det_cmp_field = f"{gt_field}.detections.{cmp_key}"
type_vals = dataset.distinct(gt_det_cmp_field)
type_selector = types.AutocompleteView()
for ty in type_vals:
type_selector.add_choice(ty, label=ty)
if not add_menu(
ctx,
inputs,
"type",
type_selector,
"Type of change to view", # "type desc"
):
return False
gt_cls_field = f"{gt_field}.detections.label"
gt_classes = dataset.distinct(gt_cls_field)
gt_classes = [
ALL_CLASSES,
] + gt_classes
class_selector = types.AutocompleteView()
for cl in gt_classes:
class_selector.add_choice(cl, label=cl)
if not add_menu(
ctx,
inputs,
"class",
class_selector,
"Class to view",
):
return False
return types.Property(
inputs, view=types.View(label="View Model Differences")
)
def _delete_comparison_menu(ctx, inputs):
dataset = ctx.dataset
info_dict = dataset.info
if CMP_KEY_INFO_DICT_KEY not in info_dict:
return False
label_fields = list(info_dict[CMP_KEY_INFO_DICT_KEY].keys())
label_field_choices = types.DropdownView()
for field_name in sorted(label_fields):
label_field_choices.add_choice(field_name, label=field_name)
if not add_menu(
ctx, inputs, "cmp_key", label_field_choices, "Comparison Key"
):
return False
return types.Property(
inputs, view=types.View(label="View Model Differences")
)
class ViewChanges(foo.Operator):
LABEL = "View Model Differences"
@property
def config(self):
return foo.OperatorConfig(
name="view_changes",
label=ViewChanges.LABEL,
dynamic=True, # execute_as_generator=True,
)
def resolve_delegation(self, ctx):
return ctx.params.get("delegate", False)
def resolve_input(self, ctx):
inputs = types.Object()
ready = _view_changes_menu(ctx, inputs)
# if ready:
# inputs.bool(
# "groupby_scene",
# default=False,
# required=True,
# label="Group by scene?",
# view=types.CheckboxView(),
# )
# groupby_scene = ctx.params.get("groupby_scene", False)
return types.Property(inputs, view=types.View(label=self.LABEL))
def execute(self, ctx):
dataset = ctx.dataset
cmp_key = ctx.params.get("cmp_key", None)
type = ctx.params.get("type", None)
label_class = ctx.params.get("class", None)
cmp_dict = dataset.info[CMP_KEY_INFO_DICT_KEY][cmp_key]
gt_field = cmp_dict["gt_field"]
pd0_field = cmp_dict["pd0_field"]
pd1_field = cmp_dict["pd1_field"]
if label_class == ALL_CLASSES:
view_expr = F(cmp_key) == type
else:
view_expr = (F(cmp_key) == type) & (F("label") == label_class)
if type == "misshit":
view = (
dataset.filter_labels(gt_field, view_expr)
.filter_labels(pd1_field, view_expr)
.filter_labels(pd0_field, view_expr, only_matches=False)
)
elif type == "hitmiss":
view = (
dataset.filter_labels(gt_field, view_expr)
.filter_labels(pd0_field, view_expr)
.filter_labels(pd1_field, view_expr, only_matches=False)
)
elif type == "missmiss":
view = (
dataset.filter_labels(gt_field, view_expr)
.filter_labels(pd0_field, view_expr, only_matches=False)
.filter_labels(pd1_field, view_expr, only_matches=False)
)
else:
view = (
dataset.filter_labels(gt_field, view_expr)
.filter_labels(pd0_field, view_expr)
.filter_labels(pd1_field, view_expr)
)
# groupby_scene = ctx.params.get('groupby_scene',None)
# if groupby_scene:
# view = view.group_by('scene',order_by='frame')
ctx.trigger("set_view", {"view": view._serialize()})
def resolve_output(self, ctx):
outputs = types.Object()
outputs.int("updated", label="Updated")
return types.Property(outputs)
class DeleteComparison(foo.Operator):
LABEL = "Delete Model Comparison"
@property
def config(self):
return foo.OperatorConfig(
name="delete_comparison",
label=DeleteComparison.LABEL,
dynamic=True,
)
def resolve_delegation(self, ctx):
return ctx.params.get("delegate", False)
def resolve_input(self, ctx):
inputs = types.Object()
ready = _delete_comparison_menu(ctx, inputs)
return types.Property(inputs, view=types.View(label=self.LABEL))
def execute(self, ctx):
dataset = ctx.dataset
fields = dataset.get_field_schema(flat=True).keys()
cmp_key = ctx.params.get("cmp_key", None)
fields_rm = [
x for x in fields if cmp_key in x
] # Assumes unique-ish cmp_key!
dataset.delete_sample_fields(fields_rm)
info_dict = dataset.info
info_dict[CMP_KEY_INFO_DICT_KEY].pop(cmp_key, None)
ctx.trigger("reload_dataset")
def resolve_output(self, ctx):
outputs = types.Object()
outputs.int("Success", label="Success")
return types.Property(outputs)
def register(p):
p.register(ViewChanges)
p.register(ComputeChanges)
p.register(DeleteComparison)