SSD在tensorflow models下实现的代码解析

声明:本文禁止转载

模型定义代码

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.pymulti_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
        
  • 10
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值