声明:本文禁止转载
模型定义代码
ssd框架代码:research/object_detection/meta_architectures/ssd_meta_arch.py
该文件负责ssd框架的定义,后续基于该代码进行展开说明各个模块的具体实现
ssd配置文件:research/object_detection/samples/configs/ssd_mobilenet_v1_coco.config
该文件是models的配置文件,选取mobilenet_v1作为特征提取网络,后续基于这个配置文件展开描述
I. Predict实现过程
下面通过1-4说明predict的实现过程,1-3是相关模块,4是meta_arch中的实现流程。
1.FeatureExtractor
research/object_detection/models/ssd_mobilenet_v1_feature_extractor.py
1.1 结构定义如下:
feature_map_layout = {
'from_layer': ['Conv2d_11_pointwise', 'Conv2d_13_pointwise', '', '',
'', ''],
'layer_depth': [-1, -1, 512, 256, 256, 128],
'use_explicit_padding': self._use_explicit_padding,
'use_depthwise': self._use_depthwise,
}
1.2 参数说明
‘from_layer’:从mobilenet中提取的namescop的名称,如果为空则表示新生成的。其value为list,长度为6,也就是6个不同尺度的featuremap。
‘layer_depth’:表示channel的深度,为空,表示从原有的net中继承。
‘use_explicit_padding’:如果使能,选择valid pading,并在valid padding之前先做一次fixed padding,其目的是为了让经过卷积后的size与使用same padding的大小一致。
使用same padding与fixed padding的差异,参考stackover flow
使用sampe padding时:
Case 1:
pad| |pad
inputs: 0 |1 2 3 4 5 |0
|_______|
|_______|
|_______|
Case 2:
|pad
inputs: 1 2 3 4 5 6 |0
|_______|
|_______|
|_______|
使用fixed padding
Case 1:
pad| |pad
inputs: 0 |1 2 3 4 5 |0
|_______|
|_______|
|_______|
Case 2:
pad| |pad
inputs: 0 |1 2 3 4 5 6 |0
|_______|
|_______|
|_______|
1.3 feature map实现
research/object_detection/models/feature_map_generators.py.py,multi_resolution_feature_maps()
1.4 输出feature map结构
name scope | channel depth | feature map size |
---|---|---|
Conv2d_11_pointwise | 512 | 19x19 |
Conv2d_13_pointwise | 1024 | 10x10 |
Conv2d_13_pointwise_2_Conv2d_2_3x3_s2_512 | 512 | 5x5 |
Conv2d_13_pointwise_2_Conv2d_3_3x3_s2_256 | 256 | 3x3 |
Conv2d_13_pointwise_2_Conv2d_4_3x3_s2_256 | 256 | 2x2 |
Conv2d_13_pointwise_2_Conv2d_5_3x3_s2_128 | 128 | 1x1 |
2. anchor生成
models/research/object_detection/anchor_generators/multiple_grid_anchor_generator.py
2.1 config文件中anchor配置如下
anchor_generator {
ssd_anchor_generator {
num_layers: 6
min_scale: 0.2
max_scale: 0.95
aspect_ratios: 1.0
aspect_ratios: 2.0
aspect_ratios: 0.5
aspect_ratios: 3.0
aspect_ratios: 0.3333
}
anchor生成中有这个限制条件没弄明白,为什么需要feature map的数量与每个位置anchor的数量相同?
if self.check_num_anchors and (
len(feature_map_shape_list) != len(self.num_anchors_per_location())):
raise ValueError('Number of feature maps is expected to equal the length '
'of `num_anchors_per_location`.')
2.2 anchor 实现
2.2.1 anchor builder实现
research/object_detection/anchor_generators/multiple_grid_anchor_generator.py
create_ssd_anchors,返回一个MultipleGridAnchorGenerator对象。主要实现box_specs_list
for layer, scale, scale_next in zip(range(num_layers), scales[:-1], scales[1:]):
layer_box_specs = []
if layer == 0 and reduce_boxes_in_lowest_layer:
layer_box_specs = [(0.1, 1.0), (scale, 2.0), (scale, 0.5)]
else:
for aspect_ratio in aspect_ratios:
layer_box_specs.append((scale, aspect_ratio))
# Add one more anchor, with a scale between the current scale, and the