Exemplo n.º 1
0
    def forward(self, x, label=None):

        assert x.size()[1] == 2

        out_anchor = F.normalize(x[:, 0, :], p=2, dim=1)
        out_positive = F.normalize(x[:, 1, :], p=2, dim=1)
        stepsize = out_anchor.size()[0]

        output = -1 * (F.pairwise_distance(
            out_anchor.unsqueeze(-1),
            out_positive.unsqueeze(-1).transpose(0, 2))**2)

        negidx = self.mineHardNegative(output.detach())

        out_negative = out_positive[negidx, :]

        labelnp = numpy.array([1] * len(out_positive) +
                              [0] * len(out_negative))

        ## calculate distances
        pos_dist = F.pairwise_distance(out_anchor, out_positive)
        neg_dist = F.pairwise_distance(out_anchor, out_negative)

        ## loss function
        nloss = torch.mean(
            F.relu(
                torch.pow(pos_dist, 2) - torch.pow(neg_dist, 2) + self.margin))

        scores = -1 * torch.cat([pos_dist, neg_dist],
                                dim=0).detach().cpu().numpy()

        errors = tuneThresholdfromScore(scores, labelnp, [])

        return nloss, errors[1]
Exemplo n.º 2
0
    def forward(self, x, label=None):

        out_anchor = x[:, 0, :]
        out_positive = x[:, 1, :]
        stepsize = out_anchor.size()[0]

        output = -1 * (F.pairwise_distance(
            out_anchor.unsqueeze(-1).expand(-1, -1, stepsize),
            out_positive.unsqueeze(-1).expand(-1, -1, stepsize).transpose(
                0, 2))**2)

        negidx = self.mineHardNegative(output.detach())

        out_negative = out_positive[negidx, :]

        labelnp = numpy.array([1] * len(out_positive) +
                              [0] * len(out_negative))

        ## calculate distances
        pos_dist = F.pairwise_distance(out_anchor, out_positive)
        neg_dist = F.pairwise_distance(out_anchor, out_negative)

        ## loss functions
        if self.loss_func == 'contrastive':
            nloss = torch.mean(
                torch.cat([
                    torch.pow(pos_dist, 2),
                    torch.pow(F.relu(self.margin - neg_dist), 2)
                ],
                          dim=0))
        elif self.loss_func == 'triplet':
            nloss = torch.mean(
                F.relu(
                    torch.pow(pos_dist, 2) - torch.pow(neg_dist, 2) +
                    self.margin))

        scores = -1 * torch.cat([pos_dist, neg_dist],
                                dim=0).detach().cpu().numpy()

        errors = tuneThresholdfromScore(scores, labelnp, [])

        return nloss, errors[1]
Exemplo n.º 3
0
    s.loadParameters(args.initial_model)
    print("Model %s loaded!" % args.initial_model)

for ii in range(0, it - 1):
    if ii % args.test_interval == 0:
        clr = s.updateLearningRate(args.lr_decay)

# ==================== EVAL ====================

if args.eval == True:

    sc, lab = s.evaluateFromListSave(args.test_list,
                                     print_interval=100,
                                     feat_dir=feat_save_path,
                                     test_path=args.test_path)
    result = tuneThresholdfromScore(sc, lab, [1, 0.1])
    print('EER %2.4f' % result[1])

    quit()

scorefile = open(result_save_path + "/scores.txt", "a+")

for items in vars(args):
    print(items,
          vars(args)[items])
    scorefile.write('%s %s\n' % (items, vars(args)[items]))
scorefile.flush()

# ==================== ASSERTION ====================

gsize_dict = {