因为是resnet50+FPN。这里的backbone.py 主要是先通过resnet.py配合config,我的是生成resnet50的网络,并且由于
"R-50-FPN": ResNet50FPNStagesTo5,又要配合FPN,所以可以看到配置是在index是1~4层,也就是conv2~conv5都作为特征提取层的。如果不配合FPN,只是标准的残差,只要conv5是特征提取层。
# ResNet-50-FPN (including all stages)
ResNet50FPNStagesTo5 = tuple(
StageSpec(index=i, block_count=c, return_features=r)
for (i, c, r) in ((1, 3, True), (2, 4, True), (3, 6, True), (4, 3, True))
)
那么backbone.py就是结合resnet.py和fpn.py 生成backbone。
body = resnet.ResNet(cfg)
fpn = fpn_module.FPN()
最终是用C2~C5来构建FPN,P2~P5。egP2参数是(batchsize,256,1/4,1/4)。并且对C5用LastLevelMaxPool,构建了P6层。它们通道数都是256.
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
from collections import OrderedDict
from torch import nn
from maskrcnn_benchmark.modeling import registry
from maskrcnn_benchmark.modeling.make_layers import conv_with_kaiming_uniform
from . import fpn as fpn_module
from . import resnet
@registry.BACKBONES.register("R-50-C4")
@registry.BACKBONES.register("R-50-C5")
@registry.BACKBONES.register("R-101-C4")
@registry.BACKBONES.register("R-101-C5")
def build_resnet_backbone(cfg):
body = resnet.ResNet(cfg)
model = nn.Sequential(OrderedDict([("body", body)]))
model.out_channels = cfg.MODEL.RESNETS.BACKBONE_OUT_CHANNELS
return model
@registry.BACKBONES.register("R-50-FPN") #here
@registry.BACKBONES.register("R-101-FPN")
@registry.BACKBONES.register("R-152-FPN")
def build_resnet_fpn_backbone(cfg):
body = resnet.ResNet(cfg)
in_channels_stage2 = cfg.MODEL.RESNETS.RES2_OUT_CHANNELS #256 C2:256
out_channels = cfg.MODEL.RESNETS.BACKBONE_OUT_CHANNELS #default 256*4 →256
fpn = fpn_module.FPN(
in_channels_list=[
in_channels_stage2, # C2:256
in_channels_stage2 * 2, # 用C3:512
in_channels_stage2 * 4, # 用C4:1024
in_channels_stage2 * 8, # 用C5:2048
], #这里是用C2~C5来构建FPN,P2~P5.eg(batchsize,256,1/4,1/4)
out_channels=out_channels, #256
conv_block=conv_with_kaiming_uniform(
cfg.MODEL.FPN.USE_GN, cfg.MODEL.FPN.USE_RELU
), #Conv2d same
top_blocks=fpn_module.LastLevelMaxPool(), #如果提供,将对最后一个(最小分辨率)FPN输出的输出执行额外操作,结果将扩展结果列表
#也就是用于MP生成P6层 [F.max_pool2d(x, 1, 2, 0)]
) #四个参数
model = nn.Sequential(OrderedDict([("body", body), ("fpn", fpn)]))
model.out_channels = out_channels
return model
@registry.BACKBONES.register("R-50-FPN-RETINANET")
@registry.BACKBONES.register("R-101-FPN-RETINANET")
def build_resnet_fpn_p3p7_backbone(cfg):
body = resnet.ResNet(cfg)
in_channels_stage2 = cfg.MODEL.RESNETS.RES2_OUT_CHANNELS
out_channels = cfg.MODEL.RESNETS.BACKBONE_OUT_CHANNELS
in_channels_p6p7 = in_channels_stage2 * 8 if cfg.MODEL.RETINANET.USE_C5 \
else out_channels
fpn = fpn_module.FPN(
in_channels_list=[
0,
in_channels_stage2 * 2,
in_channels_stage2 * 4,
in_channels_stage2 * 8,
],
out_channels=out_channels,
conv_block=conv_with_kaiming_uniform(
cfg.MODEL.FPN.USE_GN, cfg.MODEL.FPN.USE_RELU
),
top_blocks=fpn_module.LastLevelP6P7(in_channels_p6p7, out_channels),
)
'''# Sequential使用实例
model = nn.Sequential(
nn.Conv2d(1,20,5),
nn.ReLU(),
nn.Conv2d(20,64,5),
nn.ReLU()
)
# Sequential with OrderedDict使用实例
model = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(1,20,5)),
('relu1', nn.ReLU()),
('conv2', nn.Conv2d(20,64,5)),
('relu2', nn.ReLU())
]))'''
model = nn.Sequential(OrderedDict([("body", body), ("fpn", fpn)]))
model.out_channels = out_channels
print(model)
return model
def build_backbone(cfg):
assert cfg.MODEL.BACKBONE.CONV_BODY in registry.BACKBONES, \
"cfg.MODEL.BACKBONE.CONV_BODY: {} are not registered in registry".format(
cfg.MODEL.BACKBONE.CONV_BODY
)
return registry.BACKBONES[cfg.MODEL.BACKBONE.CONV_BODY](cfg) #"R-50-FPN"
#@registry.BACKBONES.register("R-50-FPN") #here