forked from gladcolor/Mp_clip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccuracy_eval.py
231 lines (173 loc) · 7.3 KB
/
accuracy_eval.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
# read the results
import glob
import os
import logging
logger = logging.getLogger()
logger.setLevel('INFO')
# logging.basicConfig(level=logging.INFO)
# logging.debug('This is a debug message')
# logging.info('This is an info message')
# logging.warning('This is a warning message')
# logging.error('This is an error message')
# logging.critical('This is a critical message')
from natsort import natsorted
from skimage import io
# from sklearn import metrics
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score
from sklearn.metrics import precision_score
# from sklearn.metrics import *
import numpy as np
label_folder = r'L:\NewYorkCity_sidewalks\sidewalks\Test'
label_suf = 'TIF'
label_images = glob.glob(os.path.join(label_folder, '*.' + label_suf))
results = 'png'
results_folder = r'L:\NewYorkCity_sidewalks\COCO\Test256\classified_padding10_432\merge\binary'
results_images = glob.glob(os.path.join(results_folder, '*' + '.png'))
# print(os.path.join(results_folder, '\*.png'))
results_images = natsorted(results_images)
label_images = natsorted(label_images)
print('result_images: ', results_images[:3])
print('result_images: ', label_images[:3])
# with open(r'H:\temp', 'w') as f:
# f.writelines('khh')
f = open(r'K:\OneDrive_NJIT\OneDrive - NJIT\Ipynb\test.txt', 'a')
f.close()
def metrics(predictions, gts, label_values, report_path):
print('range(len(gts))')
print(range(len(gts)))
print('range(len(predictions))')
print(range(len(predictions)))
print('range(len(label_values))')
print(range(len(label_values)))
# report = open(MAIN_FOLDER + 'Test_all_report_100tiles_QS.txt', 'w')
report = open(report_path, 'w')
report.writelines('Train ids: ' + str("Confusion matrix :\n"))
cm = confusion_matrix(gts, predictions, label_values)
f1 = f1_score(gts, predictions, average='micro')
print("F1_score: micro")
print(f1)
f1 = f1_score(gts, predictions, average='macro')
print("F1_score: macro")
print(f1)
f1 = f1_score(gts, predictions, average=None)
print("F1_score: None")
print(f1)
# accur = accuracy_score(gts, predictions)
# print("accuracy_score: ")
# print(accur)
# rpt = classification_report(gts, predictions, LABELS)
# print("classification_report: ")
# print(accur)
print("Confusion matrix :")
report.writelines(str("Confusion matrix : \n"))
print(cm)
report.writelines(str(cm) + '\n')
report.writelines('----------- \n ')
print("---")
# Compute global accuracy
total = sum(sum(cm))
accuracy = sum([cm[x][x] for x in range(len(cm))])
accuracy *= 100 / float(total)
print("{} pixels processed".format(total))
print("Total accuracy : {}%".format(accuracy))
print("---")
report.writelines("{} pixels processed \n".format(total))
report.writelines("Total accuracy : {}% \n".format(accuracy))
report.writelines("---\n")
# Compute F1 score
F1Score = np.zeros(len(label_values))
class_accuracy = np.zeros(len(label_values))
class_recall = np.zeros(len(label_values))
for i in range(len(label_values)):
try:
F1Score[i] = 2. * cm[i, i] / (np.sum(cm[i, :]) + np.sum(cm[:, i]))
class_accuracy[i] = cm[i, i] / np.sum(cm[:, i])
class_recall[i] = cm[i, i] / np.sum(cm[i, :])
except:
# Ignore exception if there is no element in class i for test set
pass
report.writelines("F1Score : \n")
print("F1Score :")
for l_id, score in enumerate(F1Score):
print("{}: {}".format(LABELS[label_values[l_id]], score))
report.writelines("{}: {} \n".format(LABELS[label_values[l_id]], score))
print("---")
print("class_accuracy :")
report.writelines("\n class_accuracy : \n")
for l_id, score in enumerate(class_accuracy):
print("{}: {}".format(LABELS[label_values[l_id]], score))
report.writelines("{}: {} \n".format(LABELS[label_values[l_id]], score))
print("---")
print("class_recall :")
report.writelines("\n class_recall : \n")
for l_id, score in enumerate(class_recall):
print("{}: {}".format(LABELS[label_values[l_id]], score))
report.writelines("{}: {} \n".format(LABELS[label_values[l_id]], score))
print("---")
print("\nClass summary :")
report.writelines("\nClass summary :\n")
for i in range(len(label_values)):
print('Correct, Ground truth, Predict: ', LABELS[label_values[i]], cm[i, i], np.sum(cm[i, :]), np.sum(cm[:, i]))
report.writelines('Correct, Ground truth, Predict: {}: {}, {}, {}\n'.format(LABELS[label_values[i]], cm[i, i],
np.sum(cm[i, :]), np.sum(cm[:, i])))
report.writelines("---\n")
# Compute kappa coefficient
total = np.sum(cm)
pa = np.trace(cm) / float(total)
pe = np.sum(np.sum(cm, axis=0) * np.sum(cm, axis=1)) / float(total * total)
kappa = (pa - pe) / (1 - pe);
print("Kappa: " + str(kappa))
report.writelines("Kappa: " + str(kappa) + '\n')
report.close()
return accuracy
def find_element_in_list(element, list_element):
try:
index_element = list_element.index(element)
return index_element
except ValueError:
return -1
def calcu_results_metric(label_imgs, result_imgs):
if not isinstance(label_imgs, list):
label_imgs = [label_imgs]
if not isinstance(result_imgs, list):
result_imgs = [result_imgs]
label_names = []
for file in label_imgs:
label_names.append(os.path.splitext(os.path.basename(file))[0])
print('label_names, len(): ', len(label_names), label_names)
for file in result_imgs:
base_name = os.path.splitext(os.path.basename(file))[0]
idx_lab = find_element_in_list(base_name, label_names)
if idx_lab > -1:
# pass
print('base_name: ', base_name)
res = io.imread(file).flatten()
lab = io.imread(label_imgs[idx_lab]).flatten()
lab = lab.astype(np.uint8)
print("len(res), len(lab): ", len(res), len(lab))
print("Label unique values: ", np.unique(lab))
print("Result unique values: ", np.unique(res))
metrics(res, lab, ['background', 'sidewalk'],
r'H:\temp')
# print("classification_report: \n", metrics.classification_report(lab, res))
# accur = accuracy_score(lab, res)
# print('accur:', accur)
# recall = metrics.recall_score(lab, res, pos_label=1)
# print('recall:', recall)
# precision = precision_score(lab, res, pos_label=1)
# print('precision:', precision)
# cm = confusion_matrix(gts, predictions, label_values)
# f1 = f1_score(gts, predictions, average='micro')
# print("F1_score: micro")
# print(f1)
# f1 = f1_score(lab, res, average='macro')
# print("F1_score: macro")
# print(f1)
# f1 = f1_score(gts, predictions, average=None)
# print("F1_score: None")
# print(f1)
# logger.info(f'base_name: {base_name}')
# res = io.imread(file)
# lab = io.imread(label_imgs[idx_lab])
calcu_results_metric(label_images, results_images)