-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.py
88 lines (81 loc) · 2.87 KB
/
evaluation.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
import scipy.io as io
import numpy as np
import os
def GetOverlap(Proposals, GTInsts):
NumInsts = len(GTInsts)
NumProposals = len(Proposals)
overlap = np.zeros((NumInsts, NumProposals))
for i in range(NumProposals):
Proposal = Proposals[i]
for j in range(NumInsts):
GTInst = GTInsts[j]
overlap[j, i] = np.sum(np.logical_and(Proposal, GTInst)) / np.sum(np.logical_or(Proposal, GTInst))
return overlap, NumInsts
def OverlapsToLabels(Scores, Overlaps, Thresh):
NumInsts = Overlaps.shape[0]
NumDets = Overlaps.shape[1]
Covered = np.zeros((NumInsts, 1))
Labels = np.zeros((NumDets, 1))
SortIndex = np.argsort(-Scores, axis=0)
for k in range(len(SortIndex)):
if (np.sum(Covered) == len(Covered)):
break
Idx = np.where(Covered == 0)[0]
Overlap = np.max(Overlaps[Idx, SortIndex[k]])
AssignID = np.where(Overlaps[Idx, SortIndex[k]] == Overlap)[0]
if Overlap > Thresh:
Labels[SortIndex[k], 0] = 1
Covered[Idx[AssignID], 0] = 1
return Labels
def VOCap(rec, prec):
mrec = np.insert(rec, 0, 0)
mrec = np.append(mrec, 1)
mpre = np.insert(prec, 0, 0)
mpre = np.append(mpre, 0)
for i in range(len(mpre)-2, -1, -1):
mpre[i] = np.maximum(mpre[i], mpre[i+1])
i = np.where(mrec[1:] != mrec[:-1])[0] + 1
ap = np.sum((mrec[i] - mrec[i-1]) * mpre[i])
return ap
def CalAP(Scores, Labels, NumGTs):
SortID = np.argsort(-Scores, axis=0)
tp = Labels[SortID]
fp = 1 - Labels[SortID]
tp = tp.cumsum()
fp = fp.cumsum()
prec = tp / (tp + fp)
rec = tp / NumGTs
ap = VOCap(rec, prec)
return ap
def GetAP(PredMaskScores, OverlapAll, Threshold, NumInsts):
NumGTs = 0
NumImgs = len(PredMaskScores)
TotalDetectedProps = 0
for i in range(NumImgs):
TotalDetectedProps += len(PredMaskScores[i])
Scores = np.zeros((TotalDetectedProps, 1))
Labels = np.zeros((TotalDetectedProps, 1))
AllDets = 0
for k in range(NumImgs):
NumDets = len(PredMaskScores[k])
Scores[AllDets: AllDets+NumDets, 0] = PredMaskScores[k]
NumGTs += NumInsts[k]
Overlaps = OverlapAll[k]
if Overlaps == []:
continue
Labels[AllDets: AllDets+NumDets] = OverlapsToLabels(Scores[AllDets: AllDets+NumDets, 0], Overlaps, Threshold)
AllDets += NumDets
AP = CalAP(Scores, Labels, NumGTs)
return AP
def EvalCoSegAP(PredMasks, PredScores, GTInstMasks, Threshold):
NumImgs = len(PredMasks)
Overlap = []
NumInsts = []
for i in range(NumImgs):
overlap, numinsts = GetOverlap(PredMasks[i], GTInstMasks[i])
Overlap.append(overlap)
NumInsts.append(numinsts)
APAll = np.zeros((len(Threshold), 1))
for i in range(len(Threshold)):
APAll[i] = GetAP(PredScores, Overlap, Threshold[i], NumInsts)
return APAll