Switch to unified view

a b/test_luna_props_scan_dsb.py
1
import sys
2
import lasagne as nn
3
import numpy as np
4
import theano
5
import os
6
7
import pathfinder
8
import utils
9
from configuration import config, set_configuration
10
from utils_plots import plot_slice_3d_3
11
import theano.tensor as T
12
import utils_lung
13
import blobs_detection
14
import logger
15
from collections import defaultdict
16
17
theano.config.warn_float64 = 'raise'
18
19
if len(sys.argv) < 2:
20
    sys.exit("Usage: test_luna_props_scan.py <configuration_name>")
21
22
config_name = sys.argv[1]
23
set_configuration('configs_luna_props_scan', config_name)
24
25
# predictions path
26
predictions_dir = utils.get_dir_path('model-predictions', pathfinder.METADATA_PATH)
27
outputs_path = predictions_dir + '/%s' % config_name
28
utils.auto_make_dir(outputs_path)
29
30
# logs
31
logs_dir = utils.get_dir_path('logs', pathfinder.METADATA_PATH)
32
sys.stdout = logger.Logger(logs_dir + '/%s.log' % config_name)
33
sys.stderr = sys.stdout
34
35
# builds model and sets its parameters
36
model = config().build_model()
37
38
x_shared = nn.utils.shared_empty(dim=len(model.l_in.shape))
39
givens_valid = {}
40
givens_valid[model.l_in.input_var] = x_shared
41
42
get_predictions_patch = theano.function([],
43
                                        nn.layers.get_output(model.l_out, deterministic=True),
44
                                        givens=givens_valid,
45
                                        on_unused_input='ignore')
46
47
data_iterator = config().data_iterator
48
49
#existing_preds = [f.rsplit('.') for f in os.listdir(outputs_path)]
50
#print existing_preds
51
52
print
53
print 'Data'
54
print 'n samples: %d' % data_iterator.nsamples
55
56
prev_pid = None
57
candidates = []
58
patients_count = 0
59
max_malignancy = 0.
60
for n, (x, candidate_zyxd, id) in enumerate(data_iterator.generate()):
61
    pid = id[0]
62
63
    if pid != prev_pid and prev_pid is not None:
64
        print patients_count, prev_pid, len(candidates)
65
        candidates = np.asarray(candidates)
66
        utils.save_pkl(candidates, outputs_path + '/%s.pkl' % prev_pid)
67
        patients_count += 1
68
        candidates = []
69
70
    #print 'x.shape', x.shape
71
    x_shared.set_value(x)
72
    predictions = get_predictions_patch()
73
    #print 'predictions.shape', predictions.shape
74
    #print 'candidate_zyxd', candidate_zyxd.shape
75
76
    candidate_zyxd_pred = np.append(candidate_zyxd, [predictions])
77
    #print 'candidate_zyxd_pred', candidate_zyxd_pred
78
    candidates.append(candidate_zyxd_pred)
79
80
    prev_pid = pid
81
82
# save the last one
83
print patients_count, prev_pid, len(candidates)
84
candidates = np.asarray(candidates)
85
utils.save_pkl(candidates, outputs_path + '/%s.pkl' % prev_pid)