def __init__(self,K=12):
     super(attentionGCN, self).__init__()
     self.K = K
     self.backbone = ResNet(50,num_stages=4,
                            strides=(1,2,2,1),
                            out_indices=(2,3))
     self.backbone.init_weights(pretrained='https://download.pytorch.org/models/resnet50-19c8e357.pth')
     self.attention = Attention(1024,K)
     self.attention.weight_init(None)
     #self.GCN = GCN(2048,2048)
     #self.lin = nn.Linear(2048,64)
     self.fc = nn.Linear(2048,11)
     #nn.init.xavier_normal_(self.lin.weight)
     nn.init.kaiming_normal_(self.fc.weight)
 def __init__(self):
     super(proposalModel, self).__init__()
     self.backbone = ResNet(101, 4, out_indices=(
         1,
         3,
     ))  # int obj不可迭代,加个 ,
     self.roiextract = SingleRoIExtractor(roi_layer=dict(type='RoIAlign',
                                                         out_size=7,
                                                         sample_num=2),
                                          out_channels=512,
                                          featmap_strides=[
                                              8,
                                          ])  # 没有FPN 所以只有一个步长
     self.fc = nn.Linear(2048 + 2048, 40)
     self.backbone.init_weights(
         pretrained='/home/share/LabServer/GLnet/MODELZOO/resnet50.pth')
     self.conv1 = nn.Conv2d(512, 1024, 3, 1)
     self.conv2 = nn.Conv2d(1024, 2048, 3, 1)
 def __init__(self, K=12):
     super(MaxScore, self).__init__()
     self.K = K
     self.backbone = ResNet(50,
                            num_stages=4,
                            strides=(1, 2, 2, 1),
                            out_indices=(2, 3))
     self.backbone.init_weights(
         pretrained=
         'https://download.pytorch.org/models/resnet50-19c8e357.pth')
     self.attention = Attention(1024, K)
     self.attention.weight_init(None)
     # self.lin = nn.Linear(2048,64)
     self.fc2 = nn.Linear(512, 40)
     self.fc1 = nn.Linear(2048, 512)
     self.fc = nn.Linear(80, 40)
     nn.init.kaiming_normal_(self.fc2.weight)
     nn.init.kaiming_normal_(self.fc1.weight)
Exemple #4
0
 def __init__(self, K=6):
     super(attentionReid, self).__init__()
     self.K = K
     self.backbone = ResNet(50,
                            num_stages=4,
                            strides=(1, 2, 2, 1),
                            out_indices=(2, 3))
     self.backbone.init_weights(
         pretrained=
         'https://download.pytorch.org/models/resnet50-19c8e357.pth')
     self.attention = SattentionNet(num_features=128,
                                    seqlen=1,
                                    spanum=self.K)
     self.attention.reset_params()
     #self.GCN = GCN(2048,2048//K)
     self.lin = nn.Linear(2048, 2048)
     self.fc = nn.Linear(128 * self.K, 11)
     nn.init.xavier_normal_(self.lin.weight)
     nn.init.xavier_normal_(self.fc.weight)
     self.conv2 = nn.Conv2d(1024, self.K, 1, 1)
     self.channelpool = ChannelPool(self.K)
 def __init__(self):
     super(proposalModel_scene, self).__init__()
     self.HIGHSCENE = [
         3, 4, 6, 7, 9, 10, 13, 12, 11, 16, 24, 25, 26, 32, 39
     ]
     self.backbone = ResNet(50, 4, out_indices=(
         1,
         3,
     ))  # int obj不可迭代,加个 ,
     self.roiextract = SingleRoIExtractor(roi_layer=dict(type='RoIAlign',
                                                         out_size=7,
                                                         sample_num=2),
                                          out_channels=512,
                                          featmap_strides=[
                                              8,
                                          ])  # 没有FPN 所以只有一个步长
     self.fc_high = nn.Linear(2048 + 1024, 40)
     self.fc_low = nn.Linear(1024 + 1024, 40)
     self.conv1 = nn.Conv2d(512, 1024, 3, 1, 0)
     self.conv2 = nn.Conv2d(1024, 2048, 3, 1, 0)
     self.backbone.init_weights(
         pretrained='/home/share/LabServer/GLnet/MODELZOO/resnet50.pth')
class proposalModel(nn.Module):
    def __init__(self):
        super(proposalModel, self).__init__()
        self.backbone = ResNet(101, 4, out_indices=(
            1,
            3,
        ))  # int obj不可迭代,加个 ,
        self.roiextract = SingleRoIExtractor(roi_layer=dict(type='RoIAlign',
                                                            out_size=7,
                                                            sample_num=2),
                                             out_channels=512,
                                             featmap_strides=[
                                                 8,
                                             ])  # 没有FPN 所以只有一个步长
        self.fc = nn.Linear(2048 + 2048, 40)
        self.backbone.init_weights(
            pretrained='/home/share/LabServer/GLnet/MODELZOO/resnet50.pth')
        self.conv1 = nn.Conv2d(512, 1024, 3, 1)
        self.conv2 = nn.Conv2d(1024, 2048, 3, 1)

    def forward(self, img, gtbboxes, proposals, label):
        backbonefeat, backbonefeat_ = self.backbone(img)  #  14 *14
        #print(backbonefeat.shape)
        #print(backbonefeat_.shape)
        #print(backbonefeat)
        #print('backbonefeat shape {}'.format(backbonefeat.shape))
        #print('out222  \n{}'.format(backbonefeat.shape))
        #print('out333  \n{}'.format(backbonefeat_.shape))
        globalfeat = F.adaptive_avg_pool2d(backbonefeat_, (1, 1))
        globalfeat = globalfeat.view(globalfeat.shape[0], -1)
        # proposal  gtbboxes 都是 list 里面是tensor
        #print('globalfeat \n {}'.format(globalfeat.shape))
        # 筛选proposal

        #print('before {}'.format(proposals[0].shape))
        #print('gtbox {}'.format(gtbboxes[0].shape))
        for ii in range(len(gtbboxes)):
            tem_proposal = []
            iofs = bbox_overlaps(proposals[ii][:, :4].double(),
                                 gtbboxes[ii].double(),
                                 mode='iof',
                                 is_aligned=False)  #
            iofs = iofs.sum(1)
            for jj, iof in enumerate(iofs):
                if iof >= 0.2:
                    tem_proposal.append(proposals[ii][jj])
            if tem_proposal:
                proposals[ii] = torch.stack(tem_proposal, 0)
                #print(proposals[ii].shape)
                assert proposals[ii].dim() == 2
            else:
                proposals[ii] = torch.cat(
                    (gtbboxes[ii], torch.zeros(
                        (gtbboxes[ii].size(0), 1)).cuda()),
                    1)  # 如果没有符合要求的proposal,就让person box作为 proposal
            proposals[ii] = proposals[ii].double()
        #print('after{}'.format(proposals[0].shape))
        #list 中的proposal可以是5列或者4列 都可以转换一样的roi
        proposals = bbox2roi(
            proposals
        )  #(list[Tensor]) --》Tensor: shape (n, 5), [batch_ind, x1, y1, x2, y2]
        proposals = proposals.cuda().float()
        idxs = proposals[:, 0].data.cpu().numpy()
        #print('proposal shape {}'.format(proposals.shape))
        bbox_feat = self.roiextract([backbonefeat.float()],
                                    proposals)  #只有一个尺度无FPN,所以list里面只有一个尺度的feat
        #print('bbox feat shape {}'.format(bbox_feat.shape))
        #print(torch.sum(bbox_feat[0]))
        split_bbox_feat = []
        start = 0
        for ii, index in enumerate(idxs):

            if ii != len(idxs) - 1 and index == idxs[ii + 1]:
                continue
            else:
                end = ii + 1
                mean_proposal = bbox_feat[start:end].sum(0)
                split_bbox_feat.append(mean_proposal)
                start = ii + 1
                #print(index)
        #print((split_bbox_feat))
        bbox_feat = torch.stack(split_bbox_feat, 0)  # B , c ,7 ,7
        bbox_feat = self.conv1(bbox_feat)
        bbox_feat = self.conv2(bbox_feat)
        #print(bbox_feat.shape)
        bbox_feat = F.adaptive_avg_pool2d(bbox_feat, (1, 1))  #B , c ,1 ,1
        #print('bboxfeat  \n{}'.format(bbox_feat.shape))
        #print(bbox_feat.shape)
        assert bbox_feat.shape[1] == 2048
        bbox_feat = bbox_feat.view(bbox_feat.size(0), -1)
        end_feat = torch.cat((globalfeat, bbox_feat), 1)
        output = self.fc(end_feat)
        return output
Exemple #7
0
class attentionReid(nn.Module):
    def __init__(self, K=6):
        super(attentionReid, self).__init__()
        self.K = K
        self.backbone = ResNet(50,
                               num_stages=4,
                               strides=(1, 2, 2, 1),
                               out_indices=(2, 3))
        self.backbone.init_weights(
            pretrained=
            'https://download.pytorch.org/models/resnet50-19c8e357.pth')
        self.attention = SattentionNet(num_features=128,
                                       seqlen=1,
                                       spanum=self.K)
        self.attention.reset_params()
        #self.GCN = GCN(2048,2048//K)
        self.lin = nn.Linear(2048, 2048)
        self.fc = nn.Linear(128 * self.K, 11)
        nn.init.xavier_normal_(self.lin.weight)
        nn.init.xavier_normal_(self.fc.weight)
        self.conv2 = nn.Conv2d(1024, self.K, 1, 1)
        self.channelpool = ChannelPool(self.K)

    def forward(self, input):
        feature1, feature2 = self.backbone(input)
        #print("feautre1  {}".format(feature1))
        x, reg, attmap = self.attention(feature2)

        #print(feat_att)
        """
        #edge index
        idx = [i for i in range(self.K+1)]
        a = torch.LongTensor(idx).cuda()
        a2 = a.repeat(self.K+1)
        a1 = a.view(a.size(0), 1).repeat(1, self.K+1).view(-1)
        a1 = a1.view(1, a1.size(0))
        a2 = a2.view(1, a2.size(0))
        edge_index = torch.cat((a1, a2), dim=0).cuda()
        graph_feat = []
        for ii in range(feat_att.size(0)):
            graph_x = feat_att[ii]
            #print("befor {}".format(graph_x))
            #print(self.lin(graph_x).shape)
            edge_weight = self.lin(graph_x).mm(self.lin(graph_x).t())

            edge_weight = F.softmax(edge_weight.view(-1).cuda(),dim=0)

            #print(edge_weight.size(0))
            #print(edge_index.size(1))
            graph_x_ = self.GCN(graph_x,edge_index,edge_weight)
            #print("after {}".format(graph_x_))

            graph_feat.append(graph_x_)
        graph_feat_ = torch.stack(graph_feat)
        graph_feat_ = graph_feat_.view(graph_feat_.size(0),-1)

        output = self.fc(graph_feat_)
        #print(output)"""
        feat_att = x.view(x.size(0), -1)
        output = self.fc(feat_att)

        return output, reg, attmap

    def weight_init(self, pretrained):
        if isinstance(pretrained, str):
            load_checkpoint(
                self,
                pretrained,
                strict=False,
            )
        elif pretrained is None:
            for m in self.modules():
                if isinstance(m, nn.Conv2d):
                    kaiming_init(m)
                elif isinstance(m, nn.BatchNorm2d):
                    constant_init(m, 1)
        else:
            raise TypeError('pretrained must be a str or None')
class attentionGCN(nn.Module):
    def __init__(self,K=12):
        super(attentionGCN, self).__init__()
        self.K = K
        self.backbone = ResNet(50,num_stages=4,
                               strides=(1,2,2,1),
                               out_indices=(2,3))
        self.backbone.init_weights(pretrained='https://download.pytorch.org/models/resnet50-19c8e357.pth')
        self.attention = Attention(1024,K)
        self.attention.weight_init(None)
        #self.GCN = GCN(2048,2048)
        #self.lin = nn.Linear(2048,64)
        self.fc = nn.Linear(2048,11)
        #nn.init.xavier_normal_(self.lin.weight)
        nn.init.kaiming_normal_(self.fc.weight)
        #self.conv2 = nn.Conv2d(1024,self.K,1,1)
        #self.channelpool = ChannelPool(self.K)
    def forward(self, input):
        feature1,feature2 = self.backbone(input)
        #print("feautre1  {}".format(feature1))
        local_map, global_map = self.attention(feature1)
        #local_map = self.conv2(feature1)
        #n,c,h,w = local_map.shape
        #local_map = F.softmax(local_map,dim=1)
        #global_map = self.channelpool(local_map)
        #n,c,h,w = global_map.shape

        b,c,h,w = feature2.shape
        feat = feature2.view(b,1,c,h,w)
        feat = feat.repeat(1,self.K+1,1,1,1)
        map = torch.cat((local_map,global_map),dim=1)
        assert map.size()[1]==self.K+1
        b,c,h,w = map.shape
        map  = map.view(b,c,1,h,w)
        map = map.repeat(1,1,2048,1,1)
        feat_att = feat * map
        #print(feat_att.shape)
        feat_att = F.avg_pool3d(feat_att,(1,14,7)).squeeze(-1).squeeze(-1)
        #print("feat attention")
        #print(feat_att)
        """
        #edge index
        idx = [i for i in range(self.K+1)]
        a = torch.LongTensor(idx).cuda()
        a2 = a.repeat(self.K+1)
        a1 = a.view(a.size(0), 1).repeat(1, self.K+1).view(-1)
        a1 = a1.view(1, a1.size(0))
        a2 = a2.view(1, a2.size(0))
        edge_index = torch.cat((a1, a2), dim=0).cuda()
        graph_feat = []
        for ii in range(feat_att.size(0)):
            graph_x = feat_att[ii]
            #print("befor {}".format(graph_x))
            #print(self.lin(graph_x).shape)
            edge_weight = self.lin(graph_x).mm(self.lin(graph_x).t())

            edge_weight = F.softmax(edge_weight.view(-1).cuda(),dim=0)

            #print(edge_weight.size(0))
            #print(edge_index.size(1))
            graph_x_ = self.GCN(graph_x,edge_index,edge_weight)
            #print("after {}".format(graph_x_))

            graph_feat.append(graph_x_)
        graph_feat_ = torch.stack(graph_feat)
        graph_feat_ = F.softmax(graph_feat_,2)
        graph_feat_ = graph_feat_.mean(1)
        
        output = self.fc(graph_feat_)
        """
        #print(output)"""
        #feat_att = feat_att.view(feat_att.size(0),-1)
        feat_att = feat_att.mean(1)
        output = self.fc(feat_att)

        return output,global_map,local_map

    def weight_init(self,pretrained):
        if isinstance(pretrained, str):
            load_checkpoint(self, pretrained, strict=False, )
        elif pretrained is None:
            for m in self.modules():
                if isinstance(m, nn.Conv2d):
                    kaiming_init(m)
                elif isinstance(m, nn.BatchNorm2d):
                    constant_init(m, 1)
        else:
            raise TypeError('pretrained must be a str or None')
class MaxScore(nn.Module):
    def __init__(self, K=12):
        super(MaxScore, self).__init__()
        self.K = K
        self.backbone = ResNet(50,
                               num_stages=4,
                               strides=(1, 2, 2, 1),
                               out_indices=(2, 3))
        self.backbone.init_weights(
            pretrained=
            'https://download.pytorch.org/models/resnet50-19c8e357.pth')
        self.attention = Attention(1024, K)
        self.attention.weight_init(None)
        # self.lin = nn.Linear(2048,64)
        self.fc2 = nn.Linear(512, 40)
        self.fc1 = nn.Linear(2048, 512)
        self.fc = nn.Linear(80, 40)
        nn.init.kaiming_normal_(self.fc2.weight)
        nn.init.kaiming_normal_(self.fc1.weight)
        # self.conv2 = nn.Conv2d(1024,self.K,1,1)
        # self.channelpool = ChannelPool(self.K)

    def forward(self, input, target):
        feature1, feature2 = self.backbone(input)
        primal_feat = F.avg_pool2d(feature2,
                                   (14, 14)).view(feature2.size(0),
                                                  feature2.size(1))
        primal_x = self.fc1(primal_feat)
        primal_x = self.fc2(primal_x)

        local_map, global_map = self.attention(feature1)
        # local_map = self.conv2(feature1)
        # n,c,h,w = local_map.shape
        # local_map = F.softmax(local_map,dim=1)
        # global_map = self.channelpool(local_map)
        # n,c,h,w = global_map.shape

        b, c, h, w = feature2.shape
        feat = feature2.view(b, 1, c, h, w)
        feat = feat.repeat(1, self.K, 1, 1, 1)
        map = local_map
        assert map.size()[1] == self.K
        b, c, h, w = map.shape
        map = map.view(b, c, 1, h, w)
        map = map.repeat(1, 1, 2048, 1, 1)
        feat_att = feat * map
        feat_att = F.avg_pool3d(feat_att, (1, 14, 14)).squeeze(-1).squeeze(-1)
        feat_att = feat_att.contiguous().view(b * c, 2048)
        secondary_x = self.fc1(feat_att)
        secondary_x = self.fc2(secondary_x)
        secondary_x = secondary_x.view(b, c, -1)
        secondary_att = []
        for i in range(b):
            sec = secondary_x[i]
            index = torch.argmax(sec[:, target[i]].unsqueeze(1), dim=1)
            choose = sec[index[0]]
            secondary_att.append(choose)
        secondary_att = torch.stack(secondary_att)
        #output = torch.cat((primal_x,secondary_att),1)
        output = primal_x  #+ secondary_att
        #output = self.fc(output)
        return output, global_map, local_map

    def weight_init(self, pretrained):
        if isinstance(pretrained, str):
            load_checkpoint(
                self,
                pretrained,
                strict=False,
            )
        elif pretrained is None:
            for m in self.modules():
                if isinstance(m, nn.Conv2d):
                    kaiming_init(m)
                elif isinstance(m, nn.BatchNorm2d):
                    constant_init(m, 1)
        else:
            raise TypeError('pretrained must be a str or None')
Exemple #10
0
 def __init__(self):
     super(backbone_model, self).__init__()
     self.backbone = ResNet(50, 4, out_indices=(3, ))
     self.fc = nn.Linear(2048, 11)
     self.global_pool = nn.AvgPool2d(kernel_size=(7, 7), stride=1)