-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
455 lines (376 loc) · 13.9 KB
/
utils.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import pdb
import os
import numpy as np
import nibabel as nib
import torch
import sys
import time
import logging
import logging.handlers
import pydensecrf.densecrf as dcrf
from pydensecrf.utils import compute_unary, create_pairwise_bilateral,\
create_pairwise_gaussian, softmax_to_unary, unary_from_softmax
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_curve, auc, roc_auc_score, f1_score
from sklearn.metrics import precision_recall_fscore_support
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelBinarizer
import matplotlib.pyplot as plt
import pandas as pd
from pandas.plotting import table
# _, term_width = os.popen('stty size', 'r').read().split()
# term_width = int(term_width)
term_width = 80 #(set the term_width to 80 directly)
TOTAL_BAR_LENGTH = 65.
last_time = time.time()
begin_time = last_time
def dice_coef(preds, targets, backprop=True):
smooth = 1.0
class_num = 2
if backprop:
for i in range(class_num):
pred = preds[:,i,:,:]
target = targets[:,i,:,:]
intersection = (pred * target).sum()
loss_ = 1 - ((2.0 * intersection + smooth) / (pred.sum() + target.sum() + smooth))
if i == 0:
loss = loss_
else:
loss = loss + loss_
loss = loss/class_num
return loss
else:
# Need to generalize
targets = np.array(targets.cpu().numpy().argmax(1))
if len(preds.shape) > 3:
preds = np.array(preds.cpu().numpy()).argmax(1)
for i in range(class_num):
pred = (preds==i).astype(np.uint8)
target= (targets==i).astype(np.uint8)
intersection = (pred * target).sum()
loss_ = 1 - ((2.0 * intersection + smooth) / (pred.sum() + target.sum() + smooth))
if i == 0:
loss = loss_
else:
loss = loss + loss_
loss = loss/class_num
return loss
def get_crf_img(inputs, outputs):
for i in range(outputs.shape[0]):
img = inputs[i]
softmax_prob = outputs[i]
unary = unary_from_softmax(softmax_prob)
unary = np.ascontiguousarray(unary)
d = dcrf.DenseCRF(img.shape[0] * img.shape[1], 2)
d.setUnaryEnergy(unary)
feats = create_pairwise_gaussian(sdims=(10,10), shape=img.shape[:2])
d.addPairwiseEnergy(feats, compat=3, kernel=dcrf.DIAG_KERNEL,
normalization=dcrf.NORMALIZE_SYMMETRIC)
feats = create_pairwise_bilateral(sdims=(50,50), schan=(20,20,20),
img=img, chdim=2)
d.addPairwiseEnergy(feats, compat=10, kernel=dcrf.DIAG_KERNEL,
normalization=dcrf.NORMALIZE_SYMMETRIC)
Q = d.inference(5)
res = np.argmax(Q, axis=0).reshape((img.shape[0], img.shape[1]))
if i == 0:
crf = np.expand_dims(res,axis=0)
else:
res = np.expand_dims(res,axis=0)
crf = np.concatenate((crf,res),axis=0)
return crf
def erode_dilate(outputs, kernel_size=7):
kernel = np.ones((kernel_size,kernel_size),np.uint8)
outputs = outputs.astype(np.uint8)
for i in range(outputs.shape[0]):
img = outputs[i]
img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
outputs[i] = img
return outputs
def post_process(args, inputs, outputs, input_path=None,
crf_flag=True, erode_dilate_flag=True,
save=True, overlap=True):
inputs = (np.array(inputs.squeeze()).astype(np.float32)) * 255
inputs = np.expand_dims(inputs, axis=3)
inputs = np.concatenate((inputs,inputs,inputs), axis=3)
outputs = np.array(outputs)
# Conditional Random Field
if crf_flag:
outputs = get_crf_img(inputs, outputs)
else:
outputs = outputs.argmax(1)
# Erosion and Dilation
if erode_dilate_flag:
outputs = erode_dilate(outputs, kernel_size=7)
if save == False:
return outputs
outputs = outputs*255
for i in range(outputs.shape[0]):
path = input_path[i].split('/')
output_folder = os.path.join(args.output_root, path[-2])
try:
os.mkdir(output_folder)
except:
pass
output_path = os.path.join(output_folder, path[-1])
if overlap:
img = outputs[i]
img = np.expand_dims(img, axis=2)
zeros = np.zeros(img.shape)
img = np.concatenate((zeros,zeros,img), axis=2)
img = np.array(img).astype(np.float32)
img = inputs[i] + img
if img.max() > 0:
img = (img/img.max())*255
else:
img = (img/1) * 255
cv2.imwrite(output_path, img)
else:
img = outputs[i]
cv2.imwrite(output_path, img)
return None
class Checkpoint:
def __init__(self, model, optimizer=None, epoch=0, best_score=1):
self.model = model
self.optimizer = optimizer
self.epoch = epoch
self.best_score = best_score
def load(self, path):
checkpoint = torch.load(path)
self.model.load_state_dict(checkpoint["model_state"])
self.epoch = checkpoint["epoch"]
self.best_score = checkpoint["best_score"]
if self.optimizer:
self.optimizer.load_state_dict(checkpoint["optimizer_state"])
for state in self.optimizer.state.values():
for k, v in state.items():
if torch.is_tensor(v):
state[k] = v.cuda()
def save(self, path):
state_dict = self.model.module.state_dict()
torch.save({"model_state": state_dict,
"optimizer_state": self.optimizer.state_dict(),
"epoch": self.epoch,
"best_score": self.best_score}, path)
def progress_bar(current, total, msg=None):
''' Source Code from 'kuangliu/pytorch-cifar'
(https://github.com/kuangliu/pytorch-cifar/blob/master/utils.py)
'''
global last_time, begin_time
if current == 0:
begin_time = time.time() # Reset for new bar.
cur_len = int(TOTAL_BAR_LENGTH*current/total)
rest_len = int(TOTAL_BAR_LENGTH - cur_len) - 1
sys.stdout.write(' [')
for i in range(cur_len):
sys.stdout.write('=')
sys.stdout.write('>')
for i in range(rest_len):
sys.stdout.write('.')
sys.stdout.write(']')
cur_time = time.time()
step_time = cur_time - last_time
last_time = cur_time
tot_time = cur_time - begin_time
L = []
L.append(' Step: %s' % format_time(step_time))
L.append(' | Tot: %s' % format_time(tot_time))
if msg:
L.append(' | ' + msg)
msg = ''.join(L)
sys.stdout.write(msg)
for i in range(term_width-int(TOTAL_BAR_LENGTH)-len(msg)-3):
sys.stdout.write(' ')
# Go back to the center of the bar.
for i in range(term_width-int(TOTAL_BAR_LENGTH/2)+2):
sys.stdout.write('\b')
sys.stdout.write(' %d/%d ' % (current+1, total))
if current < total-1:
sys.stdout.write('\r')
else:
sys.stdout.write('\n')
sys.stdout.flush()
def format_time(seconds):
''' Source Code from 'kuangliu/pytorch-cifar'
(https://github.com/kuangliu/pytorch-cifar/blob/master/utils.py)
'''
days = int(seconds / 3600/24)
seconds = seconds - days*3600*24
hours = int(seconds / 3600)
seconds = seconds - hours*3600
minutes = int(seconds / 60)
seconds = seconds - minutes*60
secondsf = int(seconds)
seconds = seconds - secondsf
millis = int(seconds*1000)
f = ''
i = 1
if days > 0:
f += str(days) + 'D'
i += 1
if hours > 0 and i <= 2:
f += str(hours) + 'h'
i += 1
if minutes > 0 and i <= 2:
f += str(minutes) + 'm'
i += 1
if secondsf > 0 and i <= 2:
f += str(secondsf) + 's'
i += 1
if millis > 0 and i <= 2:
f += str(millis) + 'ms'
i += 1
if f == '':
f = '0ms'
return f
def get_logger(level="DEBUG", file_level="DEBUG"):
logger = logging.getLogger(None)
logger.setLevel(level)
fomatter = logging.Formatter(
'%(asctime)s [%(levelname)s] %(message)s (%(filename)s: %(lineno)s)')
fileHandler = logging.handlers.TimedRotatingFileHandler(
'result.log', when='d', encoding='utf-8')
fileHandler.setLevel(file_level)
fileHandler.setFormatter(fomatter)
logger.addHandler(fileHandler)
return logger
#### function to plot confusion matrix
def plot_confusion_matrix(y_true, y_pred, classes,
normalize=False,
title=None,
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if not title:
if normalize:
title = 'Normalized confusion matrix'
else:
title = 'Confusion matrix, without normalization'
# Compute confusion matrix
cm = confusion_matrix(y_true, y_pred)
#transpose the matrix to make x-axis True Class and Y-axis Predicted Class
cm= np.transpose(cm)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
fig, ax = plt.subplots()
im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
ax.figure.colorbar(im, ax=ax)
# We want to show all ticks...
ax.set(xticks=np.arange(cm.shape[0]),
yticks=np.arange(cm.shape[1]),
# ... and label them with the respective list entries
xticklabels=classes, yticklabels=classes,
#here we are not printing the title
#title=title,
xlabel='True label',
ylabel='Predicted label')
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
# Loop over data dimensions and create text annotations.
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
ax.text(j, i, format(cm[i, j], fmt),
ha="center", va="center",
color="white" if cm[i, j] > thresh else "black")
fig.tight_layout()
return fig,ax
# compute the AUC per class
# implementation adapted from
# https://stackoverflow.com/questions/39685740/calculate-sklearn-roc-auc-score-for-multi-class
def class_report(y_true, y_pred, y_score=None, average='micro', sklearn_cls_report = True):
if isinstance(y_true, list):
y_true = np.array(y_true)
if isinstance(y_pred, list):
y_pred = np.array(y_pred)
if y_true.shape != y_pred.shape:
print("Error! y_true %s is not the same shape as y_pred %s" % (
y_true.shape,
y_pred.shape)
)
return
if sklearn_cls_report:
class_report_df = pd.DataFrame(classification_report(y_true= y_true, y_pred = y_pred, output_dict=True)).transpose()
else:
lb = LabelBinarizer()
if len(y_true.shape) == 1:
lb.fit(y_true)
#Value counts of predictions
labels, cnt = np.unique(
y_pred,
return_counts=True)
n_classes = len(labels)
pred_cnt = pd.Series(cnt, index=labels)
metrics_summary = precision_recall_fscore_support(
y_true=y_true,
y_pred=y_pred,
labels=labels)
avg = list(precision_recall_fscore_support(
y_true=y_true,
y_pred=y_pred,
average='weighted'))
metrics_sum_index = ['precision', 'recall', 'f1-score', 'support']
class_report_df = pd.DataFrame(
list(metrics_summary),
index=metrics_sum_index,
columns=labels)
support = class_report_df.loc['support']
total = support.sum()
class_report_df['avg / total'] = avg[:-1] + [total]
class_report_df = class_report_df.T
class_report_df['pred'] = pred_cnt
class_report_df['pred'].iloc[-1] = total
# Create a figure and axis
fig, ax = plt.subplots(figsize=(10, 10))
# Create the table and add to the axis
table = ax.table(cellText=class_report_df.values, colLabels=class_report_df.columns, loc='center')
# Set the font size of the table
table.set_fontsize(14)
# Remove the axis and add a title
ax.axis('off')
ax.set_title('Sample Table')
return fig
#return class_report_df
def computeAUROC(dataGT, dataPRED, classCount):
# Computes area under ROC curve
# dataGT: ground truth data
# dataPRED: predicted data
if classCount == 1:
outAUROC = roc_auc_score(dataGT, dataPRED)
else:
outAUROC = []
for i in range(classCount):
try:
outAUROC.append(roc_auc_score(dataGT[:, i], dataPRED[:, i]))
except ValueError:
outAUROC.append(0)
return outAUROC
def get_acc(true_labels, predicted_labels):
"""Args:
true_labels: given groundtruth labels
predicted_labels: predicted labels from the model
Returns:
overall_acc: overll accuracy
"""
overall_acc = accuracy_score(true_labels, predicted_labels)
return overall_acc
def get_f1_score(true_labels, predicted_labels, average = "binary"):
"""Args:
true_labels: given groundtruth labels
predicted_labels: predicted labels from the model
Returns:
f1score: F1 score of data
"""
if average == "binary":
f1score = f1_score(true_labels, predicted_labels)
else:
f1score = f1_score(true_labels, predicted_labels, average = "weighted")
return f1score