-
Notifications
You must be signed in to change notification settings - Fork 40
/
tryout_unbroadcasting_layer.py
154 lines (119 loc) · 6.1 KB
/
tryout_unbroadcasting_layer.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#same as dsb_a_eliasv2_c3_s2_p8a1.py, but with dropout on the dense layers
import numpy as np
import data_transforms
import data_iterators
import pathfinder
import lasagne as nn
import nn_lung
from collections import namedtuple
from functools import partial
import lasagne.layers.dnn as dnn
import theano.tensor as T
import utils
import utils_lung
# TODO: import correct config here
candidates_config = 'dsb_c3_s2_p8a1_ls_elias'
restart_from_save = '/home/eavsteen/dsb3/storage/metadata/dsb3/models/eavsteen/dsb_a_eliasv9_c3_s2_p8a1-20170316-112742.pkl'
rng = np.random.RandomState(42)
predictions_dir = utils.get_dir_path('model-predictions', pathfinder.METADATA_PATH)
candidates_path = predictions_dir + '/%s' % candidates_config
id2candidates_path = utils_lung.get_candidates_paths(candidates_path)
# transformations
p_transform = {'patch_size': (48, 48, 48),
'mm_patch_size': (48, 48, 48),
'pixel_spacing': (1., 1., 1.)
}
n_candidates_per_patient = 10
def data_prep_function(data, patch_centers, pixel_spacing, p_transform,
p_transform_augment, **kwargs):
x = data_transforms.transform_dsb_candidates(data=data,
patch_centers=patch_centers,
p_transform=p_transform,
p_transform_augment=p_transform_augment,
pixel_spacing=pixel_spacing)
x = data_transforms.pixelnormHU(x)
return x
data_prep_function_train = partial(data_prep_function, p_transform_augment=None,
p_transform=p_transform)
data_prep_function_valid = partial(data_prep_function, p_transform_augment=None,
p_transform=p_transform)
# data iterators
batch_size = 4
train_valid_ids = utils.load_pkl(pathfinder.VALIDATION_SPLIT_PATH)
train_pids, valid_pids = train_valid_ids['training'], train_valid_ids['validation']
print 'n train', len(train_pids)
print 'n valid', len(valid_pids)
train_data_iterator = data_iterators.DSBPatientsDataGenerator(data_path=pathfinder.DATA_PATH,
batch_size=batch_size,
transform_params=p_transform,
n_candidates_per_patient=n_candidates_per_patient,
data_prep_fun=data_prep_function_train,
id2candidates_path=id2candidates_path,
rng=rng,
patient_ids=train_pids,
random=True, infinite=True)
valid_data_iterator = data_iterators.DSBPatientsDataGenerator(data_path=pathfinder.DATA_PATH,
batch_size=1,
transform_params=p_transform,
n_candidates_per_patient=n_candidates_per_patient,
data_prep_fun=data_prep_function_valid,
id2candidates_path=id2candidates_path,
rng=rng,
patient_ids=valid_pids,
random=False, infinite=False)
nchunks_per_epoch = train_data_iterator.nsamples / batch_size
max_nchunks = nchunks_per_epoch * 10
validate_every = int(0.5 * nchunks_per_epoch)
save_every = int(0.25 * nchunks_per_epoch)
learning_rate_schedule = {
0: 1e-5,
int(5 * nchunks_per_epoch): 2e-6,
int(6 * nchunks_per_epoch): 1e-6,
int(7 * nchunks_per_epoch): 5e-7,
int(9 * nchunks_per_epoch): 2e-7
}
# model
conv3 = partial(dnn.Conv3DDNNLayer,
pad="valid",
filter_size=3,
nonlinearity=nn.nonlinearities.rectify,
b=nn.init.Constant(0.1),
W=nn.init.Orthogonal("relu"))
max_pool = partial(dnn.MaxPool3DDNNLayer,
pool_size=2)
drop = nn.layers.DropoutLayer
def dense_prelu_layer(l_in, num_units):
l = nn.layers.DenseLayer(l_in, num_units=num_units, W=nn.init.Orthogonal(),
nonlinearity=nn.nonlinearities.linear)
l = nn.layers.ParametricRectifierLayer(l)
return l
def build_model():
l_in = nn.layers.InputLayer((None, n_candidates_per_patient, 1,) + p_transform['patch_size'])
l_in_un = nn_lung.Unbroadcast(l_in)
l_in_rshp = nn.layers.ReshapeLayer(l_in_un, (-1, 1,) + p_transform['patch_size'])
l_target = nn.layers.InputLayer((batch_size,))
l = conv3(l_in_rshp, num_filters=128)
l = conv3(l, num_filters=128)
l = max_pool(l)
l = conv3(l, num_filters=128)
l = conv3(l, num_filters=128)
l = max_pool(l)
l = conv3(l, num_filters=256)
l = conv3(l, num_filters=256)
l = conv3(l, num_filters=256)
l = dense_prelu_layer(l, num_units=512)
l = dense_prelu_layer(l, num_units=512)
l = nn.layers.DenseLayer(l, num_units=1, W=nn.init.Orthogonal(),
nonlinearity=None)
l = nn.layers.ReshapeLayer(l, (-1, n_candidates_per_patient, 1))
l_out = nn_lung.AggAllBenignExp(l)
return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target)
def build_objective(model, deterministic=False, epsilon=1e-12):
p = nn.layers.get_output(model.l_out, deterministic=deterministic)
targets = T.flatten(nn.layers.get_output(model.l_target))
p = T.clip(p, epsilon, 1.-epsilon)
bce = T.nnet.binary_crossentropy(p, targets)
return T.mean(bce)
def build_updates(train_loss, model, learning_rate):
updates = nn.updates.adam(train_loss, nn.layers.get_all_params(model.l_out, trainable=True), learning_rate)
return updates