-
Notifications
You must be signed in to change notification settings - Fork 3
/
game.py
55 lines (46 loc) · 1.55 KB
/
game.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
# this is borrowed from https://github.com/val-iisc/lsc-cnn
import numpy as np
import torch
'''
Calculates GAME Metric as mentioned by Guerrero-Gómez-Olmedo et al. in
Extremely Overlapping Vehicle Counting, in IbPRIA, 2015.
Parameters:
-----------
level - (int) level of GAME metric.
gt - (np.ndarray) binary map of ground truth (HXW)
pred - (np.ndarray) binary map of predictions (HXW)
Returns
-------
mae - GAME for the level mentioned in the input.
'''
def game_metric(level, gt, pred):
assert(gt.shape == pred.shape)
num_cuts = np.power(2, level)
H, W = gt.shape
h = H//num_cuts
w = W//num_cuts
gt_new = np.zeros((num_cuts*num_cuts, h, w))
pred_new = np.zeros((num_cuts*num_cuts, h, w))
for y in range(num_cuts):
for x in range(num_cuts):
gt_new[y*num_cuts+x,:,:]=gt[y*h:y*h+h,x*w:x*w+w]
pred_new[y*num_cuts+x,:,:]=pred[y*h:y*h+h,x*w:x*w+w]
gt_sum = np.sum(np.sum(gt_new, axis=1), axis=1)
pred_sum = np.sum(np.sum(pred_new, axis=1), axis=1)
mae = np.sum(np.abs(gt_sum - pred_sum))
return mae
'''
Wrapper for calculating GAME Metric
Parameters:
-----------
gt - (np.ndarray) binary map of ground truth (HXW)
pred - (np.ndarray) binary map of predictions (HXW)
Returns
-------
mae_l1, mae_l2, mae_l3 - GAME for 3 different levels
'''
def find_game_metric(gt, pred):
mae_l1 = game_metric(1, gt, pred)
mae_l2 = game_metric(2, gt, pred)
mae_l3 = game_metric(3, gt, pred)
return mae_l1, mae_l2, mae_l3