-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCN_VGG.py
267 lines (224 loc) · 11.6 KB
/
FCN_VGG.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
from keras.layers import Activation, Add, Dropout, Dense, Input
from keras.layers import Conv2D, MaxPooling2D, Conv2DTranspose, Cropping2D
from keras.models import Model
def FCN8(classes=None, target_size=None, VGG_Weights_path=None):
input_height = target_size[0]
input_width = target_size[1]
assert input_height % 32 == 0
assert input_width % 32 == 0
assert classes is not None
assert target_size is not None
assert VGG_Weights_path is not None
IMAGE_ORDERING = "channels_last"
img_input = Input(shape=(input_height, input_width, 3)) # Assume 224,224,3
# Block 1
x = Conv2D(64, (3, 3), activation='relu', padding='same',
name='block1_conv1', data_format=IMAGE_ORDERING)(img_input)
x = Conv2D(64, (3, 3), activation='relu', padding='same',
name='block1_conv2', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool',
data_format=IMAGE_ORDERING)(x)
f1 = x
# Block 2
x = Conv2D(128, (3, 3), activation='relu', padding='same',
name='block2_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same',
name='block2_conv2', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool',
data_format=IMAGE_ORDERING)(x)
f2 = x
# Block 3
x = Conv2D(256, (3, 3), activation='relu', padding='same',
name='block3_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same',
name='block3_conv2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same',
name='block3_conv3', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool',
data_format=IMAGE_ORDERING)(x)
pool3 = x
# Block 4
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block4_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block4_conv2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block4_conv3', data_format=IMAGE_ORDERING)(x)
pool4 = MaxPooling2D((2, 2), strides=(
2, 2), name='block4_pool', data_format=IMAGE_ORDERING)(x) # (None, 14, 14, 512)
# Block 5
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block5_conv1', data_format=IMAGE_ORDERING)(pool4)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block5_conv2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block5_conv3', data_format=IMAGE_ORDERING)(x)
pool5 = MaxPooling2D((2, 2), strides=(
2, 2), name='block5_pool', data_format=IMAGE_ORDERING)(x) # (None, 7, 7, 512)
vgg = Model(img_input, pool5)
# loading VGG weights for the encoder parts of FCN8
vgg.load_weights(VGG_Weights_path)
n = 4096
o = (Conv2D(n, (7, 7), activation='relu', padding='same',
name="conv6", data_format=IMAGE_ORDERING))(pool5)
conv7 = (Conv2D(n, (1, 1), activation='relu', padding='same',
name="conv7", data_format=IMAGE_ORDERING))(o)
# 4 times upsamping for pool4 layer
conv7_4 = Conv2DTranspose(classes, kernel_size=(4, 4), strides=(
4, 4), use_bias=False, data_format=IMAGE_ORDERING)(conv7)
## (None, 224, 224, 10)
# 2 times upsampling for pool411
pool411 = (Conv2D(classes, (1, 1), activation='relu', padding='same',
name="pool4_11", data_format=IMAGE_ORDERING))(pool4)
pool411_2 = (Conv2DTranspose(classes, kernel_size=(2, 2), strides=(
2, 2), use_bias=False, data_format=IMAGE_ORDERING))(pool411)
pool311 = (Conv2D(classes, (1, 1), activation='relu', padding='same',
name="pool3_11", data_format=IMAGE_ORDERING))(pool3)
o = Add(name="add")([pool411_2, pool311, conv7_4])
o = Conv2DTranspose(classes, kernel_size=(8, 8), strides=(
8, 8), use_bias=False, data_format=IMAGE_ORDERING)(o)
o = (Activation('softmax'))(o)
model = Model(img_input, o)
return model
def FCN16(classes=None, target_size=None, VGG_Weights_path=None):
input_height = target_size[0]
input_width = target_size[1]
assert input_height % 32 == 0
assert input_width % 32 == 0
assert classes is not None
assert target_size is not None
assert VGG_Weights_path is not None
IMAGE_ORDERING = "channels_last"
img_input = Input(shape=(input_height, input_width, 3)) # Assume 224,224,3
# Block 1
x = Conv2D(64, (3, 3), activation='relu', padding='same',
name='block1_conv1', data_format=IMAGE_ORDERING)(img_input)
x = Conv2D(64, (3, 3), activation='relu', padding='same',
name='block1_conv2', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool',
data_format=IMAGE_ORDERING)(x)
f1 = x
# Block 2
x = Conv2D(128, (3, 3), activation='relu', padding='same',
name='block2_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same',
name='block2_conv2', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool',
data_format=IMAGE_ORDERING)(x)
f2 = x
# Block 3
x = Conv2D(256, (3, 3), activation='relu', padding='same',
name='block3_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same',
name='block3_conv2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same',
name='block3_conv3', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool',
data_format=IMAGE_ORDERING)(x)
pool3 = x
# Block 4
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block4_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block4_conv2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block4_conv3', data_format=IMAGE_ORDERING)(x)
pool4 = MaxPooling2D((2, 2), strides=(
2, 2), name='block4_pool', data_format=IMAGE_ORDERING)(x) # (None, 14, 14, 512)
# Block 5
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block5_conv1', data_format=IMAGE_ORDERING)(pool4)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block5_conv2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block5_conv3', data_format=IMAGE_ORDERING)(x)
pool5 = MaxPooling2D((2, 2), strides=(
2, 2), name='block5_pool', data_format=IMAGE_ORDERING)(x) # (None, 7, 7, 512)
vgg = Model(img_input, pool5)
# loading VGG weights for the encoder parts of FCN8
vgg.load_weights(VGG_Weights_path)
n = 4096
o = (Conv2D(n, (7, 7), activation='relu', padding='same',
name="conv6", data_format=IMAGE_ORDERING))(pool5)
conv7 = (Conv2D(n, (1, 1), activation='relu', padding='same',
name="conv7", data_format=IMAGE_ORDERING))(o)
conv7_2 = Conv2DTranspose(classes, kernel_size=(2, 2), strides=(
2, 2), use_bias=False, data_format=IMAGE_ORDERING)(conv7)
pool411 = (Conv2D(classes, (1, 1), activation='relu', padding='same',
name="pool4_11", data_format=IMAGE_ORDERING))(pool4)
o = Add(name="add")([pool411, conv7_2])
o = Conv2DTranspose(classes, kernel_size=(16, 16), strides=(
16, 16), use_bias=False, data_format=IMAGE_ORDERING)(o)
o = (Activation('softmax'))(o)
model = Model(img_input, o)
return model
def FCN32(classes=None, target_size=None, VGG_Weights_path=None):
input_height = target_size[0]
input_width = target_size[1]
assert input_height % 32 == 0
assert input_width % 32 == 0
assert classes is not None
assert target_size is not None
assert VGG_Weights_path is not None
IMAGE_ORDERING = "channels_last"
img_input = Input(shape=(input_height, input_width, 3)) # Assume 224,224,3
# Block 1
x = Conv2D(64, (3, 3), activation='relu', padding='same',
name='block1_conv1', data_format=IMAGE_ORDERING)(img_input)
x = Conv2D(64, (3, 3), activation='relu', padding='same',
name='block1_conv2', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool',
data_format=IMAGE_ORDERING)(x)
f1 = x
# Block 2
x = Conv2D(128, (3, 3), activation='relu', padding='same',
name='block2_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same',
name='block2_conv2', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool',
data_format=IMAGE_ORDERING)(x)
f2 = x
# Block 3
x = Conv2D(256, (3, 3), activation='relu', padding='same',
name='block3_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same',
name='block3_conv2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same',
name='block3_conv3', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool',
data_format=IMAGE_ORDERING)(x)
pool3 = x
# Block 4
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block4_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block4_conv2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block4_conv3', data_format=IMAGE_ORDERING)(x)
pool4 = MaxPooling2D((2, 2), strides=(
2, 2), name='block4_pool', data_format=IMAGE_ORDERING)(x) # (None, 14, 14, 512)
# Block 5
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block5_conv1', data_format=IMAGE_ORDERING)(pool4)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block5_conv2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same',
name='block5_conv3', data_format=IMAGE_ORDERING)(x)
pool5 = MaxPooling2D((2, 2), strides=(
2, 2), name='block5_pool', data_format=IMAGE_ORDERING)(x) # (None, 7, 7, 512)
vgg = Model(img_input, pool5)
# loading VGG weights for the encoder parts of FCN8
vgg.load_weights(VGG_Weights_path)
n = 4096
o = (Conv2D(n, (7, 7), activation='relu', padding='same',
name="conv6", data_format=IMAGE_ORDERING))(pool5)
conv7 = (Conv2D(n, (1, 1), activation='relu', padding='same',
name="conv7", data_format=IMAGE_ORDERING))(o)
# classifying layer
conv7_score = (Conv2D(classes, (1, 1), activation='relu', padding='same',
name="pool3_11", data_format=IMAGE_ORDERING))(conv7)
conv7_upscore = Conv2DTranspose(classes, kernel_size=(32, 32), strides=(
32, 32), use_bias=False, data_format=IMAGE_ORDERING)(conv7_score)
o = (Activation('softmax'))(conv7_upscore)
model = Model(img_input, o)
return model