forked from zhimingluo/NLDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
149 lines (98 loc) · 3.44 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
import numpy as np
import cv2
import glob2
import os
eps = 2.2204e-16
def parameter():
p = {}
p['gtThreshold'] = 0.5
p['beta'] = np.sqrt(0.3)
p['thNum'] = 100
p['thList'] = np.linspace(0, 1, p['thNum'])
return p
def im2double(im):
return cv2.normalize(im.astype('float'),
None,
0.0, 1.0,
cv2.NORM_MINMAX)
def prCount(gtMask, curSMap, p):
gtH, gtW = gtMask.shape[0:2]
algH, algW = curSMap.shape[0:2]
if gtH != algH or gtW != algW:
curSMap = cv2.resize(curSMap, (gtW, gtH))
gtMask = (gtMask >= p['gtThreshold']).astype(np.float32)
gtInd = np.where(gtMask > 0)
gtCnt = np.sum(gtMask)
if gtCnt == 0:
prec = []
recall = []
else:
hitCnt = np.zeros((p['thNum'], 1), np.float32)
algCnt = np.zeros((p['thNum'], 1), np.float32)
for k, curTh in enumerate(p['thList']):
thSMap = (curSMap >= curTh).astype(np.float32)
hitCnt[k] = np.sum(thSMap[gtInd])
algCnt[k] = np.sum(thSMap)
prec = hitCnt / (algCnt+eps)
recall = hitCnt / gtCnt
return prec, recall
def PR_Curve(resDir, gtDir, method):
p = parameter()
beta = p['beta']
gtImgs = glob2.iglob(gtDir + '/*.png')
prec = []
recall = []
for gtName in gtImgs:
dir, name = os.path.split(gtName)
mapName = os.path.join(resDir, name)
mapName = mapName.replace('.png', '_' + method + '.png')
curMap = im2double(cv2.imread(mapName, cv2.IMREAD_GRAYSCALE))
curGT = im2double(cv2.imread(gtName, cv2.IMREAD_GRAYSCALE))
if curMap.shape[0] != curGT.shape[0]:
curMap = cv2.resize(curMap, (curGT.shape[1], curGT.shape[2]))
curPrec, curRecall = prCount(curGT, curMap, p)
prec.append(curPrec)
recall.append(curRecall)
prec = np.hstack(prec[:])
recall = np.hstack(recall[:])
prec = np.mean(prec, 1)
recall = np.mean(recall, 1)
# compute the max F-Score
score = (1+beta**2)*prec*recall / (beta**2*prec + recall)
curTh = np.argmax(score)
curScore = np.max(score)
res = {}
res['prec'] = prec
res['recall'] = recall
res['curScore'] = curScore
res['curTh'] = curTh
return res
def MAE_Value(resDir, gtDir, method):
p = parameter()
gtThreshold = p['gtThreshold']
gtImgs = glob2.iglob(gtDir + '/*.png')
MAE = []
for gtName in gtImgs:
dir, name = os.path.split(gtName)
mapName= os.path.join(resDir, name)
mapName = mapName.replace('.png', '_' + method + '.png')
curMap = im2double(cv2.imread(mapName, cv2.IMREAD_GRAYSCALE))
curGT = im2double(cv2.imread(gtName, cv2.IMREAD_GRAYSCALE))
curGT = (curGT >= gtThreshold).astype(np.float32)
if curMap.shape[0] != curGT.shape[0]:
curMap = cv2.resize(curMap, (curGT.shape[1], curGT.shape[2]))
diff = np.abs(curMap - curGT)
MAE.append(np.mean(diff))
return np.mean(MAE)
if __name__ == "__main__":
method = 'NLDF'
datasets = ['MSRA-B', 'HKU-IS', 'DUT-OMRON',
'PASCAL-S', 'ECSSD', 'SOD']
for dataset in datasets:
resDir = 'Result/'+dataset+'/' + method + '/'
gtDir = 'GT/' + dataset + '/gt/'
mae = MAE_Value(resDir, gtDir, method)
pr = PR_Curve(resDir, gtDir, method)
print dataset
print 'max F:', pr['curScore']
print 'MAE:', mae