-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreport.py
320 lines (280 loc) · 11.7 KB
/
report.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import os.path
from os import listdir
import sys
import numpy as np
from collections import Counter
def group_dict(groups_file):
"""Returns dictionary of group membership, where the keys are file names.
input: file path to csv which have rows with the format: file_name,group
"""
_ = np.loadtxt(groups_file, delimiter=',', dtype='U')
groups = {}
for file_name, group in _:
groups[file_name] = group
return groups
def data_summary(data_dir, groups_file=None, csv=None):
"""Summarizes counts for images in data_dir.
Inputs:
data_dir: path to directory of images (where images are in a
subdirectory for each class)
groups_file (optional): file path to csv which have rows with the
format: file_name,group
csv (optional): file path to save csv of counts, if groups_file exists
"""
print("Data Summary:", data_dir, "\n")
class_names = [f for f in listdir(data_dir) if not os.path.isfile(os.path.join(data_dir, f))]
class_names = sorted(class_names)
class_width = max([len(x) for x in class_names] + [8])
grand_total = 0
if groups_file is not None:
groups = group_dict(groups_file)
group_names = sorted(Counter(groups.values()).keys())
group_width = max([len(x) for x in group_names] + [5])
groups_discovered = set() # needed since it's possible to use a groups_file
# that lists groups not used in the data_dir
print('{0:>{1}}'.format("Class", class_width), end=" ")
print('{0:>{1}}'.format("Group", group_width), end="")
print('{0:>8}'.format("Count"))
for class_name in class_names:
print("-"*(class_width+group_width+3+8))
file_names = os.listdir(os.path.join(data_dir, class_name))
group = []
for file_name in file_names:
group.append(groups[class_name + "/" + file_name])
group_counts = Counter(group)
groups_discovered |= set(group)
for i, group_name in enumerate(sorted(group_counts.keys())):
if i == 0:
print('{0:>{1}}'.format(class_name, class_width), end=" ")
else:
print('{0:>{1}}'.format("", class_width), end=" ")
print('{0:>{1}}'.format(group_name, group_width), end="")
print('{0:>8}'.format(group_counts[group_name]))
print('{0:>{1}}'.format("", class_width), end=" ")
print('{0:>{1}}'.format("Total", group_width), end="")
print('{0:>8}'.format(len(file_names)))
grand_total += len(file_names)
print("-"*(class_width+group_width+3+8))
rows = []
class_totals = []
group_totals = {}
row = "class,"
for group_name in group_names:
row += group_name + ","
group_totals[group_name] = 0
row += "total"
rows.append(row)
for class_name in class_names:
row = class_name + ","
total = 0
group = []
file_names = os.listdir(os.path.join(data_dir, class_name))
for file_name in file_names:
group.append(groups[class_name + "/" + file_name])
group_counts = Counter(group)
for group_name in group_names:
if group_name in group_counts.keys():
row += str(group_counts[group_name]) + ","
total += group_counts[group_name]
group_totals[group_name] += group_counts[group_name]
else:
row += str(0) + ","
row += str(total)
class_totals.append(total)
rows.append(row)
row = "total,"
for group_name in group_names:
row += str(group_totals[group_name]) + ","
row += str(grand_total)
rows.append(row)
print(len(class_names), "classes |",
len(groups_discovered), "groups |",
grand_total, "images")
counts = list(group_totals.values())
print("median class size:", round(np.median(class_totals), 2),
"| mad:", round(np.median(
np.abs(np.array(class_totals) - np.median(class_totals)))))
print("mean class size:", round(np.mean(class_totals), 2),
"| sd:", round(np.std(class_totals), 2))
print("median group size:", round(np.median(counts), 2),
"| mad:", round(np.median(
np.abs(np.array(counts) - np.median(counts)))))
print("mean group size:", round(np.mean(counts), 2),
"| sd:", round(np.std(counts), 2))
print()
# csv
for row in rows:
print(row)
if csv is not None:
with open(csv, 'w', newline="") as f:
for row in rows:
f.write("%s\n" % row)
print("csv saved to", csv)
else:
print('{0:>{1}}'.format("Class", class_width), end=" ")
print('{0:>8}'.format("Count"))
print("-"*(class_width+3+8))
class_totals =[]
for class_name in class_names:
file_names = os.listdir(os.path.join(data_dir, class_name))
print('{0:>{1}}'.format(class_name, class_width), end=" ")
print('{0:>8}'.format(len(file_names)))
class_totals.append(len(file_names))
grand_total += len(file_names)
print("-"*(class_width+3+8))
print(len(class_names), "classes |",
grand_total, "images")
print("median class size:", round(np.median(class_totals), 2),
"| mad:", round(np.median(
np.abs(np.array(class_totals) - np.median(class_totals)))))
print("mean class size:", round(np.mean(class_totals), 2),
"| sd:", round(np.std(class_totals), 2))
print()
def print_class_balance(class_labels, class_numbers,
fold_labels, fold_names):
class_label_names = sorted(set(class_labels))
first_column_width = max([len(x) for x in fold_names] + [5]) # 5 is value length
columnwidth = max([len(x) for x in class_label_names] + [5]) # 5 is value length
empty_cell = " " * first_column_width
# Print header
print(" " + empty_cell + "--- Class Balance ---".center(
(columnwidth + 1) * len(class_label_names), ' '))
print(" " + empty_cell, end=" ")
for name in class_label_names:
print("%{0}s".format(columnwidth) % name, end=" ")
print()
# Print rows
for i, fold_name in enumerate(fold_names):
print(" %{0}s".format(first_column_width) % fold_name, end=" ")
counts = Counter(fold_labels[i])
for j in range(len(class_label_names)):
if j not in fold_labels[i]:
proportion = 0
else:
proportion = counts[j] #/ float(len(fold_labels[i]))
cell = "%{0}d".format(columnwidth) % proportion
print(cell, end=" ")
print()
print(" %{0}s".format(first_column_width) % "Total", end=" ")
counts = Counter(class_numbers)
for i in range(len(class_label_names)):
proportion = counts[i] #/ float(len(class_numbers))
cell = "%{0}d".format(columnwidth) % proportion
print(cell, end=" ")
print("\n")
def save_model_summary(filename, model):
with open(filename, "w") as text_file:
sys.stdout = text_file
model.summary()
sys.stdout = sys.__stdout__
def print_model_info(batch_size, epochs, learning_rate, dropout_rate, model, base_model=None):
print('--- Hyperparameter & Model Summary ---')
if base_model is not None:
print("Base Model:", base_model.name)
print("Feature Layer:", base_model.layers[-1].name)
print("Batch Size:", batch_size)
print("Epochs:", epochs)
#print("Learning Rate:", learning_rate)
print("Dropout Rate:", dropout_rate)
print("Optimizer:", model.optimizer)
print("Optimizer Config:", model.optimizer.get_config())
#print("Model Config:", model.get_config())
print("Final Layers:")
model.summary()
def print_confusion_matrix(cm, labels,
hide_zeroes=False, hide_diagonal=False, hide_threshold=None,
normalize=True):
"""pretty print for confusion matrixes"""
if normalize:
np.seterr(divide='ignore', invalid='ignore')
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
columnwidth = max([len(x) for x in labels] + [5]) # 5 is value length
empty_cell = " " * columnwidth
# Print header
print(" " + empty_cell + "--- Confusion Matrix (actual, predicted) ---".center((columnwidth + 1) * len(labels), ' '))
#print(" " + empty_cell + "(actual, predicted)".center((columnwidth + 1) * len(labels), ' '))
print(" " + empty_cell, end=" ")
for label in labels:
print("%{0}s".format(columnwidth) % label, end=" ")
print()
# Print rows
for i, label1 in enumerate(labels):
print(" %{0}s".format(columnwidth) % label1, end=" ")
for j in range(len(labels)):
if normalize:
cell = "%{0}.2f".format(columnwidth) % cm[i, j]
else:
cell = "%{0}d".format(columnwidth) % cm[i, j]
if hide_zeroes:
cell = cell if float(cm[i, j]) != 0 else empty_cell
if hide_diagonal:
cell = cell if i != j else empty_cell
if hide_threshold:
cell = cell if cm[i, j] > hide_threshold else empty_cell
print(cell, end=" ")
print()
def getROC(ground_truth, scores, class_list):
# Compute ROC curve and ROC area for each class
fpr = dict()
tpr = dict()
roc_auc = dict()
ground_truth = np.array(ground_truth);
scores = np.array(scores);
scores = np.squeeze(scores)
for i in range(len(class_list)):
(fpr[i], tpr[i], _) = roc_curve(ground_truth[:, i], scores[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
return (fpr, tpr, roc_auc)
def multiClassROC(fpr, tpr, roc_auc, class_list,):
n_classes = len(class_list)
lw = 2 # line width
# Compute macro-average ROC curve and ROC area
# First aggregate all false positive rates
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))
# Then interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(n_classes):
mean_tpr += interp(all_fpr, fpr[i], tpr[i])
# Finally average it and compute AUC
mean_tpr /= n_classes
fpr['macro'] = all_fpr
tpr['macro'] = mean_tpr
roc_auc['macro'] = auc(fpr['macro'], tpr['macro'])
# Plot all ROC curves
# fig = plt.figure()
plt.plot(
fpr['macro'],
tpr['macro'],
label='macro (a = {0:0.2f})'.format(roc_auc['macro']),
color='navy',
linestyle=':',
linewidth=4,
)
colors = cycle([
'aqua',
'darkorange',
'cornflowerblue',
'green',
'red',
'blue',
'black',
])
for (i, color) in zip(range(n_classes), colors):
plt.plot(fpr[i], tpr[i], color=color, lw=lw,
label='{0} (a={1:0.2f})'.format(class_list[i],
roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--', lw=lw)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC plot')
plt.legend(loc='lower right')
plt.savefig('test_ROC.png')
# plt.show()
'''
def save_roc(actual_classes,
class_list = sorted(set(class_labels))
(fpr, tpr, roc_auc) = getROC(truth, prediction_scores_list, class_list)
multiClassROC(fpr, tpr, roc_auc, class_list)
'''