a b/configs_seg_patch/luna_p1.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 theano.tensor as T
10
import utils
11
import nn_lung
12
13
restart_from_save = None
14
rng = np.random.RandomState(42)
15
16
# transformations
17
p_transform = {'patch_size': (64, 64, 64),
18
               'mm_patch_size': (64, 64, 64),
19
               'pixel_spacing': (1., 1., 1.)
20
               }
21
p_transform_augment = {
22
    'translation_range_z': [-27, 27],
23
    'translation_range_y': [-27, 27],
24
    'translation_range_x': [-27, 27],
25
    'rotation_range_z': [-180, 180],
26
    'rotation_range_y': [-180, 180],
27
    'rotation_range_x': [-180, 180]
28
}
29
30
31
# data preparation function
32
def data_prep_function(data, patch_center, luna_annotations, pixel_spacing, luna_origin, p_transform,
33
                       p_transform_augment, **kwargs):
34
    x, patch_annotation_tf, annotations_tf = data_transforms.transform_patch3d(data=data,
35
                                                                               luna_annotations=luna_annotations,
36
                                                                               patch_center=patch_center,
37
                                                                               p_transform=p_transform,
38
                                                                               p_transform_augment=p_transform_augment,
39
                                                                               pixel_spacing=pixel_spacing,
40
                                                                               luna_origin=luna_origin)
41
    x = data_transforms.hu2normHU(x)
42
    y = data_transforms.make_3d_mask_from_annotations(img_shape=x.shape, annotations=annotations_tf, shape='sphere')
43
    return x, y
44
45
46
data_prep_function_train = partial(data_prep_function, p_transform_augment=p_transform_augment, p_transform=p_transform)
47
data_prep_function_valid = partial(data_prep_function, p_transform_augment=None, p_transform=p_transform)
48
49
# data iterators
50
batch_size = 1
51
nbatches_chunk = 4
52
chunk_size = batch_size * nbatches_chunk
53
54
train_valid_ids = utils.load_pkl(pathfinder.LUNA_VALIDATION_SPLIT_PATH)
55
train_pids, valid_pids = train_valid_ids['train'], train_valid_ids['valid']
56
57
train_data_iterator = data_iterators.PatchPositiveLunaDataGenerator(data_path=pathfinder.LUNA_DATA_PATH,
58
                                                                    batch_size=chunk_size,
59
                                                                    transform_params=p_transform,
60
                                                                    data_prep_fun=data_prep_function_train,
61
                                                                    rng=rng,
62
                                                                    patient_ids=train_pids,
63
                                                                    full_batch=True, random=True, infinite=True)
64
65
valid_data_iterator = data_iterators.PatchPositiveLunaDataGenerator(data_path=pathfinder.LUNA_DATA_PATH,
66
                                                                    batch_size=1,
67
                                                                    transform_params=p_transform,
68
                                                                    data_prep_fun=data_prep_function_valid,
69
                                                                    rng=rng,
70
                                                                    patient_ids=valid_pids,
71
                                                                    full_batch=False, random=False, infinite=False)
72
73
nchunks_per_epoch = train_data_iterator.nsamples / chunk_size
74
max_nchunks = nchunks_per_epoch * 30
75
76
validate_every = int(1. * nchunks_per_epoch)
77
save_every = int(0.5 * nchunks_per_epoch)
78
79
learning_rate_schedule = {
80
    0: 1e-5,
81
    int(max_nchunks * 0.4): 5e-6,
82
    int(max_nchunks * 0.5): 3e-6,
83
    int(max_nchunks * 0.6): 2e-6,
84
    int(max_nchunks * 0.85): 1e-6,
85
    int(max_nchunks * 0.95): 5e-7
86
}
87
88
# model
89
conv3d = partial(dnn.Conv3DDNNLayer,
90
                 filter_size=3,
91
                 pad='same',
92
                 W=nn.init.Orthogonal(),
93
                 b=nn.init.Constant(0.01),
94
                 nonlinearity=nn.nonlinearities.linear)
95
96
max_pool3d = partial(dnn.MaxPool3DDNNLayer,
97
                     pool_size=2)
98
99
100
def build_model():
101
    l_in = nn.layers.InputLayer((None, 1,) + p_transform['patch_size'])
102
    l_target = nn.layers.InputLayer((None, 1,) + p_transform['patch_size'])
103
104
    net = {}
105
    base_n_filters = 64
106
    net['contr_1_1'] = conv3d(l_in, base_n_filters)
107
    net['contr_1_1'] = nn.layers.ParametricRectifierLayer(net['contr_1_1'])
108
    net['contr_1_2'] = conv3d(net['contr_1_1'], base_n_filters)
109
    net['contr_1_2'] = nn.layers.ParametricRectifierLayer(net['contr_1_2'])
110
    net['contr_1_3'] = conv3d(net['contr_1_2'], base_n_filters)
111
    net['contr_1_3'] = nn.layers.ParametricRectifierLayer(net['contr_1_3'])
112
    net['pool1'] = max_pool3d(net['contr_1_3'])
113
114
    net['encode_1'] = conv3d(net['pool1'], base_n_filters)
115
    net['encode_1'] = nn.layers.ParametricRectifierLayer(net['encode_1'])
116
    net['encode_2'] = conv3d(net['encode_1'], base_n_filters)
117
    net['encode_2'] = nn.layers.ParametricRectifierLayer(net['encode_2'])
118
    net['encode_3'] = conv3d(net['encode_2'], base_n_filters)
119
    net['encode_3'] = nn.layers.ParametricRectifierLayer(net['encode_3'])
120
    net['upscale1'] = nn_lung.Upscale3DLayer(net['encode_3'], 2)
121
122
    net['concat1'] = nn.layers.ConcatLayer([net['upscale1'], net['contr_1_3']],
123
                                           cropping=(None, None, "center", "center", "center"))
124
    net['expand_1_1'] = conv3d(net['concat1'], 2 * base_n_filters)
125
    net['expand_1_1'] = nn.layers.ParametricRectifierLayer(net['expand_1_1'])
126
    net['expand_1_2'] = conv3d(net['expand_1_1'], 2 * base_n_filters)
127
    net['expand_1_2'] = nn.layers.ParametricRectifierLayer(net['expand_1_2'])
128
    net['expand_1_3'] = conv3d(net['expand_1_2'], base_n_filters)
129
    net['expand_1_3'] = nn.layers.ParametricRectifierLayer(net['expand_1_3'])
130
131
    l_out = dnn.Conv3DDNNLayer(net['expand_1_3'], num_filters=1,
132
                               filter_size=1,
133
                               W=nn.init.Constant(0.),
134
                               nonlinearity=nn.nonlinearities.sigmoid)
135
136
    return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target)
137
138
139
def build_objective(model, deterministic=False, epsilon=1e-12):
140
    predictions = T.flatten(nn.layers.get_output(model.l_out))
141
    targets = T.flatten(nn.layers.get_output(model.l_target))
142
    dice = (2. * T.sum(targets * predictions) + epsilon) / (T.sum(predictions) + T.sum(targets) + epsilon)
143
    return -1. * dice
144
145
146
def build_updates(train_loss, model, learning_rate):
147
    updates = nn.updates.adam(train_loss, nn.layers.get_all_params(model.l_out), learning_rate)
148
    return updates