-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
280 lines (191 loc) · 8.52 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
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
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import tensorflow as tf
CWD = os.path.dirname(__file__)
class DoubleConv(tf.keras.layers.Layer):
"""
Class that returns the double convultional layers required for each block of a UNet.
Args:
n_filters: Number of output features in the convolution
"""
def __init__(self, n_filters):
super(DoubleConv, self).__init__()
# conv layer
self.conv = tf.keras.layers.Conv2D(
n_filters,
kernel_size=(3, 3),
padding='same',
kernel_initializer='he_normal'
)
# batch norm layer
self.bn = tf.keras.layers.BatchNormalization()
def call(self, input_tensor, training=False):
"""
Returns the output of two Convolution -> Batch Normalization -> ReLU units given the input tensor.
Args:
input_tensor: Input tensor for the DoubleConv unit.
training: True if model is being trained, False otherwise.
Returns:
x: The output of the DoubleConv unit.
"""
# First DoubleConv unit, comprises of Convolution layer -> Batch Normalization layer -> ReLU activation
x = self.conv(input_tensor)
x = self.bn(x, training=training)
x = tf.nn.relu(x)
# Second DoubleConv unit
x = self.conv(input_tensor)
x = self.bn(x, training=training)
x = tf.nn.relu(x)
return x
class DownsamplingBlock(tf.keras.layers.Layer):
"""
Class that defines one Downsampling/Encoding unit of the UNet.
Args:
n_filters: Number of output filters for the DoubleConv unit of each downsampling unit.
dropout_prob: Dropout probability for the dropout layer.
"""
def __init__(self,
n_filters,
dropout_prob = 0
):
super(DownsamplingBlock, self).__init__()
# DoubleConv layer
self.double_conv = DoubleConv(n_filters)
# Dropout layer
self.Dropout = tf.keras.layers.Dropout(dropout_prob)
# MaxPooling layer
self.MaxPooling = tf.keras.layers.MaxPooling2D(pool_size = (2, 2))
def call(self, input_tensor, max_pooling = True, training = False):
"""
Returns the output for the next downsampling unit and the information to be passed to corresponding upsampling unit
through a skip connection.
Args:
input_tensor: Inpur tensor to the downsampling unit.
max_pooling: If max pooling is to be done before passing to the next downsampling unit. Not done for the last
downsampling unit.
training: True if model is being trained, False otherwise.
Returns:
next_layer: Output passed to the next downsampling unit.
skip_connection: Output passed to corresponding upsampling unit through a skip connection.
"""
# DoubleConv unit
x = self.double_conv(input_tensor)
# Dropout layer
x = self.Dropout(x, training = training)
# Max pooling layer
if max_pooling:
next_layer = self.MaxPooling(x)
else:
next_layer = x
skip_connection = x
return next_layer, skip_connection
class UpsamplingBlock(tf.keras.layers.Layer):
"""
Class that defines one Upsampling/Decoding unit of the UNet.
Args:
n_filters: Number of output filters for the DoubleConv unit of each downsampling unit.
"""
def __init__(self, n_filters = 32):
super(UpsamplingBlock, self).__init__()
# DoubleConv layer
self.double_conv = DoubleConv(n_filters)
# Transpose convolution layer
self.transpose = tf.keras.layers.Conv2DTranspose(n_filters,
kernel_size = (3, 3),
strides = 2,
padding = 'same')
# Concatenation layer
self.concatenate = tf.keras.layers.Concatenate(axis = 3)
def call(self, input_tensor, skip_input, training = False):
"""
Returns output for the next upsampling unit.
Args:
input_tensor: Inpur tensor to the unpsampling unit.
skip_input: Input from corresponding downsampling block passed through a skip connection.
training: True if model is being trained, False otherwise.
Returns:
x: Output of each unsampling unit.
"""
# Transpose Convolution.
up = self.transpose(input_tensor)
# Concatenation of transpose convolution output with skip_input before passing to DoubleConv layer.
x = self.concatenate([up, skip_input])
# DoubleConv layer.
x = self.double_conv(x, training = training)
return x
class UNet(tf.keras.Model):
"""
Class to return the UNet model, compiling the Downsampling and Upsampling blocks.
Args:
n_classes: Number of classes in encoded image to predict.
n_filters: number of output filtes for the DoubleConv units.
"""
def __init__(self, n_classes = 3, n_filters = 32):
super(UNet, self).__init__()
# Downsampling blocks
self.downsampling_block1 = DownsamplingBlock(n_filters)
self.downsampling_block2 = DownsamplingBlock(n_filters * 2)
self.downsampling_block3 = DownsamplingBlock(n_filters * 4)
self.downsampling_block4 = DownsamplingBlock(n_filters * 8, dropout_prob = 0.3)
self.downsampling_block5 = DownsamplingBlock(n_filters * 16, dropout_prob = 0.3)
# Upsampling blocks
self.upsampling_block1 = UpsamplingBlock(n_filters * 8)
self.upsampling_block2 = UpsamplingBlock(n_filters * 4)
self.upsampling_block3 = UpsamplingBlock(n_filters * 2)
self.upsampling_block4 = UpsamplingBlock(n_filters)
# conv layer
self.conv = tf.keras.layers.Conv2D(n_filters,
kernel_size = (3, 3),
padding = 'same',
kernel_initializer = 'he_normal')
# BatchNorm layer
self.bn = tf.keras.layers.BatchNormalization()
# Output conv layer
self.output_conv = tf.keras.layers.Conv2D(n_classes, 1, padding = 'same')
def call(self, input_tensor, training = False):
"""
Compiles and returns the output of the UNet model.
Args:
input_tensor: Input tensor to the UNet model.
training: True if model is being trained, False otherwise.
Returns:
outputs: Output of the UNet model. For an input image of size (128 x 128 x 3), outputs image of size
(128 x 128 x 3).
"""
# Downsampling blocks
down1 = self.downsampling_block1(input_tensor, training = training)
down2 = self.downsampling_block2(down1[0], training = training)
down3 = self.downsampling_block3(down2[0], training = training)
down4 = self.downsampling_block4(down3[0], training = training)
down5 = self.downsampling_block5(down4[0], max_pooling = False, training = training)
# Upsampling blocks
up1 = self.upsampling_block1(down5[0], down4[1], training = training)
up2 = self.upsampling_block2(up1, down3[1], training = training)
up3 = self.upsampling_block3(up2, down2[1], training = training)
up4 = self.upsampling_block4(up3, down1[1], training = training)
# conv layer
x = self.conv(up4)
# BatchNorm layer
x = self.bn(x, training = training)
# ReLU layer
x = tf.nn.relu(x)
# Ouput conv layer
outputs = self.output_conv(x)
return outputs
def build_graph(self, input_shape):
"""
Builds the model given input shape.
Args:
input_shape: Shape of input tensor.
Returns:
Model built given the input shape.
"""
x = tf.keras.layers.Input(shape = input_shape)
return tf.keras.Model(inputs = [x], outputs = self.call(x))
def test():
input_size = (128, 128, 3)
model = UNet()
model.build_graph(input_size).summary()
tf.keras.utils.plot_model(model.build_graph(input_size), to_file=os.path.join(CWD, 'model.png'), show_shapes=True)
if __name__ == "__main__":
test()