-
Notifications
You must be signed in to change notification settings - Fork 10
/
evaluate.py
554 lines (416 loc) · 18 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
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
import numpy as np
import scipy.io as scio
import os
import argparse
import pickle
from sklearn import metrics
import json
import socket
# data folder contain all datasets, such as ped1, ped2, avenue, shanghaitech, etc
DATA_DIR = '../Data'
# normalize scores in each sub video
NORMALIZE = True
# number of history frames, since in prediction based method, the first 4 frames can not be predicted, so that
# the first 4frames are undecidable, we just ignore the first 4 frames
DECIDABLE_IDX = 4
def parser_args():
parser = argparse.ArgumentParser(description='evaluating the model, computing the roc/auc.')
parser.add_argument('-f', '--file', type=str, help='the path of loss file.')
parser.add_argument('-t', '--type', type=str, default='compute_auc',
help='the type of evaluation, choosing type is: plot_roc, compute_auc, '
'test_func\n, the default type is compute_auc')
return parser.parse_args()
class RecordResult(object):
def __init__(self, fpr=None, tpr=None, auc=-np.inf, dataset=None, loss_file=None):
self.fpr = fpr
self.tpr = tpr
self.auc = auc
self.dataset = dataset
self.loss_file = loss_file
def __lt__(self, other):
return self.auc < other.auc
def __gt__(self, other):
return self.auc > other.auc
def __str__(self):
return 'dataset = {}, loss file = {}, auc = {}'.format(self.dataset, self.loss_file, self.auc)
class GroundTruthLoader(object):
AVENUE = 'avenue'
PED1 = 'ped1'
PED1_PIXEL_SUBSET = 'ped1_pixel_subset'
PED2 = 'ped2'
ENTRANCE = 'enter'
EXIT = 'exit'
SHANGHAITECH = 'shanghaitech'
SHANGHAITECH_LABEL_PATH = os.path.join(DATA_DIR, 'shanghaitech/testing/test_frame_mask')
NAME_MAT_MAPPING = {
AVENUE: os.path.join(DATA_DIR, 'avenue/avenue.mat'),
PED1: os.path.join(DATA_DIR, 'ped1/ped1.mat'),
PED2: os.path.join(DATA_DIR, 'ped2/ped2.mat'),
ENTRANCE: os.path.join(DATA_DIR, 'enter/enter.mat'),
EXIT: os.path.join(DATA_DIR, 'exit/exit.mat')
}
NAME_FRAMES_MAPPING = {
AVENUE: os.path.join(DATA_DIR, 'avenue/testing/frames'),
PED1: os.path.join(DATA_DIR, 'ped1/testing/frames'),
PED2: os.path.join(DATA_DIR, 'ped2/testing/frames'),
ENTRANCE: os.path.join(DATA_DIR, 'enter/testing/frames'),
EXIT: os.path.join(DATA_DIR, 'exit/testing/frames')
}
def __init__(self, mapping_json=None):
"""
Initial a ground truth loader, which loads the ground truth with given dataset name.
:param mapping_json: the mapping from dataset name to the path of ground truth.
"""
if mapping_json is not None:
with open(mapping_json, 'rb') as json_file:
self.mapping = json.load(json_file)
else:
self.mapping = GroundTruthLoader.NAME_MAT_MAPPING
def __call__(self, dataset):
""" get the ground truth by provided the name of dataset.
:type dataset: str
:param dataset: the name of dataset.
:return: np.ndarray, shape(#video)
np.array[0] contains all the start frame and end frame of abnormal events of video 0,
and its shape is (#frapsnr, )
"""
if dataset == GroundTruthLoader.SHANGHAITECH:
gt = self.__load_shanghaitech_gt()
elif dataset == GroundTruthLoader.TOY_DATA:
gt = self.__load_toydata_gt()
else:
gt = self.__load_ucsd_avenue_subway_gt(dataset)
return gt
def __load_ucsd_avenue_subway_gt(self, dataset):
assert dataset in self.mapping, 'there is no dataset named {} \n Please check {}' \
.format(dataset, GroundTruthLoader.NAME_MAT_MAPPING.keys())
mat_file = self.mapping[dataset]
abnormal_events = scio.loadmat(mat_file, squeeze_me=True)['gt']
if abnormal_events.ndim == 2:
abnormal_events = abnormal_events.reshape(-1, abnormal_events.shape[0], abnormal_events.shape[1])
num_video = abnormal_events.shape[0]
dataset_video_folder = GroundTruthLoader.NAME_FRAMES_MAPPING[dataset]
video_list = os.listdir(dataset_video_folder)
video_list.sort()
assert num_video == len(video_list), 'ground true does not match the number of testing videos. {} != {}' \
.format(num_video, len(video_list))
# get the total frames of sub video
def get_video_length(sub_video_number):
# video_name = video_name_template.format(sub_video_number)
video_name = os.path.join(dataset_video_folder, video_list[sub_video_number])
assert os.path.isdir(video_name), '{} is not directory!'.format(video_name)
length = len(os.listdir(video_name))
return length
# need to test [].append, or np.array().append(), which one is faster
gt = []
for i in range(num_video):
length = get_video_length(i)
sub_video_gt = np.zeros((length,), dtype=np.int8)
sub_abnormal_events = abnormal_events[i]
if sub_abnormal_events.ndim == 1:
sub_abnormal_events = sub_abnormal_events.reshape((sub_abnormal_events.shape[0], -1))
_, num_abnormal = sub_abnormal_events.shape
for j in range(num_abnormal):
# (start - 1, end - 1)
start = sub_abnormal_events[0, j] - 1
end = sub_abnormal_events[1, j]
sub_video_gt[start: end] = 1
gt.append(sub_video_gt)
return gt
@staticmethod
def __load_shanghaitech_gt():
video_path_list = os.listdir(GroundTruthLoader.SHANGHAITECH_LABEL_PATH)
video_path_list.sort()
gt = []
for video in video_path_list:
# print(os.path.join(GroundTruthLoader.SHANGHAITECH_LABEL_PATH, video))
gt.append(np.load(os.path.join(GroundTruthLoader.SHANGHAITECH_LABEL_PATH, video)))
return gt
@staticmethod
def __load_toydata_gt():
with open(GroundTruthLoader.TOY_DATA_LABEL_PATH, 'r') as gt_file:
gt_dict = json.load(gt_file)
gt = []
for video, video_info in gt_dict.items():
length = video_info['length']
video_gt = np.zeros((length,), dtype=np.uint8)
sub_gt = np.array(np.matrix(video_info['gt']))
for anomaly in sub_gt:
start = anomaly[0]
end = anomaly[1] + 1
video_gt[start: end] = 1
gt.append(video_gt)
return gt
@staticmethod
def get_pixel_masks_file_list(dataset):
# pixel mask folder
pixel_mask_folder = os.path.join(DATA_DIR, dataset, 'pixel_masks')
pixel_mask_file_list = os.listdir(pixel_mask_folder)
pixel_mask_file_list.sort()
# get all testing videos
dataset_video_folder = GroundTruthLoader.NAME_FRAMES_MAPPING[dataset]
video_list = os.listdir(dataset_video_folder)
video_list.sort()
# get all testing video names with pixel masks
pixel_video_ids = []
ids = 0
for pixel_mask_name in pixel_mask_file_list:
while ids < len(video_list):
if video_list[ids] + '.npy' == pixel_mask_name:
pixel_video_ids.append(ids)
ids += 1
break
else:
ids += 1
assert len(pixel_video_ids) == len(pixel_mask_file_list)
for i in range(len(pixel_mask_file_list)):
pixel_mask_file_list[i] = os.path.join(pixel_mask_folder, pixel_mask_file_list[i])
return pixel_mask_file_list, pixel_video_ids
def load_psnr_gt(loss_file):
with open(loss_file, 'rb') as reader:
# results {
# 'dataset': the name of dataset
# 'psnr': the psnr of each testing videos,
# }
# psnr_records['psnr'] is np.array, shape(#videos)
# psnr_records[0] is np.array ------> 01.avi
# psnr_records[1] is np.array ------> 02.avi
# ......
# psnr_records[n] is np.array ------> xx.avi
results = pickle.load(reader)
dataset = results['dataset']
psnr_records = results['psnr']
num_videos = len(psnr_records)
# load ground truth
gt_loader = GroundTruthLoader()
gt = gt_loader(dataset=dataset)
assert num_videos == len(gt), 'the number of saved videos does not match the ground truth, {} != {}' \
.format(num_videos, len(gt))
return dataset, psnr_records, gt
def load_psnr_gt_flow(loss_file):
with open(loss_file, 'rb') as reader:
# results {
# 'dataset': the name of dataset
# 'psnr': the psnr of each testing videos,
# }
# psnr_records['psnr'] is np.array, shape(#videos)
# psnr_records[0] is np.array ------> 01.avi
# psnr_records[1] is np.array ------> 02.avi
# ......
# psnr_records[n] is np.array ------> xx.avi
results = pickle.load(reader)
dataset = results['dataset']
psnrs = results['psnr']
flows = results['flow']
num_videos = len(psnrs)
# load ground truth
gt_loader = GroundTruthLoader()
gt = gt_loader(dataset=dataset)
assert num_videos == len(gt), 'the number of saved videos does not match the ground truth, {} != {}' \
.format(num_videos, len(gt))
return dataset, psnrs, flows, gt
def load_psnr(loss_file):
"""
load image psnr or optical flow psnr.
:param loss_file: loss file path
:return:
"""
with open(loss_file, 'rb') as reader:
results = pickle.load(reader)
psnrs = results['psnr']
return psnrs
def get_scores_labels(loss_file):
# the name of dataset, loss, and ground truth
dataset, psnr_records, gt = load_psnr_gt(loss_file=loss_file)
# the number of videos
num_videos = len(psnr_records)
scores = np.array([], dtype=np.float32)
labels = np.array([], dtype=np.int8)
# video normalization
for i in range(num_videos):
distance = psnr_records[i]
if NORMALIZE:
distance -= distance.min() # distances = (distance - min) / (max - min)
distance /= distance.max()
# distance = 1 - distance
scores = np.concatenate((scores[:], distance[DECIDABLE_IDX:]), axis=0)
labels = np.concatenate((labels[:], gt[i][DECIDABLE_IDX:]), axis=0)
return dataset, scores, labels
def precision_recall_auc(loss_file):
if not os.path.isdir(loss_file):
loss_file_list = [loss_file]
else:
loss_file_list = os.listdir(loss_file)
loss_file_list = [os.path.join(loss_file, sub_loss_file) for sub_loss_file in loss_file_list]
optimal_results = RecordResult()
for sub_loss_file in loss_file_list:
dataset, scores, labels = get_scores_labels(sub_loss_file)
precision, recall, thresholds = metrics.precision_recall_curve(labels, scores, pos_label=0)
auc = metrics.auc(recall, precision)
results = RecordResult(recall, precision, auc, dataset, sub_loss_file)
if optimal_results < results:
optimal_results = results
if os.path.isdir(loss_file):
print(results)
print('##### optimal result and model = {}'.format(optimal_results))
return optimal_results
def cal_eer(fpr, tpr):
# makes fpr + tpr = 1
eer = fpr[np.nanargmin(np.absolute((fpr + tpr - 1)))]
return eer
def compute_eer(loss_file):
if not os.path.isdir(loss_file):
loss_file_list = [loss_file]
else:
loss_file_list = os.listdir(loss_file)
loss_file_list = [os.path.join(loss_file, sub_loss_file) for sub_loss_file in loss_file_list]
optimal_results = RecordResult(auc=np.inf)
for sub_loss_file in loss_file_list:
dataset, scores, labels = get_scores_labels(sub_loss_file)
fpr, tpr, thresholds = metrics.roc_curve(labels, scores, pos_label=0)
eer = cal_eer(fpr, tpr)
results = RecordResult(fpr, tpr, eer, dataset, sub_loss_file)
if optimal_results > results:
optimal_results = results
if os.path.isdir(loss_file):
print(results)
print('##### optimal result and model = {}'.format(optimal_results))
return optimal_results
def compute_auc(loss_file):
if not os.path.isdir(loss_file):
loss_file_list = [loss_file]
else:
loss_file_list = os.listdir(loss_file)
loss_file_list = [os.path.join(loss_file, sub_loss_file) for sub_loss_file in loss_file_list]
optimal_results = RecordResult()
for sub_loss_file in loss_file_list:
# the name of dataset, loss, and ground truth
dataset, psnr_records, gt = load_psnr_gt(loss_file=sub_loss_file)
# the number of videos
num_videos = len(psnr_records)
scores = np.array([], dtype=np.float32)
labels = np.array([], dtype=np.int8)
# video normalization
for i in range(num_videos):
distance = psnr_records[i]
if NORMALIZE:
distance -= distance.min() # distances = (distance - min) / (max - min)
distance /= distance.max() - distance.min()
# distance = 1 - distance
scores = np.concatenate((scores, distance[DECIDABLE_IDX:]), axis=0)
labels = np.concatenate((labels, gt[i][DECIDABLE_IDX:]), axis=0)
fpr, tpr, thresholds = metrics.roc_curve(labels, scores, pos_label=0)
auc = metrics.auc(fpr, tpr)
print(fpr,tpr,auc)
results = RecordResult(fpr, tpr, auc, dataset, sub_loss_file)
if optimal_results < results:
optimal_results = results
if os.path.isdir(loss_file):
print(results)
print('##### optimal result and model = {}'.format(optimal_results))
return optimal_results
def average_psnr(loss_file):
if not os.path.isdir(loss_file):
loss_file_list = [loss_file]
else:
loss_file_list = os.listdir(loss_file)
loss_file_list = [os.path.join(loss_file, sub_loss_file) for sub_loss_file in loss_file_list]
max_avg_psnr = -np.inf
max_file = ''
for file in loss_file_list:
psnr_records = load_psnr(file)
psnr_records = np.concatenate(psnr_records, axis=0)
avg_psnr = np.mean(psnr_records)
if max_avg_psnr < avg_psnr:
max_avg_psnr = avg_psnr
max_file = file
print('{}, average psnr = {}'.format(file, avg_psnr))
print('max average psnr file = {}, psnr = {}'.format(max_file, max_avg_psnr))
def calculate_psnr(loss_file):
optical_result = compute_auc(loss_file)
print('##### optimal result and model = {}'.format(optical_result))
mean_psnr = []
for file in os.listdir(loss_file):
file = os.path.join(loss_file, file)
dataset, psnr_records, gt = load_psnr_gt(file)
psnr_records = np.concatenate(psnr_records, axis=0)
gt = np.concatenate(gt, axis=0)
mean_normal_psnr = np.mean(psnr_records[gt == 0])
mean_abnormal_psnr = np.mean(psnr_records[gt == 1])
mean = np.mean(psnr_records)
print('mean normal psrn = {}, mean abnormal psrn = {}, mean = {}'.format(
mean_normal_psnr,
mean_abnormal_psnr,
mean)
)
mean_psnr.append(mean)
print('max mean psnr = {}'.format(np.max(mean_psnr)))
def calculate_score(loss_file):
if not os.path.isdir(loss_file):
loss_file_path = loss_file
else:
optical_result = compute_auc(loss_file)
loss_file_path = optical_result.loss_file
print('##### optimal result and model = {}'.format(optical_result))
dataset, psnr_records, gt = load_psnr_gt(loss_file=loss_file_path)
# the number of videos
num_videos = len(psnr_records)
scores = np.array([], dtype=np.float32)
labels = np.array([], dtype=np.int8)
# video normalization
for i in range(num_videos):
distance = psnr_records[i]
distance = (distance - distance.min()) / (distance.max() - distance.min())
scores = np.concatenate((scores, distance[DECIDABLE_IDX:]), axis=0)
labels = np.concatenate((labels, gt[i][DECIDABLE_IDX:]), axis=0)
mean_normal_scores = np.mean(scores[labels == 0])
mean_abnormal_scores = np.mean(scores[labels == 1])
print('mean normal scores = {}, mean abnormal scores = {}, '
'delta = {}'.format(mean_normal_scores, mean_abnormal_scores, mean_normal_scores - mean_abnormal_scores))
def test_func(*args):
# simulate testing on CUHK AVENUE dataset
dataset = GroundTruthLoader.AVENUE
# load the ground truth
gt_loader = GroundTruthLoader()
gt = gt_loader(dataset=dataset)
num_videos = len(gt)
simulated_results = {
'dataset': dataset,
'psnr': []
}
simulated_psnr = []
for i in range(num_videos):
sub_video_length = gt[i].shape[0]
simulated_psnr.append(np.random.random(size=sub_video_length))
simulated_results['psnr'] = simulated_psnr
# writing to file, 'generated_loss.bin'
with open('generated_loss.bin', 'wb') as writer:
pickle.dump(simulated_results, writer, pickle.HIGHEST_PROTOCOL)
print(file_path.name)
result = compute_auc(file_path.name)
print('optimal = {}'.format(result))
eval_type_function = {
'compute_auc': compute_auc,
'compute_eer': compute_eer,
'precision_recall_auc': precision_recall_auc,
'calculate_psnr': calculate_psnr,
'calculate_score': calculate_score,
'average_psnr': average_psnr,
'average_psnr_sample': average_psnr
}
def evaluate(eval_type, save_file):
assert eval_type in eval_type_function, 'there is no type of evaluation {}, please check {}' \
.format(eval_type, eval_type_function.keys())
eval_func = eval_type_function[eval_type]
optimal_results = eval_func(save_file)
return optimal_results
if __name__ == '__main__':
args = parser_args()
eval_type = args.type
file_path = args.file
print('Evaluate type = {}'.format(eval_type))
print('File path = {}'.format(file_path))
if eval_type == 'test_func':
test_func()
else:
evaluate(eval_type, file_path)