-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
199 lines (161 loc) · 8.11 KB
/
model.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from tensorflow.keras.layers import Conv2D, MaxPooling2D, UpSampling2D, BatchNormalization, Reshape, AveragePooling2D, \
Permute, Activation, Flatten, Dense, Input, \
add, multiply
from tensorflow.keras.layers import concatenate, Dropout
from tensorflow.keras.models import Model
from tensorflow.keras.layers import concatenate
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.layers import Lambda
import tensorflow.keras.backend as K
from tensorflow.keras.losses import categorical_crossentropy
import tensorflow as tf
def up_and_concate(down_layer, layer, data_format='channels_first'):
if data_format == 'channels_first':
in_channel = down_layer.get_shape().as_list()[1]
else:
in_channel = down_layer.get_shape().as_list()[3]
# up = Conv2DTranspose(out_channel, [2, 2], strides=[2, 2])(down_layer)
up = UpSampling2D(size=(2, 2), data_format=data_format)(down_layer)
if data_format == 'channels_first':
my_concat = Lambda(lambda x: K.concatenate([x[0], x[1]], axis=1))
else:
my_concat = Lambda(lambda x: K.concatenate([x[0], x[1]], axis=3))
concate = my_concat([up, layer])
return concate
def res_block(input_layer, out_n_filters, batch_normalization=False, kernel_size=[3, 3], stride=[1, 1],
padding='same', data_format='channels_first'):
if data_format == 'channels_first':
input_n_filters = input_layer.get_shape().as_list()[1]
else:
input_n_filters = input_layer.get_shape().as_list()[3]
layer = input_layer
for i in range(2):
layer = Conv2D(out_n_filters // 4, [1, 1], strides=stride, padding=padding, data_format=data_format)(layer)
if batch_normalization:
layer = BatchNormalization()(layer)
layer = Activation('relu')(layer)
layer = Conv2D(out_n_filters // 4, kernel_size, strides=stride, padding=padding, data_format=data_format)(layer)
layer = Conv2D(out_n_filters, [1, 1], strides=stride, padding=padding, data_format=data_format)(layer)
if out_n_filters != input_n_filters:
skip_layer = Conv2D(out_n_filters, [1, 1], strides=stride, padding=padding, data_format=data_format)(
input_layer)
else:
skip_layer = input_layer
out_layer = add([layer, skip_layer])
return out_layer
# Recurrent Residual Convolutional Neural Network based on U-Net (R2U-Net)
def rec_res_block(input_layer, out_n_filters, batch_normalization=False, kernel_size=[3, 3], stride=[1, 1],
padding='same', data_format='channels_first'):
if data_format == 'channels_first':
input_n_filters = input_layer.get_shape().as_list()[1]
else:
input_n_filters = input_layer.get_shape().as_list()[3]
if out_n_filters != input_n_filters:
skip_layer = Conv2D(out_n_filters, [1, 1], strides=stride, padding=padding, data_format=data_format)(
input_layer)
else:
skip_layer = input_layer
layer = skip_layer
for j in range(2):
for i in range(2):
if i == 0:
layer1 = Conv2D(out_n_filters, kernel_size, strides=stride, padding=padding, data_format=data_format)(
layer)
if batch_normalization:
layer1 = BatchNormalization()(layer1)
layer1 = Activation('relu')(layer1)
layer1 = Conv2D(out_n_filters, kernel_size, strides=stride, padding=padding, data_format=data_format)(
add([layer1, layer]))
if batch_normalization:
layer1 = BatchNormalization()(layer1)
layer1 = Activation('relu')(layer1)
layer = layer1
out_layer = add([layer, skip_layer])
return out_layer
########################################################################################################
# Define the neural network
def unet(img_w, img_h, n_label, data_format='channels_first'):
inputs = Input((img_w, img_h, 1)) # (4, 4, 484)
x = inputs
depth = 2
features = 32
skips = []
for i in range(depth):
x = Conv2D(features, (3, 3), activation='relu', padding='same', data_format=data_format)(x)
# width=((W-F+2*P )/S)+1
x = Dropout(0.2)(x)
x = Conv2D(features, (3, 3), activation='relu', padding='same', data_format=data_format)(x)
skips.append(x)
x = MaxPooling2D((2, 2), data_format=data_format)(x)
features = features * 2
x = Conv2D(features, (3, 3), activation='relu', padding='same', data_format=data_format)(x)
x = Dropout(0.2)(x)
x = Conv2D(features, (3, 3), activation='relu', padding='same', data_format=data_format)(x)
for i in reversed(range(depth)):
features = features // 2
# attention_up_and_concate(x,[skips[i])
x = UpSampling2D(size=(2, 2), data_format=data_format)(x)
x = concatenate([skips[i], x], axis=3)
x = Conv2D(features, (3, 3), activation='relu', padding='same', data_format=data_format)(x)
x = Dropout(0.2)(x)
x = Conv2D(features, (3, 3), activation='relu', padding='same', data_format=data_format)(x)
conv6 = Conv2D(n_label, (1, 1), padding='same', data_format=data_format)(x)
conv7 = Activation('sigmoid')(conv6)
model = Model(inputs=inputs, outputs=conv7)
model.compile(optimizer=Adam(lr=1e-4),
loss='mse')
# model.compile(optimizer=Adam(lr=1e-5), loss=[focal_loss()], metrics=['accuracy', dice_coef])
return model
########################################################################################################
# Define the residual U-net neural network
def r_unet(img_w, img_h, n_label, data_format='channels_first'):
inputs = Input((img_w, img_h, 1))
x = inputs
depth = 2
features = 32
skips = []
for i in range(depth):
x = res_block(x, features, data_format=data_format)
skips.append(x)
x = MaxPooling2D((2, 2), data_format=data_format)(x)
features = features * 2
x = rec_res_block(x, features, data_format=data_format)
for i in reversed(range(depth)):
features = features // 2
x = up_and_concate(x, skips[i], data_format=data_format)
x = res_block(x, features, data_format=data_format)
conv6 = Conv2D(n_label, (1, 1), padding='same', data_format=data_format)(x)
conv7 = Activation('sigmoid')(conv6)
model = Model(inputs=inputs, outputs=conv7)
model.compile(optimizer=Adam(lr=1e-4),
loss=['mse'])
# model.compile(optimizer=keras.optimizers.SGD(lr=0.001,momentum=0.8),loss=Tanimoto_loss,metrics=['accuracy'])
# model.compile(optimizer=Adam(lr=1e-6), loss=[dice_coef_loss], metrics=['accuracy', dice_coef])
return model
########################################################################################################
# Recurrent Residual Convolutional Neural Network based on U-Net (R2U-Net)
def r2_unet(img_w, img_h, n_label, data_format='channels_first'):
inputs = Input((img_w, img_h, 1))
x = inputs
depth = 2
features = 32
skips = []
for i in range(depth):
x = rec_res_block(x, features, data_format=data_format)
skips.append(x)
x = MaxPooling2D((2, 2), data_format=data_format)(x)
features = features * 2
x = rec_res_block(x, features, data_format=data_format)
for i in reversed(range(depth)):
features = features // 2
x = up_and_concate(x, skips[i], data_format=data_format)
x = rec_res_block(x, features, data_format=data_format)
conv6 = Conv2D(n_label, (1, 1), padding='same', data_format=data_format)(x)
conv7 = Activation('sigmoid')(conv6)
model = Model(inputs=inputs, outputs=conv7)
model.compile(optimizer=Adam(lr=1e-4),
loss=['mse'])
# model.compile(optimizer=keras.optimizers.SGD(lr=0.001,momentum=0.8),loss=Tanimoto_loss,metrics=['accuracy'])
# model.compile(optimizer=Adam(lr=1e-6), loss=[dice_coef_loss], metrics=['accuracy', dice_coef])
return model