forked from pierrebaque/DeepOcclusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpom_evaluator.py
256 lines (194 loc) · 8.9 KB
/
pom_evaluator.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
import random
import numpy as np
import munkres
from munkres import Munkres, print_matrix
import codecs
import json
import copy
import Config
#To remove!
import matplotlib
matplotlib.use("nbagg")
import skimage
import skimage.filters
import skimage.morphology
import math
import numpy.linalg as la
import numpy as np
import matplotlib.pyplot as plt
class POM_evaluator(object):
def __init__(self,room,GT_labels_path_json = '../NDF_peds/data/ETH/labels_json/%08d.json',HW_GT_grid = (1240,480),shiftX_GT = 130,radius_match = 5, q_thresh = 0.5):
self.room = room
self.GT_labels_path_json = GT_labels_path_json
self.H_GT_grid,self.W_GT_grid = HW_GT_grid[0],HW_GT_grid[1]
self.shiftX = shiftX_GT
self.radius_match = radius_match # This is with respect to the room resolution
self.q_thresh = q_thresh
#For ETH dataset
def get_GT_coordinates_fromjson(self,fid):
#ShiftX is a momnetaneous hack to match detection and labelling after modification
'''
Input: frame ID as in original dataset
Output: coordinates with respect to the resolution of detection (H_grid,W_grid)
'''
room = self.room
obj_text =codecs.open(self.GT_labels_path_json%fid, 'r', encoding='utf-8').read()
b_new = json.loads(obj_text)
c_new = np.int32(b_new[1:])
n_cams = 7
detections =[[] for cam in range(room.n_cams)]
GT_coordinates = []
for det_id in range(c_new.shape[0]):
rec_ID = c_new[det_id,0]
#print rec_ID
if (rec_ID/self.W_GT_grid - self.shiftX)*(1.0*room.H_grid)/(1.0*self.H_GT_grid) > -1 :
GT_coordinates.append(((rec_ID/self.W_GT_grid- self.shiftX)*(1.0*room.H_grid)/(1.0*self.H_GT_grid),(rec_ID%self.W_GT_grid)*(1.0*room.W_grid)/(1.0*self.W_GT_grid)))
return np.asarray(GT_coordinates)
#For Terrace Dataset
def get_GT_coordinates_terrace(self,fid):
#ShiftX is a momnetaneous hack to match detection and labelling after modification
'''
Input: frame ID as in original dataset
Output: coordinates with respect to the resolution of detection (H_grid,W_grid)
'''
room = self.room
all_detections = np.loadtxt('./ground_truth/gt_terrace1.txt')
detections = all_detections[fid]
GT_coordinates = []
for det_id in range(detections.shape[0]):
rec_ID = detections[det_id]
#print rec_ID
if rec_ID > -1:
x = rec_ID%room.H_grid
y = room.W_grid - rec_ID/room.H_grid
#HACK GT
# !!!! KEEP PARAMETERS!!!
#x = np.round(0.82*x + 2)
#y = np.round(0.77*y + 5)
x = np.round(0.89*x + 2)
y = np.round(0.82*y + 5)
GT_coordinates.append((x,y))
return np.asarray(GT_coordinates)
#For Terrace Dataset
def get_GT_coordinates_SALSA(self,fid,showWarning = True):
#ShiftX is a momnetaneous hack to match detection and labelling after modification
'''
Input: frame ID as in original dataset (15 fps)
Output: coordinates with respect to the resolution of detection (H_grid,W_grid)
'''
room = self.room
all_detections = np.loadtxt('./groundtruth.txt')
if (fid - 3 )%45 != 0 and showWarning:
print "for accurate ground truth , use fid = 3 + i*45"
gt_line = (fid - 3) / 45
detections = all_detections[:,gt_line]
GT_coordinates = []
for det_id in range(detections.shape[0]):
rec_ID = detections[det_id]
#print rec_ID
if rec_ID > -1:
y = rec_ID%room.H_grid
x = rec_ID/room.H_grid
GT_coordinates.append((x,y))
return np.asarray(GT_coordinates)
def hungarian_matching(self,GT_coordinates,det_coordinates,verbose = False):
n_dets = det_coordinates.shape[0]
n_gts = GT_coordinates.shape[0]
n_max = max(n_dets,n_gts)
matrix = np.zeros((n_max,n_max)) + 1
for i_d in range(n_dets):
for i_gt in range(n_gts):
if ((det_coordinates[i_d,0] - GT_coordinates[i_gt,0])**2 + (det_coordinates[i_d,1] - GT_coordinates[i_gt,1])**2) <= self.radius_match**2:
matrix[i_d,i_gt] = 0
m = Munkres()
indexes = m.compute(copy.copy(matrix))
total = 0
TP = [] #True positive
FP = [] #False positive
FN = [] #False negative
for row, column in indexes:
value = matrix[row][column]
total += value
if verbose:
print '(%d, %d) -> %d' % (row, column, value)
if value == 0:
TP.append((int(det_coordinates[row,0]),int(det_coordinates[row,1])))
if value >0:
if row < n_dets:
FP.append((int(det_coordinates[row,0]),int(det_coordinates[row,1])))
if column < n_gts :
FN.append((int(GT_coordinates[column,0]),int(GT_coordinates[column,1])))
if verbose:
print 'total cost: %d' % total
return total,np.asarray(TP),np.asarray(FP),np.asarray(FN)
def get_loss_arrays(self,TP,FP,FN):
H_grid, W_grid = self.room.H_grid,self.room.W_grid
LossMask = np.zeros(H_grid*W_grid)
LossMask[W_grid*TP[:,0] + TP[:,1] ] = 1
LossMask[W_grid*FP[:,0] + FP[:,1] ] = 1
LossMask[W_grid*FN[:,0] + FN[:,1] ] = 1
LossLabels = np.zeros(H_grid*W_grid)
LossLabels[W_grid*TP[:,0] + TP[:,1] ] = 1
LossLabels[W_grid*FP[:,0] + FP[:,1] ] = 0
LossLabels[W_grid*FN[:,0] + FN[:,1] ] = 1
return LossLabels,LossMask
def get_Hungarian_score(self,Q_out,fid, show = False):
GT_coordinates = self.get_GT_coordinates_fromjson(fid)
det_coordinates = self.room.get_coordinates_from_Q(Q_out[-1],q_thresh = self.q_thresh)
total,TP,FP,FN = self.hungarian_matching(GT_coordinates,det_coordinates,verbose = False)
if show:
MAP_out = np.zeros((self.room.H_grid,self.room.W_grid))
for (i,j) in GT_coordinates:
MAP_out[int(i),int(j)] = 1
for (i,j) in det_coordinates:
MAP_out[int(i),int(j)] += 2
plt.imshow(MAP_out)
plt.colorbar()
plt.show()
return total,TP.shape[0],FP.shape[0],FN.shape[0]
def get_Hungarian_score_terrace(self,Q_out,fid,show = False):
GT_coordinates = self.get_GT_coordinates_terrace(fid)
det_coordinates = self.room.get_coordinates_from_Q(Q_out[-1],q_thresh = self.q_thresh)
total,TP,FP,FN = self.hungarian_matching(GT_coordinates,det_coordinates,verbose = False)
if show:
MAP_out = np.zeros((self.room.H_grid,self.room.W_grid))
for (i,j) in GT_coordinates:
MAP_out[int(i),int(j)] = 1
for (i,j) in det_coordinates:
MAP_out[int(i),int(j)] += 2
plt.imshow(MAP_out)
plt.colorbar()
plt.show()
return total,TP.shape[0],FP.shape[0],FN.shape[0]
def get_Hungarian_score_SALSA(self,Q_out,fid,show = False,showWarning = True):
GT_coordinates = self.get_GT_coordinates_SALSA(fid,showWarning)
det_coordinates = self.room.get_coordinates_from_Q(Q_out[-1],q_thresh = self.q_thresh)
total,TP,FP,FN = self.hungarian_matching(GT_coordinates,det_coordinates,verbose = False)
if show:
MAP_out = np.zeros((self.room.H_grid,self.room.W_grid))
for (i,j) in GT_coordinates:
MAP_out[int(i),int(j)] = 1
for (i,j) in det_coordinates:
MAP_out[int(i),int(j)] += 2
plt.imshow(MAP_out)
plt.colorbar()
plt.show()
return total,TP.shape[0],FP.shape[0],FN.shape[0]
#Read GT
#Carefull in coordinates, j is before i
# def get_GT_coordinates_fromtxt(self,fid):
# '''
# Input: frame ID as in original dataset
# Output: coordinates with respect to the resolution of detection (H_grid,W_grid)
# '''
# f = open(GT_labels_path%img_index_list[fid], 'r')
# n_cams = 7
# detections =[[] for cam in range(n_cams)]
# GT_coordinates = []
# for lid,line in enumerate(f):
# if lid >1:
# line_tab = np.asarray(np.fromstring(line, dtype=int, sep=','))
# rec_ID = line_tab[0]
# #print rec_ID
# GT_coordinates.append(((rec_ID/W_GT_grid)*(1.0*H_grid)/(1.0*H_GT_grid),(rec_ID%W_GT_grid)*(1.0*W_grid)/(1.0*W_GT_grid)))
# return np.asarray(GT_coordinates)