forked from rykov8/ssd_keras
-
Notifications
You must be signed in to change notification settings - Fork 85
/
ssd_model.py
339 lines (275 loc) · 13.9 KB
/
ssd_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
"""Keras implementation of SSD."""
from keras.layers import Activation
from keras.layers import Conv2D
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import Input
from keras.layers import MaxPool2D
from keras.layers import concatenate
from keras.layers import Reshape
from keras.layers import ZeroPadding2D
from keras.models import Model
from utils.layers import Normalize
from ssd_model_dense import dsod300_body, dsod512_body
from ssd_model_resnet import ssd512_resnet_body
def ssd300_body(x):
source_layers = []
# Block 1
x = Conv2D(64, 3, strides=1, padding='same', name='conv1_1', activation='relu')(x)
x = Conv2D(64, 3, strides=1, padding='same', name='conv1_2', activation='relu')(x)
x = MaxPool2D(pool_size=2, strides=2, padding='same', name='pool1')(x)
# Block 2
x = Conv2D(128, 3, strides=1, padding='same', name='conv2_1', activation='relu')(x)
x = Conv2D(128, 3, strides=1, padding='same', name='conv2_2', activation='relu')(x)
x = MaxPool2D(pool_size=2, strides=2, padding='same', name='pool2')(x)
# Block 3
x = Conv2D(256, 3, strides=1, padding='same', name='conv3_1', activation='relu')(x)
x = Conv2D(256, 3, strides=1, padding='same', name='conv3_2', activation='relu')(x)
x = Conv2D(256, 3, strides=1, padding='same', name='conv3_3', activation='relu')(x)
x = MaxPool2D(pool_size=2, strides=2, padding='same', name='pool3')(x)
# Block 4
x = Conv2D(512, 3, strides=1, padding='same', name='conv4_1', activation='relu')(x)
x = Conv2D(512, 3, strides=1, padding='same', name='conv4_2', activation='relu')(x)
x = Conv2D(512, 3, strides=1, padding='same', name='conv4_3', activation='relu')(x)
source_layers.append(x)
x = MaxPool2D(pool_size=2, strides=2, padding='same', name='pool4')(x)
# Block 5
x = Conv2D(512, 3, strides=1, padding='same', name='conv5_1', activation='relu')(x)
x = Conv2D(512, 3, strides=1, padding='same', name='conv5_2', activation='relu')(x)
x = Conv2D(512, 3, strides=1, padding='same', name='conv5_3', activation='relu')(x)
x = MaxPool2D(pool_size=3, strides=1, padding='same', name='pool5')(x)
# FC6
x = Conv2D(1024, 3, strides=1, dilation_rate=(6, 6), padding='same', name='fc6', activation='relu')(x)
# FC7
x = Conv2D(1024, 1, strides=1, padding='same', name='fc7', activation='relu')(x)
source_layers.append(x)
# Block 6
x = Conv2D(256, 1, strides=1, padding='same', name='conv6_1', activation='relu')(x)
x = ZeroPadding2D((1,1))(x)
x = Conv2D(512, 3, strides=2, padding='valid', name='conv6_2', activation='relu')(x)
source_layers.append(x)
# Block 7
x = Conv2D(128, 1, strides=1, padding='same', name='conv7_1', activation='relu')(x)
x = ZeroPadding2D((1,1))(x)
x = Conv2D(256, 3, strides=2, padding='valid', name='conv7_2', activation='relu')(x)
source_layers.append(x)
# Block 8
x = Conv2D(128, 1, strides=1, padding='same', name='conv8_1', activation='relu')(x)
x = Conv2D(256, 3, strides=1, padding='valid', name='conv8_2', activation='relu')(x)
source_layers.append(x)
# Block 9
x = Conv2D(128, 1, strides=1, padding='same', name='conv9_1', activation='relu')(x)
x = Conv2D(256, 3, strides=1, padding='valid', name='conv9_2', activation='relu')(x)
source_layers.append(x)
return source_layers
def ssd512_body(x):
source_layers = []
# Block 1
x = Conv2D(64, 3, strides=1, padding='same', name='conv1_1', activation='relu')(x)
x = Conv2D(64, 3, strides=1, padding='same', name='conv1_2', activation='relu')(x)
x = MaxPool2D(pool_size=2, strides=2, padding='same', name='pool1')(x)
# Block 2
x = Conv2D(128, 3, strides=1, padding='same', name='conv2_1', activation='relu')(x)
x = Conv2D(128, 3, strides=1, padding='same', name='conv2_2', activation='relu')(x)
x = MaxPool2D(pool_size=2, strides=2, padding='same', name='pool2')(x)
# Block 3
x = Conv2D(256, 3, strides=1, padding='same', name='conv3_1', activation='relu')(x)
x = Conv2D(256, 3, strides=1, padding='same', name='conv3_2', activation='relu')(x)
x = Conv2D(256, 3, strides=1, padding='same', name='conv3_3', activation='relu')(x)
x = MaxPool2D(pool_size=2, strides=2, padding='same', name='pool3')(x)
# Block 4
x = Conv2D(512, 3, strides=1, padding='same', name='conv4_1', activation='relu')(x)
x = Conv2D(512, 3, strides=1, padding='same', name='conv4_2', activation='relu')(x)
x = Conv2D(512, 3, strides=1, padding='same', name='conv4_3', activation='relu')(x)
source_layers.append(x)
x = MaxPool2D(pool_size=2, strides=2, padding='same', name='pool4')(x)
# Block 5
x = Conv2D(512, 3, strides=1, padding='same', name='conv5_1', activation='relu')(x)
x = Conv2D(512, 3, strides=1, padding='same', name='conv5_2', activation='relu')(x)
x = Conv2D(512, 3, strides=1, padding='same', name='conv5_3', activation='relu')(x)
x = MaxPool2D(pool_size=3, strides=1, padding='same', name='pool5')(x)
# FC6
x = Conv2D(1024, 3, strides=1, dilation_rate=(6, 6), padding='same', name='fc6', activation='relu')(x)
# FC7
x = Conv2D(1024, 1, strides=1, padding='same', name='fc7', activation='relu')(x)
source_layers.append(x)
# Block 6
x = Conv2D(256, 1, strides=1, padding='same', name='conv6_1', activation='relu')(x)
x = ZeroPadding2D((1,1))(x)
x = Conv2D(512, 3, strides=2, padding='valid', name='conv6_2', activation='relu')(x)
source_layers.append(x)
# Block 7
x = Conv2D(128, 1, strides=1, padding='same', name='conv7_1', activation='relu')(x)
x = ZeroPadding2D((1,1))(x)
x = Conv2D(256, 3, strides=2, padding='valid', name='conv7_2', activation='relu')(x)
source_layers.append(x)
# Block 8
x = Conv2D(128, 1, strides=1, padding='same', name='conv8_1', activation='relu')(x)
x = ZeroPadding2D((1,1))(x)
x = Conv2D(256, 3, strides=2, padding='valid', name='conv8_2', activation='relu')(x)
source_layers.append(x)
# Block 9
x = Conv2D(128, 1, strides=1, padding='same', name='conv9_1', activation='relu')(x)
x = ZeroPadding2D((1,1))(x)
x = Conv2D(256, 3, strides=2, padding='valid', name='conv9_2', activation='relu')(x)
source_layers.append(x)
# Block 10
x = Conv2D(128, 1, strides=1, padding='same', name='conv10_1', activation='relu')(x)
x = ZeroPadding2D((1,1))(x)
x = Conv2D(256, 4, strides=2, padding='valid', name='conv10_2', activation='relu')(x)
source_layers.append(x)
return source_layers
def multibox_head(source_layers, num_priors, num_classes, normalizations=None, softmax=True):
class_activation = 'softmax' if softmax else 'sigmoid'
mbox_conf = []
mbox_loc = []
for i in range(len(source_layers)):
x = source_layers[i]
name = x.name.split('/')[0]
# normalize
if normalizations is not None and normalizations[i] > 0:
name = name + '_norm'
x = Normalize(normalizations[i], name=name)(x)
# confidence
name1 = name + '_mbox_conf'
x1 = Conv2D(num_priors[i] * num_classes, 3, padding='same', name=name1)(x)
x1 = Flatten(name=name1+'_flat')(x1)
mbox_conf.append(x1)
# location
name2 = name + '_mbox_loc'
x2 = Conv2D(num_priors[i] * 4, 3, padding='same', name=name2)(x)
x2 = Flatten(name=name2+'_flat')(x2)
mbox_loc.append(x2)
mbox_loc = concatenate(mbox_loc, axis=1, name='mbox_loc')
mbox_loc = Reshape((-1, 4), name='mbox_loc_final')(mbox_loc)
mbox_conf = concatenate(mbox_conf, axis=1, name='mbox_conf')
mbox_conf = Reshape((-1, num_classes), name='mbox_conf_logits')(mbox_conf)
mbox_conf = Activation(class_activation, name='mbox_conf_final')(mbox_conf)
predictions = concatenate([mbox_loc, mbox_conf], axis=2, name='predictions')
return predictions
def SSD300(input_shape=(300, 300, 3), num_classes=21, softmax=True):
"""SSD300 architecture.
# Arguments
input_shape: Shape of the input image.
num_classes: Number of classes including background.
# Notes
In order to stay compatible with pre-trained models, the parameters
were chosen as in the caffee implementation.
# References
https://arxiv.org/abs/1512.02325
"""
x = input_tensor = Input(shape=input_shape)
source_layers = ssd300_body(x)
# Add multibox head for classification and regression
num_priors = [4, 6, 6, 6, 4, 4]
normalizations = [20, -1, -1, -1, -1, -1]
output_tensor = multibox_head(source_layers, num_priors, num_classes, normalizations, softmax)
model = Model(input_tensor, output_tensor)
model.num_classes = num_classes
# parameters for prior boxes
model.image_size = input_shape[:2]
model.source_layers = source_layers
# stay compatible with caffe models
model.aspect_ratios = [[1,2,1/2], [1,2,1/2,3,1/3], [1,2,1/2,3,1/3], [1,2,1/2,3,1/3], [1,2,1/2], [1,2,1/2]]
model.minmax_sizes = [(30, 60), (60, 111), (111, 162), (162, 213), (213, 264), (264, 315)]
model.steps = [8, 16, 32, 64, 100, 300]
model.special_ssd_boxes = True
return model
def SSD512(input_shape=(512, 512, 3), num_classes=21, softmax=True):
"""SSD512 architecture.
# Arguments
input_shape: Shape of the input image.
num_classes: Number of classes including background.
# Notes
In order to stay compatible with pre-trained models, the parameters
were chosen as in the caffee implementation.
# References
https://arxiv.org/abs/1512.02325
"""
x = input_tensor = Input(shape=input_shape)
source_layers = ssd512_body(x)
# Add multibox head for classification and regression
num_priors = [4, 6, 6, 6, 6, 4, 4]
normalizations = [20, -1, -1, -1, -1, -1, -1]
output_tensor = multibox_head(source_layers, num_priors, num_classes, normalizations, softmax)
model = Model(input_tensor, output_tensor)
model.num_classes = num_classes
# parameters for prior boxes
model.image_size = input_shape[:2]
model.source_layers = source_layers
# stay compatible with caffe models
model.aspect_ratios = [[1,2,1/2], [1,2,1/2,3,1/3], [1,2,1/2,3,1/3], [1,2,1/2,3,1/3], [1,2,1/2,3,1/3], [1,2,1/2], [1,2,1/2]]
#model.minmax_sizes = [(35, 76), (76, 153), (153, 230), (230, 307), (307, 384), (384, 460), (460, 537)]
model.minmax_sizes = [(20.48, 51.2), (51.2, 133.12), (133.12, 215.04), (215.04, 296.96), (296.96, 378.88), (378.88, 460.8), (460.8, 542.72)]
model.steps = [8, 16, 32, 64, 128, 256, 512]
model.special_ssd_boxes = True
return model
def DSOD300(input_shape=(300, 300, 3), num_classes=21, activation='relu', softmax=True):
"""DSOD, DenseNet based SSD300 architecture.
# Arguments
input_shape: Shape of the input image.
num_classes: Number of classes including background.
activation: Type of activation functions.
# References
https://arxiv.org/abs/1708.01241
"""
x = input_tensor = Input(shape=input_shape)
source_layers = dsod300_body(x, activation=activation)
num_priors = [4, 6, 6, 6, 4, 4]
normalizations = [20, 20, 20, 20, 20, 20]
output_tensor = multibox_head(source_layers, num_priors, num_classes, normalizations, softmax)
model = Model(input_tensor, output_tensor)
model.num_classes = num_classes
# parameters for prior boxes
model.image_size = input_shape[:2]
model.source_layers = source_layers
model.aspect_ratios = [[1,2,1/2], [1,2,1/2,3,1/3], [1,2,1/2,3,1/3], [1,2,1/2,3,1/3], [1,2,1/2], [1,2,1/2]]
model.minmax_sizes = [(30, 60), (60, 111), (111, 162), (162, 213), (213, 264), (264, 315)]
model.steps = [8, 16, 32, 64, 100, 300]
model.special_ssd_boxes = True
return model
SSD300_dense = DSOD300
def DSOD512(input_shape=(512, 512, 3), num_classes=21, activation='relu', softmax=True):
"""DSOD, DenseNet based SSD512 architecture.
# Arguments
input_shape: Shape of the input image.
num_classes: Number of classes including background.
activation: Type of activation functions.
# References
https://arxiv.org/abs/1708.01241
"""
x = input_tensor = Input(shape=input_shape)
source_layers = dsod512_body(x, activation=activation)
num_priors = [4, 6, 6, 6, 6, 4, 4]
normalizations = [20, 20, 20, 20, 20, 20, 20]
output_tensor = multibox_head(source_layers, num_priors, num_classes, normalizations, softmax)
model = Model(input_tensor, output_tensor)
model.num_classes = num_classes
# parameters for prior boxes
model.image_size = input_shape[:2]
model.source_layers = source_layers
model.aspect_ratios = [[1,2,1/2], [1,2,1/2,3,1/3], [1,2,1/2,3,1/3], [1,2,1/2,3,1/3], [1,2,1/2,3,1/3], [1,2,1/2], [1,2,1/2]]
model.minmax_sizes = [(35, 76), (76, 153), (153, 230), (230, 307), (307, 384), (384, 460), (460, 537)]
model.steps = [8, 16, 32, 64, 128, 256, 512]
model.special_ssd_boxes = True
return model
SSD512_dense = DSOD512
def SSD512_resnet(input_shape=(512, 512, 3), num_classes=21, softmax=True):
# TODO: it does not converge!
x = input_tensor = Input(shape=input_shape)
source_layers = ssd512_resnet_body(x)
# Add multibox head for classification and regression
num_priors = [4, 6, 6, 6, 6, 4, 4]
normalizations = [20, 20, 20, 20, 20, 20, 20]
output_tensor = multibox_head(source_layers, num_priors, num_classes, normalizations, softmax)
model = Model(input_tensor, output_tensor)
model.num_classes = num_classes
# parameters for prior boxes
model.image_size = input_shape[:2]
model.source_layers = source_layers
# stay compatible with caffe models
model.aspect_ratios = [[1,2,1/2], [1,2,1/2,3,1/3], [1,2,1/2,3,1/3], [1,2,1/2,3,1/3], [1,2,1/2,3,1/3], [1,2,1/2], [1,2,1/2]]
model.minmax_sizes = [(35, 76), (76, 153), (153, 230), (230, 307), (307, 384), (384, 460), (460, 537)]
model.steps = [8, 16, 32, 64, 128, 256, 512]
model.special_ssd_boxes = True
return model