Example #1
0
 def __init__(self, seq_len, pre_trained=False):
     super(ResNet, self).__init__()
     self._resnet = None
     self._seq_len = seq_len
     self._resnet = backbone.get_backbone(arch="resnet50",
                                          n_frames=self._seq_len,
                                          pretrained=pre_trained)
Example #2
0
    def __init__(self,
                 backbone="ResNet50",
                 maximum_num_classes=21,
                 depth=256,
                 drop_rate=0.5,
                 support_free=False,
                 visualize=True):
        super(DENet, self).__init__()
        assert backbone in ['ResNet50', 'DenseNet121'], \
            'get unsupported backbone "%s" for DENet.' % backbone

        self.maximum_num_classes = maximum_num_classes
        self.depth = depth
        self.visualize = visualize
        self.vis = dict()

        self.backbone = get_backbone(backbone)()
        freeze_weights(self.backbone)

        self.embedding = nn.Sequential(
            nn.Conv2d(1024, self.depth, kernel_size=3, padding=2, dilation=2),
            nn.ReLU(), nn.Dropout2d(p=drop_rate))
        self.deeplab_head = DeepLabHead(256)

        self.estimator = WeightEstimator(self.maximum_num_classes,
                                         self.depth,
                                         support_free=support_free)
Example #3
0
    def __init__(self, backbone='ResNet50', refine_time=3):
        super(CAN, self).__init__()
        assert backbone in [
            'ResNet50', 'DenseNet121'
        ], 'get un supported backbone `%s` for CAN.' % backbone
        self.relu = nn.ReLU(inplace=False)
        self.backbone = get_backbone(backbone)()
        assert refine_time >= 1, 'refine time must >= 1, but got `%d`' % refine_time
        self.refine_time = refine_time
        freeze_weights(self.backbone)

        self.embedding = nn.Sequential(
            nn.Conv2d(in_channels=1536,
                      out_channels=256,
                      kernel_size=3,
                      padding=2,
                      dilation=2), nn.ReLU(), nn.Dropout2d(p=0.5))

        self.fuse = nn.Sequential(
            nn.Conv2d(in_channels=256 * 2,
                      out_channels=256,
                      kernel_size=3,
                      padding=2,
                      dilation=2), nn.ReLU(inplace=False), nn.Dropout2d(p=0.5))

        self.fuse_history = nn.Sequential(
            nn.Conv2d(256 + 2, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=False),
            nn.Conv2d(256, 256, kernel_size=3, padding=1))

        self.residual1 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=False),
            nn.Conv2d(256, 256, kernel_size=3, padding=1))

        self.residual2 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=False),
            nn.Conv2d(256, 256, kernel_size=3, padding=1))

        self.aspp = RegASPP()

        self.classifier = nn.Conv2d(256, 2, kernel_size=1)
        self.feature_reuse = None
Example #4
0
    def __init__(self, backbone="ResNet50"):
        super(PGN, self).__init__()
        assert backbone in ['ResNet50',
                            'DenseNet121'], 'get unsupported backbone "%s" for GPN.' % backbone
        DEPTH = 256
        self.backbone = get_backbone(backbone)()
        freeze_weights(self.backbone)

        self.embedding = nn.Sequential(
            nn.Conv2d(in_channels=1536, out_channels=256, kernel_size=3, padding=2, dilation=2),
            nn.ReLU(),
            nn.Dropout2d(p=0.5))

        self.g = nn.Sequential(nn.Conv2d(in_channels=DEPTH, out_channels=DEPTH, kernel_size=1),
                               nn.ReLU(inplace=True))

        self.gau_1 = GAU(depth=DEPTH)
        self.gau_2 = GAU(depth=DEPTH)
        self.gau_3 = GAU(depth=DEPTH)
        self.gau_4 = GAU(depth=DEPTH)

        self.residual1 = nn.Sequential(
            nn.Conv2d(in_channels=DEPTH, out_channels=DEPTH, kernel_size=3, padding=1),
            nn.ReLU(inplace=False),
            nn.Conv2d(in_channels=DEPTH, out_channels=DEPTH, kernel_size=3, padding=1)
        )

        self.residual2 = nn.Sequential(
            nn.Conv2d(in_channels=DEPTH, out_channels=DEPTH, kernel_size=3, padding=1),
            nn.ReLU(inplace=False),
            nn.Conv2d(in_channels=DEPTH, out_channels=DEPTH, kernel_size=3, padding=1)
        )

        self.residual3 = nn.Sequential(
            nn.Conv2d(in_channels=DEPTH, out_channels=DEPTH, kernel_size=3, padding=1),
            nn.ReLU(inplace=False),
            nn.Conv2d(in_channels=DEPTH, out_channels=DEPTH, kernel_size=3, padding=1)
        )
        self.relu = nn.ReLU(inplace=False)

        self.aspp = RegASPP()
        self.classifier = nn.Conv2d(in_channels=DEPTH, out_channels=2, kernel_size=1)
        self.feature_reuse = None
Example #5
0
    def __init__(self,
                 backbone='VGG16Base',
                 maximum_num_classes=21,
                 depth=256):
        super(AMP, self).__init__()
        assert backbone in [
            None, 'VGG16BNBase', 'VGG16Base'
        ], 'get un supported backbone `%s` for AMP.' % backbone
        for k, v in get_backbone(backbone)(ceil_mode=True).__dict__.items():
            if k == '_modules':
                self.__setattr__(k, v)

        self.num_classes = maximum_num_classes
        self.depth = depth
        self.nmap = NormMaskedAveragePooling(depth=self.depth, project=False)

        # set conv4 modules's three conv2d to padding and dilation to (1, 1), (1, 1), (2, 2)
        conv4_modules = []
        conv4_count = 0
        conv4_padding_dilation = [(1, 1), (1, 1), (2, 2)]
        for m in self.conv4:
            if type(m) == nn.Conv2d:
                padding, dilation = conv4_padding_dilation[conv4_count]
                conv4_count += 1
                new_m = nn.Conv2d(in_channels=m.in_channels,
                                  out_channels=m.out_channels,
                                  kernel_size=m.kernel_size,
                                  stride=m.stride,
                                  padding=padding,
                                  dilation=dilation,
                                  groups=m.groups,
                                  bias=m.bias is not None)
                new_m.weight.data = m.weight.data
                if m.bias is not None:
                    new_m.bias.data = m.bias.data
                m = new_m
            conv4_modules.append(m)
        self.conv4 = nn.Sequential(*conv4_modules)

        # set conv5 modules's three conv2d to padding and dilation to (2, 2), (4, 4), (4, 4)
        conv5_modules = []
        conv5_count = 0
        conv5_padding_dilation = [(2, 2), (4, 4), (4, 4)]
        for m in self.conv5:
            if type(m) == nn.Conv2d:
                padding, dilation = conv5_padding_dilation[conv5_count]
                conv5_count += 1
                new_m = nn.Conv2d(in_channels=m.in_channels,
                                  out_channels=m.out_channels,
                                  kernel_size=m.kernel_size,
                                  stride=m.stride,
                                  padding=padding,
                                  dilation=dilation,
                                  groups=m.groups,
                                  bias=m.bias is not None)
                new_m.weight.data = m.weight.data
                if m.bias is not None:
                    new_m.bias.data = m.bias.data
                m = new_m
            conv5_modules.append(m)
        self.conv5 = nn.Sequential(*conv5_modules)

        freeze_weights(
            nn.Sequential(self.conv1, self.conv2, self.conv3, self.conv4,
                          self.conv5))

        self.shot = 1

        # DFCN8s setting
        # self.fully_conv = nn.Sequential(
        #     nn.Conv2d(self.depth * 2, 4096, 1),
        #     nn.ReLU(inplace=True),
        #     nn.Dropout2d(),
        #     nn.Conv2d(4096, 4096, 1),
        #     nn.ReLU(inplace=True),
        #     nn.Dropout2d(),
        #     nn.Conv2d(4096, self.depth, 1),
        #     nn.ReLU(inplace=True),
        #     nn.Dropout2d()
        # )

        # reduced DFCN8s setting
        self.fully_conv = nn.Sequential(
            nn.Conv2d(2 * self.depth, self.depth, 1))

        self.level1 = nn.Sequential(self.conv1, self.pool1, self.conv2,
                                    self.pool2, self.conv3, self.pool3)
        self.level2 = nn.Sequential(self.conv4, self.pool4)
        self.level3 = nn.Sequential(self.conv5, self.pool5, self.fully_conv)

        self.level1_classifier = nn.Conv2d(self.depth,
                                           self.num_classes,
                                           1,
                                           bias=False)
        self.level2_classifier = nn.Conv2d(self.depth * 2,
                                           self.num_classes,
                                           1,
                                           bias=False)
        self.level3_classifier = nn.Conv2d(self.depth,
                                           self.num_classes,
                                           1,
                                           bias=False)