-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevaluate.py
63 lines (52 loc) · 2.05 KB
/
evaluate.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
# _*_coding:utf-8 _*_
# Author : Tao
"""
This Code is ...
"""
import numpy as np
def evaluate_summary(machine_summary, user_summary, eval_metric='avg'):
"""
Compare machine summary with user summary (keyshot-based).
:param machine_summary: should be binary vectors of ndarray type
:param user_summary: should be binary vectors of ndarray type
:param eval_metric: eval_metric = {'avg', 'max'}
'avg' averages results of comparing multiple human summaries
'max' takes the maximum (best) out of multiple comparisons.
:return:
"""
machine_summary = machine_summary.astype(np.float32)
user_summary = user_summary.astype(np.float32)
n_users, n_frames = user_summary.shape
# binarization
machine_summary[machine_summary > 0] = 1
user_summary[user_summary > 0] = 1
if len(machine_summary) > n_frames:
machine_summary = machine_summary[:n_frames]
elif len(machine_summary) < n_frames:
zero_padding = np.zeros((n_frames - len(machine_summary)))
machine_summary = np.concatenate([machine_summary, zero_padding])
f_scores = []
prec_arr = []
rec_arr = []
for user_idx in range(n_users):
gt_summary = user_summary[user_idx, :]
overlap_duration = (machine_summary * gt_summary).sum()
precision = overlap_duration / (machine_summary.sum() + 1e-8)
recall = overlap_duration / (gt_summary.sum() + 1e-8)
if precision == 0 and recall == 0:
f_score = 0.
else:
f_score = (2 * precision * recall) / (precision + recall)
f_scores.append(f_score)
prec_arr.append(precision)
rec_arr.append(recall)
if eval_metric == 'avg':
final_f_score = np.mean(f_scores)
final_prec = np.mean(prec_arr)
final_rec = np.mean(rec_arr)
elif eval_metric == 'max':
final_f_score = np.max(f_scores)
max_idx = np.argmax(f_scores)
final_prec = prec_arr[max_idx]
final_rec = rec_arr[max_idx]
return final_f_score, final_prec, final_rec