神经网络的Backbone

backbone

backbone这个单词原意指的是人的脊梁骨,后来引申为支柱,核心的意思,在神经网络中,指的是网络的主干网络。

用途

在使用pytorch搭建我们神经网络,特别是在CV领域,一般先对图像进行特征提取(常见的有vggnet,resnet,谷歌的inception),这一部分是整个CV任务的根基,因为后续的下游任务都是基于提取出来的图像特征去做文章(比如分类,生成等等)。

加载预训练模型

我们以pytorch源码中的FCN网络定义来看GITHUB地址,首先FCN是基于ResNet 的,即 FCN的backbone是ResNet ,只不过由于ResNet 最后两层是全连接层,我们需要将最后两层改为全连接层。

直接上代码:

# FCN特征提取部分
def _segm_resnet(name, backbone_name, num_classes, aux,
    pretrained_backbone=True):
    
    # __dict__ 查看对象内部所有属性名和属性值组成的字典
    backbone = resnet.__dict__[backbone_name](
        pretrained=pretrained_backbone,
        replace_stride_with_dilation=[False, True, True])

    # 将 'layer3' 和 'layer4' 名字进行替换
    return_layers = {'layer4': 'out'}
    if aux:
        return_layers['layer3'] = 'aux'   # layer3 用来进行辅助训练,不会用于预测,故命名为 aux
    backbone = IntermediateLayerGetter(backbone, return_layers=return_layers)    # 从模型返回中间层的模块包装器, return_layers 一个dict,key是要输出的feature名字,value是要被封装成的新名字


    aux_classifier = None
    if aux:
        inplanes = 1024
        aux_classifier = FCNHead(inplanes, num_classes)

    model_map = {
        'deeplabv3': (DeepLabHead, DeepLabV3),
        'fcn': (FCNHead, FCN),
    }
    inplanes = 2048
    classifier = model_map[name][0](inplanes, num_classes)
    base_model = model_map[name][1]

    model = base_model(backbone, classifier, aux_classifier)
    return model

可以看到,这里用了一个 return_layers, 将’layer4’改为’out’, ‘layer3’ 改为 ‘aux’,它的model_map如下:

model_map = {
    'deeplabv3': (DeepLabHead, DeepLabV3),
    'fcn': (FCNHead, FCN),
}

即,这里使用了DeepLabHead, DeepLabV3网络,注意:FCNHead, FCN全是自己定义的,如下:

# FCN模块部分,直接继承了 _SimpleSegmentationModel
class FCN(_SimpleSegmentationModel):
    pass

class _SimpleSegmentationModel(nn.Module):
    def __init__(self, backbone, classifier, aux_classifier=None):
        super(_SimpleSegmentationModel, self).__init__()
        self.backbone = backbone
        self.classifier = classifier
        self.aux_classifier = aux_classifier

    def forward(self, x):
        input_shape = x.shape[-2:]
        features = self.backbone(x)

        result = OrderedDict()
        x = features["out"]
        x = self.classifier(x)
        x = F.interpolate(x, size=input_shape, mode='bilinear',
            align_corners=False)
        result["out"] = x

        if self.aux_classifier is not None:
            x = features["aux"]
            x = self.aux_classifier(x)
            x = F.interpolate(x, size=input_shape, mode='bilinear',
                align_corners=False)     # interpolate 上下采样函数
            result["aux"] = x

        return result

# FCNHead部分
class FCNHead(nn.Sequential):
    def __init__(self, in_channels, channels):
        inter_channels = in_channels // 4
        layers = [
            nn.Conv2d(in_channels, inter_channels, 3, padding=1,
                bias=False),
            nn.BatchNorm2d(inter_channels),
            nn.ReLU(),
            nn.Dropout(0.1),
            nn.Conv2d(inter_channels, channels, 1)
        ]

        super(FCNHead, self).__init__(*layers)   # 一个星(*):表示接收的参数作为元组来处理; 两个星(**):表示接收的参数作为字典来处理 

这里FCN一般用于图像的特征提取,但是它的backbone是ResNet.
同理,我们也可以替换为其他的网络,比如vggnet,inception等等。

  • 5
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值