-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
119 lines (105 loc) · 3.29 KB
/
metrics.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
import numpy as np
# inputs are numpy arrays of shape b x r x c
# array values are only 0's or 1's
# returns batch detection accuracy
def getBatchDetectionAcc(label_mask, pred_mask):
assert label_mask.shape == pred_mask.shape
assert ((label_mask == 0) | (label_mask == 1)).all()
assert ((pred_mask == 0) | (pred_mask == 1)).all()
# get individual masks
masks = getIndividualMasks(label_mask[0])
detection = []
# find the prediction for each individual mask
for mask in masks:
# make individual mask same shape as pred mask
mask = mask.reshape((1, mask.shape[0], mask.shape[1]))
mask = np.repeat(mask, label_mask.shape[0], axis=0)
# get intersection for individual mask
intersection = mask * pred_mask
# get prediction for individual mask
num_ones = (intersection == 1).sum(axis=(1,2))
num_ones[num_ones > 0] = 1
detection.append(num_ones)
# combine individual masks prediction to find acc
detection = np.column_stack(detection)
acc = detection.mean(axis=-1)
batch_acc = acc.mean(axis=-1)
return batch_acc
# input is a masked numpy array of r x c
# returns the individual masks
def getIndividualMasks(mask):
masks = []
explore = np.zeros_like(mask)
for i in range(mask.shape[0]):
for j in range(mask.shape[1]):
if mask[i][j] == 1 and explore[i][j] == 0:
k, l = i, j
while k != mask.shape[0] and mask[k][l] == 1:
k += 1
height = k - i
k, l = i, j
while l != mask.shape[1] and mask[k][l] == 1:
l += 1
width = l - j
found_mask = np.zeros_like(mask)
for k in range(mask.shape[0]):
for l in range(mask.shape[1]):
if (k >= i) and (k < (i + height)) and (l >= j) and (l < (j + width)):
found_mask[k][l]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 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 0, 0, 1, 1]
])
batch1 = np.stack((mask2, mask2))
batch2 = np.stack((mask1, mask3))
print(getBatchDetectionAcc(batch1, batch2))
# masks = getIndividualMasks(mask4)
# for mask in masks:
# print(mask)