-
Notifications
You must be signed in to change notification settings - Fork 40
/
test_luna_size_scan_dsb.py
91 lines (73 loc) · 2.81 KB
/
test_luna_size_scan_dsb.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
import sys
import lasagne as nn
import numpy as np
import theano
import os
import pathfinder
import utils
from configuration import config, set_configuration
from utils_plots import plot_slice_3d_3
import theano.tensor as T
import utils_lung
import blobs_detection
import logger
from collections import defaultdict
theano.config.warn_float64 = 'raise'
if len(sys.argv) < 2:
sys.exit("Usage: test_luna_size_scan.py <configuration_name>")
config_name = sys.argv[1]
set_configuration('configs_luna_size_scan', config_name)
# predictions path
predictions_dir = utils.get_dir_path('model-predictions', pathfinder.METADATA_PATH)
outputs_path = predictions_dir + '/%s' % config_name
utils.auto_make_dir(outputs_path)
# logs
logs_dir = utils.get_dir_path('logs', pathfinder.METADATA_PATH)
sys.stdout = logger.Logger(logs_dir + '/%s.log' % config_name)
sys.stderr = sys.stdout
# builds model and sets its parameters
model = config().build_model()
x_shared = nn.utils.shared_empty(dim=len(model.l_in.shape))
givens_valid = {}
givens_valid[model.l_in.input_var] = x_shared
get_predictions_patch = theano.function([],
nn.layers.get_output(model.l_out, deterministic=True),
givens=givens_valid,
on_unused_input='ignore')
data_iterator = config().data_iterator
#existing_preds = [f.rsplit('.') for f in os.listdir(outputs_path)]
#print existing_preds
print
print 'Data'
print 'n samples: %d' % data_iterator.nsamples
prev_pid = None
candidates = []
patients_count = 0
max_malignancy = 0.
for n, (x, candidate_zyxd, id) in enumerate(data_iterator.generate()):
pid = id[0]
if pid != prev_pid and prev_pid is not None:
print patients_count, prev_pid, len(candidates)
candidates = np.asarray(candidates)
a = np.asarray(sorted(candidates, key=lambda x: x[-1], reverse=True))
print 'max malignancies', a[:10,-1]
utils.save_pkl(a, outputs_path + '/%s.pkl' % prev_pid)
print 'saved predictions'
patients_count += 1
candidates = []
x_shared.set_value(x)
predictions = get_predictions_patch()
#print 'predictions.shape', predictions.shape
total_malignancy = np.sum(config().malignancy_weights*predictions)
#print 'total_malignancy', total_malignancy
#print 'candidate_zyxd', candidate_zyxd
candidate_zyxd_pred = np.append(candidate_zyxd, [predictions])
candidate_zyxd_pred_mal = np.append(candidate_zyxd_pred, [[total_malignancy]])
candidates.append(candidate_zyxd_pred_mal)
prev_pid = pid
# save the last one
print patients_count, prev_pid, len(candidates)
candidates = np.asarray(candidates)
a = np.asarray(sorted(candidates, key=lambda x: x[-1], reverse=True))
utils.save_pkl(a, outputs_path + '/%s.pkl' % prev_pid)
print 'saved predictions'