def create_block_list(self):
        self.block_out_channels = []
        self.index = 0

        c1_channels = 24
        c4_channels = 320

        backbone = self.factory.get_base_model(BackboneName.MobileNetV2_1_0)
        base_out_channels = backbone.get_outchannel_list()
        self.add_block_list(BlockType.BaseNet, backbone, base_out_channels[-1])

        aspp = ASPP(c4_channels, 256, bn_name=self.bn_name,
                    activation_name=self.activation_name)
        out_channel = 256
        self.add_block_list(aspp.get_name(), aspp, out_channel)

        up1 = Upsample(scale_factor=8, mode='bilinear')
        self.add_block_list(up1.get_name(), up1, self.block_out_channels[-1])

        layer1 = RouteLayer('3')
        output_channel = sum([base_out_channels[i] if i >= 0
                              else self.block_out_channels[i] for i in layer1.layers])
        self.add_block_list(layer1.get_name(), layer1, output_channel)

        conv1 = ConvBNActivationBlock(in_channels=c1_channels,
                                      out_channels=48,
                                      kernel_size=1,
                                      bias=False,
                                      bnName=self.bn_name,
                                      activationName=self.activation_name)
        self.add_block_list(conv1.get_name(), conv1, 48)

        layer2 = RouteLayer('-3, -1')
        output_channel = sum([base_out_channels[i] if i >= 0
                              else self.block_out_channels[i] for i in layer2.layers])
        self.add_block_list(layer2.get_name(), layer2, output_channel)

        block1 = SeparableConv2dBNActivation(output_channel, 256, 3, relu_first=False,
                                             bn_name=self.bn_name,
                                             activation_name=self.activation_name)
        self.add_block_list(block1.get_name(), block1, 256)

        block2 = SeparableConv2dBNActivation(256, 256, 3, relu_first=False,
                                             bn_name=self.bn_name,
                                             activation_name=self.activation_name)
        self.add_block_list(block2.get_name(), block2, 256)

        conv2 = nn.Conv2d(256, self.class_number, 1)
        self.add_block_list(LayerType.Convolutional, conv2, self.class_number)

        layer3 = Upsample(scale_factor=4, mode='bilinear')
        self.add_block_list(layer3.get_name(), layer3, self.block_out_channels[-1])

        self.create_loss()
Пример #2
0
    def dsn_head(self, base_out_channels):
        route = RouteLayer('14')
        output_channel = sum([
            base_out_channels[i] if i >= 0 else self.block_out_channels[i]
            for i in route.layers
        ])
        self.add_block_list(route.get_name(), route, output_channel)

        conv1 = ConvBNActivationBlock(in_channels=output_channel,
                                      out_channels=512,
                                      kernel_size=3,
                                      stride=1,
                                      padding=1,
                                      bias=True,
                                      bnName=self.bn_name,
                                      activationName=self.activation_name)
        self.add_block_list(conv1.get_name(), conv1, 512)

        dropout = nn.Dropout2d(0.1)
        self.add_block_list(LayerType.Dropout, dropout,
                            self.block_out_channels[-1])

        out_conv = nn.Conv2d(512,
                             self.class_number,
                             kernel_size=1,
                             stride=1,
                             padding=0,
                             bias=True)
        self.add_block_list(LayerType.Convolutional, out_conv,
                            self.class_number)

        up1 = Upsample(scale_factor=16, mode='bilinear')
        self.add_block_list(up1.get_name(), up1, self.block_out_channels[-1])
Пример #3
0
    def create_block_list(self):
        self.clear_list()
        self.lossList = []

        backbone = self.factory.get_base_model(BackboneName.ResNet50)
        base_out_channels = backbone.get_outchannel_list()
        self.add_block_list(BlockType.BaseNet, backbone, base_out_channels[-1])

        head = ASPPBlock(2048,
                         bn_name=self.bn_name,
                         activation_name=self.activation_name)
        self.add_block_list(head.get_name(), head, 512)

        out_conv = nn.Conv2d(512,
                             self.class_number,
                             kernel_size=1,
                             stride=1,
                             padding=0,
                             bias=True)
        self.add_block_list(LayerType.Convolutional, out_conv,
                            self.class_number)

        up = Upsample(scale_factor=32, mode='bilinear')
        self.add_block_list(up.get_name(), up, self.block_out_channels[-1])

        self.loss_flag = 0
        self.clear_list()

        self.dsn_head(base_out_channels)

        self.loss_flag = 1
        self.clear_list()
Пример #4
0
    def create_block_list(self):
        self.clear_list()

        if (self.scale < 0.99) or (self.scale > 1.001):
            up = Upsample(scale_factor=self.scale, mode='nearest')
            self.add_block_list(up.get_name(), up, self.data_channel)

        stage1 = ConvBNActivationBlock(in_channels=self.data_channel,
                                       out_channels=self.out_channels[0],
                                       kernel_size=3,
                                       stride=2,
                                       padding=1,
                                       bias=False,
                                       bnName=self.bn_name,
                                       activationName=self.activation_name)
        self.add_block_list(stage1.get_name(), stage1, self.out_channels[0])

        for index, number in enumerate(self.repeats):
            self.make_layers(number, self.out_channels[index],
                             self.out_channels[index + 1],
                             self.kernel_sizes[index], self.strides[index],
                             self.expands[index])
            activate = ActivationLayer(activation_name=self.activation_name)
            self.add_block_list(activate.get_name(), activate,
                                self.block_out_channels[-1])

        stage9 = ConvBNActivationBlock(in_channels=self.out_channels[7],
                                       out_channels=self.out_channels[8],
                                       kernel_size=1,
                                       stride=1,
                                       padding=0,
                                       bias=False,
                                       bnName=self.bn_name,
                                       activationName=self.activation_name)
        self.add_block_list(stage9.get_name(), stage9, self.out_channels[8])
Пример #5
0
    def create_head(self):
        cff_24 = CascadeFeatureFusion(2048, 512, 128, 7, bn_name=self.bn_name,
                                      activation_name=self.activation_name)
        self.add_block_list(cff_24.get_name(), cff_24, 128)

        cff_12 = CascadeFeatureFusion(128, 64, 128, 2, bn_name=self.bn_name,
                                      activation_name=self.activation_name)
        self.add_block_list(cff_12.get_name(), cff_12, 128)

        up1 = Upsample(scale_factor=2, mode='bilinear')
        self.add_block_list(up1.get_name(), up1, 128)

        conv_cls = nn.Conv2d(128, self.class_number, 1, bias=False)
        self.add_block_list(LayerType.Convolutional, conv_cls, self.class_number)

        up2 = Upsample(scale_factor=4, mode='bilinear')
        self.add_block_list(up2.get_name(), up2, self.class_number)
    def make_layer(self, base_out_channels, conv_output_channel, scale_factor, route_layer_indexs):
        layer1 = Upsample(scale_factor=scale_factor, mode='bilinear')
        self.add_block_list(layer1.get_name(), layer1, self.block_out_channels[-1])

        layer2 = RouteLayer(route_layer_indexs)
        output_channel = sum([base_out_channels[i] if i >= 0
                              else self.block_out_channels[i] for i in layer2.layers])
        self.add_block_list(layer2.get_name(), layer2, output_channel)

        conv1 = ConvBNActivationBlock(self.block_out_channels[-1], conv_output_channel,
                                      kernel_size=1, stride=1, padding=0,
                                      bnName=self.bn_name, activationName=self.activation_name)
        self.add_block_list(conv1.get_name(), conv1, conv_output_channel)

        layer3 = RouteLayer('-1,-3')
        output_channel = sum([base_out_channels[i] if i >= 0 else
                              self.block_out_channels[i] for i in layer3.layers])
        self.add_block_list(layer3.get_name(), layer3, output_channel)
Пример #7
0
    def create_block_list(self):
        self.clear_list()

        backbone = self.factory.get_base_model(BackboneName.ResNet101)
        base_out_channels = backbone.get_outchannel_list()
        self.add_block_list(BlockType.BaseNet, backbone, base_out_channels[-1])

        if self.is_jpu:
            jup = JPUBlock(layers='4,8,31,34',
                           in_planes=(512, 1024, 2048),
                           width=512,
                           bn_name=self.bn_name,
                           activation_name=self.activation_name)
            self.add_block_list(jup.get_name(), jup, 512 + 512 + 512 + 512)
            scale_factor = 8
        else:
            scale_factor = 32

        psp = PyramidPooling(2048,
                             bn_name=self.bn_name,
                             activation_name=self.activation_name)
        self.add_block_list(psp.get_name(), psp, 2048 * 2)

        conv1 = ConvBNActivationBlock(in_channels=2048 * 2,
                                      out_channels=512,
                                      kernel_size=3,
                                      padding=1,
                                      bias=False,
                                      bnName=self.bn_name,
                                      activationName=self.activation_name)
        self.add_block_list(conv1.get_name(), conv1, 512)

        dropout = nn.Dropout(0.1)
        self.add_block_list(LayerType.Dropout, dropout,
                            self.block_out_channels[-1])

        conv2 = nn.Conv2d(512, self.class_number, 1)
        self.add_block_list(LayerType.Convolutional, conv2, self.class_number)

        layer = Upsample(scale_factor=scale_factor, mode='bilinear')
        self.add_block_list(layer.get_name(), layer,
                            self.block_out_channels[-1])

        self.create_loss()
    def create_block_list(self):
        self.block_out_channels = []
        self.index = 0

        basic_model = MobileNetV2(bnName=self.bn_name, activationName=self.activation_name)
        base_out_channels = basic_model.get_outchannel_list()
        self.add_block_list(BlockType.BaseNet, basic_model, base_out_channels[-1])

        input_channel = self.block_out_channels[-1]
        output_channel = base_out_channels[-1] // 2
        self.make_conv_block(input_channel, output_channel)

        self.make_layer(base_out_channels, output_channel, 2, '13')

        input_channel = self.block_out_channels[-1]
        output_channel = base_out_channels[-1] // 4
        self.make_conv_block(input_channel, output_channel)

        self.make_layer(base_out_channels, output_channel, 2, '6')

        input_channel = self.block_out_channels[-1]
        output_channel = base_out_channels[-1] // 8
        self.make_conv_block(input_channel, output_channel)

        self.make_layer(base_out_channels, output_channel, 2, '3')

        input_channel = self.block_out_channels[-1]
        output_channel = base_out_channels[-1] // 16
        self.make_conv_block(input_channel, output_channel)

        input_channel = self.block_out_channels[-1]
        output_channel = self.class_number
        conv4 = ConvActivationBlock(input_channel, output_channel,
                                    kernel_size=1, stride=1, padding=0,
                                    activationName=ActivationType.Linear)
        self.add_block_list(conv4.get_name(), conv4, output_channel)

        layer10 = Upsample(scale_factor=4, mode='bilinear')
        self.add_block_list(layer10.get_name(), layer10, self.block_out_channels[-1])

        self.create_loss()
Пример #9
0
    def create_block_list(self):
        self.block_out_channels = []
        self.index = 0

        self.learning_to_downsample()

        global_feature_extractor = GlobalFeatureExtractor(64, [64, 96, 128],
                                                          128, 6, [3, 3, 3],
                                                          bn_name=self.bn_name,
                                                          activation_name=self.activation_name)
        self.add_block_list(global_feature_extractor.get_name(), global_feature_extractor, 128)

        feature_fusion = FeatureFusionBlock(64, 128, 128,
                                            bn_name=self.bn_name,
                                            activation_name=self.activation_name)
        self.add_block_list(feature_fusion.get_name(), feature_fusion, 128)

        self.classifer(128)

        layer = Upsample(scale_factor=8, mode='bilinear')
        self.add_block_list(layer.get_name(), layer, self.block_out_channels[-1])

        self.create_loss()
Пример #10
0
    def create_block_list(self):
        self.clear_list()

        spatial_path = SpatialPath(self.data_channel,
                                   128,
                                   bn_name=self.bn_name,
                                   activation_name=self.activation_name)
        self.add_block_list(spatial_path.get_name(), spatial_path, 128)

        backbone = self.factory.get_base_model(BackboneName.ResNet18)
        base_out_channels = backbone.get_outchannel_list()
        self.add_block_list(BlockType.BaseNet, backbone, base_out_channels[-1])

        inter_channels = 128
        global_context = GlobalAvgPooling(512,
                                          inter_channels,
                                          bn_name=self.bn_name,
                                          activation_name=self.activation_name)
        self.add_block_list(global_context.get_name(), global_context,
                            base_out_channels[-1])

        layer1 = RouteLayer('9')
        output_channel = sum([
            base_out_channels[i] if i >= 0 else self.block_out_channels[i]
            for i in layer1.layers
        ])
        self.add_block_list(layer1.get_name(), layer1, output_channel)

        context_path1 = ContextPath(512,
                                    inter_channels,
                                    bn_name=self.bn_name,
                                    activation_name=self.activation_name)
        self.add_block_list(context_path1.get_name(), context_path1,
                            inter_channels)

        layer2 = RouteLayer('7')
        output_channel = sum([
            base_out_channels[i] if i >= 0 else self.block_out_channels[i]
            for i in layer2.layers
        ])
        self.add_block_list(layer2.get_name(), layer2, output_channel)

        context_path2 = ContextPath(256,
                                    inter_channels,
                                    bn_name=self.bn_name,
                                    activation_name=self.activation_name)
        self.add_block_list(context_path2.get_name(), context_path2,
                            inter_channels)

        ffm = FeatureFusionBlock(256,
                                 256,
                                 4,
                                 bn_name=self.bn_name,
                                 activation_name=self.activation_name)
        self.add_block_list(ffm.get_name(), ffm, 256)

        conv1 = ConvBNActivationBlock(in_channels=256,
                                      out_channels=64,
                                      kernel_size=3,
                                      padding=1,
                                      bias=False,
                                      bnName=self.bn_name,
                                      activationName=self.activation_name)
        self.add_block_list(conv1.get_name(), conv1, 64)

        dropout = nn.Dropout(0.1)
        self.add_block_list(LayerType.Dropout, dropout,
                            self.block_out_channels[-1])

        conv2 = nn.Conv2d(64, self.class_number, 1)
        self.add_block_list(LayerType.Convolutional, conv2, self.class_number)

        layer = Upsample(scale_factor=8, mode='bilinear')
        self.add_block_list(layer.get_name(), layer,
                            self.block_out_channels[-1])

        self.create_loss()
Пример #11
0
    def enc_head(self, in_channels, base_out_channels):
        if self.is_jpu:
            conv1 = ConvBNActivationBlock(in_channels=in_channels,
                                          out_channels=512,
                                          kernel_size=1,
                                          bias=False,
                                          bnName=self.bn_name,
                                          activationName=self.activation_name)
            self.add_block_list(conv1.get_name(), conv1, 512)
        else:
            conv1 = ConvBNActivationBlock(in_channels=in_channels,
                                          out_channels=512,
                                          kernel_size=3,
                                          padding=1,
                                          bias=False,
                                          bnName=self.bn_name,
                                          activationName=self.activation_name)
            self.add_block_list(conv1.get_name(), conv1, 512)

        if self.lateral:
            route1 = RouteLayer('8')
            output_channel = sum([
                base_out_channels[i] if i >= 0 else self.block_out_channels[i]
                for i in route1.layers
            ])
            self.add_block_list(route1.get_name(), route1, output_channel)

            connect1 = ConvBNActivationBlock(
                in_channels=output_channel,
                out_channels=512,
                kernel_size=1,
                bias=False,
                bnName=self.bn_name,
                activationName=self.activation_name)
            self.add_block_list(connect1.get_name(), connect1, 512)

            route2 = RouteLayer('14')
            output_channel = sum([
                base_out_channels[i] if i >= 0 else self.block_out_channels[i]
                for i in route2.layers
            ])
            self.add_block_list(route2.get_name(), route2, output_channel)

            connect2 = ConvBNActivationBlock(
                in_channels=output_channel,
                out_channels=512,
                kernel_size=1,
                bias=False,
                bnName=self.bn_name,
                activationName=self.activation_name)
            self.add_block_list(connect2.get_name(), connect2, 512)

            route3 = RouteLayer('-5,-3,-1')
            output_channel = sum([
                base_out_channels[i] if i >= 0 else self.block_out_channels[i]
                for i in route2.layers
            ])
            self.add_block_list(route3.get_name(), route3, output_channel)

            fusion = ConvBNActivationBlock(in_channels=output_channel,
                                           out_channels=512,
                                           kernel_size=3,
                                           padding=1,
                                           bias=False,
                                           bnName=self.bn_name,
                                           activationName=self.activation_name)
            self.add_block_list(fusion.get_name(), fusion, 512)

        encmodule = EncBlock(in_channels=512,
                             nclass=self.class_number,
                             se_loss=self.is_se_loss,
                             bn_name=self.bn_name,
                             activation_name=self.activation_name)
        self.add_block_list(encmodule.get_name(), encmodule, 512)

        dropout = nn.Dropout2d(0.1, False)
        self.add_block_list(LayerType.Dropout, dropout,
                            self.block_out_channels[-1])

        conv2 = nn.Conv2d(self.block_out_channels[-1], self.class_number, 1)
        self.add_block_list(LayerType.Convolutional, conv2, self.class_number)

        up = Upsample(scale_factor=8, mode='bilinear')
        self.add_block_list(up.get_name(), up, self.class_number)
Пример #12
0
    def create_block_list(self):
        self.block_out_channels = []
        self.index = 0

        backbone = self.factory.get_base_model(BackboneName.ResNet101)
        base_out_channels = backbone.get_outchannel_list()
        self.add_block_list(BlockType.BaseNet, backbone, base_out_channels[-1])

        dropout1 = nn.Dropout(p=0.5)
        self.add_block_list(LayerType.Dropout, dropout1,
                            self.block_out_channels[-1])

        layer1 = RouteLayer('31')
        output_channel = sum([
            base_out_channels[i] if i >= 0 else self.block_out_channels[i]
            for i in layer1.layers
        ])
        self.add_block_list(layer1.get_name(), layer1, output_channel)

        dropout2 = nn.Dropout(p=0.5)
        self.add_block_list(LayerType.Dropout, dropout2,
                            self.block_out_channels[-1])

        layer2 = RouteLayer('-3')
        output_channel = sum([
            base_out_channels[i] if i >= 0 else self.block_out_channels[i]
            for i in layer1.layers
        ])
        self.add_block_list(layer2.get_name(), layer2, output_channel)

        conv1 = nn.Conv2d(2048, 512, 1, bias=False)
        self.add_block_list(LayerType.Convolutional, conv1, 512)

        layer3 = RouteLayer('-3')
        output_channel = sum([
            base_out_channels[i] if i >= 0 else self.block_out_channels[i]
            for i in layer1.layers
        ])
        self.add_block_list(layer3.get_name(), layer3, output_channel)

        conv2 = nn.Conv2d(1024, 256, 1, bias=False)
        self.add_block_list(LayerType.Convolutional, conv2, 256)

        refinenet1 = RefineNetBlock(512,
                                    512,
                                    4,
                                    activation_name=self.activation_name)
        output_channel = 256
        self.add_block_list(refinenet1.get_name(), refinenet1, output_channel)

        layer4 = RouteLayer('8')
        output_channel = sum([
            base_out_channels[i] if i >= 0 else self.block_out_channels[i]
            for i in layer1.layers
        ])
        self.add_block_list(layer4.get_name(), layer4, output_channel)

        conv3 = nn.Conv2d(512, 256, 1, bias=False)
        self.add_block_list(LayerType.Convolutional, conv3, 256)

        refinenet2 = RefineNetBlock(256,
                                    256,
                                    4,
                                    activation_name=self.activation_name)
        output_channel = 256
        self.add_block_list(refinenet2.get_name(), refinenet2, output_channel)

        layer5 = RouteLayer('4')
        output_channel = sum([
            base_out_channels[i] if i >= 0 else self.block_out_channels[i]
            for i in layer1.layers
        ])
        self.add_block_list(layer5.get_name(), layer5, output_channel)

        conv4 = nn.Conv2d(256, 256, 1, bias=False)
        self.add_block_list(LayerType.Convolutional, conv4, 256)

        refinenet3 = RefineNetBlock(256,
                                    256,
                                    4,
                                    activation_name=self.activation_name)
        output_channel = 256
        self.add_block_list(refinenet3.get_name(), refinenet3, output_channel)

        crp = CRPBlock(256, 256, 4)
        output_channel = 256
        self.add_block_list(crp.get_name(), crp, output_channel)

        conv5 = nn.Conv2d(256,
                          self.class_number,
                          kernel_size=3,
                          stride=1,
                          padding=1,
                          bias=True)
        self.add_block_list(LayerType.Convolutional, conv5, self.class_number)

        layer6 = Upsample(scale_factor=4, mode='bilinear')
        self.add_block_list(layer6.get_name(), layer6,
                            self.block_out_channels[-1])

        self.create_loss()