|
a |
|
b/fetal_net/model/unet3d/isensee2017.py |
|
|
1 |
from functools import partial |
|
|
2 |
|
|
|
3 |
from keras.layers import Input, LeakyReLU, Add, UpSampling3D, Activation, SpatialDropout3D, Conv3D |
|
|
4 |
from keras.engine import Model |
|
|
5 |
from keras.optimizers import Adam |
|
|
6 |
|
|
|
7 |
from .unet import create_convolution_block, concatenate |
|
|
8 |
from ...metrics import weighted_dice_coefficient_loss, vod_coefficient, dice_coefficient_loss, dice_coefficient |
|
|
9 |
|
|
|
10 |
import numpy as np |
|
|
11 |
|
|
|
12 |
create_convolution_block = partial(create_convolution_block, activation=LeakyReLU, instance_normalization=True) |
|
|
13 |
|
|
|
14 |
|
|
|
15 |
def isensee2017_model_3d(input_shape=(1, 128, 128, 128), n_base_filters=16, depth=5, dropout_rate=0.3, |
|
|
16 |
n_segmentation_levels=1, n_labels=1, optimizer=Adam, initial_learning_rate=5e-4, |
|
|
17 |
loss_function=dice_coefficient_loss, activation_name="sigmoid", mask_shape=None, |
|
|
18 |
**kargs): |
|
|
19 |
""" |
|
|
20 |
This function builds a model proposed by Isensee et al. for the BRATS 2017 competition: |
|
|
21 |
https://www.cbica.upenn.edu/sbia/Spyridon.Bakas/MICCAI_BraTS/MICCAI_BraTS_2017_proceedings_shortPapers.pdf |
|
|
22 |
|
|
|
23 |
This network is highly similar to the model proposed by Kayalibay et al. "CNN-based Segmentation of Medical |
|
|
24 |
Imaging Data", 2017: https://arxiv.org/pdf/1701.03056.pdf |
|
|
25 |
|
|
|
26 |
|
|
|
27 |
:param input_shape: |
|
|
28 |
:param n_base_filters: |
|
|
29 |
:param depth: |
|
|
30 |
:param dropout_rate: |
|
|
31 |
:param n_segmentation_levels: |
|
|
32 |
:param n_labels: |
|
|
33 |
:param optimizer: |
|
|
34 |
:param initial_learning_rate: |
|
|
35 |
:param loss_function: |
|
|
36 |
:param activation_name: |
|
|
37 |
:return: |
|
|
38 |
""" |
|
|
39 |
inputs = Input(input_shape) |
|
|
40 |
|
|
|
41 |
current_layer = inputs |
|
|
42 |
level_output_layers = list() |
|
|
43 |
level_filters = list() |
|
|
44 |
for level_number in range(depth): |
|
|
45 |
n_level_filters = (2 ** level_number) * n_base_filters |
|
|
46 |
level_filters.append(n_level_filters) |
|
|
47 |
|
|
|
48 |
if current_layer is inputs: |
|
|
49 |
in_conv = create_convolution_block(current_layer, n_level_filters) |
|
|
50 |
else: |
|
|
51 |
in_conv = create_convolution_block(current_layer, n_level_filters, strides=(2, 2, 2)) |
|
|
52 |
|
|
|
53 |
context_output_layer = create_context_module(in_conv, n_level_filters, dropout_rate=dropout_rate) |
|
|
54 |
|
|
|
55 |
summation_layer = Add()([in_conv, context_output_layer]) |
|
|
56 |
level_output_layers.append(summation_layer) |
|
|
57 |
current_layer = summation_layer |
|
|
58 |
|
|
|
59 |
segmentation_layers = list() |
|
|
60 |
for level_number in range(depth - 2, -1, -1): |
|
|
61 |
up_sampling = create_up_sampling_module(current_layer, level_filters[level_number]) |
|
|
62 |
concatenation_layer = concatenate([level_output_layers[level_number], up_sampling], axis=1) |
|
|
63 |
localization_output = create_localization_module(concatenation_layer, level_filters[level_number]) |
|
|
64 |
current_layer = localization_output |
|
|
65 |
if level_number < n_segmentation_levels: |
|
|
66 |
segmentation_layers.insert(0, Conv3D(n_labels, (1, 1, 1))(current_layer)) |
|
|
67 |
|
|
|
68 |
output_layer = None |
|
|
69 |
for level_number in reversed(range(n_segmentation_levels)): |
|
|
70 |
segmentation_layer = segmentation_layers[level_number] |
|
|
71 |
if output_layer is None: |
|
|
72 |
output_layer = segmentation_layer |
|
|
73 |
else: |
|
|
74 |
output_layer = Add()([output_layer, segmentation_layer]) |
|
|
75 |
|
|
|
76 |
if level_number > 0: |
|
|
77 |
output_layer = UpSampling3D(size=(2, 2, 2))(output_layer) |
|
|
78 |
|
|
|
79 |
activation_block = Activation(activation_name)(output_layer) |
|
|
80 |
|
|
|
81 |
metrics = ['binary_accuracy', vod_coefficient] |
|
|
82 |
if loss_function != dice_coefficient_loss: |
|
|
83 |
metrics += [dice_coefficient] |
|
|
84 |
|
|
|
85 |
if mask_shape is not None: |
|
|
86 |
mask_input = Input(shape=mask_shape) |
|
|
87 |
inputs = [inputs, mask_input] |
|
|
88 |
loss_function = loss_function(mask_input) |
|
|
89 |
|
|
|
90 |
model = Model(inputs=inputs, outputs=activation_block, name='isensee2017_3d_Model_'+str(np.random.random())) |
|
|
91 |
model.compile(optimizer=optimizer(lr=initial_learning_rate), loss=loss_function, metrics=metrics) |
|
|
92 |
return model |
|
|
93 |
|
|
|
94 |
|
|
|
95 |
def create_localization_module(input_layer, n_filters): |
|
|
96 |
convolution1 = create_convolution_block(input_layer, n_filters) |
|
|
97 |
convolution2 = create_convolution_block(convolution1, n_filters, kernel=(1, 1, 1)) |
|
|
98 |
return convolution2 |
|
|
99 |
|
|
|
100 |
|
|
|
101 |
def create_up_sampling_module(input_layer, n_filters, size=(2, 2, 2)): |
|
|
102 |
up_sample = UpSampling3D(size=size)(input_layer) |
|
|
103 |
convolution = create_convolution_block(up_sample, n_filters) |
|
|
104 |
return convolution |
|
|
105 |
|
|
|
106 |
|
|
|
107 |
def create_context_module(input_layer, n_level_filters, dropout_rate=0.3, data_format="channels_first"): |
|
|
108 |
convolution1 = create_convolution_block(input_layer=input_layer, n_filters=n_level_filters) |
|
|
109 |
dropout = SpatialDropout3D(rate=dropout_rate, data_format=data_format)(convolution1) |
|
|
110 |
convolution2 = create_convolution_block(input_layer=dropout, n_filters=n_level_filters) |
|
|
111 |
return convolution2 |