-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdense_unet.py
132 lines (121 loc) · 6.85 KB
/
dense_unet.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
import os
from keras.models import Model # Functional API
from keras.layers import (
Conv2D, Input, MaxPooling2D,
Conv2DTranspose, Dropout, Lambda,
concatenate, BatchNormalization, Activation,)
from keras.optimizers import Adam
#from tensorflow.keras import backend as K
from src.metrics import (dice, jaccard)
from src.engine import scale_input
class Dense_UNet:
def __init__(self,
pre_trained=False, # if True, set weights_path
weights_path=None, # full-path to the pre-trained models_weights
n_classes=None,
input_h=None,
input_w=None,
activation='elu',
kernel_init='he_normal',
model_name=None
):
self.pre_trained = pre_trained
self.weights_path = weights_path
self.n_classes = n_classes
self.input_h = input_h
self.input_w = input_w
self.activation = activation
self.kernel_init = kernel_init
self.model_name = model_name
self.dense_block_size = 15 # arbitrary size
self.dense_filter_size = 32
def dense_block(self, layer, n_filters):
layer = BatchNormalization()(layer)
layer = Activation(self.activation)(layer)
layer = Conv2D(n_filters, kernel_size=(3, 3), kernel_initializer=self.kernel_init, padding='same')(layer)
layer = Dropout(0.2)(layer)
return layer
def build(self):
# ======================================== INPUT ==========================================
inBlock = Input(shape=(self.input_h, self.input_w, 3), dtype='float32')
# Lambda layer: scale input before feeding the network
inScaled = Lambda(lambda x: scale_input(x))(inBlock)
# ======================================== ENCODER ========================================
# Block 1d
convB1d = Conv2D(64, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(inScaled)
convB1d = BatchNormalization()(convB1d)
convB1d = Conv2D(64, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(convB1d)
convB1d = BatchNormalization()(convB1d)
poolB1d = MaxPooling2D(pool_size=(2, 2))(convB1d)
# Block 2d
convB2d = Conv2D(128, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(poolB1d)
convB2d = BatchNormalization()(convB2d)
convB2d = Conv2D(128, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(convB2d)
convB2d = BatchNormalization()(convB2d)
poolB2d = MaxPooling2D(pool_size=(2, 2))(convB2d)
# Block 3d
convB3d = Conv2D(256, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(poolB2d)
convB3d = BatchNormalization()(convB3d)
convB3d = Conv2D(256, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(convB3d)
convB3d = BatchNormalization()(convB3d)
poolB3d = MaxPooling2D(pool_size=(2, 2))(convB3d)
# Block 4d
convB4d = Conv2D(512, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(poolB3d)
convB4d = BatchNormalization()(convB4d)
convB4d = Conv2D(512, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(convB4d)
convB4d = BatchNormalization()(convB4d)
poolB4d = MaxPooling2D(pool_size=(2, 2))(convB4d)
# ===================================== BOTTLENECK ======================================
# Implementation of Dense-Block
stackBtN = poolB4d
for i in range(self.dense_block_size):
# DB: Dense-Block
l = self.dense_block(stackBtN, self.dense_filter_size)
stackBtN = concatenate([stackBtN, l])
# ====================================== DECODER =======================================
# Block 4u
convB4u = Conv2DTranspose(512, (2, 2), strides=(2, 2), padding='same')(stackBtN)
convB4u = concatenate([convB4u, convB4d])
convB4u = Conv2D(512, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(convB4u)
convB4u = BatchNormalization()(convB4u)
convB4u = Conv2D(256, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(convB4u)
convB4u = BatchNormalization()(convB4u)
# Block 3u
convB3u = Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(convB4u)
convB3u = concatenate([convB3u, convB3d])
convB3u = Conv2D(256, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(convB3u)
convB3u = BatchNormalization()(convB3u)
convB3u = Conv2D(128, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(convB3u)
convB3u = BatchNormalization()(convB3u)
# Block B2u
convB2u = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(convB3u)
convB2u = concatenate([convB2u, convB2d])
convB2u = Conv2D(128, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(convB2u)
convB2u = BatchNormalization()(convB2u)
convB2u = Conv2D(64, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(convB2u)
convB2u = BatchNormalization()(convB2u)
# Block B1u
convB1u = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(convB2u)
convB1u = concatenate([convB1u, convB1d], axis=3)
convB1u = Conv2D(64, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(convB1u)
convB1u = BatchNormalization()(convB1u)
convB1u = Conv2D(64, (3, 3), activation=self.activation, kernel_initializer=self.kernel_init, padding='same')(convB1u)
convB1u = BatchNormalization()(convB1u)
# ======================================== OUTPUT ==========================================
if self.n_classes == 2:
outBlock = Conv2D(1, (1, 1), activation='sigmoid', padding='same')(convB1u)
else:
outBlock = Conv2D(self.n_classes, (1, 1), activation='softmax', padding='same')(convB1u)
# Create model
model = Model(inputs=inBlock, outputs=outBlock, name=self.model_name)
model.compile(optimizer=Adam(),
loss="categorical_crossentropy",
metrics=[dice, jaccard, ]
)
# Load models_weights if pre-trained
if self.pre_trained:
if os.path.exists(self.weights_path):
model.load_weights(self.weights_path)
else:
raise Exception(f'Failed to load weights at {self.weights_path}')
return model