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