-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdensenets.py
436 lines (378 loc) · 13.5 KB
/
densenets.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
"""
Implementation of DenseNets, right now only the DenseTiramisu
"""
import logging
import tensorflow as tf
from tensorflow.keras import Model
from .utils import get_regularizer, select_final_activation
# configure logger
logger = logging.getLogger(__name__)
def conv_layer(
x, filters: int, size=3, activation="relu", regularizer=None, drop_out=None, name="conv"
):
"""
Forms the atomic layer of the tiramisu, does three operation in sequence:
batch normalization -> Relu -> 2D/3D Convolution.
Parameters
----------
x: Tensor
input feature map.
filters: int
indicating the number of filters in the output feat. map.
size : int, optional
The kernel size (the same will be used in all dimensions), by default 3
activation : str, optional
which activation should be used, by default 'relu'
regularizer : tf.keras.regularizers.Regularizer, optional
the regularizer to use (or None), by default None
drop_out : tuple, optional
if dropout should be used, by default (True, 0.2)
name: str, optional
name of the layer, by default conv
Returns
-------
Tensor
Result of applying batch norm -> Relu -> Convolution.
"""
rank = len(x.shape) - 2
if rank == 2:
conv_layer_type = tf.keras.layers.Conv2D
dropout_layer_type = tf.keras.layers.SpatialDropout2D
elif rank == 3:
conv_layer_type = tf.keras.layers.Conv3D
dropout_layer_type = tf.keras.layers.SpatialDropout3D
else:
raise NotImplementedError("Rank should be 2 or 3")
bn_layer = tf.keras.layers.BatchNormalization(name=name + "/bn")
x = bn_layer(x)
activation_layer = tf.keras.layers.Activation(activation, name=name + "/act")
x = activation_layer(x)
convolutional_layer = conv_layer_type(
filters=filters,
kernel_size=size,
strides=(1,) * rank,
padding="same",
dilation_rate=(1,) * rank,
activation=None,
use_bias=False,
kernel_regularizer=regularizer,
name=name + f"/conv{rank}d",
)
x = convolutional_layer(x)
if drop_out[0]:
dropout_layer = dropout_layer_type(rate=drop_out[1], name=name + "/dropout")
x = dropout_layer(x)
return x
def dense_block(
x,
n_layers: int,
growth_rate=16,
size=3,
activation="relu",
regularizer=None,
drop_out=None,
name="dense_block",
):
"""
Forms the dense block of the Tiramisu to calculate features at a specified growth rate.
Each conv layer in the dense block calculate self.options['growth_rate'] feature maps,
which are sequentially concatenated to build a larger final output.
Parameters
----------
x: Tensor
input to the Dense Block.
n_layers: int
the number of layers in the block
growth_rate : int, optional
the growth rate in the dense blocks, by default 16
size : int, optional
The kernel size (the same will be used in all dimensions), by default 3
activation : str, optional
which activation should be used, by default 'relu'
regularizer : tf.keras.regularizers.Regularizer, optional
the regularizer to use (or None), by default None
drop_out : tuple, optional
if dropout should be used, by default (True, 0.2)
name: str, optional
name of the layer, by default dense_block
Returns
-------
Tensor
the output of the dense block.
"""
rank = len(x.shape) - 2
layer_outputs = []
for i in range(n_layers):
conv = conv_layer(
x,
growth_rate,
name=name + f"/conv{i}",
size=size,
regularizer=regularizer,
drop_out=drop_out,
activation=activation,
)
layer_outputs.append(conv)
if i != n_layers - 1:
concat_layer = tf.keras.layers.Concatenate(
axis=rank + 1, name=name + f"/concat{i}"
)
x = concat_layer([conv, x])
final_concat_layer = tf.keras.layers.Concatenate(
axis=rank + 1, name=name + "/concat_conv"
)
x = final_concat_layer(layer_outputs)
return x
def transition_down(
x, filters: int, activation="relu", regularizer=None, drop_out=None, name="down"
):
"""
Down-samples the input feature map by half using maxpooling.
Parameters
----------
x: Tensor
input to downsample.
filters: int
number of output filters.
activation : str, optional
which activation should be used, by default 'relu'
regularizer : tf.keras.regularizers.Regularizer, optional
the regularizer to use (or None), by default None
drop_out : tuple, optional
if dropout should be used, by default (True, 0.2)
name: str, optional
name of the layer, by default down
Returns
-------
Tensor
result of downsampling.
"""
rank = len(x.shape) - 2
if rank == 2:
conv_layer_type = tf.keras.layers.Conv2D
maxpool_layer_type = tf.keras.layers.MaxPool2D
dropout_layer_type = tf.keras.layers.SpatialDropout2D
elif rank == 3:
conv_layer_type = tf.keras.layers.Conv3D
maxpool_layer_type = tf.keras.layers.MaxPool3D
dropout_layer_type = tf.keras.layers.SpatialDropout3D
else:
raise NotImplementedError("Rank should be 2 or 3")
bn_layer = tf.keras.layers.BatchNormalization(name=name + "/bn")
x = bn_layer(x)
activation_layer = tf.keras.layers.Activation(activation, name=name + "/act")
x = activation_layer(x)
convolutional_layer = conv_layer_type(
filters=filters,
kernel_size=(1,) * rank,
strides=(1,) * rank,
padding="same",
dilation_rate=(1,) * rank,
activation=None,
use_bias=False,
kernel_regularizer=regularizer,
name=name + f"/conv{rank}d",
)
x = convolutional_layer(x)
if drop_out[0]:
dropout_layer = dropout_layer_type(rate=drop_out[1], name=name + "/dropout")
x = dropout_layer(x)
pooling_layer = maxpool_layer_type(
pool_size=(2,) * rank, strides=(2,) * rank, name=name + f"/maxpool{rank}d"
)
x = pooling_layer(x)
return x
def transition_up(x, filters: int, size=3, regularizer=None, name="up"):
"""
Up-samples the input feature maps using transpose convolutions.
Parameters
----------
x: Tensor
input feature map to upsample.
filters: int
number of filters in the output.
size : int, optional
The kernel size (the same will be used in all dimensions), by default 3
regularizer : tf.keras.regularizers.Regularizer, optional
the regularizer to use (or None), by default None
name: str
name of the layer
Returns
-------
Tensor
result of up-sampling.
"""
rank = len(x.shape) - 2
if rank == 2:
conv_transpose_layer_type = tf.keras.layers.Conv2DTranspose
elif rank == 3:
conv_transpose_layer_type = tf.keras.layers.Conv3DTranspose
else:
raise NotImplementedError("Rank should be 2 or 3")
conv_transpose_layer = conv_transpose_layer_type(
filters=filters,
kernel_size=size,
strides=(2,) * rank,
padding="same",
use_bias=False,
kernel_regularizer=regularizer,
name=name + "_trans_up",
)
x = conv_transpose_layer(x)
return x
# Original names are used for better readability
def DenseTiramisu( # pylint: disable=invalid-name
input_tensor: tf.Tensor,
out_channels: int,
loss: str,
is_training=True,
kernel_dims=3,
growth_rate=16,
layers_per_block=(4, 5, 7, 10, 12),
bottleneck_layers=15,
drop_out=(True, 0.2),
regularize=(True, "L2", 0.001),
do_batch_normalization=True,
do_bias=False,
activation="relu",
model=Model,
) -> Model:
"""Initialize the 100 layers Tiramisu
see https://arxiv.org/abs/1611.09326
Parameters
----------
input_tensor : tf.Tensor
the tensor used as input, used to derive the rank and the output will have
the same shape except the input channels will be replaced by the output ones.
the spatial dimensions have to be divisible by 2**len(layers_per_block)
out_channels : int
The number of output channels to use (number of classes + background)
loss : str
the type of loss to use
is_training : bool, optional
if in training, by default True
kernel_dims : int, optional
the dimensions of the kernel (dimension is automatic), by default 3
growth_rate : int, optional
the growth rate in the dense blocks, by default 16
layers_per_block : tuple, optional
number of layers per block, by default (4, 5, 7, 10, 12) (from paper)
bottleneck_layers : int, optional
number of layers in the bottleneck, by default 15 (from paper)
drop_out : tuple, optional
if dropout should be used, by default (True, 0.2) (from paper)
regularize : tuple, optional
if there should be regularization, by default (True, 'L2', 0.001)
do_batch_normalization : bool, optional
has to be true for dense nets, by default True
do_bias : bool, optional
has to be false because batch norm, by default False
activation : str, optional
which activation should be used, by default 'relu'
model : tf.keras.Model, optional
Which model should be used for generation, can be used to use a sub-
classed model with custom functionality, by default tf.keras.Model
"""
assert is_training, "only use this for training"
if do_bias:
print("use no bias with this network, bias set to False")
do_bias = False
if not do_batch_normalization:
print("always uses batch norm, set to True")
do_batch_normalization = True
rank = len(input_tensor.shape) - 2
# TODO: parameters for pooling and dilations
n_blocks = len(layers_per_block)
con_ax = rank + 1
# define a standard config
regularizer = get_regularizer(*regularize)
std = {"regularizer": regularizer, "drop_out": drop_out, "activation": activation}
if rank == 2:
conv_layer_type = tf.keras.layers.Conv2D
elif rank == 3:
conv_layer_type = tf.keras.layers.Conv3D
else:
raise NotImplementedError("Rank should be 2 or 3")
x = input_tensor
logger.debug("Start model definition")
logger.debug("Input Shape: %s", x.get_shape())
concats = []
# encoder
first_layer = conv_layer_type(
filters=48,
kernel_size=kernel_dims,
strides=(1,) * rank,
padding="same",
dilation_rate=(1,) * rank,
kernel_regularizer=regularizer,
name=f"DT{rank}D-encoder/conv{rank}d",
)
x = first_layer(x)
logger.debug("First Convolution Out: %s", x.get_shape())
for block_nb in range(0, n_blocks):
dense = dense_block(
x,
layers_per_block[block_nb],
name=f"DT{rank}D-down_block{block_nb}",
growth_rate=growth_rate,
size=kernel_dims,
**std,
)
concat_layer = tf.keras.layers.Concatenate(
axis=con_ax, name=f"DT{rank}D-concat_output_down{block_nb-1}"
)
x = concat_layer([x, dense])
concats.append(x)
x = transition_down(
x, x.get_shape()[-1], name=f"DT{rank}D-transition_down{block_nb}", **std
)
logger.debug("Downsample Out: %s", x.get_shape())
logger.debug("m=%i", x.get_shape()[-1])
x = dense_block(x, bottleneck_layers, name=f"DT{rank}D-bottleneck", **std)
logger.debug("Bottleneck Block: %s", x.get_shape())
# decoder
for i, block_nb in enumerate(range(n_blocks - 1, -1, -1)):
logger.debug("Block %i", i)
logger.debug("Block to upsample: %s", x.get_shape())
x = transition_up(
x,
x.get_shape()[-1],
size=kernel_dims,
regularizer=regularizer,
name=f"DT{rank}D-transition_up{i}",
)
logger.debug("Upsample out: %s", x.get_shape())
concat_layer = tf.keras.layers.Concatenate(
axis=con_ax, name=f"DT{rank}D-concat_input{i}"
)
x_con = concat_layer([x, concats[len(concats) - i - 1]])
logger.debug("Skip connect: %s", concats[len(concats) - i - 1].get_shape())
logger.debug("Concat out: %s", x_con.get_shape())
x = dense_block(
x_con, layers_per_block[block_nb], name=f"DT{rank}D-up_block{i}", **std
)
logger.debug("Dense out: %s", x.get_shape())
logger.debug("m=%i", x.get_shape()[3] + x_con.get_shape()[3])
# concatenate the last dense block
concat_layer = tf.keras.layers.Concatenate(axis=con_ax, name=f"DT{rank}D-last_concat")
x = concat_layer([x, x_con])
logger.debug("Last layer in: %s", x.get_shape())
# prediction
last_layer = conv_layer_type(
filters=out_channels,
kernel_size=(1,) * rank,
padding="same",
dilation_rate=(1,) * rank,
kernel_regularizer=regularizer,
activation=None,
use_bias=False,
name=f"DT{rank}D-prediction/conv{rank}d",
)
x = last_layer(x)
last_activation_layer = tf.keras.layers.Activation(
select_final_activation(loss, out_channels), name=f"DT{rank}D-prediction/act"
)
probabilities = last_activation_layer(x)
logger.debug("Mask Prediction: %s", x.get_shape())
logger.debug("Finished model definition.")
return model(inputs=input_tensor, outputs=probabilities)