-
Notifications
You must be signed in to change notification settings - Fork 13
/
decoders.py
168 lines (151 loc) · 8.51 KB
/
decoders.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
from config import *
from keras.layers import *
from keras.models import *
def get_decoder_model(input, output):
img_input = input
o = output
o_shape = Model(img_input, o).output_shape
i_shape = Model(img_input, o).input_shape
if IMAGE_ORDERING == 'channels_first':
output_height = o_shape[2]
output_width = o_shape[3]
input_height = i_shape[2]
input_width = i_shape[3]
n_classes = o_shape[1]
o = (Reshape((-1, output_height * output_width)))(o)
o = (Permute((2, 1)))(o)
elif IMAGE_ORDERING == 'channels_last':
output_height = o_shape[1]
output_width = o_shape[2]
input_height = i_shape[1]
input_width = i_shape[2]
n_classes = o_shape[3]
o = (Reshape((output_height * output_width, -1)))(o)
o = (Activation('softmax'))(o)
model = Model(img_input, o)
model.output_width = output_width
model.output_height = output_height
model.n_classes = n_classes
model.input_height = input_height
model.input_width = input_width
model.encoder_name= ""
return model
# UNet decoder
def get_unet_decoder(n_classes, encoder, input_height=224, input_width=224, depth=3, filter_size=32, encoder_name=None, up_layer=False, trainable=True):
img_input, levels = encoder(input_height=input_height, input_width=input_width, depth=depth, filter_size=filter_size, encoder_name=encoder_name)
[f1, f2, f3, f4, f5] = levels
# 512
up6 = Conv2D(filter_size*2**3, (2, 2), activation = 'relu', padding = 'same', data_format='channels_last')(UpSampling2D(size = (2,2))(f5))
merge6 = concatenate([f4,up6], axis = 3)
conv6 = Conv2D(filter_size*2**3, (3, 3), activation = 'relu', padding = 'same', data_format='channels_last')(merge6)
conv6 = Conv2D(filter_size*2**3, (3, 3), activation = 'relu', padding = 'same', data_format='channels_last')(conv6)
# 256
up7 = Conv2D(filter_size*2**2, (2, 2), activation = 'relu', padding = 'same', data_format='channels_last')(UpSampling2D(size = (2,2))(conv6))
merge7 = concatenate([f3,up7], axis = 3)
conv7 = Conv2D(filter_size*2**2, (3, 3), activation = 'relu', padding = 'same', data_format='channels_last')(merge7)
conv7 = Conv2D(filter_size*2**2, (3, 3), activation = 'relu', padding = 'same', data_format='channels_last')(conv7)
# 128
up8 = Conv2D(filter_size*2, (2, 2), activation = 'relu', padding = 'same', data_format='channels_last')(UpSampling2D(size = (2,2))(conv7))
merge8 = concatenate([f2,up8], axis = 3)
conv8 = Conv2D(filter_size*2, (3, 3), activation = 'relu', padding = 'same', data_format='channels_last')(merge8)
conv8 = Conv2D(filter_size*2, (3, 3), activation = 'relu', padding = 'same', data_format='channels_last')(conv8)
# 64
up9 = Conv2D(filter_size, (2, 2), activation = 'relu', padding = 'same', data_format='channels_last')(UpSampling2D(size = (2,2))(conv8))
merge9 = concatenate([f1,up9], axis = 3)
o = Conv2D(filter_size, (3, 3), activation = 'relu', padding = 'same', data_format='channels_last')(merge9)
o = Conv2D(filter_size, (3, 3), activation = 'relu', padding = 'same', data_format='channels_last')(o)
if(up_layer):
up10 = Conv2D(filter_size, (2, 2), activation = 'relu', padding = 'same', data_format='channels_last')(UpSampling2D(size = (2,2))(o))
merge10 = concatenate([img_input,up10], axis = 3)
o = Conv2D(int(filter_size/2), (3, 3), activation = 'relu', padding = 'same', data_format='channels_last')(merge10)
o = Conv2D(int(filter_size/2), (3, 3), activation = 'relu', padding = 'same', data_format='channels_last')(o)
o = Conv2D(n_classes, (3, 3), activation = 'relu', padding = 'same', data_format='channels_last')(o)
model = get_decoder_model(img_input, o)
if (encoder_name == 'ResNet50') or (encoder_name == 'NASNetMobile') or (encoder_name == 'NasNetLarge'):
n_pads = 2 # for one_side_pad
elif encoder_name == 'Xception':
n_pads = 5 # for 3 one_side_pad
else:
n_pads = 0 # for reshape
if not trainable and up_layer:
for layer in model.layers:
layer.trainable=False # Make the layer non-trainable
for layer in model.layers[-26-n_pads:]:
layer.trainable=True # Make only the last layers trainable
elif not trainable:
for layer in model.layers:
layer.trainable=False # Make the layer non-trainable
for layer in model.layers[-22-n_pads:]:
layer.trainable=True # Make only the last layers trainable
return model
# Modified UNet decoder
def get_unet_modified_decoder(n_classes, encoder, input_height=224, input_width=224, depth=3, filter_size=64, encoder_name=None, up_layer=False, trainable=True):
img_input, levels = encoder(input_height=input_height, input_width=input_width, depth=depth, filter_size=filter_size, encoder_name=encoder_name)
[f1, f2, f3, f4, f5] = levels
# 512
up6 = Conv2D(filter_size*2**3, (2, 2), activation = 'relu', padding = 'same', data_format='channels_last')(UpSampling2D(size = (2,2))(f5))
merge6 = concatenate([f4,up6], axis = 3)
conv6 = Conv2D(filter_size*2**3, (3, 3), padding = 'same', data_format='channels_last')(merge6)
conv6 = BatchNormalization()(conv6)
conv6 = Activation('relu')(conv6)
conv6 = Conv2D(filter_size*2**3, (3, 3), padding = 'same', data_format='channels_last')(conv6)
conv6 = BatchNormalization()(conv6)
conv6 = Activation('relu')(conv6)
# conv6 = Dropout(0.5)(conv6)
# 256
up7 = Conv2D(filter_size*2**2, (2, 2), activation = 'relu', padding = 'same', data_format='channels_last')(UpSampling2D(size = (2,2))(conv6))
merge7 = concatenate([f3,up7], axis = 3)
conv7 = Conv2D(filter_size*2**2, (3, 3), padding = 'same', data_format='channels_last')(merge7)
conv7 = BatchNormalization()(conv7)
conv7 = Activation('relu')(conv7)
conv7 = Conv2D(filter_size*2**2, (3, 3), padding = 'same', data_format='channels_last')(conv7)
conv7 = BatchNormalization()(conv7)
conv7 = Activation('relu')(conv7)
# 128
up8 = Conv2D(filter_size*2, (2, 2), activation = 'relu', padding = 'same', data_format='channels_last')(UpSampling2D(size = (2,2))(conv7))
merge8 = concatenate([f2,up8], axis = 3)
conv8 = Conv2D(filter_size*2, (3, 3), padding = 'same', data_format='channels_last')(merge8)
conv8 = BatchNormalization()(conv8)
conv8 = Activation('relu')(conv8)
conv8 = Conv2D(filter_size*2, (3, 3), padding = 'same', data_format='channels_last')(conv8)
conv8 = BatchNormalization()(conv8)
conv8 = Activation('relu')(conv8)
# 64
up9 = Conv2D(filter_size, (2, 2), activation = 'relu', padding = 'same', data_format='channels_last')(UpSampling2D(size = (2,2))(conv8))
merge9 = concatenate([f1,up9], axis = 3)
o = Conv2D(filter_size, (3, 3), padding = 'same', data_format='channels_last')(merge9)
o = BatchNormalization()(o)
o = Activation('relu')(o)
o = Conv2D(filter_size, (3, 3), padding = 'same', data_format='channels_last')(merge9)
o = BatchNormalization()(o)
o = Activation('relu')(o)
if(up_layer):
up10 = Conv2D(filter_size, (2, 2), activation = 'relu', padding = 'same', data_format='channels_last')(UpSampling2D(size = (2,2))(o))
merge10 = concatenate([img_input,up10], axis = 3)
o = Conv2D(int(filter_size/2), (3, 3), padding = 'same', data_format='channels_last')(merge10)
o = BatchNormalization()(o)
o = Activation('relu')(o)
o = Conv2D(int(filter_size/2), (3, 3), padding = 'same', data_format='channels_last')(o)
o = BatchNormalization()(o)
o = Activation('relu')(o)
o = Conv2D(n_classes, (3, 3), padding = 'same', data_format='channels_last')(o)
o = BatchNormalization()(o)
o = Activation('relu')(o)
model = get_decoder_model(img_input, o)
if (encoder_name == 'ResNet50') or (encoder_name == 'NASNetMobile') or (encoder_name == 'NasNetLarge'):
n_pads = 2 # for one_side_pad
elif encoder_name == 'Xception':
n_pads = 5 # for 3 one_side_pad
else:
n_pads = 0 # for reshape
if not trainable and up_layer:
for layer in model.layers:
layer.trainable=False # Make the layer non-trainable
for layer in model.layers[-26-n_pads:]:
layer.trainable=True # Make only the last layers trainable
elif not trainable:
for layer in model.layers:
layer.trainable=False # Make the layer non-trainable
for layer in model.layers[-22-n_pads:]:
layer.trainable=True # Make only the last layers trainable
return model