forked from ellisdg/3DUnetCNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
70 lines (53 loc) · 2.15 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
import numpy as np
import nibabel as nib
import os
import glob
import pandas as pd
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
def get_whole_tumor_mask(data):
return data > 0
def get_tumor_core_mask(data):
return np.logical_or(data == 1, data == 4)
def get_enhancing_tumor_mask(data):
return data == 4
def dice_coefficient(truth, prediction):
return 2 * np.sum(truth * prediction)/(np.sum(truth) + np.sum(prediction))
def main():
header = ("WholeTumor", "TumorCore", "EnhancingTumor")
masking_functions = (get_whole_tumor_mask, get_tumor_core_mask, get_enhancing_tumor_mask)
rows = list()
subject_ids = list()
for case_folder in glob.glob("prediction/*"):
if not os.path.isdir(case_folder):
continue
subject_ids.append(os.path.basename(case_folder))
truth_file = os.path.join(case_folder, "truth.nii.gz")
truth_image = nib.load(truth_file)
truth = truth_image.get_data()
prediction_file = os.path.join(case_folder, "prediction.nii.gz")
prediction_image = nib.load(prediction_file)
prediction = prediction_image.get_data()
rows.append([dice_coefficient(func(truth), func(prediction))for func in masking_functions])
df = pd.DataFrame.from_records(rows, columns=header, index=subject_ids)
df.to_csv("./prediction/brats_scores.csv")
scores = dict()
for index, score in enumerate(df.columns):
values = df.values.T[index]
scores[score] = values[np.isnan(values) == False]
plt.boxplot(list(scores.values()), labels=list(scores.keys()))
plt.ylabel("Dice Coefficient")
plt.savefig("validation_scores_boxplot.png")
plt.close()
if os.path.exists("./training.log"):
training_df = pd.read_csv("./training.log").set_index('epoch')
plt.plot(training_df['loss'].values, label='training loss')
plt.plot(training_df['val_loss'].values, label='validation loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.xlim((0, len(training_df.index)))
plt.legend(loc='upper right')
plt.savefig('loss_graph.png')
if __name__ == "__main__":
main()