Example #1
0
    def __init__(
            self,
            n_classes=19,
            block_config=[3, 4, 6, 3],
            input_size=(1025, 2049),
            version=None,
            is_batchnorm=True,
    ):

        super(icnet, self).__init__()

        bias = not is_batchnorm

        self.block_config = (icnet_specs[version]["block_config"]
                             if version is not None else block_config)
        self.n_classes = icnet_specs[version][
            "n_classes"] if version is not None else n_classes
        self.input_size = icnet_specs[version][
            "input_size"] if version is not None else input_size

        # Encoder
        self.convbnrelu1_1 = conv2DBatchNormRelu(
            in_channels=3,
            k_size=3,
            n_filters=32,
            padding=1,
            stride=2,
            bias=bias,
            is_batchnorm=is_batchnorm,
        )
        self.convbnrelu1_2 = conv2DBatchNormRelu(
            in_channels=32,
            k_size=3,
            n_filters=32,
            padding=1,
            stride=1,
            bias=bias,
            is_batchnorm=is_batchnorm,
        )
        self.convbnrelu1_3 = conv2DBatchNormRelu(
            in_channels=32,
            k_size=3,
            n_filters=64,
            padding=1,
            stride=1,
            bias=bias,
            is_batchnorm=is_batchnorm,
        )

        # Vanilla Residual Blocks
        self.res_block2 = residualBlockPSP(self.block_config[0],
                                           64,
                                           32,
                                           128,
                                           1,
                                           1,
                                           is_batchnorm=is_batchnorm)
        self.res_block3_conv = residualBlockPSP(
            self.block_config[1],
            128,
            64,
            256,
            2,
            1,
            include_range="conv",
            is_batchnorm=is_batchnorm,
        )
        self.res_block3_identity = residualBlockPSP(
            self.block_config[1],
            128,
            64,
            256,
            2,
            1,
            include_range="identity",
            is_batchnorm=is_batchnorm,
        )

        # Dilated Residual Blocks
        self.res_block4 = residualBlockPSP(self.block_config[2],
                                           256,
                                           128,
                                           512,
                                           1,
                                           2,
                                           is_batchnorm=is_batchnorm)
        self.res_block5 = residualBlockPSP(self.block_config[3],
                                           512,
                                           256,
                                           1024,
                                           1,
                                           4,
                                           is_batchnorm=is_batchnorm)

        # Pyramid Pooling Module
        self.pyramid_pooling = pyramidPooling(1024, [6, 3, 2, 1],
                                              model_name="icnet",
                                              fusion_mode="sum",
                                              is_batchnorm=is_batchnorm)

        # Final conv layer with kernel 1 in sub4 branch
        self.conv5_4_k1 = conv2DBatchNormRelu(
            in_channels=1024,
            k_size=1,
            n_filters=256,
            padding=0,
            stride=1,
            bias=bias,
            is_batchnorm=is_batchnorm,
        )

        # High-resolution (sub1) branch
        self.convbnrelu1_sub1 = conv2DBatchNormRelu(
            in_channels=3,
            k_size=3,
            n_filters=32,
            padding=1,
            stride=2,
            bias=bias,
            is_batchnorm=is_batchnorm,
        )
        self.convbnrelu2_sub1 = conv2DBatchNormRelu(
            in_channels=32,
            k_size=3,
            n_filters=32,
            padding=1,
            stride=2,
            bias=bias,
            is_batchnorm=is_batchnorm,
        )
        self.convbnrelu3_sub1 = conv2DBatchNormRelu(
            in_channels=32,
            k_size=3,
            n_filters=64,
            padding=1,
            stride=2,
            bias=bias,
            is_batchnorm=is_batchnorm,
        )
        self.classification = nn.Conv2d(128, self.n_classes, 1, 1, 0)

        # Cascade Feature Fusion Units
        self.cff_sub24 = cascadeFeatureFusion(self.n_classes,
                                              256,
                                              256,
                                              128,
                                              is_batchnorm=is_batchnorm)
        self.cff_sub12 = cascadeFeatureFusion(self.n_classes,
                                              128,
                                              64,
                                              128,
                                              is_batchnorm=is_batchnorm)

        # Define auxiliary loss function
        self.loss = multi_scale_cross_entropy2d
Example #2
0
    def __init__(self,
                 n_classes=21,
                 block_config=[3, 4, 23, 3],
                 input_size=(473, 473),
                 version=None):

        super(pspnet, self).__init__()

        self.block_config = (pspnet_specs[version]["block_config"]
                             if version is not None else block_config)
        self.n_classes = pspnet_specs[version][
            "n_classes"] if version is not None else n_classes
        self.input_size = pspnet_specs[version][
            "input_size"] if version is not None else input_size

        # Encoder
        self.convbnrelu1_1 = conv2DBatchNormRelu(in_channels=3,
                                                 k_size=3,
                                                 n_filters=64,
                                                 padding=1,
                                                 stride=2,
                                                 bias=False)
        self.convbnrelu1_2 = conv2DBatchNormRelu(in_channels=64,
                                                 k_size=3,
                                                 n_filters=64,
                                                 padding=1,
                                                 stride=1,
                                                 bias=False)
        self.convbnrelu1_3 = conv2DBatchNormRelu(in_channels=64,
                                                 k_size=3,
                                                 n_filters=128,
                                                 padding=1,
                                                 stride=1,
                                                 bias=False)

        # Vanilla Residual Blocks
        self.res_block2 = residualBlockPSP(self.block_config[0], 128, 64, 256,
                                           1, 1)
        self.res_block3 = residualBlockPSP(self.block_config[1], 256, 128, 512,
                                           2, 1)

        # Dilated Residual Blocks
        self.res_block4 = residualBlockPSP(self.block_config[2], 512, 256,
                                           1024, 1, 2)
        self.res_block5 = residualBlockPSP(self.block_config[3], 1024, 512,
                                           2048, 1, 4)

        # Pyramid Pooling Module
        self.pyramid_pooling = pyramidPooling(2048, [6, 3, 2, 1])

        # Final conv layers
        self.cbr_final = conv2DBatchNormRelu(4096, 512, 3, 1, 1, False)
        self.dropout = nn.Dropout2d(p=0.1, inplace=False)
        self.classification = nn.Conv2d(512, self.n_classes, 1, 1, 0)

        # Auxiliary layers for training
        self.convbnrelu4_aux = conv2DBatchNormRelu(in_channels=1024,
                                                   k_size=3,
                                                   n_filters=256,
                                                   padding=1,
                                                   stride=1,
                                                   bias=False)
        self.aux_cls = nn.Conv2d(256, self.n_classes, 1, 1, 0)

        # Define auxiliary loss function
        self.loss = multi_scale_cross_entropy2d