示例#1
0
    def __init__(self, cfg):
        super(DeeplabV3Plus, self).__init__()
        self.backbone = resnet50_atrous(pretrained=True, os=cfg.OUTPUT_STRIDE)
        input_channel = 2048
        self.aspp = ASPP(in_chans=input_channel, out_chans=cfg.ASPP_OUTDIM, rate=16//cfg.OUTPUT_STRIDE)
        self.dropout1 = nn.Dropout(0.5)
        self.upsample4 = nn.UpsamplingBilinear2d(scale_factor=4)
        self.upsample_sub = nn.UpsamplingBilinear2d(scale_factor=cfg.OUTPUT_STRIDE//4)

        indim = 256
        self.shortcut_conv = nn.Sequential(
                nn.Conv2d(indim, cfg.SHORTCUT_DIM, cfg.SHORTCUT_KERNEL, 1, padding=cfg.SHORTCUT_KERNEL//2,bias=False),
                nn.BatchNorm2d(cfg.SHORTCUT_DIM),
                nn.ReLU(inplace=True),
        )
        self.cat_conv = nn.Sequential(
                nn.Conv2d(cfg.ASPP_OUTDIM+cfg.SHORTCUT_DIM, cfg.ASPP_OUTDIM, 3, 1, padding=1,bias=False),
                nn.BatchNorm2d(cfg.ASPP_OUTDIM),
                nn.ReLU(inplace=True),
                nn.Dropout(0.5),
                nn.Conv2d(cfg.ASPP_OUTDIM, cfg.ASPP_OUTDIM, 3, 1, padding=1, bias=False),
                nn.BatchNorm2d(cfg.ASPP_OUTDIM),
                nn.ReLU(inplace=True),
                nn.Dropout(0.1),
        )
        self.cls_conv = nn.Conv2d(cfg.ASPP_OUTDIM, cfg.NUM_CLASSES, 1, 1, padding=0)
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
示例#2
0
    def __init__(self, cfg):
        super(DeeplabV3Plus, self).__init__()
        # 定义DeeplavV3Plus的backbone使用res50_atrous 返回值是outstride=2, 4, 8, 1 时不同layers下的x list
        self.backbone = resnet50_atrous(pretrained=True, os=cfg.OUTPUT_STRIDE)
        input_channel = 2048
        # 定义aspp, 默认参数为:ASPP(2048, 256, 1)
        self.aspp = ASPP(in_chans=input_channel,
                         out_chans=cfg.ASPP_OUTDIM,
                         rate=16 // cfg.OUTPUT_STRIDE)
        # 定义droput
        self.dropout1 = nn.Dropout(0.5)
        # 定义双线性插值上采样UpsamplingBilinear2d,默认参数为4倍
        self.upsample4 = nn.UpsamplingBilinear2d(scale_factor=4)
        self.upsample_sub = nn.UpsamplingBilinear2d(
            scale_factor=cfg.OUTPUT_STRIDE // 4)

        indim = 256
        # 定义shortcut_conv(Sequential操作),nn.Conv2d(256, 48, 1, 1, 0, bias=False)+bn+relu
        self.shortcut_conv = nn.Sequential(
            nn.Conv2d(indim,
                      cfg.SHORTCUT_DIM,
                      cfg.SHORTCUT_KERNEL,
                      1,
                      padding=cfg.SHORTCUT_KERNEL // 2,
                      bias=False),
            nn.BatchNorm2d(cfg.SHORTCUT_DIM),
            nn.ReLU(inplace=True),
        )
        # 定义cat_conv(Sequential操作),nn.Conv2d(256+48, 256, 3, 1, 1, bias=False)+bn+relu+dropout+...
        # nn.Conv2d(256, 256, 3, 1, 1, bias=False)+bn+relu+dropout
        self.cat_conv = nn.Sequential(
            nn.Conv2d(cfg.ASPP_OUTDIM + cfg.SHORTCUT_DIM,
                      cfg.ASPP_OUTDIM,
                      3,
                      1,
                      padding=1,
                      bias=False),
            nn.BatchNorm2d(cfg.ASPP_OUTDIM),
            nn.ReLU(inplace=True),
            nn.Dropout(0.5),
            nn.Conv2d(cfg.ASPP_OUTDIM,
                      cfg.ASPP_OUTDIM,
                      3,
                      1,
                      padding=1,
                      bias=False),
            nn.BatchNorm2d(cfg.ASPP_OUTDIM),
            nn.ReLU(inplace=True),
            nn.Dropout(0.1),
        )
        # 定义cls.conv = nn.Conv2d(256, 8, 1, 1, 0)
        self.cls_conv = nn.Conv2d(cfg.ASPP_OUTDIM,
                                  cfg.NUM_CLASSES,
                                  1,
                                  1,
                                  padding=0)

        # 每层进行kaiming初始化
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight,
                                        mode='fan_out',
                                        nonlinearity='relu')
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)