-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhuman_accuracy.py
151 lines (124 loc) · 5.95 KB
/
human_accuracy.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
import dateparser
import collections
def load_ha(ha_data):
return HumanAccuracyPortable(annot_data=ha_data["annot_data"],
labeled_images=ha_data["labeled_images"],
image_labels=ha_data["image_labels"],
split_wnids=ha_data["split_wnids"],
image_map=ha_data["image_map"],
all_candidates=ha_data["all_candidates"],
wnid_map=ha_data["wnid_map"])
class HumanAccuracyPortable(object):
def __init__(self, annot_data, labeled_images, image_map, image_labels, split_wnids, all_candidates, wnid_map):
self._image_map = image_map
self.labeled_images = labeled_images
self.image_labels = image_labels
self.annot_data = annot_data
self.all_candidates = all_candidates
self.PROBLEMATIC_FILTER = lambda image, image_labels, user_label: not image["problematic"]
self.V2_FILTER = lambda image, image_labels, user_label: image["id"] in self.all_candidates
self.VAL_FILTER = lambda image, image_labels, user_label: not image["id"] in self.all_candidates
self.split_wnids = split_wnids
self.wnid_map = wnid_map
self.rev_wnid_map = collections.defaultdict(list)
for k,v in self.wnid_map.items():
self.rev_wnid_map[v].append(k)
self.fast_imgs = set()
self.slow_imgs = set()
def NOTDOG_FILTER(image, image_labels, user_label):
wnid = self.wnid_map[image['id']]
if wnid in self.split_wnids[('organism', 'dog')]:
return False
else:
return True
self.NOTDOG_FILTER = NOTDOG_FILTER
def FASTIMAGES_FILTER(image, image_labels, user_label):
if image['id'] in self.fast_imgs:
return True
elif image['id'] in self.slow_imgs:
return False
human_users = ['human_a', 'human_b', 'human_c', 'human_d', 'human_e']
times_spent = []
for user in human_users:
start_time = dateparser.parse(self.annot_data[user][image['id']]['extra_info']['start_time'])
end_time = dateparser.parse(self.annot_data[user][image['id']]['extra_info']['end_time'])
times_spent.append((end_time - start_time).seconds)
if np.median(times_spent) < 60:
self.fast_imgs.add(image['id'])
return True
else:
self.slow_imgs.add(image['id'])
return False
self.FASTIMAGES_FILTER = FASTIMAGES_FILTER
def NOT_PROBLEMATIC(image, image_labels, user_label):
if image['problematic']:
return False
else:
return True
self.NOT_PROBLEMATIC = NOT_PROBLEMATIC
def OBJECT_FILTER(image, image_labels, user_label):
wnid = self.wnid_map[image['id']]
if wnid in self.split_wnids[('object',)]:
return True
else:
return False
self.OBJECT_FILTER = OBJECT_FILTER
def ORGANISM_FILTER(image, image_labels, user_label):
wnid = self.wnid_map[image['id']]
if wnid in self.split_wnids[('organism',)]:
return True
else:
return False
self.ORGANISM_FILTER = ORGANISM_FILTER
def VAL_SET(image, image_labels, user_label):
prefix = "ILSVRC2012_val_"
len_prefix = len(prefix)
if image['id'][:len_prefix] == prefix:
return True
else:
return False
self.VAL_SET = VAL_SET
def VTWO_SET(image, image_labels, user_label):
return not VAL_SET(image, image_labels, user_label)
self.VTWO_SET = VTWO_SET
def ORGANISM_NOT_DOG_FILTER(image, image_labels, user_label):
wnid = self.wnid_map[image['id']]
if wnid in self.split_wnids[('object')]:
return True
else:
return False
self.ORGANISM_NOT_DOG_FILTER = ORGANISM_NOT_DOG_FILTER
def grade_predictions(self, username, filters=[], filter_join=all, correct_state=["correct"], top1=False):
correct = []
incorrect = []
user_annot_data = self.annot_data[username]
for image_id in self.labeled_images:
user_label = user_annot_data[image_id]
image = self._image_map[image_id]
cur_image_labels = self.image_labels[image_id]
filter_result = [f(image, cur_image_labels, user_label) for f in filters]
if not filter_join(filter_result):
continue
if top1:
if user_label['label'] == self.wnid_map[image_id]:
correct.append(image_id)
else:
incorrect.append(image_id)
else:
for state in correct_state:
if user_label["label"] in cur_image_labels[state]:
correct.append(image_id)
break
else:
incorrect.append(image_id)
return correct, incorrect
def compute_accuracy(self, username, filters=[], filter_join=all, correct_state=["correct"], top1=False):
correct, incorrect = self.grade_predictions(username, filters, filter_join, correct_state, top1=top1)
total = (len(incorrect) + len(correct))
accuracy = len(correct)/ total
return accuracy, total
def compute_acc_dict(h_a, username, extra_filters=[], top1=False):
val_acc, _ = h_a.compute_accuracy(username, filters=[h_a.PROBLEMATIC_FILTER, h_a.VAL_SET] + extra_filters, top1=top1)
v2_acc, _ = h_a.compute_accuracy(username, filters=[h_a.PROBLEMATIC_FILTER, h_a.VTWO_SET] + extra_filters, top1=top1)
drop = val_acc - v2_acc
return {"username": username, "val": round(val_acc, 4)*100, "v2": round(v2_acc, 4)*100}