-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdetnet59.py
194 lines (162 loc) · 9.15 KB
/
detnet59.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
'''
version:
TensorFlow==1.7.0
Keras==2.2.4
Python==3.6.5
'''
from keras.models import Model
from keras.layers import Input, Dense, Flatten
from keras.layers import Conv2D, Add, ZeroPadding2D, MaxPooling2D, AveragePooling2D
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import ReLU
from keras.utils import plot_model
# DetNet59 Model Structure
# _______________________________________________________________________________
# Stage |output size| kernel size | num_filters | num_blocks | stride | dilate
# _______________________________________________________________________________
# Stage 1 | 112x112 | 7x7 | 64 | --- | 2 | 1
# -------------------------------------------------------------------------------
# | | 3x3 | max pool | 2 |
# | | 1x1 | 64 | | |
# Stage 2 | 56x56 | 3x3 | 64 | 3 | 1 | 1
# | | 1x2 | 256 | | |
# -------------------------------------------------------------------------------
# | | 1x1 | 128 | | |
# Stage 3 | 28x28 | 3x3 | 128 | 4 | 2 | 1
# | | 1x2 | 512 | | |
# -------------------------------------------------------------------------------
# | | 1x1 | 256 | | |
# Stage 4 | 14x14 | 3x3 | 256 | 6 | 2 | 1
# | | 1x2 | 1024 | | |
# -------------------------------------------------------------------------------
# | | 1x1 | 256 | | |
# Stage 5 | 14x14 | 3x3 | 256 | 3 | 1 | 2
# | | 1x2 | 256 | | |
# -------------------------------------------------------------------------------
# | | 1x1 | 256 | | |
# Stage 6 | 14x14 | 3x3 | 256 | 3 | 1 | 2
# | | 1x2 | 256 | | |
# -------------------------------------------------------------------------------
# | 1x1 | 14x14 | ave pool, 1000-d fc, softmax
# -------------------------------------------------------------------------------
def res_block(x, filters_list, strides=1, use_bias=True, name=None):
'''
y = f3(f2(f1(x))) + x
# Conv2D default arguments:
strides=1
padding='valid'
data_format='channels_last'
dilation_rate=1
activation=None
use_bias=True
'''
out = Conv2D(filters=filters_list[0], kernel_size=1, strides=1, use_bias=False, name='%s_1'%(name))(x)
out = BatchNormalization(name='%s_1_bn'%(name))(out)
out = ReLU(name='%s_1_relu'%(name))(out)
out = Conv2D(filters=filters_list[1], kernel_size=3, strides=1, padding='same', use_bias=False, name='%s_2'%(name))(out)
out = BatchNormalization(name='%s_2_bn'%(name))(out)
out = ReLU(name='%s_2_relu'%(name))(out)
out = Conv2D(filters=filters_list[2], kernel_size=1, strides=1, use_bias=False, name='%s_3'%(name))(out)
out = BatchNormalization(name='%s_3_bn'%(name))(out)
out = Add(name='%s_add'%(name))([x, out])
out = ReLU(name='%s_relu'%(name))(out)
return out
def res_block_proj(x, filters_list, strides=2, use_bias=True, name=None):
'''
y = f3(f2(f1(x))) + proj(x)
'''
out = Conv2D(filters=filters_list[0], kernel_size=1, strides=strides, use_bias=False, name='%s_1'%(name))(x)
out = BatchNormalization(name='%s_1_bn'%(name))(out)
out = ReLU(name='%s_1_relu'%(name))(out)
out = Conv2D(filters=filters_list[1], kernel_size=3, strides=1, padding='same', use_bias=False, name='%s_2'%(name))(out)
out = BatchNormalization(name='%s_2_bn'%(name))(out)
out = ReLU(name='%s_2_relu'%(name))(out)
out = Conv2D(filters=filters_list[2], kernel_size=1, strides=1, use_bias=False, name='%s_3'%(name))(out)
out = BatchNormalization(name='%s_3_bn'%(name))(out)
x = Conv2D(filters=filters_list[2], kernel_size=1, strides=strides, use_bias=False, name='%s_proj'%(name))(x)
x = BatchNormalization(name='%s_proj_bn'%(name))(x)
out = Add(name='%s_add'%(name))([x, out])
out = ReLU(name='%s_relu'%(name))(out)
return out
def dilated_res_block(x, filters_list, strides=1, use_bias=True, name=None):
'''
y = f3(f2(f1(x))) + x
'''
out = Conv2D(filters=filters_list[0], kernel_size=1, strides=1, use_bias=False, name='%s_1'%(name))(x)
out = BatchNormalization(name='%s_1_bn'%(name))(out)
out = ReLU(name='%s_1_relu'%(name))(out)
out = Conv2D(filters=filters_list[1], kernel_size=3, strides=1, padding='same', dilation_rate=2, use_bias=False, name='%s_2'%(name))(out)
out = BatchNormalization(name='%s_2_bn'%(name))(out)
out = ReLU(name='%s_2_relu'%(name))(out)
out = Conv2D(filters=filters_list[2], kernel_size=1, strides=1, use_bias=False, name='%s_3'%(name))(out)
out = BatchNormalization(name='%s_3_bn'%(name))(out)
out = Add(name='%s_add'%(name))([x, out])
out = ReLU(name='%s_relu'%(name))(out)
return out
def dilated_res_block_proj(x, filters_list, strides=1, use_bias=True, name=None):
'''
y = f3(f2(f1(x))) + proj(x)
'''
out = Conv2D(filters=filters_list[0], kernel_size=1, strides=1, use_bias=False, name='%s_1'%(name))(x)
out = BatchNormalization(name='%s_1_bn'%(name))(out)
out = ReLU(name='%s_1_relu'%(name))(out)
out = Conv2D(filters=filters_list[1], kernel_size=3, strides=1, padding='same', dilation_rate=2, use_bias=False, name='%s_2'%(name))(out)
out = BatchNormalization(name='%s_2_bn'%(name))(out)
out = ReLU(name='%s_2_relu'%(name))(out)
out = Conv2D(filters=filters_list[2], kernel_size=1, strides=1, use_bias=False, name='%s_3'%(name))(out)
out = BatchNormalization(name='%s_3_bn'%(name))(out)
x = Conv2D(filters=filters_list[2], kernel_size=1, strides=1, use_bias=False, name='%s_proj'%(name))(x)
x = BatchNormalization(name='%s_proj_bn'%(name))(x)
out = Add(name='%s_add'%(name))([x, out])
out = ReLU(name='%s_relu'%(name))(out)
return out
def resnet_body(x, filters_list, num_blocks, strides=2, name=None):
out = res_block_proj(x=x, filters_list=filters_list, strides=strides, name='%s_1'%(name))
for i in range(1, num_blocks):
out = res_block(x=out, filters_list=filters_list, name='%s_%s'%(name, str(i+1)))
return out
def detnet_body(x, filters_list, num_blocks, strides=1, name=None):
out = dilated_res_block_proj(x=x, filters_list=filters_list, name='%s_1'%(name))
for i in range(1, num_blocks):
out = dilated_res_block(x=out, filters_list=filters_list, name='%s_%s'%(name, str(i+1)))
return out
def detnet_59(inputs, filters_list, blocks_list, num_classes):
# stage 1
inputs_pad = ZeroPadding2D(padding=3, name='inputs_pad')(inputs)
conv1 = Conv2D(filters=filters_list[0][0], kernel_size=7, strides=2, use_bias=False, name='conv1')(inputs_pad)
conv1 = BatchNormalization(name='conv1_bn')(conv1)
conv1 = ReLU(name='conv1_relu')(conv1)
# stage 2
conv1_pad = ZeroPadding2D(padding=1, name='conv1_pad')(conv1)
conv1_pool = MaxPooling2D(pool_size=3, strides=2, name='conv1_maxpool')(conv1_pad)
conv2_x = resnet_body(x=conv1_pool, filters_list=filters_list[1], num_blocks=blocks_list[1], strides=1, name='res2')
# stage 3
conv3_x = resnet_body(x=conv2_x, filters_list=filters_list[2], num_blocks=blocks_list[2], strides=2, name='res3')
# stage 4
conv4_x = resnet_body(x=conv3_x, filters_list=filters_list[3], num_blocks=blocks_list[3], strides=2, name='res4')
# stage 5
conv5_x = detnet_body(x=conv4_x, filters_list=filters_list[4], num_blocks=blocks_list[4], strides=1, name='dires5')
# stage 6
conv6_x = detnet_body(x=conv5_x, filters_list=filters_list[5], num_blocks=blocks_list[5], strides=1, name='dires6')
out = AveragePooling2D(pool_size=14, strides=1, name='final_avepool')(conv6_x)
out = Flatten(name='flatten')(out)
outputs = Dense(units=num_classes, activation="softmax", kernel_initializer='he_normal', name='dense')(out)
model = Model(inputs=inputs, outputs=outputs)
return model
if __name__=='__main__':
# create a keras model detnet_59
inputs = Input(shape=(224,224,3), name='inputs')
filters_list=[[64],
[64,64,256],
[128,128,512],
[256,256,1024],
[256,256,256],
[256,256,256]]
blocks_list=[1,3,4,6,3,3]
num_classes = 1000
model = detnet_59(inputs, filters_list, blocks_list, num_classes)
# check the model
model.summary()
plot_model(model, to_file='detnet59.png')
model.compile(optimizer='sgd', loss='categorical_crossentropy')
print('Model Compiled')