Exemple #1
0
    def __init__(self, block, layers, n_classes=21):
        super(DRNSegIRB_A, self).__init__()
        self.inplanes = 64
        self.out_dim = 512 * block.expansion
        self.conv1 = nn.Conv2d(3,
                               64,
                               kernel_size=7,
                               stride=2,
                               padding=3,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        for i in self.bn1.parameters():
            i.requires_grad = False
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block,
                                       256,
                                       layers[2],
                                       stride=1,
                                       dilation=2)
        self.layer4 = self._make_layer(block,
                                       512,
                                       layers[3],
                                       stride=1,
                                       dilation=4)
        self.out_conv = nn.Conv2d(self.out_dim, n_classes, kernel_size=1)

        # ----no irb----
        # self.up = nn.UpsamplingBilinear2d(scale_factor=8)
        # ----no irb----

        # ----irb 1----
        self.up = nn.UpsamplingBilinear2d(scale_factor=8)
        self.irbunit_1 = AlignedResInception(512)
        # ----irb 1----

        # ----irb 2----
        # self.refineunit_1 = RefineUnit(64, n_classes)
        # self.refineunit_2 = RefineUnit(64, n_classes)
        # self.up = nn.UpsamplingBilinear2d(scale_factor=2)
        # ----irb 2----

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                m.weight.data.normal_(0, 0.01)
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
Exemple #2
0
    def __init__(self, n_classes=21, pretrained=False):
        super(segnet_alignres, self).__init__()
        self.down1 = segnetDown2(3, 64)
        self.down2 = segnetDown2(64, 128)
        self.down3 = segnetDown3(128, 256)
        self.down4 = segnetDown3(256, 512)
        self.down5 = segnetDown3(512, 512)

        self.alignres = AlignedResInception(512)

        self.up5 = segnetUp3(512, 512)
        self.up4 = segnetUp3(512, 256)
        self.up3 = segnetUp3(256, 128)
        self.up2 = segnetUp2(128, 64)
        self.up1 = segnetUp2(64, n_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal(m.weight, mode='fan_out')
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant(m.weight, 1)
                nn.init.constant(m.bias, 0)

        self.init_weights(pretrained=pretrained)
Exemple #3
0
    def __init__(self,
                 block,
                 layers,
                 num_classes=1000,
                 channels=(16, 32, 64, 128, 256, 512, 512, 512),
                 out_map=False,
                 out_middle=False,
                 pool_size=28,
                 arch='D'):
        super(DRN, self).__init__()
        print(layers)
        self.inplanes = channels[0]
        self.out_map = out_map
        self.out_dim = channels[-1]
        self.out_middle = out_middle
        # 默认架构为arch=D
        self.arch = arch

        # 不同架构主要在构成的网络模块基本组成模块上不同,在C架构上主要由basic block块组成,而其他由conv组成
        if arch == 'C':
            self.conv1 = nn.Conv2d(3,
                                   channels[0],
                                   kernel_size=7,
                                   stride=1,
                                   padding=3,
                                   bias=False)
            self.bn1 = nn.BatchNorm2d(channels[0])
            self.relu = nn.ReLU(inplace=True)

            self.layer1 = self._make_layer(BasicBlock,
                                           channels[0],
                                           layers[0],
                                           stride=1)
            self.layer2 = self._make_layer(BasicBlock,
                                           channels[1],
                                           layers[1],
                                           stride=2)
        elif arch == 'D' or arch == 'E':
            # -7+2*3/1+1=0将channel为3的rgb原始图像数据转换为channels[0]的数据
            self.layer0 = nn.Sequential(
                nn.Conv2d(3,
                          channels[0],
                          kernel_size=7,
                          stride=1,
                          padding=3,
                          bias=False), nn.BatchNorm2d(channels[0]),
                nn.ReLU(inplace=True))

            self.layer1 = self._make_conv_layers(channels[0],
                                                 layers[0],
                                                 stride=1)
            self.layer2 = self._make_conv_layers(channels[1],
                                                 layers[1],
                                                 stride=2)

        self.layer3 = self._make_layer(block, channels[2], layers[2], stride=2)
        self.layer4 = self._make_layer(block, channels[3], layers[3], stride=2)
        self.layer5 = self._make_layer(block,
                                       channels[4],
                                       layers[4],
                                       dilation=2,
                                       new_level=False)
        self.layer6 = None if layers[5] == 0 else self._make_layer(
            block, channels[5], layers[5], dilation=4, new_level=False)

        if arch == 'C':
            self.layer7 = None if layers[6] == 0 else self._make_layer(
                BasicBlock,
                channels[6],
                layers[6],
                dilation=2,
                new_level=False,
                residual=False)
            self.layer8 = None if layers[7] == 0 else self._make_layer(
                BasicBlock,
                channels[7],
                layers[7],
                dilation=1,
                new_level=False,
                residual=False)
        elif arch == 'D' or arch == 'E':
            # 无残差模块
            self.layer7 = None if layers[6] == 0 else self._make_conv_layers(
                channels[6], layers[6], dilation=2)
            self.layer8 = None if layers[7] == 0 else self._make_conv_layers(
                channels[7], layers[7], dilation=1)

        self.layer9 = None
        if arch == 'E':
            # self.layer9 = Inception(512, 128, 128, 256, 24,  64,  64)
            # self.layer9 = ResInception(512, 128, 128, 256, 24,  64,  64)
            # self.layer9 = CascadeResInception()
            # self.layer9 = CascadeAlignedResInception(in_planes=512)
            self.layer9 = AlignedResInception(in_planes=512)

        # 最后的网络输出语义图
        if num_classes > 0:
            self.avgpool = nn.AvgPool2d(pool_size)
            self.fc = nn.Conv2d(self.out_dim,
                                num_classes,
                                kernel_size=1,
                                stride=1,
                                padding=0,
                                bias=True)

        # 网络模块权重和偏置初始化
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
Exemple #4
0
 def __init__(self, in_planes):
     super(CascadeAlignedResInception, self).__init__()
     self.relu = nn.ReLU(inplace=True)
     self.res1 = AlignedResInception(in_planes=in_planes, stride=2)
     self.res2 = AlignedResInception(in_planes=in_planes, stride=7)
     self.res3 = AlignedResInception(in_planes=in_planes, stride=14)