Esempio n. 1
0
    def __init__(self, n_class, arch):
        super(ISICModel_sv_att, self).__init__()
        self.mode = 'sv_att'

        self.cfg = gl.get_value('cfg')
        self.M = self.cfg.MODEL.ATT_PARTS

        if arch in ['resnet50']:
            self.backbone = getattr(resnet,
                                    arch)(pretrained=True).get_features()
            self.feat_dim = 512 * self.backbone[-1][-1].expansion

        elif arch == 'sk_resnet50':
            model_backbone = sk_resnet50(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip
            self.feat_dim = 2048

        self.center_feat = torch.zeros(n_class, self.feat_dim * self.M)

        device = get_device(self.cfg)
        self.center_feat = self.center_feat.to(device)

        # Attention Maps
        self.attentions = BasicConv2d(self.feat_dim,
                                      self.cfg.MODEL.ATT_PARTS,
                                      kernel_size=1)

        # Bilinear Attention Pooling
        self.bap = BAP(pool='GAP')

        # Classification Layer
        self.head_im = nn.Linear(self.feat_dim * self.M, n_class, bias=False)

        self.init()
        if self.cfg.MODEL.PRETRAIN_PATH is not None and os.path.exists(
                self.cfg.MODEL.PRETRAIN_PATH):
            self.load_state_dict(torch.load(self.cfg.MODEL.PRETRAIN_PATH))
Esempio n. 2
0
    def __init__(self, n_class, arch, use_CBAM=False):
        super(ISICModel_singleview_reid, self).__init__()
        self.mode = 'singleview_reid'

        cfg = gl.get_value('cfg')
        self.cfg = cfg
        if arch == 'resnet50':

            if cfg.MODEL.USE_ADL is True:
                model_backbone = resnet50_adl(
                    pretrained=True,
                    num_classes=n_class,
                    ADL_position=cfg.MODEL.ADL_POSITION,
                    drop_rate=cfg.MODEL.ADLRATE,
                    drop_thr=cfg.MODEL.ADLTHR)
            else:
                model_backbone = models.resnet50(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'sk_resnet50':
            model_backbone = sk_resnet50(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'resnet50d':
            model_backbone = resnet50d(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'sge_resnet50':
            model_backbone = sge_resnet50(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip
        elif arch == 'resnext50_32x4d':

            model_backbone = models.resnext50_32x4d(pretrained=True)
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'se_resnext50':

            model_backbone = SENet(block=SEResNeXtBottleneck,
                                   layers=[3, 4, 6, 3],
                                   groups=32,
                                   reduction=16,
                                   dropout_p=None,
                                   inplanes=64,
                                   input_3x3=False,
                                   downsample_kernel_size=1,
                                   downsample_padding=0,
                                   last_stride=2)
            param_dict = torch.load(
                '../models/se_resnext50_32x4d-a260b3a4.pth')

            for i in param_dict:
                if 'classifier' in i or 'last_linear' in i:
                    continue
                model_backbone.state_dict()[i].copy_(param_dict[i])

            self.backbone = model_backbone  #(nn.Sequential(*list(model_backbone.children())[:-3]) )
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'effnetb4':
            model_backbone = EfficientNet.from_pretrained('efficientnet-b4')
            self.backbone = model_backbone  #(nn.Sequential(*list(model_backbone.children())[:-3]) )
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        self.imfeat_dim = cfg.MODEL.IMG_FCS  #(4096,512)

        self.use_fc = cfg.MODEL.REID_USE_FC

        self.num_classes = n_class
        self.pdrop_lin = cfg.MODEL.REID_PDROP_LIN
        self.neck_feat = cfg.MODEL.REID_NECK_FEAT

        if self.use_fc is True:
            self.in_planes = self.imfeat_dim[1]
            self.after_backbone = nn.Sequential(
                layers.AvgPool(), nn.Dropout(p=self.pdrop_lin),
                nn.Linear(self.imfeat_dim[0] // 2, self.in_planes, bias=False))
            self.bottleneck = nn.BatchNorm1d(self.imfeat_dim[1])
            self.classifier = nn.Linear(self.in_planes,
                                        self.num_classes,
                                        bias=False)
        else:
            self.in_planes = self.imfeat_dim[0] // 2
            self.after_backbone = layers.AvgPool()
            self.bottleneck = nn.BatchNorm1d(self.in_planes)
            self.classifier = nn.Sequential(
                nn.Dropout(p=self.pdrop_lin),
                nn.Linear(self.in_planes, self.num_classes, bias=False))

        self.bottleneck.bias.requires_grad_(False)  # no shift

        self.center_feat = torch.zeros(n_class, self.in_planes)
        device = get_device(self.cfg)
        self.center_feat = self.center_feat.to(device)

        #self.head_im = nn.Sequential(self.after_backbone,self.bottleneck)

        init_cnn(self.after_backbone)
        init_cnn(self.bottleneck)
        init_cnn(self.classifier)

        self.meta_fc = nn.ReLU(inplace=True)
        self.final_conv = nn.ReLU(inplace=True)  #skip

        #if cfg.MODEL.BACKBONE_PRETRAIN_PATH is not None  and os.path.exists(cfg.MODEL.BACKBONE_PRETRAIN_PATH):
        #    self.backbone.load_state_dict(torch.load(cfg.MODEL.BACKBONE_PRETRAIN_PATH))

        if cfg.MODEL.PRETRAIN_PATH is not None and os.path.exists(
                cfg.MODEL.PRETRAIN_PATH):
            self.load_state_dict(torch.load(cfg.MODEL.PRETRAIN_PATH))
Esempio n. 3
0
    def __init__(self, n_class, arch, use_CBAM=False):
        super(ISICModel_singleview_meta, self).__init__()
        self.mode = 'metasingleview'
        cfg = gl.get_value('cfg')

        self.use_heatmap = cfg.MISC.CALC_HEATMAP

        if arch == 'resnet50':

            model_backbone = models.resnet50(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip
        elif arch == 'sk_resnet50':
            model_backbone = sk_resnet50(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'resnext50_32x4d':

            model_backbone = models.resnext50_32x4d(pretrained=True)
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'effnetb4':

            model_backbone = EfficientNet.from_pretrained(
                'efficientnet-b4')  #(3584,512)
            self.backbone = model_backbone  #(nn.Sequential(*list(model_backbone.children())[:-3]) )
            self.backbone_lc = nn.ReLU(inplace=True)  #skip
        elif arch == 'effnetb3':
            model_backbone = EfficientNet.from_pretrained(
                'efficientnet-b3')  #(3072,512)
            self.backbone = model_backbone  #(nn.Sequential(*list(model_backbone.children())[:-3]) )
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'effnetb5':
            model_backbone = EfficientNet.from_pretrained(
                'efficientnet-b5')  #(4096,512)
            self.backbone = model_backbone  #(nn.Sequential(*list(model_backbone.children())[:-3]) )
            self.backbone_lc = nn.ReLU(inplace=True)  #skip
        elif arch == 'effnetb2':
            model_backbone = EfficientNet.from_pretrained(
                'efficientnet-b2')  #(2816,512)
            self.backbone = model_backbone  #(nn.Sequential(*list(model_backbone.children())[:-3]) )
            self.backbone_lc = nn.ReLU(inplace=True)  #skip
        elif arch == 'resnest50':
            model_backbone = resnest50(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'resnetbit':
            model_backbone = resnet_bit(pretrained=True,
                                        block_units=[3, 4, 6, 3],
                                        width_factor=1,
                                        model_type='resnet50')
            model_backbone.head = None
            self.backbone = model_backbone
            self.backbone_lc = nn.ReLU(inplace=True)  #skip
        elif arch == 'resnet101bit':
            model_backbone = resnet_bit(pretrained=True,
                                        block_units=[3, 4, 23, 3],
                                        width_factor=1,
                                        model_type='resnet101')
            model_backbone.head = None
            self.backbone = model_backbone
            self.backbone_lc = nn.ReLU(inplace=True)  #skip
        self.imfeat_dim = cfg.MODEL.IMG_FCS  #(4096,512)
        self.meta_dim = sum(cfg.MODEL.META_DIMS)  #(29)
        self.meta_dim_fc = cfg.MODEL.META_FCS  #(64,128)
        self.final_dim = cfg.MODEL.FINAL_DIM  # (320)

        if use_CBAM is False:
            self.head_im = nn.Sequential(
                layers.MaxAvgPool(),
                nn.BatchNorm1d(self.imfeat_dim[0],
                               eps=1e-5,
                               momentum=0.1,
                               affine=True,
                               track_running_stats=True), nn.Dropout(p=0.25),
                nn.Linear(in_features=self.imfeat_dim[0],
                          out_features=self.imfeat_dim[1],
                          bias=False),
                nn.BatchNorm1d(self.imfeat_dim[1],
                               eps=1e-5,
                               momentum=0.1,
                               affine=True,
                               track_running_stats=True),
                nn.ReLU(inplace=True), nn.Dropout(p=0.5))

        else:
            self.head_im = nn.Sequential(
                CBAM(gate_channels=self.imfeat_dim[0] // 2), layers.AvgPool(),
                nn.BatchNorm1d(self.imfeat_dim[0] // 2,
                               eps=1e-5,
                               momentum=0.1,
                               affine=True,
                               track_running_stats=True), nn.Dropout(p=0.25),
                nn.Linear(in_features=self.imfeat_dim[0] // 2,
                          out_features=self.imfeat_dim[1],
                          bias=False),
                nn.BatchNorm1d(self.imfeat_dim[1],
                               eps=1e-5,
                               momentum=0.1,
                               affine=True,
                               track_running_stats=True),
                nn.ReLU(inplace=True), nn.Dropout(p=0.5))

        self.meta_fc = nn.Sequential(
            nn.BatchNorm1d(self.meta_dim,
                           momentum=0.1,
                           affine=True,
                           track_running_stats=True),
            nn.Linear(in_features=self.meta_dim,
                      out_features=self.meta_dim_fc[0],
                      bias=False),
            nn.BatchNorm1d(self.meta_dim_fc[0],
                           eps=1e-5,
                           momentum=0.1,
                           affine=True,
                           track_running_stats=True), nn.ReLU(inplace=True),
            nn.Dropout(p=0.25),
            nn.Linear(in_features=self.meta_dim_fc[0],
                      out_features=self.meta_dim_fc[1],
                      bias=False),
            nn.BatchNorm1d(self.meta_dim_fc[1],
                           eps=1e-5,
                           momentum=0.1,
                           affine=True,
                           track_running_stats=True), nn.ReLU(inplace=True),
            nn.Dropout(p=0.5))

        self.final_conv = nn.Sequential(
            nn.Linear(in_features=self.meta_dim_fc[1] + self.imfeat_dim[1],
                      out_features=self.final_dim,
                      bias=False),
            nn.BatchNorm1d(self.final_dim,
                           eps=1e-5,
                           momentum=0.1,
                           affine=True,
                           track_running_stats=True), nn.ReLU(inplace=True),
            nn.Dropout(p=0.5),
            nn.Linear(in_features=self.final_dim,
                      out_features=n_class,
                      bias=True))

        self.init()
        if cfg.MODEL.PRETRAIN_PATH is not None and os.path.exists(
                cfg.MODEL.PRETRAIN_PATH):
            self.load_state_dict(torch.load(cfg.MODEL.PRETRAIN_PATH))
Esempio n. 4
0
    def __init__(self, n_class, arch, use_CBAM=False):
        super(ISICModel_singleview, self).__init__()
        self.mode = 'singleview'

        cfg = gl.get_value('cfg')
        self.cfg = cfg
        if arch == 'resnet50':

            if cfg.MODEL.USE_ADL is True:
                model_backbone = resnet50_adl(
                    pretrained=True,
                    num_classes=n_class,
                    ADL_position=cfg.MODEL.ADL_POSITION,
                    drop_rate=cfg.MODEL.ADLRATE,
                    drop_thr=cfg.MODEL.ADLTHR)
            else:
                model_backbone = models.resnet50(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'sk_resnet50':
            model_backbone = sk_resnet50(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'resnet50d':
            model_backbone = resnet50d(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))

        elif arch == 'sge_resnet50':
            model_backbone = sge_resnet50(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip
        elif arch == 'resnext50_32x4d':

            model_backbone = models.resnext50_32x4d(pretrained=True)
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'effnetb4':
            model_backbone = EfficientNet.from_pretrained('efficientnet-b4')
            self.backbone = model_backbone  #(nn.Sequential(*list(model_backbone.children())[:-3]) )
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'effnetb3':
            model_backbone = EfficientNet.from_pretrained(
                'efficientnet-b3')  #(3072,512)
            self.backbone = model_backbone  #(nn.Sequential(*list(model_backbone.children())[:-3]) )
            self.backbone_lc = nn.ReLU(inplace=True)  #skip
        elif arch == 'effnetb5':
            model_backbone = EfficientNet.from_pretrained(
                'efficientnet-b5')  #(4096,512)
            self.backbone = model_backbone  #(nn.Sequential(*list(model_backbone.children())[:-3]) )
            self.backbone_lc = nn.ReLU(inplace=True)  #skip
        elif arch == 'effnetb2':
            model_backbone = EfficientNet.from_pretrained(
                'efficientnet-b2')  #(2816,512)
            self.backbone = model_backbone  #(nn.Sequential(*list(model_backbone.children())[:-3]) )
            self.backbone_lc = nn.ReLU(inplace=True)  #skip
        elif arch == 'resnest50':
            model_backbone = resnest50(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'resnetbit':
            model_backbone = resnet_bit(pretrained=True,
                                        block_units=[3, 4, 6, 3],
                                        width_factor=1,
                                        model_type='resnet50')
            model_backbone.head = None
            self.backbone = model_backbone
            self.backbone_lc = nn.ReLU(inplace=True)  #skip
        elif arch == 'resnet101bit':
            model_backbone = resnet_bit(pretrained=True,
                                        block_units=[3, 4, 23, 3],
                                        width_factor=1,
                                        model_type='resnet101')
            model_backbone.head = None
            self.backbone = model_backbone
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        self.imfeat_dim = cfg.MODEL.IMG_FCS  #(4096,512)
        if use_CBAM is False and cfg.MODEL.USE_ADL is False:

            if cfg.MODEL.POOLMODE == 'maxavg':
                self.head_im = nn.Sequential(
                    layers.MaxAvgPool(),
                    nn.BatchNorm1d(self.imfeat_dim[0],
                                   eps=1e-5,
                                   momentum=0.1,
                                   affine=True,
                                   track_running_stats=True),
                    nn.Dropout(p=0.25),
                    nn.Linear(in_features=self.imfeat_dim[0],
                              out_features=self.imfeat_dim[1],
                              bias=False),
                    nn.BatchNorm1d(self.imfeat_dim[1],
                                   eps=1e-5,
                                   momentum=0.1,
                                   affine=True,
                                   track_running_stats=True),
                    nn.ReLU(inplace=True), nn.Dropout(p=0.5),
                    nn.Linear(in_features=self.imfeat_dim[1],
                              out_features=n_class,
                              bias=True))
            elif cfg.MODEL.POOLMODE == 'avg':
                self.head_im = nn.Sequential(
                    layers.AvgPool(),
                    nn.BatchNorm1d(self.imfeat_dim[0] // 2,
                                   eps=1e-5,
                                   momentum=0.1,
                                   affine=True,
                                   track_running_stats=True),
                    nn.Dropout(p=0.25),
                    nn.Linear(in_features=self.imfeat_dim[0] // 2,
                              out_features=self.imfeat_dim[1],
                              bias=False),
                    nn.BatchNorm1d(self.imfeat_dim[1],
                                   eps=1e-5,
                                   momentum=0.1,
                                   affine=True,
                                   track_running_stats=True),
                    nn.ReLU(inplace=True), nn.Dropout(p=0.5),
                    nn.Linear(in_features=self.imfeat_dim[1],
                              out_features=n_class,
                              bias=True))

        elif cfg.MODEL.USE_ADL is True:

            self.head_im = nn.Sequential(
                layers.AvgPool(),
                nn.BatchNorm1d(self.imfeat_dim[0] // 2,
                               eps=1e-5,
                               momentum=0.1,
                               affine=True,
                               track_running_stats=True), nn.Dropout(p=0.25),
                nn.Linear(in_features=self.imfeat_dim[0] // 2,
                          out_features=self.imfeat_dim[1],
                          bias=False),
                nn.BatchNorm1d(self.imfeat_dim[1],
                               eps=1e-5,
                               momentum=0.1,
                               affine=True,
                               track_running_stats=True),
                nn.ReLU(inplace=True), nn.Dropout(p=0.5),
                nn.Linear(in_features=self.imfeat_dim[1],
                          out_features=n_class,
                          bias=True))
        else:
            self.head_im = nn.Sequential(
                CBAM(gate_channels=self.imfeat_dim[0] // 2), layers.AvgPool(),
                nn.BatchNorm1d(self.imfeat_dim[0] // 2,
                               eps=1e-5,
                               momentum=0.1,
                               affine=True,
                               track_running_stats=True), nn.Dropout(p=0.25),
                nn.Linear(in_features=self.imfeat_dim[0] // 2,
                          out_features=self.imfeat_dim[1],
                          bias=False),
                nn.BatchNorm1d(self.imfeat_dim[1],
                               eps=1e-5,
                               momentum=0.1,
                               affine=True,
                               track_running_stats=True),
                nn.ReLU(inplace=True), nn.Dropout(p=0.5),
                nn.Linear(in_features=self.imfeat_dim[1],
                          out_features=n_class,
                          bias=True))
        self.meta_fc = nn.ReLU(inplace=True)
        self.final_conv = nn.ReLU(inplace=True)  #skip

        #if cfg.MODEL.BACKBONE_PRETRAIN_PATH is not None  and os.path.exists(cfg.MODEL.BACKBONE_PRETRAIN_PATH):
        #    self.backbone.load_state_dict(torch.load(cfg.MODEL.BACKBONE_PRETRAIN_PATH))

        self.init()
        if cfg.MODEL.PRETRAIN_PATH is not None and os.path.exists(
                cfg.MODEL.PRETRAIN_PATH):
            self.load_state_dict(torch.load(cfg.MODEL.PRETRAIN_PATH))
Esempio n. 5
0
    def __init__(self, n_class, arch, cls_layer='FC'):
        super(SV_BNN, self).__init__()
        self.mode = 'SV_BNN'

        cfg = gl.get_value('cfg')
        self.cfg = cfg
        if arch == 'resnet50':
            model_backbone = models.resnet50(pretrained=True)
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'sk_resnet50':
            model_backbone = sk_resnet50(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'resnet50d':
            model_backbone = resnet50d(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))

        elif arch == 'sge_resnet50':
            model_backbone = sge_resnet50(pretrained=True)
            #in_features =  4096
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip
        elif arch == 'resnext50_32x4d':

            model_backbone = models.resnext50_32x4d(pretrained=True)
            self.backbone = (nn.Sequential(
                *list(model_backbone.children())[:-2]))
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        elif arch == 'effnetb4':
            model_backbone = EfficientNet.from_pretrained('efficientnet-b4')
            self.backbone = model_backbone  #(nn.Sequential(*list(model_backbone.children())[:-3]) )
            self.backbone_lc = nn.ReLU(inplace=True)  #skip

        self.imfeat_dim = cfg.MODEL.IMG_FCS  #(4096,512)

        self.blk_cb = nn.Sequential(
            resblock(self.imfeat_dim[0] // 2, self.imfeat_dim[0] // 8),
            layers.AvgPool())

        self.blk_rb = nn.Sequential(
            resblock(self.imfeat_dim[0] // 2, self.imfeat_dim[0] // 8),
            layers.AvgPool())

        # default only ave pool
        if cls_layer == 'FC':
            self.classifer = nn.Linear(in_features=self.imfeat_dim[0],
                                       out_features=n_class,
                                       bias=True)
        elif cls_layer == 'FCNorm':
            self.classifer = FCNorm(in_features=self.imfeat_dim[0],
                                    out_features=n_class)

        self.init()
        if cfg.MODEL.PRETRAIN_PATH is not None and os.path.exists(
                cfg.MODEL.PRETRAIN_PATH):
            self.load_state_dict(torch.load(cfg.MODEL.PRETRAIN_PATH))