|
a |
|
b/generate_features_dsb.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: generate_features_dsb.py <configuration_name>") |
|
|
19 |
|
|
|
20 |
config_name = sys.argv[1] |
|
|
21 |
set_configuration('configs_gen_features', 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_featuremap = theano.function([], nn.layers.get_output(model.l_out, deterministic=True), |
|
|
41 |
givens=givens_valid, |
|
|
42 |
on_unused_input='ignore') |
|
|
43 |
|
|
|
44 |
data_iterator = config().data_iterator |
|
|
45 |
|
|
|
46 |
print |
|
|
47 |
print 'Data' |
|
|
48 |
print 'n samples: %d' % data_iterator.nsamples |
|
|
49 |
|
|
|
50 |
prev_pid = None |
|
|
51 |
candidates = [] |
|
|
52 |
patients_count = 0 |
|
|
53 |
patch_size = 48 |
|
|
54 |
stride = 16 |
|
|
55 |
for n, (x, id) in enumerate(data_iterator.generate()): |
|
|
56 |
pid = id |
|
|
57 |
|
|
|
58 |
print(pid) |
|
|
59 |
print model.l_out.output_shape |
|
|
60 |
predictions = np.empty(((x.shape[2]-patch_size+1)//stride, (x.shape[3]-patch_size+1)//stride, (x.shape[4]-patch_size+1)//stride,) + (model.l_out.output_shape[1],)) |
|
|
61 |
print predictions.shape |
|
|
62 |
print 'x.shape', x.shape |
|
|
63 |
for idxi, i in enumerate(np.arange(0,x.shape[2]-patch_size,stride)): |
|
|
64 |
print 'slice idxi', idxi |
|
|
65 |
for idxj, j in enumerate(np.arange(0,x.shape[3]-patch_size,stride)): |
|
|
66 |
for idxk, k in enumerate(np.arange(0,x.shape[4]-patch_size,stride)): |
|
|
67 |
#print i, j, k, '|', idxi, idxj, idxk, x.shape[4], x.shape[4]-patch_size+1 |
|
|
68 |
x_in = x[0,0,i:i+patch_size,j:j+patch_size,k:k+patch_size] |
|
|
69 |
#print x_in.shape |
|
|
70 |
x_shared.set_value(x_in[None,:,:,:]) |
|
|
71 |
fm = get_featuremap() |
|
|
72 |
#print fm.shape |
|
|
73 |
predictions[idxi,idxj,idxk] = fm[0] |
|
|
74 |
|
|
|
75 |
result = np.concatenate(predictions,axis=0) |
|
|
76 |
|
|
|
77 |
utils.save_pkl(result, outputs_path + '/%s.pkl' % pid) |