-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
766 lines (673 loc) · 27.2 KB
/
metrics.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
import numpy as np
import gzip
from os.path import join
from multimatch import docomparison
# from saliency_metrics import cc, nss
import cv2
from tqdm import tqdm
def zero_one_similarity(a, b):
if a == b:
return 1.0
else:
return 0.0
def nw_matching(pred_string, gt_string, gap=0.0):
# NW string matching with zero_one_similarity
F = np.zeros((len(pred_string) + 1, len(gt_string) + 1), dtype=np.float32)
for i in range(1 + len(pred_string)):
F[i, 0] = gap * i
for j in range(1 + len(gt_string)):
F[0, j] = gap * j
for i in range(1, 1 + len(pred_string)):
for j in range(1, 1 + len(gt_string)):
a = pred_string[i - 1]
b = gt_string[j - 1]
match = F[i - 1, j - 1] + zero_one_similarity(a, b)
delete = F[i - 1, j] + gap
insert = F[i, j - 1] + gap
F[i, j] = np.max([match, delete, insert])
score = F[len(pred_string), len(gt_string)]
return score / max(len(pred_string), len(gt_string))
def scanpath2clusters(meanshift, scanpath):
string = []
xs = scanpath['X']
ys = scanpath['Y']
for i in range(len(xs)):
symbol = meanshift.predict([[xs[i], ys[i]]])[0]
string.append(symbol)
return string
def postprocessScanpaths(trajs):
# convert actions to scanpaths
scanpaths = []
for traj in trajs:
task_name, img_name, condition, subject, fixs = traj
scanpaths.append({
'X': fixs[:, 1],
'Y': fixs[:, 0],
'T': fixs[:, 2],
'subject':subject,
'name': img_name,
'task': task_name,
'condition': condition
})
return scanpaths
# compute sequence score
def compute_SS(preds, clusters, truncate, reduce='mean', print_clusters = False):
results = []
for scanpath in tqdm(preds):
key = 'test-{}-{}-{}'.format(scanpath['condition'], scanpath['task'],
scanpath['name'][:-4])
ms = clusters[key]
strings = ms['strings']
cluster = ms['cluster']
pred = scanpath2clusters(cluster, scanpath)
scores = []
for gt in strings.values():
if len(gt) > 0:
pred = pred[:truncate] if len(pred) > truncate else pred
gt = gt[:truncate] if len(gt) > truncate else gt
if print_clusters:
print(pred, gt)
score = nw_matching(pred, gt)
scores.append(score)
result = {}
result['condition'] = scanpath['condition']
result['task'] = scanpath['task']
result['name'] = scanpath['name']
if reduce == 'mean':
result['score'] = np.array(scores).mean()
elif reduce == 'max':
result['score'] = max(scores)
else:
raise NotImplementedError
results.append(result)
return results
# compute sequence score
def compute_SS_Time(preds, clusters, truncate, time_dict, reduce='mean', print_clusters = False, tempbin = 50):
results = []
for scanpath in tqdm(preds):
key = 'test-{}-{}-{}'.format(scanpath['condition'], scanpath['task'],
scanpath['name'][:-4])
ms = clusters[key]
strings = ms['strings']
cluster = ms['cluster']
pred = scanpath2clusters(cluster, scanpath)
scores = []
for subj, gt in strings.items():
if len(gt) > 0:
time_string = time_dict[key+'-'+str(subj)]
pred = pred[:truncate] if len(pred) > truncate else pred
gtime_string = time_string[:truncate] if len(time_string) > truncate else time_string
ptime_string = scanpath['T'][:truncate] if len(scanpath['T']) > truncate else scanpath['T']
gt = gt[:truncate] if len(gt) > truncate else gt
if print_clusters:
print(pred, gt)
pred_time = []
gt_time = []
for p, t_p in zip(pred, ptime_string):
pred_time.extend([p for _ in range(int(t_p/tempbin))])
for g, t_g in zip(gt, gtime_string):
gt_time.extend([g for _ in range(int(t_g/tempbin))])
score = nw_matching(pred_time, gt_time)
scores.append(score)
result = {}
result['condition'] = scanpath['condition']
result['task'] = scanpath['task']
result['name'] = scanpath['name']
if reduce == 'mean':
result['score'] = np.array(scores).mean()
elif reduce == 'max':
result['score'] = max(scores)
else:
raise NotImplementedError
results.append(result)
return results
def get_seq_score(preds, clusters, max_step, tasks=None, print_clusters = False):
results = compute_SS(preds, clusters, truncate=max_step, print_clusters=print_clusters)
if tasks is None:
return np.mean([r['score'] for r in results])
else:
scores = []
for task in tasks:
scores.append(
np.mean([r['score'] for r in results if r['task'] == task]))
return dict(zip(tasks, scores))
def get_seq_score_time(preds, clusters, max_step, time_dict, tasks=None, print_clusters = False):
results = compute_SS_Time(preds, clusters, truncate=max_step, time_dict = time_dict, print_clusters=print_clusters)
if tasks is None:
return np.mean([r['score'] for r in results])
else:
scores = []
for task in tasks:
scores.append(
np.mean([r['score'] for r in results if r['task'] == task]))
return dict(zip(tasks, scores))
def scanpath2categories(seg_map, scanpath):
string = []
xs = scanpath['X']
ys = scanpath['Y']
ts = scanpath['T']
for x,y,t in zip(xs, ys, ts):
symbol = str(int(seg_map[int(y), int(x)]))
string.append((symbol, t))
return string
# compute semantic sequence score
def compute_SSS(preds, fixations, truncate, segmentation_map_dir, reduce='mean'):
results = []
for scanpath in preds:
#print(len(results), '/', len(preds), end='\r')
key = 'test-{}-{}-{}'.format(scanpath['condition'], scanpath['task'],
scanpath['name'][:-4])
strings = list(fixations[key])
with gzip.GzipFile(join(segmentation_map_dir, scanpath['name'][:-3]+'npy.gz'), "r") as r:
segmentation_map = np.load(r, allow_pickle=True)
r.close()
pred = scanpath2categories(segmentation_map, scanpath)
scores = []
human_scores = []
pred = pred[:truncate] if len(pred) > truncate else pred
pred_noT = [i[0] for i in pred]
for gt in strings:
if len(gt) > 0:
gt = gt[:truncate] if len(gt) > truncate else gt
gt_noT = [i[0] for i in gt]
score = nw_matching(pred_noT, gt_noT)
scores.append(score)
result = {}
result['condition'] = scanpath['condition']
result['task'] = scanpath['task']
result['name'] = scanpath['name']
if reduce == 'mean':
result['score'] = np.array(scores).mean()
elif reduce == 'max':
result['score'] = max(scores)
else:
raise NotImplementedError
results.append(result)
return results
# compute semantic sequence score
def compute_SSS_time(preds, fixations, truncate, segmentation_map_dir, reduce='mean', tempbin=50):
results = []
for scanpath in preds:
#print(len(results), '/', len(preds), end='\r')
key = 'test-{}-{}-{}'.format(scanpath['condition'], scanpath['task'],
scanpath['name'][:-4])
strings = list(fixations[key])
with gzip.GzipFile(join(segmentation_map_dir, scanpath['name'][:-3]+'npy.gz'), "r") as r:
segmentation_map = np.load(r, allow_pickle=True)
r.close()
pred = scanpath2categories(segmentation_map, scanpath)
scores = []
human_scores = []
pred_T = []
pred = pred[:truncate] if len(pred) > truncate else pred
for p in pred:
pred_T.extend([p[0] for _ in range(int(p[1]/tempbin))])
for gt in strings:
gt_T = []
if len(gt) > 0:
gt = gt[:truncate] if len(gt) > truncate else gt
for g in gt:
gt_T.extend([g[0] for _ in range(int(g[1]/tempbin))])
score = nw_matching(pred_T, gt_T)
scores.append(score)
del gt_T
result = {}
result['condition'] = scanpath['condition']
result['task'] = scanpath['task']
result['name'] = scanpath['name']
if reduce == 'mean':
result['score'] = np.array(scores).mean()
elif reduce == 'max':
result['score'] = max(scores)
else:
raise NotImplementedError
results.append(result)
return results
def get_semantic_seq_score(preds, fixations, max_step, segmentation_map_dir, tasks=None):
results = compute_SSS(preds, fixations, truncate=max_step, segmentation_map_dir = segmentation_map_dir)
if tasks is None:
return np.mean([r['score'] for r in results])
else:
scores = []
for task in tasks:
scores.append(
np.mean([r['score'] for r in results if r['task'] == task]))
return dict(zip(tasks, scores))
def get_semantic_seq_score_time(preds, fixations, max_step, segmentation_map_dir, tasks=None):
results = compute_SSS_time(preds, fixations, truncate=max_step, segmentation_map_dir = segmentation_map_dir)
if tasks is None:
return np.mean([r['score'] for r in results])
else:
scores = []
for task in tasks:
scores.append(
np.mean([r['score'] for r in results if r['task'] == task]))
return dict(zip(tasks, scores))
def multimatch(s1, s2, im_size):
s1x = s1['X']
s1y = s1['Y']
s1t = s1['T']
l1 = len(s1x)
if l1 < 3:
scanpath1 = np.ones((3, 3), dtype=np.float32)
scanpath1[:l1, 0] = s1x
scanpath1[:l1, 1] = s1y
scanpath1[:l1, 2] = s1t[:l1]
else:
scanpath1 = np.ones((l1, 3), dtype=np.float32)
scanpath1[:, 0] = s1x
scanpath1[:, 1] = s1y
scanpath1[:, 2] = s1t[:l1]
s2x = s2['X']
s2y = s2['Y']
s2t = s2['T']
l2 = len(s2x)
if l2 < 3:
scanpath2 = np.ones((3, 3), dtype=np.float32)
scanpath2[:l2, 0] = s2x
scanpath2[:l2, 1] = s2y
scanpath2[:l2, 2] = s2t[:l2]
else:
scanpath2 = np.ones((l2, 3), dtype=np.float32)
scanpath2[:, 0] = s2x
scanpath2[:, 1] = s2y
scanpath2[:, 2] = s2t[:l2]
mm = docomparison(scanpath1, scanpath2, sz=im_size)
return mm[0]
def compute_mm(human_trajs, model_trajs, im_w, im_h, tasks=None):
"""
compute scanpath similarity using multimatch
"""
all_mm_scores = []
for traj in model_trajs:
img_name = traj['name']
task = traj['task']
gt_trajs = list(
filter(lambda x: x['name'] == img_name and x['task'] == task,
human_trajs))
all_mm_scores.append((task,
np.mean([
multimatch(traj, gt_traj, (im_w, im_h))#[:4]
for gt_traj in gt_trajs
],
axis=0)))
if tasks is not None:
mm_tasks = {}
for task in tasks:
mm = np.array([x[1] for x in all_mm_scores if x[0] == task])
mm_tasks[task] = np.mean(mm, axis=0)
return mm_tasks
else:
return np.mean([x[1] for x in all_mm_scores], axis=0)
def _Levenshtein_Dmatrix_initializer(len1, len2):
Dmatrix = []
for i in range(len1):
Dmatrix.append([0] * len2)
for i in range(len1):
Dmatrix[i][0] = i
for j in range(len2):
Dmatrix[0][j] = j
return Dmatrix
def _Levenshtein_cost_step(Dmatrix, string_1, string_2, i, j, substitution_cost=1):
char_1 = string_1[i - 1]
char_2 = string_2[j - 1]
# insertion
insertion = Dmatrix[i - 1][j] + 1
# deletion
deletion = Dmatrix[i][j - 1] + 1
# substitution
substitution = Dmatrix[i - 1][j - 1] + substitution_cost * (char_1 != char_2)
# pick the cheapest
Dmatrix[i][j] = min(insertion, deletion, substitution)
def _Levenshtein(string_1, string_2, substitution_cost=1):
# get strings lengths and initialize Distances-matrix
len1 = len(string_1)
len2 = len(string_2)
Dmatrix = _Levenshtein_Dmatrix_initializer(len1 + 1, len2 + 1)
# compute cost for each step in dynamic programming
for i in range(len1):
for j in range(len2):
_Levenshtein_cost_step(Dmatrix,
string_1, string_2,
i + 1, j + 1,
substitution_cost=substitution_cost)
if substitution_cost == 1:
max_dist = max(len1, len2)
elif substitution_cost == 2:
max_dist = len1 + len2
return Dmatrix[len1][len2]
def compute_ED(preds, clusters, truncate, reduce='mean', print_clusters = False):
results = []
for scanpath in preds:
key = 'test-{}-{}-{}'.format(scanpath['condition'], scanpath['task'],
scanpath['name'][:-4])
ms = clusters[key]
strings = ms['strings']
cluster = ms['cluster']
pred = scanpath2clusters(cluster, scanpath)
scores = []
for gt in strings.values():
if len(gt) > 0:
pred = pred[:truncate] if len(pred) > truncate else pred
gt = gt[:truncate] if len(gt) > truncate else gt
if print_clusters:
print(pred, gt)
score = _Levenshtein(pred, gt)
scores.append(score)
result = {}
result['condition'] = scanpath['condition']
result['task'] = scanpath['task']
result['name'] = scanpath['name']
if reduce == 'mean':
result['score'] = np.array(scores).mean()
elif reduce == 'max':
result['score'] = max(scores)
else:
raise NotImplementedError
results.append(result)
return results
def compute_ED_Time(preds, clusters, truncate, time_dict, reduce='mean', print_clusters = False, tempbin = 50):
results = []
for scanpath in preds:
key = 'test-{}-{}-{}'.format(scanpath['condition'], scanpath['task'],
scanpath['name'][:-4])
ms = clusters[key]
strings = ms['strings']
cluster = ms['cluster']
pred = scanpath2clusters(cluster, scanpath)
scores = []
for subj, gt in strings.items():
if len(gt) > 0:
time_string = time_dict[key+'-'+str(subj)]
pred = pred[:truncate] if len(pred) > truncate else pred
gtime_string = time_string[:truncate] if len(time_string) > truncate else time_string
ptime_string = scanpath['T'][:truncate] if len(scanpath['T']) > truncate else scanpath['T']
gt = gt[:truncate] if len(gt) > truncate else gt
if print_clusters:
print(pred, gt)
pred_time = []
gt_time = []
for p, t_p in zip(pred, ptime_string):
pred_time.extend([p for _ in range(int(t_p/tempbin))])
for g, t_g in zip(gt, gtime_string):
gt_time.extend([g for _ in range(int(t_g/tempbin))])
score = _Levenshtein(pred_time, gt_time)
scores.append(score)
result = {}
result['condition'] = scanpath['condition']
result['task'] = scanpath['task']
result['name'] = scanpath['name']
if reduce == 'mean':
result['score'] = np.array(scores).mean()
elif reduce == 'max':
result['score'] = max(scores)
else:
raise NotImplementedError
results.append(result)
return results
def get_ed(preds, clusters, max_step, tasks=None, print_clusters = False):
results = compute_ED(preds, clusters, truncate=max_step, print_clusters=print_clusters)
if tasks is None:
return np.mean([r['score'] for r in results])
else:
scores = []
for task in tasks:
scores.append(
np.mean([r['score'] for r in results if r['task'] == task]))
return dict(zip(tasks, scores))
def get_ed_time(preds, clusters, max_step, time_dict, tasks=None, print_clusters = False):
results = compute_ED_Time(preds, clusters, truncate=max_step, time_dict = time_dict, print_clusters=print_clusters)
if tasks is None:
return np.mean([r['score'] for r in results])
else:
scores = []
for task in tasks:
scores.append(
np.mean([r['score'] for r in results if r['task'] == task]))
return dict(zip(tasks, scores))
# compute semantic sequence score
def compute_SED(preds, fixations, truncate, segmentation_map_dir, reduce='mean'):
results = []
for scanpath in preds:
#print(len(results), '/', len(preds), end='\r')
key = 'test-{}-{}-{}'.format(scanpath['condition'], scanpath['task'],
scanpath['name'][:-4])
strings = list(fixations[key])
with gzip.GzipFile(join(segmentation_map_dir, scanpath['name'][:-3]+'npy.gz'), "r") as r:
segmentation_map = np.load(r, allow_pickle=True)
r.close()
pred = scanpath2categories(segmentation_map, scanpath)
scores = []
human_scores = []
pred = pred[:truncate] if len(pred) > truncate else pred
pred_noT = [i[0] for i in pred]
for gt in strings:
if len(gt) > 0:
gt = gt[:truncate] if len(gt) > truncate else gt
gt_noT = [i[0] for i in gt]
score = _Levenshtein(pred_noT, gt_noT)
scores.append(score)
result = {}
result['condition'] = scanpath['condition']
result['task'] = scanpath['task']
result['name'] = scanpath['name']
if reduce == 'mean':
result['score'] = np.array(scores).mean()
elif reduce == 'max':
result['score'] = max(scores)
else:
raise NotImplementedError
results.append(result)
return results
# compute semantic sequence score
def compute_SED_time(preds, fixations, truncate, segmentation_map_dir, reduce='mean', tempbin=50):
results = []
for scanpath in preds:
#print(len(results), '/', len(preds), end='\r')
key = 'test-{}-{}-{}'.format(scanpath['condition'], scanpath['task'],
scanpath['name'][:-4])
strings = list(fixations[key])
with gzip.GzipFile(join(segmentation_map_dir, scanpath['name'][:-3]+'npy.gz'), "r") as r:
segmentation_map = np.load(r, allow_pickle=True)
r.close()
pred = scanpath2categories(segmentation_map, scanpath)
scores = []
human_scores = []
pred_T = []
pred = pred[:truncate] if len(pred) > truncate else pred
for p in pred:
pred_T.extend([p[0] for _ in range(int(p[1]/tempbin))])
for gt in strings:
gt_T = []
if len(gt) > 0:
gt = gt[:truncate] if len(gt) > truncate else gt
for g in gt:
gt_T.extend([g[0] for _ in range(int(g[1]/tempbin))])
score = _Levenshtein(pred_T, gt_T)
scores.append(score)
del gt_T
result = {}
result['condition'] = scanpath['condition']
result['task'] = scanpath['task']
result['name'] = scanpath['name']
if reduce == 'mean':
result['score'] = np.array(scores).mean()
elif reduce == 'max':
result['score'] = max(scores)
else:
raise NotImplementedError
results.append(result)
return results
def get_semantic_ed(preds, fixations, max_step, segmentation_map_dir, tasks=None):
results = compute_SED(preds, fixations, truncate=max_step, segmentation_map_dir = segmentation_map_dir)
if tasks is None:
return np.mean([r['score'] for r in results])
else:
scores = []
for task in tasks:
scores.append(
np.mean([r['score'] for r in results if r['task'] == task]))
return dict(zip(tasks, scores))
def get_semantic_ed_time(preds, fixations, max_step, segmentation_map_dir, tasks=None):
results = compute_SED_time(preds, fixations, truncate=max_step, segmentation_map_dir = segmentation_map_dir)
if tasks is None:
return np.mean([r['score'] for r in results])
else:
scores = []
for task in tasks:
scores.append(
np.mean([r['score'] for r in results if r['task'] == task]))
return dict(zip(tasks, scores))
# def get_cc(pred_dict, gt_dict):
# cc_res = []
# for key in gt_dict.keys():
# gt_list = gt_dict[key]
# pred_list = pred_dict[key]
# for g in gt_list:
# gt_map = cv2.imread(g,0)
# res = []
# for p in pred_list:
# pred_map = cv2.imread(p,0)
# res.append(cc(pred_map, gt_map))
# cc_res.append(np.mean(res))
# return np.mean(cc_res)
#
#
# def get_nss(pred_dict, gt_dict):
# nss_res = []
# for key in gt_dict.keys():
# gt_list = gt_dict[key]
# pred_list = pred_dict[key]
# for g in gt_list:
# gt_map = cv2.imread(g,0)
# res = []
# for p in pred_list:
# pred_map = cv2.imread(p,0)
# res.append(nss(pred_map, gt_map))
# nss_res.append(np.mean(res))
# return np.mean(nss_res)
def compute_spatial_metrics_by_step(predicted_trajs,
gt_scanpaths,
im_w=512,
im_h=320,
end_step=1):
sample_ids = np.unique(
[traj['task'] + '_' + traj['name'] for traj in predicted_trajs])
num_fixs = 0
cc = 0
nss = 0
for sample_id in sample_ids:
task, image = sample_id.split('_')
trajs = list(
filter(lambda x: x['task'] == task and x['name'] == image,
predicted_trajs))
assert len(trajs) > 0, 'empty trajs.'
# removing the predifined first fixation
Xs = np.concatenate([traj['X'] for traj in trajs])
Ys = np.concatenate([traj['Y'] for traj in trajs])
if Xs.size == 0:
continue
fixs = np.stack([Xs, Ys]).T.astype(np.int32)
pred_smap = convert_fixations_to_map(fixs,
im_w,
im_h,
smooth=True)
gt_trajs = list(
filter(lambda x: x['task'] == task and x['name'] == image,
gt_scanpaths))
assert len(gt_trajs) > 0, 'empty trajs.'
Xs = np.concatenate([traj['X'] for traj in gt_trajs])
Ys = np.concatenate([traj['Y'] for traj in gt_trajs])
gt_fixs = np.stack([Xs, Ys]).T.astype(np.int32)
gt_smap = convert_fixations_to_map(gt_fixs,
im_w,
im_h,
smooth=True)
num_fixs += len(gt_fixs)
cc += CC(pred_smap, gt_smap)
nss += NSS(pred_smap, gt_fixs)
return cc / len(sample_ids), nss / num_fixs
def convert_fixations_to_map(fixs,
width,
height,
return_distribution=True,
smooth=True,
visual_angle=16):
assert len(fixs) > 0, 'Empty fixation list!'
fmap = np.zeros((height, width))
for i in range(len(fixs)):
x, y = fixs[i][0], fixs[i][1]
fmap[y, x] += 1
if smooth:
# fmap = filters.gaussian_filter(fmap, sigma=visual_angle)
fmap = cv2.GaussianBlur(fmap, [0,0], 9,9)
if return_distribution:
fmap /= fmap.sum()
return fmap
def info_gain(predicted_probs, gt_fixs, base_probs, eps=2.2204e-16):
fired_probs = predicted_probs[gt_fixs[:, 1], gt_fixs[:, 0]]
fired_base_probs = base_probs[gt_fixs[:, 1], gt_fixs[:, 0]]
IG = np.sum(np.log2(fired_probs + eps) - np.log2(fired_base_probs + eps))
return IG
def CC(saliency_map_1, saliency_map_2):
def normalize(saliency_map):
saliency_map -= saliency_map.mean()
std = saliency_map.std()
if std:
saliency_map /= std
return saliency_map, std == 0
smap1, constant1 = normalize(saliency_map_1.copy())
smap2, constant2 = normalize(saliency_map_2.copy())
if constant1 and not constant2:
return 0.0
else:
return np.corrcoef(smap1.flatten(), smap2.flatten())[0, 1]
def NSS(saliency_map, gt_fixs):
xs, ys = gt_fixs[:, 0], gt_fixs[:, 1]
mean = saliency_map.mean()
std = saliency_map.std()
value = saliency_map[ys, xs].copy()
value -= mean
if std:
value /= std
return value.sum()
def get_num_step2target(X, Y, bbox):
X, Y = np.array(X), np.array(Y)
on_target_X = np.logical_and(X > bbox[0], X < bbox[0] + bbox[2])
on_target_Y = np.logical_and(Y > bbox[1], Y < bbox[1] + bbox[3])
on_target = np.logical_and(on_target_X, on_target_Y)
if np.sum(on_target) > 0:
first_on_target_idx = np.argmax(on_target)
return first_on_target_idx + 1
else:
return 1000 # some big enough number
def scanpath_ratio(traj, bbox):
X1, Y1 = traj['X'][:-1], traj['Y'][:-1]
X2, Y2 = traj['X'][1:], traj['Y'][1:]
traj_dist = np.sum(np.sqrt((X1 - X2)**2 + (Y1 - Y2)**2))
cx, cy = traj['X'][0], traj['Y'][0]
tx, ty = bbox[0] + bbox[2] / 2.0, bbox[1] + bbox[3] / 2.0
target_dist = np.sqrt((tx - cx)**2 + (ty - cy)**2)
if traj_dist == 0:
print("error traj", traj)
return min(target_dist / traj_dist, 1.0)
def compute_avgSPRatio(trajs, target_annos, max_step, tasks=None):
all_sp_ratios = []
for traj in trajs:
key = traj['task'] + '_' + traj['name']
bbox = target_annos[key]
num_step = get_num_step2target(traj['X'], traj['Y'], bbox)
if num_step > max_step + 1: # skip failed scanpaths
continue
sp = {'X': traj['X'][:num_step], 'Y': traj['Y'][:num_step]}
if len(sp['X']) == 1: # skip single-step scanpaths
continue
all_sp_ratios.append((traj['task'], scanpath_ratio(sp, bbox)))
if tasks is not None:
avg_sp_ratios = {}
for task in tasks:
sp_ratios = [x[1] for x in all_sp_ratios if x[0] == task]
avg_sp_ratios[task] = np.mean(sp_ratios)
return avg_sp_ratios
else:
return np.mean([x[1] for x in all_sp_ratios])