|
a |
|
b/configs_gen_features/gf1.py |
|
|
1 |
import numpy as np |
|
|
2 |
import data_transforms |
|
|
3 |
import data_iterators |
|
|
4 |
import pathfinder |
|
|
5 |
import lasagne as nn |
|
|
6 |
from collections import namedtuple |
|
|
7 |
from functools import partial |
|
|
8 |
import lasagne.layers.dnn as dnn |
|
|
9 |
import lasagne |
|
|
10 |
import theano.tensor as T |
|
|
11 |
import utils |
|
|
12 |
import os |
|
|
13 |
restart_from_save = None |
|
|
14 |
rng = np.random.RandomState(42) |
|
|
15 |
|
|
|
16 |
# transformations |
|
|
17 |
p_transform_data_in = {'patch_size': (432, 432, 432), |
|
|
18 |
'mm_patch_size': (432, 432, 432), |
|
|
19 |
'pixel_spacing': (1., 1., 1.) |
|
|
20 |
} |
|
|
21 |
|
|
|
22 |
p_transform = {'patch_size': (48, 48, 48), |
|
|
23 |
'mm_patch_size': (48, 48, 48), |
|
|
24 |
'pixel_spacing': (1., 1., 1.) |
|
|
25 |
} |
|
|
26 |
|
|
|
27 |
|
|
|
28 |
# data preparation function |
|
|
29 |
def data_prep_function(data, pixel_spacing, p_transform=p_transform): |
|
|
30 |
# TODO: MAKE SURE THAT DATA IS PREPROCESSED THE SAME WAY |
|
|
31 |
#lung_mask = lung_segmentation.segment_HU_scan(data) |
|
|
32 |
x, tf_matrix = data_transforms.transform_scan3d(data=data, |
|
|
33 |
pixel_spacing=pixel_spacing, |
|
|
34 |
p_transform=p_transform_data_in, |
|
|
35 |
lung_mask=None, |
|
|
36 |
p_transform_augment=None) |
|
|
37 |
print 'x.shape', x.shape |
|
|
38 |
x = data_transforms.pixelnormHU(x) |
|
|
39 |
print 'x.shape', x.shape |
|
|
40 |
return x, tf_matrix |
|
|
41 |
|
|
|
42 |
|
|
|
43 |
# data iterators |
|
|
44 |
batch_size = 1 |
|
|
45 |
nbatches_chunk = 1 |
|
|
46 |
chunk_size = batch_size * nbatches_chunk |
|
|
47 |
train_valid_ids = utils.load_pkl(pathfinder.VALIDATION_SPLIT_PATH) |
|
|
48 |
train_pids, valid_pids, test_pids = train_valid_ids['training'], train_valid_ids['validation'],train_valid_ids['test'] |
|
|
49 |
print 'n train', len(train_pids) |
|
|
50 |
print 'n valid', len(valid_pids) |
|
|
51 |
print 'n valid', len(test_pids) |
|
|
52 |
|
|
|
53 |
all_pids = [] |
|
|
54 |
all_pids.extend(train_pids) |
|
|
55 |
all_pids.extend(test_pids) |
|
|
56 |
all_pids.extend(valid_pids) |
|
|
57 |
|
|
|
58 |
data_iterator = data_iterators.DSBDataGenerator(data_path=pathfinder.DATA_PATH, |
|
|
59 |
batch_size=chunk_size, |
|
|
60 |
transform_params=None, |
|
|
61 |
data_prep_fun=data_prep_function, |
|
|
62 |
rng=rng, |
|
|
63 |
patient_pids=all_pids, |
|
|
64 |
infinite=True |
|
|
65 |
) |
|
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
nchunks_per_epoch = data_iterator.nsamples / chunk_size |
|
|
70 |
max_nchunks = nchunks_per_epoch * 100 |
|
|
71 |
|
|
|
72 |
validate_every = int(5. * nchunks_per_epoch) |
|
|
73 |
save_every = int(1. * nchunks_per_epoch) |
|
|
74 |
|
|
|
75 |
learning_rate_schedule = { |
|
|
76 |
0: 5e-4, |
|
|
77 |
int(max_nchunks * 0.5): 2e-4, |
|
|
78 |
int(max_nchunks * 0.6): 1e-4, |
|
|
79 |
int(max_nchunks * 0.7): 5e-5, |
|
|
80 |
int(max_nchunks * 0.8): 2e-5, |
|
|
81 |
int(max_nchunks * 0.9): 1e-5 |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
# model |
|
|
85 |
conv3d = partial(dnn.Conv3DDNNLayer, |
|
|
86 |
filter_size=3, |
|
|
87 |
pad='same', |
|
|
88 |
W=nn.init.Orthogonal(), |
|
|
89 |
nonlinearity=nn.nonlinearities.very_leaky_rectify) |
|
|
90 |
|
|
|
91 |
max_pool3d = partial(dnn.MaxPool3DDNNLayer, |
|
|
92 |
pool_size=2) |
|
|
93 |
|
|
|
94 |
drop = lasagne.layers.DropoutLayer |
|
|
95 |
|
|
|
96 |
dense = partial(lasagne.layers.DenseLayer, |
|
|
97 |
W=lasagne.init.Orthogonal(), |
|
|
98 |
nonlinearity=lasagne.nonlinearities.very_leaky_rectify) |
|
|
99 |
|
|
|
100 |
|
|
|
101 |
def inrn_v2(lin): |
|
|
102 |
n_base_filter = 32 |
|
|
103 |
|
|
|
104 |
l1 = conv3d(lin, n_base_filter, filter_size=1) |
|
|
105 |
|
|
|
106 |
l2 = conv3d(lin, n_base_filter, filter_size=1) |
|
|
107 |
l2 = conv3d(l2, n_base_filter, filter_size=3) |
|
|
108 |
|
|
|
109 |
l3 = conv3d(lin, n_base_filter, filter_size=1) |
|
|
110 |
l3 = conv3d(l3, n_base_filter, filter_size=3) |
|
|
111 |
l3 = conv3d(l3, n_base_filter, filter_size=3) |
|
|
112 |
|
|
|
113 |
l = lasagne.layers.ConcatLayer([l1, l2, l3]) |
|
|
114 |
|
|
|
115 |
l = conv3d(l, lin.output_shape[1], filter_size=1) |
|
|
116 |
|
|
|
117 |
l = lasagne.layers.ElemwiseSumLayer([l, lin]) |
|
|
118 |
|
|
|
119 |
l = lasagne.layers.NonlinearityLayer(l, nonlinearity=lasagne.nonlinearities.rectify) |
|
|
120 |
|
|
|
121 |
return l |
|
|
122 |
|
|
|
123 |
|
|
|
124 |
def inrn_v2_red(lin): |
|
|
125 |
# We want to reduce our total volume /4 |
|
|
126 |
|
|
|
127 |
den = 16 |
|
|
128 |
nom2 = 4 |
|
|
129 |
nom3 = 5 |
|
|
130 |
nom4 = 7 |
|
|
131 |
|
|
|
132 |
ins = lin.output_shape[1] |
|
|
133 |
|
|
|
134 |
l1 = max_pool3d(lin) |
|
|
135 |
|
|
|
136 |
l2 = conv3d(lin, ins // den * nom2, filter_size=3, stride=2) |
|
|
137 |
|
|
|
138 |
l3 = conv3d(lin, ins // den * nom2, filter_size=1) |
|
|
139 |
l3 = conv3d(l3, ins // den * nom3, filter_size=3, stride=2) |
|
|
140 |
|
|
|
141 |
l4 = conv3d(lin, ins // den * nom2, filter_size=1) |
|
|
142 |
l4 = conv3d(l4, ins // den * nom3, filter_size=3) |
|
|
143 |
l4 = conv3d(l4, ins // den * nom4, filter_size=3, stride=2) |
|
|
144 |
|
|
|
145 |
l = lasagne.layers.ConcatLayer([l1, l2, l3, l4]) |
|
|
146 |
|
|
|
147 |
return l |
|
|
148 |
|
|
|
149 |
|
|
|
150 |
def feat_red(lin): |
|
|
151 |
# We want to reduce the feature maps by a factor of 2 |
|
|
152 |
ins = lin.output_shape[1] |
|
|
153 |
l = conv3d(lin, ins // 2, filter_size=1) |
|
|
154 |
return l |
|
|
155 |
|
|
|
156 |
|
|
|
157 |
def build_model(): |
|
|
158 |
l_in = nn.layers.InputLayer((None, ) + p_transform['patch_size']) |
|
|
159 |
l_dim = nn.layers.DimshuffleLayer(l_in, pattern=[0,'x',1,2,3]) |
|
|
160 |
l_target = nn.layers.InputLayer((None, 1)) |
|
|
161 |
|
|
|
162 |
l = conv3d(l_dim, 64) |
|
|
163 |
l = inrn_v2_red(l) |
|
|
164 |
l = inrn_v2(l) |
|
|
165 |
l = feat_red(l) |
|
|
166 |
l = inrn_v2(l) |
|
|
167 |
|
|
|
168 |
l = inrn_v2_red(l) |
|
|
169 |
l = inrn_v2(l) |
|
|
170 |
l = feat_red(l) |
|
|
171 |
l = inrn_v2(l) |
|
|
172 |
|
|
|
173 |
l = feat_red(l) |
|
|
174 |
|
|
|
175 |
l_out = dense(l, 128) |
|
|
176 |
|
|
|
177 |
# l_out = nn.layers.DenseLayer(l, num_units=2, |
|
|
178 |
# W=nn.init.Constant(0.), |
|
|
179 |
# nonlinearity=nn.nonlinearities.softmax) |
|
|
180 |
|
|
|
181 |
metadata = utils.load_pkl(os.path.join("/home/eavsteen/dsb3/storage/metadata/dsb3/models/ikorshun/","luna_c3-20170226-174919.pkl")) |
|
|
182 |
|
|
|
183 |
for i in range(-20,0): |
|
|
184 |
print metadata['param_values'][i].shape |
|
|
185 |
|
|
|
186 |
nn.layers.set_all_param_values(l_out, metadata['param_values'][:-2]) |
|
|
187 |
|
|
|
188 |
return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target) |
|
|
189 |
|
|
|
190 |
|
|
|
191 |
def build_objective(model, deterministic=False, epsilon=1e-12): |
|
|
192 |
predictions = nn.layers.get_output(model.l_out, deterministic=deterministic) |
|
|
193 |
targets = T.cast(T.flatten(nn.layers.get_output(model.l_target)), 'int32') |
|
|
194 |
p = predictions[T.arange(predictions.shape[0]), targets] |
|
|
195 |
p = T.clip(p, epsilon, 1.) |
|
|
196 |
loss = T.mean(T.log(p)) |
|
|
197 |
return -loss |
|
|
198 |
|
|
|
199 |
|
|
|
200 |
def build_updates(train_loss, model, learning_rate): |
|
|
201 |
updates = nn.updates.adam(train_loss, nn.layers.get_all_params(model.l_out, trainable=True), learning_rate) |
|
|
202 |
return updates |
|
|
203 |
|