|
a |
|
b/model_test.py |
|
|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
""" |
|
|
3 |
Created on Wed Mar 01 17:08:10 2017 |
|
|
4 |
|
|
|
5 |
@author: Shreyas_V |
|
|
6 |
""" |
|
|
7 |
import Brain_pipeline |
|
|
8 |
import Metrics |
|
|
9 |
import numpy as np |
|
|
10 |
|
|
|
11 |
#For testing a model |
|
|
12 |
|
|
|
13 |
def predict_labels(test_images, model, mu, sigma): |
|
|
14 |
''' |
|
|
15 |
INPUT: a numpy array of 4x240x240 elements and a keras model already trained over various images |
|
|
16 |
OUTPUT: a numpy array of 240x240 label elements |
|
|
17 |
''' |
|
|
18 |
predicted_images = [] # list to maintain predicted labels |
|
|
19 |
for i in test_images: |
|
|
20 |
msk = ((i[0]+i[1]+i[2]+i[3])!=0.) |
|
|
21 |
patches = Brain_pipeline.test_patches(i, mu, sigma) |
|
|
22 |
if patches.shape[0] == 0: |
|
|
23 |
predicted_images.append(np.full((240, 240), 0)) |
|
|
24 |
continue |
|
|
25 |
print "running..." |
|
|
26 |
predicted_slice = model.predict_classes(patches) |
|
|
27 |
predicted_slice = Brain_pipeline.reconstruct_labels(msk, predicted_slice) |
|
|
28 |
predicted_images.append(predicted_slice) |
|
|
29 |
return np.array(predicted_images) |
|
|
30 |
|
|
|
31 |
def get_metrics(test_images, gt, msk): |
|
|
32 |
DSC = [] |
|
|
33 |
acc = [] |
|
|
34 |
DSC_core = [] |
|
|
35 |
PPV = [] |
|
|
36 |
for i, j in zip(test_images, gt): |
|
|
37 |
DSC.append(Metrics.DSC(i, j)) |
|
|
38 |
acc.append(Metrics.accuracy(i, j, msk)) |
|
|
39 |
DSC_core.append(Metrics.DSC_core_tumor(i, j)) |
|
|
40 |
PPV.append(Metrics.PPV(i, j)) |
|
|
41 |
return DSC, acc, DSC_core, PPV |
|
|
42 |
|
|
|
43 |
def test_slices(test_images, gt, model, mu=0, sigma=1): |
|
|
44 |
pred = predict_labels(test_images, model, mu, sigma) |
|
|
45 |
msk = [] |
|
|
46 |
for i in test_images: |
|
|
47 |
msk.append((i[0]+i[1]+i[2]+i[3])!=0.) |
|
|
48 |
return pred, get_metrics(pred, gt, msk) |
|
|
49 |
|
|
|
50 |
|