forked from imisra/latent-noise-icnm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cap_eval_utils.py
311 lines (273 loc) · 10.5 KB
/
cap_eval_utils.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
# --------------------------------------------------------
# Written by Saurabh Gupta
# Modified by Ishan Misra
# --------------------------------------------------------
import numpy as np
import sg_utils
import im_utils
from scipy.interpolate import interp1d
from IPython.core.debugger import Tracer
import code
def calc_pr_ovr(counts, out, K):
"""
[P, R, score, ap] = calc_pr_ovr(counts, out, K)
Input :
counts : number of occurrences of this word in the ith image
out : score for this image
K : number of references
Output :
P, R : precision and recall
score : score which corresponds to the particular precision and recall
ap : average precision
"""
K = np.float64(K)
tog = np.hstack((counts[:,np.newaxis].astype(np.float64), out[:, np.newaxis].astype(np.float64)))
ind = np.argsort(out)
ind = ind[::-1]
score = np.array([tog[i,1] for i in ind])
sortcounts = np.array([tog[i,0] for i in ind])
tp = sortcounts*(1.-1./K);
fp = sortcounts.copy();
for i in xrange(sortcounts.shape[0]):
if sortcounts[i] > 1:
fp[i] = 0.;
elif sortcounts[i] == 0:
fp[i] = 1.;
elif sortcounts[i] == 1:
fp[i] = 1./K;
P = np.cumsum(tp)/(np.cumsum(tp) + np.cumsum(fp));
# c = accumarray(sortcounts(:)+1, 1);
c = [np.sum(np.array(sortcounts) == i) for i in xrange(int(max(sortcounts)+1))]
ind = np.array(range(0, len(c)));
numinst = ind*c*(K-1.)/K;
numinst = np.sum(numinst, axis = 0)
R = np.cumsum(tp)/numinst
ap = voc_ap(R,P)
return P, R, score, ap
def calc_pr_ovr_noref(counts, out):
"""
[P, R, score, ap] = calc_pr_ovr(counts, out, K)
Input :
counts : number of occurrences of this word in the ith image
out : score for this image
K : number of references
Output :
P, R : precision and recall
score : score which corresponds to the particular precision and recall
ap : average precision
"""
#binarize counts
counts = np.array(counts > 0, dtype=np.float32);
tog = np.hstack((counts[:,np.newaxis].astype(np.float64), out[:, np.newaxis].astype(np.float64)))
ind = np.argsort(out)
ind = ind[::-1]
score = np.array([tog[i,1] for i in ind])
sortcounts = np.array([tog[i,0] for i in ind])
tp = sortcounts;
fp = sortcounts.copy();
for i in xrange(sortcounts.shape[0]):
if sortcounts[i] >= 1:
fp[i] = 0.;
elif sortcounts[i] < 1:
fp[i] = 1.;
P = np.cumsum(tp)/(np.cumsum(tp) + np.cumsum(fp));
numinst = np.sum(counts);
R = np.cumsum(tp)/numinst
ap = voc_ap(R,P)
return P, R, score, ap
def voc_ap(rec, prec):
"""
ap = voc_ap(rec, prec)
Computes the AP under the precision recall curve.
"""
rec = rec.reshape(rec.size,1); prec = prec.reshape(prec.size,1)
z = np.zeros((1,1)); o = np.ones((1,1));
mrec = np.vstack((z, rec, o))
mpre = np.vstack((z, prec, z))
for i in range(len(mpre)-2, -1, -1):
mpre[i] = max(mpre[i], mpre[i+1])
I = np.where(mrec[1:] != mrec[0:-1])[0]+1;
ap = 0;
for i in I:
ap = ap + (mrec[i] - mrec[i-1])*mpre[i];
return ap
def compute_precision_score_mapping(thresh, prec, score):
ind = np.argsort(thresh);
thresh = thresh[ind];
prec = prec[ind];
for i in xrange(1, len(prec)):
prec[i] = max(prec[i], prec[i-1]);
indexes = np.unique(thresh, return_index=True)[1]
indexes = np.sort(indexes);
thresh = thresh[indexes]
prec = prec[indexes]
thresh = np.vstack((min(-1000, min(thresh)-1), thresh[:, np.newaxis], max(1000, max(thresh)+1)));
prec = np.vstack((prec[0], prec[:, np.newaxis], prec[-1]));
f = interp1d(thresh[:,0], prec[:,0])
val = f(score)
return val
def human_agreement(gt, K):
"""
function [prec, recall] = human_agreement(gt, K)
"""
c = np.zeros((K+1,1), dtype=np.float64)
# namespace = globals().copy()
# namespace.update(locals())
# code.interact(local=namespace)
for i in xrange(len(gt)):
if gt[i]<K+1:
c[gt[i]] += 1;
#maxRun = len(gt);
# if len(gt) > K+1:
# print 'warning: '
# maxRun = K+1;
# for i in xrange(maxRun):
# c[gt[i]] += 1;
c = c/np.sum(c);
ind = np.array(range(len(c)))[:, np.newaxis]
n_tp = sum(ind*(ind-1)*c)/K;
n_fp = c[1]/K;
numinst = np.sum(c * (K-1) * ind) / K;
prec = n_tp / (n_tp+n_fp);
recall = n_tp / numinst;
return prec, recall
#follows from http://arxiv.org/pdf/1312.4894v2.pdf (Sec 4.2)
def compute_warpstyle_pr(gtLabel, predMat, topK):
assert gtLabel.shape == predMat.shape, 'gt {}; pred {}'.format(gtLabel.shape, predMat.shape)
gtLabel = gtLabel.astype(np.float64)
predMat = predMat.astype(np.float64)
numTags = gtLabel.shape[1];
numIm = gtLabel.shape[0];
#first look at topK predictions per image
topPreds = np.zeros_like(predMat);
for imInd in range(numIm):
topKInds = im_utils.maxk(predMat[imInd,...], topK);
topPreds[imInd, topKInds] = 1;
# tb.print_stack();namespace = globals().copy();namespace.update(locals());code.interact(local=namespace)
gtLabel = (gtLabel > 0).astype(np.float64)
topPreds = (topPreds > 0).astype(np.float64)
corrMat = np.logical_and(gtLabel, topPreds).astype(np.float64)
nc_per_tag = corrMat.sum(axis=0).astype(np.float64);
ng_per_tag = gtLabel.sum(axis=0).astype(np.float64);
np_per_tag = topPreds.sum(axis=0).astype(np.float64);
#mean per-class
perclass_recall = 0.0;
perclass_precision = 0.0;
eps = 1e-6;
for t in range(numTags):
cr = nc_per_tag[t]/(ng_per_tag[t]+eps);
cp = nc_per_tag[t]/(np_per_tag[t]+eps);
perclass_precision += cp;
perclass_recall += cr;
perclass_precision = (1.0/numTags) * perclass_precision;
perclass_recall = (1.0/numTags) * perclass_recall;
#overall
overall_recall = nc_per_tag.sum()/(ng_per_tag.sum()+eps);
overall_precision = nc_per_tag.sum()/(np_per_tag.sum()+eps);
return perclass_precision, perclass_recall, overall_precision, overall_recall;
def print_benchmark_latex(evalFile, vocab = None, sortBy = "words", \
printWords = False, printPos = True, printAgg = False, possOrder=None):
#evalFile has the following ['details', 'agg', 'vocab', 'imdb']
evalData = sg_utils.load_variables(evalFile);
if vocab==None:
vocab = evalData['vocab'];
if 'details' in evalData:
details = evalData['details'];
else:
details = evalData;
ap = details['ap'];
prec_at_human_rec = details['prec_at_human_rec'];
human_prec = details['prec_at_human_rec'];
words = vocab['words'];
ind = 0;
if possOrder is None:
possOrder = ['NN', 'VB', 'JJ', 'DT', 'PRP', 'IN', 'other']
print ' '.join(possOrder);
for pos in possOrder:
ind = [i for i,x in enumerate(vocab['poss']) if pos == x]
ind = np.asarray(ind,dtype=np.int32)
if any( np.isnan(ap[0,ind] )):
#print 'nan numbers ... skipping them for mean'
print 'nan numbers ... setting them to zero for mean stats'
ap[0, ind[np.where(np.isnan(ap[0, ind]))]] = 0;
print '%.1f &'%(100*np.mean(ap[0,ind])),
print '%.1f & &'%(100*np.mean(ap[0, :]))
for pos in possOrder:
ind = [i for i,x in enumerate(vocab['poss']) if pos == x]
ind = np.asarray(ind,dtype=np.int32)
if any( np.isnan(prec_at_human_rec[0,ind] )) or \
any( np.isnan(human_prec[0,ind] )) :
#print 'nan numbers ... skipping them for mean'
print 'nan numbers ... setting them to zero for mean stats'
prec_at_human_rec[0, ind[np.where(np.isnan(prec_at_human_rec[0, ind]))]] = 0;
human_prec[0, ind[np.where(np.isnan(human_prec[0, ind]))]] = 0;
print '%.1f &'%(100*np.mean(prec_at_human_rec[0,ind])),
print '%.1f \\\\'%(100*np.mean(prec_at_human_rec[0, :]))
def print_benchmark_plain(evalFile, vocab = None, \
sortBy = "words", printWords = False, printPos = True, printAgg = False):
#evalFile has the following ['details', 'agg', 'vocab', 'imdb']
evalData = sg_utils.load_variables(evalFile);
if vocab==None:
vocab = evalData['vocab'];
if 'details' in evalData:
details = evalData['details'];
else:
details = evalData;
ap = details['ap'];
prec_at_human_rec = details['prec_at_human_rec'];
human_prec = details['prec_at_human_rec'];
words = vocab['words'];
ind = 0;
if sortBy == "words":
srtInds = np.argsort(words);
elif sortBy == "ap":
srtInds = np.argsort(ap);
srtInds = srtInds[0];
srtInds = srtInds[::-1];
if printWords == True:
print "{:>50s}".format("-"*50)
print "{:^50s}".format("Word metrics")
print "{:>50s}".format("-"*50)
print "{:>15s} {:>8s} {:>6s} : {:^5s} {:^5s}". \
format("Words","POS","Counts","mAP", "p@H")
for i in srtInds:
print "{:>15s} {:>8s} {:6d} : {:5.2f} {:5.2f}". \
format(words[i], vocab['poss'][i], vocab['counts'][i], 100*np.mean(ap[0, i]), 100*np.mean(prec_at_human_rec[0, i]));
if printPos:
print "{:>50s}".format("-"*50)
print "{:^50s}".format("POS metrics")
print "{:>50s}".format("-"*50)
print "{:>15s} : {:^5s} {:^5s} {:^5s}". \
format("POS", "mAP", "p@H", "h")
for pos in list(set(vocab['poss'])):
ind = [i for i,x in enumerate(vocab['poss']) if pos == x]
ind = np.asarray(ind)
if any( np.isnan(ap[0,ind] )) or \
any( np.isnan(prec_at_human_rec[0,ind] )) or \
any( np.isnan(human_prec[0,ind] )) :
print 'nan numbers ... setting them to zero for mean stats'
ap[0, ind[np.where(np.isnan(ap[0, ind]))]] = 0;
prec_at_human_rec[0, ind[np.where(np.isnan(prec_at_human_rec[0, ind]))]] = 0;
human_prec[0, ind[np.where(np.isnan(human_prec[0, ind]))]] = 0;
print "{:>11s} [{:4d}]: {:5.2f} {:5.2f} {:5.2f}". \
format(pos, len(ind), 100*np.mean(ap[0, ind]), 100*np.mean(prec_at_human_rec[0, ind]), \
100*np.mean(human_prec[0, ind]))
if printAgg:
print "{:>50s}".format("-"*50)
print "{:^50s}".format("Agg metrics")
print "{:>50s}".format("-"*50)
print "{:>15s} : {:^5s} {:^5s} {:^5s}". \
format("agg", "mAP", "p@H", "h")
pos = 'all';
ind = srtInds;
ind = np.asarray(ind);
if any( np.isnan(ap[0,ind] )) or \
any( np.isnan(prec_at_human_rec[0,ind] )) or \
any( np.isnan(human_prec[0,ind] )) :
print 'nan numbers ... setting them to zero for mean stats'
ap[0, ind[np.where(np.isnan(ap[0, ind]))]] = 0;
prec_at_human_rec[0, ind[np.where(np.isnan(prec_at_human_rec[0, ind]))]] = 0;
human_prec[0, ind[np.where(np.isnan(human_prec[0, ind]))]] = 0;
print "{:>11s} [{:^4d}] : {:^5.2f} {:5.2f} {:5.2f}". \
format(pos, len(ind), 100*np.mean(ap[0, ind]), 100*np.mean(prec_at_human_rec[0, ind]), \
100*np.mean(human_prec[0, ind]))