-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathicnet.py
244 lines (217 loc) · 12.9 KB
/
icnet.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
import tensorflow as tf
from tensorflow.keras.layers import Lambda, Conv2D, Add, AveragePooling2D, ZeroPadding2D
from tensorflow.keras.layers import BatchNormalization, MaxPooling2D, Activation, Input
from tensorflow.keras.models import Model
import tensorflow.keras.backend as K
def ICNET(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), align_corners=True), 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), align_corners=True), 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), align_corners=True), 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), align_corners=True), 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), align_corners=True), 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), align_corners=True), 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), align_corners=True), 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), align_corners=True), 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), align_corners=True), 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