This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathunet_c.py
201 lines (165 loc) · 6.7 KB
/
unet_c.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
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# U-Net
# Trainable params: 34,658,112
# Paper: https://arxiv.org/pdf/1505.04597.pdf
import tensorflow as tf
from tensorflow.keras import Model, Input
from tensorflow.keras.layers import Conv2D, ReLU, MaxPooling2D, Concatenate, Cropping2D
from tensorflow.keras.layers import Conv2DTranspose, Dropout
from tensorflow.keras.regularizers import l2
import sys
sys.path.append('../')
from models_c import Composable
class UNet(Composable):
""" U-Net Convolutional Neural Network for Segmentation Tasks
"""
groups = [ { 'n_filters': 64, 'crop': 88 },
{ 'n_filters': 128, 'crop': 40 },
{ 'n_filters': 256, 'crop': 16 },
{ 'n_filters': 512, 'crop': 4 }
]
# Initial Hyperparameters
hyperparameters = { 'initializer': 'he_normal',
'regularizer': None,
'relu_clip' : None,
'bn_epsilon' : None,
'use_bias' : False
}
def __init__(self, groups=None,
input_shape=(572, 572, 3), n_classes=2, include_top=True,
**hyperparameters):
""" Construct a U-Net Convolutiuonal Neural Network
groups : contracting path groups
input_shape : input shape
n_classes : number of output classes
include_top : whether to include classifier
initializer : kernel initializer
regularizer : kernel regularizer
relu_clip : max value for ReLU
bn_epsilon : epsilon for batch norm
use_bias : whether to use bias with batchnorm
"""
# Configure the base (super) class
Composable.__init__(self, input_shape, include_top, self.hyperparameters, **hyperparameters)
# Predefined
if groups is None:
groups = self.groups
# The input tensor
inputs = Input(input_shape)
# The stem convolutional group
x = self.stem(inputs)
# The learner
outputs = self.learner(x, groups=groups)
# The classifier
# Add hidden dropout for training-time regularization
if include_top:
outputs = self.classifier(outputs, n_classes, dropout=0.0)
# Instantiate the Model
self._model = Model(inputs, outputs)
def stem(self, inputs):
""" Construct the Stem Convolutional Group
inputs : the input vector
"""
# no stem operation
return inputs
def learner(self, x, groups):
""" Construct the Learner
x : input to the learner
groups: contracting path groups
"""
# Contracting Path
x, e_groups = self.contracting(x, groups=groups)
# Expansive Path
x = self.expandsive(x, groups=e_groups)
return x
def contracting(self, x, **metaparameters):
""" Construct Contracting Path (leftside)
x : input tensor to contracting path
groups : contracting path groups
"""
groups = metaparameters['groups']
# Add each group on contracting path
e_groups = [] # construct expansive groups
for group in groups:
n_filters = group['n_filters']
crop = group['crop']
x, f = self.contract_group(x, n_filters, crop)
# add parameters for corresponding expansive group
e_groups.insert( 0, { 'n_filters': n_filters, 'fmap': f } )
# Output from contracting path
# Double the number of filters from the last path
x = self.Conv2D(x, n_filters*2, (3, 3), strides=1, padding='valid')
return x, e_groups
def contract_group(self, x, n_filters, crop):
""" Construct Contracting Group
x : input tensor to group
n_filters: number of filters
crop : crop size for feature maps
"""
# B(3, 3) convolutions
x = self.Conv2D(x, n_filters, (3, 3), strides=1, padding='valid')
x = self.ReLU(x)
x = self.Conv2D(x, n_filters, (3, 3), strides=1, padding='valid')
x = self.ReLU(x)
# Crop the Feature Map
f = Cropping2D(((crop, crop), (crop, crop)))(x)
# Downsampling
x = MaxPooling2D((2, 2), strides=2)(x)
return x, f
def expandsive(self, x, **metaparameters):
""" Construct Expansive Path (rightside)
x : input tensor to expansive path
groups : expansive path groups
"""
groups = metaparameters['groups']
# Input to expansive path
n_filters = groups[0]['n_filters']
x = self.Conv2D(x, n_filters, (3, 3), strides=1, padding='valid')
# Add each group on expansive path
for group in groups:
n_filters = group['n_filters']
fmap = group['fmap']
x = self.expand_group(x, fmap, n_filters)
return x
def expand_group(self, x, f, n_filters):
""" Construct Expanding Group
x : input tensor to group
f : corresponding feature maps from contracting path
n_filters: number of filters
"""
# The Up Convolution (double feature map size)
x = self.Conv2DTranspose(x, n_filters, (2, 2), strides=2)
# Concatenate corresponding feature maps from contracting side
x = Concatenate()([x, f])
# Dimensionality Expansion
x = self.Conv2D(x, n_filters*2, (3, 3), strides=1, padding='valid')
x = self.ReLU(x)
# Dimensionality Restoration
x = self.Conv2D(x, n_filters, (3, 3), strides=1, padding='valid')
x = self.ReLU(x)
return x
def classifier(self, x, n_classes, **metaparameters):
""" Construct the classifier
x : input to the classifier
n_classes: number of classes
dropout : percentage of dropout
"""
dropout = metaparameters['dropout']
if dropout > 0.0:
x = Dropout(dropout)(x)
x = self.Conv2D(x, n_classes, (1, 1), strides=1, padding='valid', activation='sigmoid')
return x
# Example U-Net
# unet = UNet()