-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmodel.py
426 lines (373 loc) · 21.7 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
from keras.layers import Activation
from keras.layers import Lambda
from keras.layers import Conv2D
from keras.layers import Add
from keras.layers import MaxPooling2D
from keras.layers import AveragePooling2D
from keras.layers import ZeroPadding2D
from keras.layers import Input
from keras.layers import BatchNormalization
from keras.models import Model
import keras.backend as K
import tensorflow as tf
def build(width, height, n_classes, weights_path=None, train=False):
inp = Input(shape=(height, width, 3))
x = Lambda(lambda x: (x - 127.5)/255.0)(inp)
# (1/2)
y = Lambda(lambda x: tf.image.resize_bilinear(x, size=(int(x.shape[1])//2, int(x.shape[2])//2)), name='data_sub2')(x)
y = Conv2D(32, 3, strides=2, padding='same', activation='relu', name='conv1_1_3x3_s2')(y)
y = Conv2D(32, 3, padding='same', activation='relu', name='conv1_2_3x3')(y)
y = Conv2D(64, 3, padding='same', activation='relu', name='conv1_3_3x3')(y)
y_ = MaxPooling2D(pool_size=3, strides=2, name='pool1_3x3_s2')(y)
y = Conv2D(128, 1, name='conv2_1_1x1_proj')(y_)
y_ = Conv2D(32, 1, activation='relu', name='conv2_1_1x1_reduce')(y_)
y_ = ZeroPadding2D(name='padding1')(y_)
y_ = Conv2D(32, 3, activation='relu', name='conv2_1_3x3')(y_)
y_ = Conv2D(128, 1, name='conv2_1_1x1_increase')(y_)
y = Add(name='conv2_1')([y,y_])
y_ = Activation('relu', name='conv2_1/relu')(y)
y = Conv2D(32, 1, activation='relu', name='conv2_2_1x1_reduce')(y_)
y = ZeroPadding2D(name='padding2')(y)
y = Conv2D(32, 3, activation='relu', name='conv2_2_3x3')(y)
y = Conv2D(128, 1, name='conv2_2_1x1_increase')(y)
y = Add(name='conv2_2')([y,y_])
y_ = Activation('relu', name='conv2_2/relu')(y)
y = Conv2D(32, 1, activation='relu', name='conv2_3_1x1_reduce')(y_)
y = ZeroPadding2D(name='padding3')(y)
y = Conv2D(32, 3, activation='relu', name='conv2_3_3x3')(y)
y = Conv2D(128, 1, name='conv2_3_1x1_increase')(y)
y = Add(name='conv2_3')([y,y_])
y_ = Activation('relu', name='conv2_3/relu')(y)
y = Conv2D(256, 1, strides=2, name='conv3_1_1x1_proj')(y_)
y_ = Conv2D(64, 1, strides=2, activation='relu', name='conv3_1_1x1_reduce')(y_)
y_ = ZeroPadding2D(name='padding4')(y_)
y_ = Conv2D(64, 3, activation='relu', name='conv3_1_3x3')(y_)
y_ = Conv2D(256, 1, name='conv3_1_1x1_increase')(y_)
y = Add(name='conv3_1')([y,y_])
z = Activation('relu', name='conv3_1/relu')(y)
# (1/4)
y_ = Lambda(lambda x: tf.image.resize_bilinear(x, size=(int(x.shape[1])//2, int(x.shape[2])//2)), name='conv3_1_sub4')(z)
y = Conv2D(64, 1, activation='relu', name='conv3_2_1x1_reduce')(y_)
y = ZeroPadding2D(name='padding5')(y)
y = Conv2D(64, 3, activation='relu', name='conv3_2_3x3')(y)
y = Conv2D(256, 1, name='conv3_2_1x1_increase')(y)
y = Add(name='conv3_2')([y,y_])
y_ = Activation('relu', name='conv3_2/relu')(y)
y = Conv2D(64, 1, activation='relu', name='conv3_3_1x1_reduce')(y_)
y = ZeroPadding2D(name='padding6')(y)
y = Conv2D(64, 3, activation='relu', name='conv3_3_3x3')(y)
y = Conv2D(256, 1, name='conv3_3_1x1_increase')(y)
y = Add(name='conv3_3')([y,y_])
y_ = Activation('relu', name='conv3_3/relu')(y)
y = Conv2D(64, 1, activation='relu', name='conv3_4_1x1_reduce')(y_)
y = ZeroPadding2D(name='padding7')(y)
y = Conv2D(64, 3, activation='relu', name='conv3_4_3x3')(y)
y = Conv2D(256, 1, name='conv3_4_1x1_increase')(y)
y = Add(name='conv3_4')([y,y_])
y_ = Activation('relu', name='conv3_4/relu')(y)
y = Conv2D(512, 1, name='conv4_1_1x1_proj')(y_)
y_ = Conv2D(128, 1, activation='relu', name='conv4_1_1x1_reduce')(y_)
y_ = ZeroPadding2D(padding=2, name='padding8')(y_)
y_ = Conv2D(128, 3, dilation_rate=2, activation='relu', name='conv4_1_3x3')(y_)
y_ = Conv2D(512, 1, name='conv4_1_1x1_increase')(y_)
y = Add(name='conv4_1')([y,y_])
y_ = Activation('relu', name='conv4_1/relu')(y)
y = Conv2D(128, 1, activation='relu', name='conv4_2_1x1_reduce')(y_)
y = ZeroPadding2D(padding=2, name='padding9')(y)
y = Conv2D(128, 3, dilation_rate=2, activation='relu', name='conv4_2_3x3')(y)
y = Conv2D(512, 1, name='conv4_2_1x1_increase')(y)
y = Add(name='conv4_2')([y,y_])
y_ = Activation('relu', name='conv4_2/relu')(y)
y = Conv2D(128, 1, activation='relu', name='conv4_3_1x1_reduce')(y_)
y = ZeroPadding2D(padding=2, name='padding10')(y)
y = Conv2D(128, 3, dilation_rate=2, activation='relu', name='conv4_3_3x3')(y)
y = Conv2D(512, 1, name='conv4_3_1x1_increase')(y)
y = Add(name='conv4_3')([y,y_])
y_ = Activation('relu', name='conv4_3/relu')(y)
y = Conv2D(128, 1, activation='relu', name='conv4_4_1x1_reduce')(y_)
y = ZeroPadding2D(padding=2, name='padding11')(y)
y = Conv2D(128, 3, dilation_rate=2, activation='relu', name='conv4_4_3x3')(y)
y = Conv2D(512, 1, name='conv4_4_1x1_increase')(y)
y = Add(name='conv4_4')([y,y_])
y_ = Activation('relu', name='conv4_4/relu')(y)
y = Conv2D(128, 1, activation='relu', name='conv4_5_1x1_reduce')(y_)
y = ZeroPadding2D(padding=2, name='padding12')(y)
y = Conv2D(128, 3, dilation_rate=2, activation='relu', name='conv4_5_3x3')(y)
y = Conv2D(512, 1, name='conv4_5_1x1_increase')(y)
y = Add(name='conv4_5')([y,y_])
y_ = Activation('relu', name='conv4_5/relu')(y)
y = Conv2D(128, 1, activation='relu', name='conv4_6_1x1_reduce')(y_)
y = ZeroPadding2D(padding=2, name='padding13')(y)
y = Conv2D(128, 3, dilation_rate=2, activation='relu', name='conv4_6_3x3')(y)
y = Conv2D(512, 1, name='conv4_6_1x1_increase')(y)
y = Add(name='conv4_6')([y,y_])
y = Activation('relu', name='conv4_6/relu')(y)
y_ = Conv2D(1024, 1, name='conv5_1_1x1_proj')(y)
y = Conv2D(256, 1, activation='relu', name='conv5_1_1x1_reduce')(y)
y = ZeroPadding2D(padding=4, name='padding14')(y)
y = Conv2D(256, 3, dilation_rate=4, activation='relu', name='conv5_1_3x3')(y)
y = Conv2D(1024, 1, name='conv5_1_1x1_increase')(y)
y = Add(name='conv5_1')([y,y_])
y_ = Activation('relu', name='conv5_1/relu')(y)
y = Conv2D(256, 1, activation='relu', name='conv5_2_1x1_reduce')(y_)
y = ZeroPadding2D(padding=4, name='padding15')(y)
y = Conv2D(256, 3, dilation_rate=4, activation='relu', name='conv5_2_3x3')(y)
y = Conv2D(1024, 1, name='conv5_2_1x1_increase')(y)
y = Add(name='conv5_2')([y,y_])
y_ = Activation('relu', name='conv5_2/relu')(y)
y = Conv2D(256, 1, activation='relu', name='conv5_3_1x1_reduce')(y_)
y = ZeroPadding2D(padding=4, name='padding16')(y)
y = Conv2D(256, 3, dilation_rate=4, activation='relu', name='conv5_3_3x3')(y)
y = Conv2D(1024, 1, name='conv5_3_1x1_increase')(y)
y = Add(name='conv5_3')([y,y_])
y = Activation('relu', name='conv5_3/relu')(y)
h, w = y.shape[1:3].as_list()
pool1 = AveragePooling2D(pool_size=(h,w), strides=(h,w), name='conv5_3_pool1')(y)
pool1 = Lambda(lambda x: tf.image.resize_bilinear(x, size=(h,w)), name='conv5_3_pool1_interp')(pool1)
pool2 = AveragePooling2D(pool_size=(h/2,w/2), strides=(h//2,w//2), name='conv5_3_pool2')(y)
pool2 = Lambda(lambda x: tf.image.resize_bilinear(x, size=(h,w)), name='conv5_3_pool2_interp')(pool2)
pool3 = AveragePooling2D(pool_size=(h/3,w/3), strides=(h//3,w//3), name='conv5_3_pool3')(y)
pool3 = Lambda(lambda x: tf.image.resize_bilinear(x, size=(h,w)), name='conv5_3_pool3_interp')(pool3)
pool6 = AveragePooling2D(pool_size=(h/4,w/4), strides=(h//4,w//4), name='conv5_3_pool6')(y)
pool6 = Lambda(lambda x: tf.image.resize_bilinear(x, size=(h,w)), name='conv5_3_pool6_interp')(pool6)
y = Add(name='conv5_3_sum')([y, pool1, pool2, pool3, pool6])
y = Conv2D(256, 1, activation='relu', name='conv5_4_k1')(y)
aux_1 = Lambda(lambda x: tf.image.resize_bilinear(x, size=(int(x.shape[1])*2, int(x.shape[2])*2)), name='conv5_4_interp')(y)
y = ZeroPadding2D(padding=2, name='padding17')(aux_1)
y = Conv2D(128, 3, dilation_rate=2, name='conv_sub4')(y)
y_ = Conv2D(128, 1, name='conv3_1_sub2_proj')(z)
y = Add(name='sub24_sum')([y,y_])
y = Activation('relu', name='sub24_sum/relu')(y)
aux_2 = Lambda(lambda x: tf.image.resize_bilinear(x, size=(int(x.shape[1])*2, int(x.shape[2])*2)), name='sub24_sum_interp')(y)
y = ZeroPadding2D(padding=2, name='padding18')(aux_2)
y_ = Conv2D(128, 3, dilation_rate=2, name='conv_sub2')(y)
# (1)
y = Conv2D(32, 3, strides=2, padding='same', activation='relu', name='conv1_sub1')(x)
y = Conv2D(32, 3, strides=2, padding='same', activation='relu', name='conv2_sub1')(y)
y = Conv2D(64, 3, strides=2, padding='same', activation='relu', name='conv3_sub1')(y)
y = Conv2D(128, 1, name='conv3_sub1_proj')(y)
y = Add(name='sub12_sum')([y,y_])
y = Activation('relu', name='sub12_sum/relu')(y)
y = Lambda(lambda x: tf.image.resize_bilinear(x, size=(int(x.shape[1])*2, int(x.shape[2])*2)), name='sub12_sum_interp')(y)
out = Conv2D(n_classes, 1, activation='softmax', name='conv6_cls')(y)
if train:
aux_1 = Conv2D(n_classes, 1, activation='softmax', name='sub4_out')(aux_1)
aux_2 = Conv2D(n_classes, 1, activation='softmax', name='sub24_out')(aux_2)
model = Model(inputs=inp, outputs=[out, aux_2, aux_1])
else:
model = Model(inputs=inp, outputs=out)
if weights_path is not None:
model.load_weights(weights_path, by_name=True)
return model
def build_bn(width, height, n_classes, weights_path=None, train=False):
inp = Input(shape=(height, width, 3))
x = Lambda(lambda x: (x - 127.5)/255.0)(inp)
# (1/2)
y = Lambda(lambda x: tf.image.resize_bilinear(x, size=(int(x.shape[1])//2, int(x.shape[2])//2)), name='data_sub2')(x)
y = Conv2D(32, 3, strides=2, padding='same', activation='relu', name='conv1_1_3x3_s2')(y)
y = BatchNormalization(name='conv1_1_3x3_s2_bn')(y)
y = Conv2D(32, 3, padding='same', activation='relu', name='conv1_2_3x3')(y)
y = BatchNormalization(name='conv1_2_3x3_s2_bn')(y)
y = Conv2D(64, 3, padding='same', activation='relu', name='conv1_3_3x3')(y)
y = BatchNormalization(name='conv1_3_3x3_bn')(y)
y_ = MaxPooling2D(pool_size=3, strides=2, name='pool1_3x3_s2')(y)
y = Conv2D(128, 1, name='conv2_1_1x1_proj')(y_)
y = BatchNormalization(name='conv2_1_1x1_proj_bn')(y)
y_ = Conv2D(32, 1, activation='relu', name='conv2_1_1x1_reduce')(y_)
y_ = BatchNormalization(name='conv2_1_1x1_reduce_bn')(y_)
y_ = ZeroPadding2D(name='padding1')(y_)
y_ = Conv2D(32, 3, activation='relu', name='conv2_1_3x3')(y_)
y_ = BatchNormalization(name='conv2_1_3x3_bn')(y_)
y_ = Conv2D(128, 1, name='conv2_1_1x1_increase')(y_)
y_ = BatchNormalization(name='conv2_1_1x1_increase_bn')(y_)
y = Add(name='conv2_1')([y,y_])
y_ = Activation('relu', name='conv2_1/relu')(y)
y = Conv2D(32, 1, activation='relu', name='conv2_2_1x1_reduce')(y_)
y = BatchNormalization(name='conv2_2_1x1_reduce_bn')(y)
y = ZeroPadding2D(name='padding2')(y)
y = Conv2D(32, 3, activation='relu', name='conv2_2_3x3')(y)
y = BatchNormalization(name='conv2_2_3x3_bn')(y)
y = Conv2D(128, 1, name='conv2_2_1x1_increase')(y)
y = BatchNormalization(name='conv2_2_1x1_increase_bn')(y)
y = Add(name='conv2_2')([y,y_])
y_ = Activation('relu', name='conv2_2/relu')(y)
y = Conv2D(32, 1, activation='relu', name='conv2_3_1x1_reduce')(y_)
y = BatchNormalization(name='conv2_3_1x1_reduce_bn')(y)
y = ZeroPadding2D(name='padding3')(y)
y = Conv2D(32, 3, activation='relu', name='conv2_3_3x3')(y)
y = BatchNormalization(name='conv2_3_3x3_bn')(y)
y = Conv2D(128, 1, name='conv2_3_1x1_increase')(y)
y = BatchNormalization(name='conv2_3_1x1_increase_bn')(y)
y = Add(name='conv2_3')([y,y_])
y_ = Activation('relu', name='conv2_3/relu')(y)
y = Conv2D(256, 1, strides=2, name='conv3_1_1x1_proj')(y_)
y = BatchNormalization(name='conv3_1_1x1_proj_bn')(y)
y_ = Conv2D(64, 1, strides=2, activation='relu', name='conv3_1_1x1_reduce')(y_)
y_ = BatchNormalization(name='conv3_1_1x1_reduce_bn')(y_)
y_ = ZeroPadding2D(name='padding4')(y_)
y_ = Conv2D(64, 3, activation='relu', name='conv3_1_3x3')(y_)
y_ = BatchNormalization(name='conv3_1_3x3_bn')(y_)
y_ = Conv2D(256, 1, name='conv3_1_1x1_increase')(y_)
y_ = BatchNormalization(name='conv3_1_1x1_increase_bn')(y_)
y = Add(name='conv3_1')([y,y_])
z = Activation('relu', name='conv3_1/relu')(y)
# (1/4)
y_ = Lambda(lambda x: tf.image.resize_bilinear(x, size=(int(x.shape[1])//2, int(x.shape[2])//2)), name='conv3_1_sub4')(z)
y = Conv2D(64, 1, activation='relu', name='conv3_2_1x1_reduce')(y_)
y = BatchNormalization(name='conv3_2_1x1_reduce_bn')(y)
y = ZeroPadding2D(name='padding5')(y)
y = Conv2D(64, 3, activation='relu', name='conv3_2_3x3')(y)
y = BatchNormalization(name='conv3_2_3x3_bn')(y)
y = Conv2D(256, 1, name='conv3_2_1x1_increase')(y)
y = BatchNormalization(name='conv3_2_1x1_increase_bn')(y)
y = Add(name='conv3_2')([y,y_])
y_ = Activation('relu', name='conv3_2/relu')(y)
y = Conv2D(64, 1, activation='relu', name='conv3_3_1x1_reduce')(y_)
y = BatchNormalization(name='conv3_3_1x1_reduce_bn')(y)
y = ZeroPadding2D(name='padding6')(y)
y = Conv2D(64, 3, activation='relu', name='conv3_3_3x3')(y)
y = BatchNormalization(name='conv3_3_3x3_bn')(y)
y = Conv2D(256, 1, name='conv3_3_1x1_increase')(y)
y = BatchNormalization(name='conv3_3_1x1_increase_bn')(y)
y = Add(name='conv3_3')([y,y_])
y_ = Activation('relu', name='conv3_3/relu')(y)
y = Conv2D(64, 1, activation='relu', name='conv3_4_1x1_reduce')(y_)
y = BatchNormalization(name='conv3_4_1x1_reduce_bn')(y)
y = ZeroPadding2D(name='padding7')(y)
y = Conv2D(64, 3, activation='relu', name='conv3_4_3x3')(y)
y = BatchNormalization(name='conv3_4_3x3_bn')(y)
y = Conv2D(256, 1, name='conv3_4_1x1_increase')(y)
y = BatchNormalization(name='conv3_4_1x1_increase_bn')(y)
y = Add(name='conv3_4')([y,y_])
y_ = Activation('relu', name='conv3_4/relu')(y)
y = Conv2D(512, 1, name='conv4_1_1x1_proj')(y_)
y = BatchNormalization(name='conv4_1_1x1_proj_bn')(y)
y_ = Conv2D(128, 1, activation='relu', name='conv4_1_1x1_reduce')(y_)
y_ = BatchNormalization(name='conv4_1_1x1_reduce_bn')(y_)
y_ = ZeroPadding2D(padding=2, name='padding8')(y_)
y_ = Conv2D(128, 3, dilation_rate=2, activation='relu', name='conv4_1_3x3')(y_)
y_ = BatchNormalization(name='conv4_1_3x3_bn')(y_)
y_ = Conv2D(512, 1, name='conv4_1_1x1_increase')(y_)
y_ = BatchNormalization(name='conv4_1_1x1_increase_bn')(y_)
y = Add(name='conv4_1')([y,y_])
y_ = Activation('relu', name='conv4_1/relu')(y)
y = Conv2D(128, 1, activation='relu', name='conv4_2_1x1_reduce')(y_)
y = BatchNormalization(name='conv4_2_1x1_reduce_bn')(y)
y = ZeroPadding2D(padding=2, name='padding9')(y)
y = Conv2D(128, 3, dilation_rate=2, activation='relu', name='conv4_2_3x3')(y)
y = BatchNormalization(name='conv4_2_3x3_bn')(y)
y = Conv2D(512, 1, name='conv4_2_1x1_increase')(y)
y = BatchNormalization(name='conv4_2_1x1_increase_bn')(y)
y = Add(name='conv4_2')([y,y_])
y_ = Activation('relu', name='conv4_2/relu')(y)
y = Conv2D(128, 1, activation='relu', name='conv4_3_1x1_reduce')(y_)
y = BatchNormalization(name='conv4_3_1x1_reduce_bn')(y)
y = ZeroPadding2D(padding=2, name='padding10')(y)
y = Conv2D(128, 3, dilation_rate=2, activation='relu', name='conv4_3_3x3')(y)
y = BatchNormalization(name='conv4_3_3x3_bn')(y)
y = Conv2D(512, 1, name='conv4_3_1x1_increase')(y)
y = BatchNormalization(name='conv4_3_1x1_increase_bn')(y)
y = Add(name='conv4_3')([y,y_])
y_ = Activation('relu', name='conv4_3/relu')(y)
y = Conv2D(128, 1, activation='relu', name='conv4_4_1x1_reduce')(y_)
y = BatchNormalization(name='conv4_4_1x1_reduce_bn')(y)
y = ZeroPadding2D(padding=2, name='padding11')(y)
y = Conv2D(128, 3, dilation_rate=2, activation='relu', name='conv4_4_3x3')(y)
y = BatchNormalization(name='conv4_4_3x3_bn')(y)
y = Conv2D(512, 1, name='conv4_4_1x1_increase')(y)
y = BatchNormalization(name='conv4_4_1x1_increase_bn')(y)
y = Add(name='conv4_4')([y,y_])
y_ = Activation('relu', name='conv4_4/relu')(y)
y = Conv2D(128, 1, activation='relu', name='conv4_5_1x1_reduce')(y_)
y = BatchNormalization(name='conv4_5_1x1_reduce_bn')(y)
y = ZeroPadding2D(padding=2, name='padding12')(y)
y = Conv2D(128, 3, dilation_rate=2, activation='relu', name='conv4_5_3x3')(y)
y = BatchNormalization(name='conv4_5_3x3_bn')(y)
y = Conv2D(512, 1, name='conv4_5_1x1_increase')(y)
y = BatchNormalization(name='conv4_5_1x1_increase_bn')(y)
y = Add(name='conv4_5')([y,y_])
y_ = Activation('relu', name='conv4_5/relu')(y)
y = Conv2D(128, 1, activation='relu', name='conv4_6_1x1_reduce')(y_)
y = BatchNormalization(name='conv4_6_1x1_reduce_bn')(y)
y = ZeroPadding2D(padding=2, name='padding13')(y)
y = Conv2D(128, 3, dilation_rate=2, activation='relu', name='conv4_6_3x3')(y)
y = BatchNormalization(name='conv4_6_3x3_bn')(y)
y = Conv2D(512, 1, name='conv4_6_1x1_increase')(y)
y = BatchNormalization(name='conv4_6_1x1_increase_bn')(y)
y = Add(name='conv4_6')([y,y_])
y = Activation('relu', name='conv4_6/relu')(y)
y_ = Conv2D(1024, 1, name='conv5_1_1x1_proj')(y)
y_ = BatchNormalization(name='conv5_1_1x1_proj_bn')(y_)
y = Conv2D(256, 1, activation='relu', name='conv5_1_1x1_reduce')(y)
y = BatchNormalization(name='conv5_1_1x1_reduce_bn')(y)
y = ZeroPadding2D(padding=4, name='padding14')(y)
y = Conv2D(256, 3, dilation_rate=4, activation='relu', name='conv5_1_3x3')(y)
y = BatchNormalization(name='conv5_1_3x3_bn')(y)
y = Conv2D(1024, 1, name='conv5_1_1x1_increase')(y)
y = BatchNormalization(name='conv5_1_1x1_increase_bn')(y)
y = Add(name='conv5_1')([y,y_])
y_ = Activation('relu', name='conv5_1/relu')(y)
y = Conv2D(256, 1, activation='relu', name='conv5_2_1x1_reduce')(y_)
y = BatchNormalization(name='conv5_2_1x1_reduce_bn')(y)
y = ZeroPadding2D(padding=4, name='padding15')(y)
y = Conv2D(256, 3, dilation_rate=4, activation='relu', name='conv5_2_3x3')(y)
y = BatchNormalization(name='conv5_2_3x3_bn')(y)
y = Conv2D(1024, 1, name='conv5_2_1x1_increase')(y)
y = BatchNormalization(name='conv5_2_1x1_increase_bn')(y)
y = Add(name='conv5_2')([y,y_])
y_ = Activation('relu', name='conv5_2/relu')(y)
y = Conv2D(256, 1, activation='relu', name='conv5_3_1x1_reduce')(y_)
y = BatchNormalization(name='conv5_3_1x1_reduce_bn')(y)
y = ZeroPadding2D(padding=4, name='padding16')(y)
y = Conv2D(256, 3, dilation_rate=4, activation='relu', name='conv5_3_3x3')(y)
y = BatchNormalization(name='conv5_3_3x3_bn')(y)
y = Conv2D(1024, 1, name='conv5_3_1x1_increase')(y)
y = BatchNormalization(name='conv5_3_1x1_increase_bn')(y)
y = Add(name='conv5_3')([y,y_])
y = Activation('relu', name='conv5_3/relu')(y)
h, w = y.shape[1:3].as_list()
pool1 = AveragePooling2D(pool_size=(h,w), strides=(h,w), name='conv5_3_pool1')(y)
pool1 = Lambda(lambda x: tf.image.resize_bilinear(x, size=(h,w)), name='conv5_3_pool1_interp')(pool1)
pool2 = AveragePooling2D(pool_size=(h/2,w/2), strides=(h//2,w//2), name='conv5_3_pool2')(y)
pool2 = Lambda(lambda x: tf.image.resize_bilinear(x, size=(h,w)), name='conv5_3_pool2_interp')(pool2)
pool3 = AveragePooling2D(pool_size=(h/3,w/3), strides=(h//3,w//3), name='conv5_3_pool3')(y)
pool3 = Lambda(lambda x: tf.image.resize_bilinear(x, size=(h,w)), name='conv5_3_pool3_interp')(pool3)
pool6 = AveragePooling2D(pool_size=(h/4,w/4), strides=(h//4,w//4), name='conv5_3_pool6')(y)
pool6 = Lambda(lambda x: tf.image.resize_bilinear(x, size=(h,w)), name='conv5_3_pool6_interp')(pool6)
y = Add(name='conv5_3_sum')([y, pool1, pool2, pool3, pool6])
y = Conv2D(256, 1, activation='relu', name='conv5_4_k1')(y)
y = BatchNormalization(name='conv5_4_k1_bn')(y)
aux_1 = Lambda(lambda x: tf.image.resize_bilinear(x, size=(int(x.shape[1])*2, int(x.shape[2])*2)), name='conv5_4_interp')(y)
y = ZeroPadding2D(padding=2, name='padding17')(aux_1)
y = Conv2D(128, 3, dilation_rate=2, name='conv_sub4')(y)
y = BatchNormalization(name='conv_sub4_bn')(y)
y_ = Conv2D(128, 1, name='conv3_1_sub2_proj')(z)
y_ = BatchNormalization(name='conv3_1_sub2_proj_bn')(y_)
y = Add(name='sub24_sum')([y,y_])
y = Activation('relu', name='sub24_sum/relu')(y)
aux_2 = Lambda(lambda x: tf.image.resize_bilinear(x, size=(int(x.shape[1])*2, int(x.shape[2])*2)), name='sub24_sum_interp')(y)
y = ZeroPadding2D(padding=2, name='padding18')(aux_2)
y_ = Conv2D(128, 3, dilation_rate=2, name='conv_sub2')(y)
y_ = BatchNormalization(name='conv_sub2_bn')(y_)
# (1)
y = Conv2D(32, 3, strides=2, padding='same', activation='relu', name='conv1_sub1')(x)
y = BatchNormalization(name='conv1_sub1_bn')(y)
y = Conv2D(32, 3, strides=2, padding='same', activation='relu', name='conv2_sub1')(y)
y = BatchNormalization(name='conv2_sub1_bn')(y)
y = Conv2D(64, 3, strides=2, padding='same', activation='relu', name='conv3_sub1')(y)
y = BatchNormalization(name='conv3_sub1_bn')(y)
y = Conv2D(128, 1, name='conv3_sub1_proj')(y)
y = BatchNormalization(name='conv3_sub1_proj_bn')(y)
y = Add(name='sub12_sum')([y,y_])
y = Activation('relu', name='sub12_sum/relu')(y)
y = Lambda(lambda x: tf.image.resize_bilinear(x, size=(int(x.shape[1])*2, int(x.shape[2])*2)), name='sub12_sum_interp')(y)
out = Conv2D(n_classes, 1, activation='softmax', name='conv6_cls')(y)
if train:
aux_1 = Conv2D(n_classes, 1, activation='softmax', name='sub4_out')(aux_1)
aux_2 = Conv2D(n_classes, 1, activation='softmax', name='sub24_out')(aux_2)
model = Model(inputs=inp, outputs=[out, aux_2, aux_1])
else:
model = Model(inputs=inp, outputs=out)
if weights_path is not None:
model.load_weights(weights_path, by_name=True)
return model