This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 306
/
ssd_vgg16.py
344 lines (271 loc) · 12.5 KB
/
ssd_vgg16.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
from __future__ import division
import numpy as np
import chainer
import chainer.functions as F
from chainer import initializers
import chainer.links as L
from chainercv.links.model.ssd import Multibox
from chainercv.links.model.ssd import Normalize
from chainercv.links.model.ssd import SSD
from chainercv import utils
# RGB, (C, 1, 1) format
_imagenet_mean = np.array((123, 117, 104)).reshape((-1, 1, 1))
class VGG16(chainer.Chain):
"""An extended VGG-16 model for SSD300 and SSD512.
This is an extended VGG-16 model proposed in [#]_.
The differences from original VGG-16 [#]_ are shown below.
* :obj:`conv5_1`, :obj:`conv5_2` and :obj:`conv5_3` are changed from \
:class:`~chainer.links.Convolution2d` to \
:class:`~chainer.links.DilatedConvolution2d`.
* :class:`~chainercv.links.model.ssd.Normalize` is \
inserted after :obj:`conv4_3`.
* The parameters of max pooling after :obj:`conv5_3` are changed.
* :obj:`fc6` and :obj:`fc7` are converted to :obj:`conv6` and :obj:`conv7`.
.. [#] Wei Liu, Dragomir Anguelov, Dumitru Erhan,
Christian Szegedy, Scott Reed, Cheng-Yang Fu, Alexander C. Berg.
SSD: Single Shot MultiBox Detector. ECCV 2016.
.. [#] Karen Simonyan, Andrew Zisserman.
Very Deep Convolutional Networks for Large-Scale Image Recognition.
ICLR 2015.
"""
def __init__(self):
super(VGG16, self).__init__()
with self.init_scope():
self.conv1_1 = L.Convolution2D(64, 3, pad=1)
self.conv1_2 = L.Convolution2D(64, 3, pad=1)
self.conv2_1 = L.Convolution2D(128, 3, pad=1)
self.conv2_2 = L.Convolution2D(128, 3, pad=1)
self.conv3_1 = L.Convolution2D(256, 3, pad=1)
self.conv3_2 = L.Convolution2D(256, 3, pad=1)
self.conv3_3 = L.Convolution2D(256, 3, pad=1)
self.conv4_1 = L.Convolution2D(512, 3, pad=1)
self.conv4_2 = L.Convolution2D(512, 3, pad=1)
self.conv4_3 = L.Convolution2D(512, 3, pad=1)
self.norm4 = Normalize(512, initial=initializers.Constant(20))
self.conv5_1 = L.DilatedConvolution2D(512, 3, pad=1)
self.conv5_2 = L.DilatedConvolution2D(512, 3, pad=1)
self.conv5_3 = L.DilatedConvolution2D(512, 3, pad=1)
self.conv6 = L.DilatedConvolution2D(1024, 3, pad=6, dilate=6)
self.conv7 = L.Convolution2D(1024, 1)
def forward(self, x):
ys = []
h = F.relu(self.conv1_1(x))
h = F.relu(self.conv1_2(h))
h = F.max_pooling_2d(h, 2)
h = F.relu(self.conv2_1(h))
h = F.relu(self.conv2_2(h))
h = F.max_pooling_2d(h, 2)
h = F.relu(self.conv3_1(h))
h = F.relu(self.conv3_2(h))
h = F.relu(self.conv3_3(h))
h = F.max_pooling_2d(h, 2)
h = F.relu(self.conv4_1(h))
h = F.relu(self.conv4_2(h))
h = F.relu(self.conv4_3(h))
ys.append(self.norm4(h))
h = F.max_pooling_2d(h, 2)
h = F.relu(self.conv5_1(h))
h = F.relu(self.conv5_2(h))
h = F.relu(self.conv5_3(h))
h = F.max_pooling_2d(h, 3, stride=1, pad=1)
h = F.relu(self.conv6(h))
h = F.relu(self.conv7(h))
ys.append(h)
return ys
class VGG16Extractor300(VGG16):
"""A VGG-16 based feature extractor for SSD300.
This is a feature extractor for :class:`~chainercv.links.model.ssd.SSD300`.
This extractor is based on :class:`~chainercv.links.model.ssd.VGG16`.
"""
insize = 300
grids = (38, 19, 10, 5, 3, 1)
def __init__(self):
init = {
'initialW': initializers.LeCunUniform(),
'initial_bias': initializers.Zero(),
}
super(VGG16Extractor300, self).__init__()
with self.init_scope():
self.conv8_1 = L.Convolution2D(256, 1, **init)
self.conv8_2 = L.Convolution2D(512, 3, stride=2, pad=1, **init)
self.conv9_1 = L.Convolution2D(128, 1, **init)
self.conv9_2 = L.Convolution2D(256, 3, stride=2, pad=1, **init)
self.conv10_1 = L.Convolution2D(128, 1, **init)
self.conv10_2 = L.Convolution2D(256, 3, **init)
self.conv11_1 = L.Convolution2D(128, 1, **init)
self.conv11_2 = L.Convolution2D(256, 3, **init)
def forward(self, x):
"""Compute feature maps from a batch of images.
This method extracts feature maps from
:obj:`conv4_3`, :obj:`conv7`, :obj:`conv8_2`,
:obj:`conv9_2`, :obj:`conv10_2`, and :obj:`conv11_2`.
Args:
x (ndarray): An array holding a batch of images.
The images should be resized to :math:`300\\times 300`.
Returns:
list of Variable:
Each variable contains a feature map.
"""
ys = super(VGG16Extractor300, self).forward(x)
for i in range(8, 11 + 1):
h = ys[-1]
h = F.relu(self['conv{:d}_1'.format(i)](h))
h = F.relu(self['conv{:d}_2'.format(i)](h))
ys.append(h)
return ys
class VGG16Extractor512(VGG16):
"""A VGG-16 based feature extractor for SSD512.
This is a feature extractor for :class:`~chainercv.links.model.ssd.SSD512`.
This extractor is based on :class:`~chainercv.links.model.ssd.VGG16`.
"""
insize = 512
grids = (64, 32, 16, 8, 4, 2, 1)
def __init__(self):
init = {
'initialW': initializers.LeCunUniform(),
'initial_bias': initializers.Zero(),
}
super(VGG16Extractor512, self).__init__()
with self.init_scope():
self.conv8_1 = L.Convolution2D(256, 1, **init)
self.conv8_2 = L.Convolution2D(512, 3, stride=2, pad=1, **init)
self.conv9_1 = L.Convolution2D(128, 1, **init)
self.conv9_2 = L.Convolution2D(256, 3, stride=2, pad=1, **init)
self.conv10_1 = L.Convolution2D(128, 1, **init)
self.conv10_2 = L.Convolution2D(256, 3, stride=2, pad=1, **init)
self.conv11_1 = L.Convolution2D(128, 1, **init)
self.conv11_2 = L.Convolution2D(256, 3, stride=2, pad=1, **init)
self.conv12_1 = L.Convolution2D(128, 1, **init)
self.conv12_2 = L.Convolution2D(256, 4, pad=1, **init)
def forward(self, x):
"""Compute feature maps from a batch of images.
This method extracts feature maps from
:obj:`conv4_3`, :obj:`conv7`, :obj:`conv8_2`,
:obj:`conv9_2`, :obj:`conv10_2`, :obj:`conv11_2`, and :obj:`conv12_2`.
Args:
x (ndarray): An array holding a batch of images.
The images should be resized to :math:`512\\times 512`.
Returns:
list of Variable:
Each variable contains a feature map.
"""
ys = super(VGG16Extractor512, self).forward(x)
for i in range(8, 12 + 1):
h = ys[-1]
h = F.relu(self['conv{:d}_1'.format(i)](h))
h = F.relu(self['conv{:d}_2'.format(i)](h))
ys.append(h)
return ys
class SSD300(SSD):
"""Single Shot Multibox Detector with 300x300 inputs.
This is a model of Single Shot Multibox Detector [#]_.
This model uses :class:`~chainercv.links.model.ssd.VGG16Extractor300` as
its feature extractor.
.. [#] Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy,
Scott Reed, Cheng-Yang Fu, Alexander C. Berg.
SSD: Single Shot MultiBox Detector. ECCV 2016.
Args:
n_fg_class (int): The number of classes excluding the background.
pretrained_model (string): The weight file to be loaded.
This can take :obj:`'voc0712'`, `filepath` or :obj:`None`.
The default value is :obj:`None`.
* :obj:`'voc0712'`: Load weights trained on trainval split of \
PASCAL VOC 2007 and 2012. \
The weight file is downloaded and cached automatically. \
:obj:`n_fg_class` must be :obj:`20` or :obj:`None`. \
These weights were converted from the Caffe model provided by \
`the original implementation \
<https://github.com/weiliu89/caffe/tree/ssd>`_. \
The conversion code is `chainercv/examples/ssd/caffe2npz.py`.
* :obj:`'imagenet'`: Load weights of VGG-16 trained on ImageNet. \
The weight file is downloaded and cached automatically. \
This option initializes weights partially and the rests are \
initialized randomly. In this case, :obj:`n_fg_class` \
can be set to any number.
* `filepath`: A path of npz file. In this case, :obj:`n_fg_class` \
must be specified properly.
* :obj:`None`: Do not load weights.
"""
_models = {
'voc0712': {
'param': {'n_fg_class': 20},
'url': 'https://chainercv-models.preferred.jp/'
'ssd300_voc0712_converted_2017_06_06.npz',
'cv2': True
},
'imagenet': {
'url': 'https://chainercv-models.preferred.jp/'
'ssd_vgg16_imagenet_converted_2017_06_09.npz',
'cv2': True
},
}
def __init__(self, n_fg_class=None, pretrained_model=None):
param, path = utils.prepare_pretrained_model(
{'n_fg_class': n_fg_class}, pretrained_model, self._models)
super(SSD300, self).__init__(
extractor=VGG16Extractor300(),
multibox=Multibox(
n_class=param['n_fg_class'] + 1,
aspect_ratios=((2,), (2, 3), (2, 3), (2, 3), (2,), (2,))),
steps=(8, 16, 32, 64, 100, 300),
sizes=(30, 60, 111, 162, 213, 264, 315),
mean=_imagenet_mean)
if path:
chainer.serializers.load_npz(path, self, strict=False)
class SSD512(SSD):
"""Single Shot Multibox Detector with 512x512 inputs.
This is a model of Single Shot Multibox Detector [#]_.
This model uses :class:`~chainercv.links.model.ssd.VGG16Extractor512` as
its feature extractor.
.. [#] Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy,
Scott Reed, Cheng-Yang Fu, Alexander C. Berg.
SSD: Single Shot MultiBox Detector. ECCV 2016.
Args:
n_fg_class (int): The number of classes excluding the background.
pretrained_model (string): The weight file to be loaded.
This can take :obj:`'voc0712'`, `filepath` or :obj:`None`.
The default value is :obj:`None`.
* :obj:`'voc0712'`: Load weights trained on trainval split of \
PASCAL VOC 2007 and 2012. \
The weight file is downloaded and cached automatically. \
:obj:`n_fg_class` must be :obj:`20` or :obj:`None`. \
These weights were converted from the Caffe model provided by \
`the original implementation \
<https://github.com/weiliu89/caffe/tree/ssd>`_. \
The conversion code is `chainercv/examples/ssd/caffe2npz.py`.
* :obj:`'imagenet'`: Load weights of VGG-16 trained on ImageNet. \
The weight file is downloaded and cached automatically. \
This option initializes weights partially and the rests are \
initialized randomly. In this case, :obj:`n_fg_class` \
can be set to any number.
* `filepath`: A path of npz file. In this case, :obj:`n_fg_class` \
must be specified properly.
* :obj:`None`: Do not load weights.
"""
_models = {
'voc0712': {
'param': {'n_fg_class': 20},
'url': 'https://chainercv-models.preferred.jp/'
'ssd512_voc0712_converted_2017_06_06.npz',
'cv2': True
},
'imagenet': {
'url': 'https://chainercv-models.preferred.jp/'
'ssd_vgg16_imagenet_converted_2017_06_09.npz',
'cv2': True
},
}
def __init__(self, n_fg_class=None, pretrained_model=None):
param, path = utils.prepare_pretrained_model(
{'n_fg_class': n_fg_class}, pretrained_model, self._models)
super(SSD512, self).__init__(
extractor=VGG16Extractor512(),
multibox=Multibox(
n_class=param['n_fg_class'] + 1,
aspect_ratios=(
(2,), (2, 3), (2, 3), (2, 3), (2, 3), (2,), (2,))),
steps=(8, 16, 32, 64, 128, 256, 512),
sizes=(35.84, 76.8, 153.6, 230.4, 307.2, 384.0, 460.8, 537.6),
mean=_imagenet_mean)
if path:
chainer.serializers.load_npz(path, self, strict=False)