|
a |
|
b/test_fpred_scan.py |
|
|
1 |
import sys |
|
|
2 |
import lasagne as nn |
|
|
3 |
import numpy as np |
|
|
4 |
import theano |
|
|
5 |
import pathfinder |
|
|
6 |
import utils |
|
|
7 |
from configuration import config, set_configuration |
|
|
8 |
from utils_plots import plot_slice_3d_3 |
|
|
9 |
import theano.tensor as T |
|
|
10 |
import utils_lung |
|
|
11 |
import blobs_detection |
|
|
12 |
import logger |
|
|
13 |
from collections import defaultdict |
|
|
14 |
|
|
|
15 |
theano.config.warn_float64 = 'raise' |
|
|
16 |
|
|
|
17 |
if len(sys.argv) < 2: |
|
|
18 |
sys.exit("Usage: test_luna_scan.py <configuration_name>") |
|
|
19 |
|
|
|
20 |
config_name = sys.argv[1] |
|
|
21 |
set_configuration('configs_fpred_scan', config_name) |
|
|
22 |
|
|
|
23 |
# predictions path |
|
|
24 |
predictions_dir = utils.get_dir_path('model-predictions', pathfinder.METADATA_PATH) |
|
|
25 |
outputs_path = predictions_dir + '/%s' % config_name |
|
|
26 |
utils.auto_make_dir(outputs_path) |
|
|
27 |
|
|
|
28 |
# logs |
|
|
29 |
logs_dir = utils.get_dir_path('logs', pathfinder.METADATA_PATH) |
|
|
30 |
sys.stdout = logger.Logger(logs_dir + '/%s.log' % config_name) |
|
|
31 |
sys.stderr = sys.stdout |
|
|
32 |
|
|
|
33 |
# builds model and sets its parameters |
|
|
34 |
model = config().build_model() |
|
|
35 |
|
|
|
36 |
x_shared = nn.utils.shared_empty(dim=len(model.l_in.shape)) |
|
|
37 |
givens_valid = {} |
|
|
38 |
givens_valid[model.l_in.input_var] = x_shared |
|
|
39 |
|
|
|
40 |
get_predictions_patch = theano.function([], |
|
|
41 |
nn.layers.get_output(model.l_out, deterministic=True), |
|
|
42 |
givens=givens_valid, |
|
|
43 |
on_unused_input='ignore') |
|
|
44 |
|
|
|
45 |
data_iterator = config().data_iterator |
|
|
46 |
|
|
|
47 |
print |
|
|
48 |
print 'Data' |
|
|
49 |
print 'n samples: %d' % data_iterator.nsamples |
|
|
50 |
|
|
|
51 |
nblob2prob, nblob2label = {}, {} |
|
|
52 |
pid2candidates = defaultdict(list) |
|
|
53 |
for n, (x, candidate_zyxd, id) in enumerate(data_iterator.generate()): |
|
|
54 |
pid = id[0] |
|
|
55 |
x_shared.set_value(x) |
|
|
56 |
predictions = get_predictions_patch() |
|
|
57 |
label = candidate_zyxd[-1] |
|
|
58 |
p1 = predictions[0][1] |
|
|
59 |
nblob2prob[n] = p1 |
|
|
60 |
nblob2label[n] = label |
|
|
61 |
candidate_zyxdp = np.append(candidate_zyxd, [[p1]]) |
|
|
62 |
pid2candidates[pid].append(candidate_zyxdp) |
|
|
63 |
|
|
|
64 |
|
|
|
65 |
for k in pid2candidates.iterkeys(): |
|
|
66 |
candidates = np.asarray(pid2candidates[k]) |
|
|
67 |
candidates_wo_dupes = utils_lung.filter_close_neighbors(candidates) |
|
|
68 |
a = np.asarray(sorted(candidates_wo_dupes, key=lambda x: x[-1], reverse=True)) |
|
|
69 |
utils.save_pkl(a, outputs_path + '/%s.pkl' % k) |