mmDetection源码分析(四):Faster R-CNN模块解读(二)

Faster R-CNN模块解读(二) Backbone的构建

上一篇博客介绍了检测器的构建,是通过build_from_cfg(cfg.model, DETECTORS, dict(train_cfg=train_cfg, test_cfg=test_cfg))调用得到,而构建检测器时,必须要构建Backbone,在TwoStageDetector类初始化时,就需要构建self.backbone = builder.build_backbone(backbone)。而在

mmdetection/mmdet/models/builder.py

1
2
3
4
5
6
7
8
9
10
11
12
13

def build(cfg, registry, default_args=None):
if isinstance(cfg, list):
modules = [
build_from_cfg(cfg_, registry, default_args) for cfg_ in cfg
]
return nn.Sequential(*modules)
else:
return build_from_cfg(cfg, registry, default_args)


def build_backbone(cfg):
return build(cfg, BACKBONES)

可以看到同样是调用build_from_cfg(cfg.model.backbone, BACKBONES, dict(train_cfg=train_cfg, test_cfg=test_cfg))来构建Backbone的。

mmdetection/configs/faster_rcnn_r50_fpn_1x.py中,backbone的字典参数如下:

1
2
3
4
5
6
7
backbone=dict(
type='ResNet',
depth=50,
num_stages=4,
out_indices=(0, 1, 2, 3),
frozen_stages=1,
style='pytorch'),

此处type为ResNet,则将会去构造mmdetection/mmdet/models/backbones/resnet.py这个类。

该类则是大名鼎鼎的ResNet,下面将会与ResNet论文 中的网络结构一起来学习ResNet的实现。主要介绍ResNet-18与ResNet-50。

对网络结构不是很熟悉的同学,可能还要借助caffe的prototxt可视化一起,会有更深层的理解,也会对编写该代码的作者更加崇拜。

ResNet-18 prototxt:https://raw.githubusercontent.com/marvis/pytorch-caffe-darknet-convert/master/cfg/resnet-18.prototxt

可视化网页:
https://ethereon.github.io/netscope/#/editor

将prototxt复制进左边的输入框,shift+enter即可以看到可视化的网络结构。

ResNet-50,何恺明大大已经可视化了:http://ethereon.github.io/netscope/#/gist/db945b393d40bfa26006

准备工作做好后,我们便正式开始学习Backbone(ResNet)的构建了。

先跳过BasicBlockBottleneck这两个类,看ResNet这个类。

__init__函数之前,

1
2
3
4
5
6
7
arch_settings = {
18: (BasicBlock, (2, 2, 2, 2)),
34: (BasicBlock, (3, 4, 6, 3)),
50: (Bottleneck, (3, 4, 6, 3)),
101: (Bottleneck, (3, 4, 23, 3)),
152: (Bottleneck, (3, 8, 36, 3))
}

代码中很清晰的将不同层数的ResNet的区别,以及不同层数的ResNet的配置都一一告知。

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

def __init__(self,
depth,
num_stages=4,
strides=(1, 2, 2, 2),
dilations=(1, 1, 1, 1),
out_indices=(0, 1, 2, 3),
style='pytorch',
frozen_stages=-1,
conv_cfg=None,
norm_cfg=dict(type='BN', requires_grad=True),
norm_eval=True,
dcn=None,
stage_with_dcn=(False, False, False, False),
gcb=None,
stage_with_gcb=(False, False, False, False),
gen_attention=None,
stage_with_gen_attention=((), (), (), ()),
with_cp=False,
zero_init_residual=True):

super(ResNet, self).__init__()
if depth not in self.arch_settings:
raise KeyError('invalid depth {} for resnet'.format(depth))
self.depth = depth
self.num_stages = num_stages
assert num_stages >= 1 and num_stages <= 4
self.strides = strides
self.dilations = dilations
assert len(strides) == len(dilations) == num_stages
self.out_indices = out_indices
assert max(out_indices) < num_stages
self.style = style
self.frozen_stages = frozen_stages
self.conv_cfg = conv_cfg
self.norm_cfg = norm_cfg
self.with_cp = with_cp
self.norm_eval = norm_eval
self.dcn = dcn
self.stage_with_dcn = stage_with_dcn
if dcn is not None:
assert len(stage_with_dcn) == num_stages
self.gen_attention = gen_attention
self.gcb = gcb
self.stage_with_gcb = stage_with_gcb
if gcb is not None:
assert len(stage_with_gcb) == num_stages
self.zero_init_residual = zero_init_residual
self.block, stage_blocks = self.arch_settings[depth]
self.stage_blocks = stage_blocks[:num_stages]
self.inplanes = 64

self._make_stem_layer()

self.res_layers = []
for i, num_blocks in enumerate(self.stage_blocks):
stride = strides[i]
dilation = dilations[i]
dcn = self.dcn if self.stage_with_dcn[i] else None
gcb = self.gcb if self.stage_with_gcb[i] else None
planes = 64 * 2**i
res_layer = make_res_layer(
self.block,
self.inplanes,
planes,
num_blocks,
stride=stride,
dilation=dilation,
style=self.style,
with_cp=with_cp,
conv_cfg=conv_cfg,
norm_cfg=norm_cfg,
dcn=dcn,
gcb=gcb,
gen_attention=gen_attention,
gen_attention_blocks=stage_with_gen_attention[i])
self.inplanes = planes * self.block.expansion
layer_name = 'layer{}'.format(i + 1)
self.add_module(layer_name, res_layer)
self.res_layers.append(layer_name)

self._freeze_stages()

self.feat_dim = self.block.expansion * 64 * 2**(
len(self.stage_blocks) - 1)

__init__函数中,我们可以看到,首先是将一系列的卷积层的变量传递到该类当中,比如:stride(步长)dilation(扩张率)等,之后调用_make_stem_layer()

1
2
3
4
5
6
7
8
9
10
11
12
13
def _make_stem_layer(self):
self.conv1 = build_conv_layer(
self.conv_cfg,
3,
64,
kernel_size=7,
stride=2,
padding=3,
bias=False)
self.norm1_name, norm1 = build_norm_layer(self.norm_cfg, 64, postfix=1)
self.add_module(self.norm1_name, norm1)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

从这个类,我们可以看到定义了conv1是一个7x7、步长为2,padding为3,输入channel为3,输出的channel为64,之后再接BatchNorm层与ReLU层,最后接一个kernel_size为3,步长为2的maxPooling 层

可以看到与可视化的第一部分一致,经过conv1与maxPooling之后,Feature map的尺寸变成原来的1/4。

之后便是一个循环,构造每个stage的卷积层。通过调用make_res_layer,构造每个stage的卷积层。make_res_layer有个参数block,用于指定使用的是BasicBlock还是Bottleneck。通过__init__中的
self.block, stage_blocks = self.arch_settings[depth]设置,可以看到ResNet-18使用的是BasicBlock,而ResNet-50使用的是Bottleneck。两者的区别可以从图1不同层数的ResNet结构图看出,BasicBlock是堆叠了两个3x3的卷积核,Bottleneck则是1x1->3x3->1x1。具体结构如下图:

通过代码,我们同样可以看出区别。

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
class BasicBlock(nn.Module):
expansion = 1

self.norm1_name, norm1 = build_norm_layer(norm_cfg, planes, postfix=1)
self.norm2_name, norm2 = build_norm_layer(norm_cfg, planes, postfix=2)

self.conv1 = build_conv_layer(
conv_cfg,
inplanes,
planes,
3,
stride=stride,
padding=dilation,
dilation=dilation,
bias=False)
self.add_module(self.norm1_name, norm1)
self.conv2 = build_conv_layer(
conv_cfg, planes, planes, 3, padding=1, bias=False)
self.add_module(self.norm2_name, norm2)

self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
self.dilation = dilation
assert not with_cp

Bottleneck,此处的代码忽略了一些判断,dcn,gcb等。

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
class Bottleneck(nn.Module):
expansion = 4

self.conv1 = build_conv_layer(
conv_cfg,
inplanes,
planes,
kernel_size=1,
stride=self.conv1_stride,
bias=False)
self.add_module(self.norm1_name, norm1)
fallback_on_stride = False
self.with_modulated_dcn = False
if self.with_dcn:
fallback_on_stride = dcn.get('fallback_on_stride', False)
self.with_modulated_dcn = dcn.get('modulated', False)
if not self.with_dcn or fallback_on_stride:
self.conv2 = build_conv_layer(
conv_cfg,
planes,
planes,
kernel_size=3,
stride=self.conv2_stride,
padding=dilation,
dilation=dilation,
bias=False)
self.conv3 = build_conv_layer(
conv_cfg,
planes,
planes * self.expansion,
kernel_size=1,
bias=False)
self.add_module(self.norm3_name, norm3)

self.relu = nn.ReLU(inplace=True)
self.downsample = downsample

简单了解了下BasicBlockBottleneck之后,我们来看下make_res_layer

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
def make_res_layer(block,
inplanes,
planes,
blocks,
stride=1,
dilation=1,
style='pytorch',
with_cp=False,
conv_cfg=None,
norm_cfg=dict(type='BN'),
dcn=None,
gcb=None,
gen_attention=None,
gen_attention_blocks=[]):

downsample = None
if stride != 1 or inplanes != planes * block.expansion:
downsample = nn.Sequential(
build_conv_layer(
conv_cfg,
inplanes,
planes * block.expansion,
kernel_size=1,
stride=stride,
bias=False),
build_norm_layer(norm_cfg, planes * block.expansion)[1],
)

layers = []
layers.append(
block(
inplanes=inplanes,
planes=planes,
stride=stride,
dilation=dilation,
downsample=downsample,
style=style,
with_cp=with_cp,
conv_cfg=conv_cfg,
norm_cfg=norm_cfg,
dcn=dcn,
gcb=gcb,
gen_attention=gen_attention if
(0 in gen_attention_blocks) else None))
inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(
block(
inplanes=inplanes,
planes=planes,
stride=1,
dilation=dilation,
style=style,
with_cp=with_cp,
conv_cfg=conv_cfg,
norm_cfg=norm_cfg,
dcn=dcn,
gcb=gcb,
gen_attention=gen_attention if
(i in gen_attention_blocks) else None))

return nn.Sequential(*layers)

可以看到若stride不等于1或者输入的Feature map的channel不等于64 × expansion(BasicBlock.expansion = 1,Bottleneck.expansion = 4),则downsample设置成1x1x(64 × expansion),在可视化部分如图所示,downsample便指的是这一块的卷积层。

shortcut的操作(恒等映射)在BasicBlockBottleneckforward函数中。

class BasicBlock(nn.Module):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

def forward(self, x):
identity = x

out = self.conv1(x)
out = self.norm1(out)
out = self.relu(out)

out = self.conv2(out)
out = self.norm2(out)

if self.downsample is not None:
identity = self.downsample(x)

out += identity
out = self.relu(out)

return out

class Bottleneck(nn.Module):

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
def forward(self, x):

def _inner_forward(x):
identity = x

out = self.conv1(x)
out = self.norm1(out)
out = self.relu(out)

if not self.with_dcn:
out = self.conv2(out)
elif self.with_modulated_dcn:
offset_mask = self.conv2_offset(out)
offset = offset_mask[:, :18, :, :]
mask = offset_mask[:, -9:, :, :].sigmoid()
out = self.conv2(out, offset, mask)
else:
offset = self.conv2_offset(out)
out = self.conv2(out, offset)
out = self.norm2(out)
out = self.relu(out)

if self.with_gen_attention:
out = self.gen_attention_block(out)

out = self.conv3(out)
out = self.norm3(out)

if self.with_gcb:
out = self.context_block(out)

if self.downsample is not None:
identity = self.downsample(x)

out += identity

return out

if self.with_cp and x.requires_grad:
out = cp.checkpoint(_inner_forward, x)
else:
out = _inner_forward(x)

out = self.relu(out)

return out

需要注意的是在每个stage,只有第一次才会传入downsample,之后是不传入的。这和ResNet的可视化结果一致。ResNet-50第一个stage也确实是堆叠了3个Bottleneck,与代码中也一致。

此外还需要说明的是在每个stage之后channel数会发生变化。

另外,在resnet.py中还有init_weightsforwardtrain函数。

  • init_weightstwo_stage.py被调用,将预训练权重加载到backbone中。

  • forward函数则是在two_stage.py中的extract_feat(img)中通过self.backbone(img)调用,这是因为ResNet类继承至nn.Module,而在nn.Module中实现了__call__,在nn.Module的__call__result = self.forward(*input, **kwargs),因此可以通过self.backbone(img)直接调用self.backbone.forward(img)

在本篇博客中,我们详细介绍了mmDetection框架中backbone的构建,此处以ResNet为例,结合论文、caffe的prototxt的可视化和代码,一起讲述了ResNet网络的构建过程。