Beispiel #1
0
 def evaluate(self, conf, carray, issame, nrof_folds=5, tta=False):
     self.backbone.eval()
     self.idprehead.eval()
     idx = 0
     embeddings = np.zeros([len(carray), conf.embedding_size])
     with torch.no_grad():
         while idx + conf.batch_size <= len(carray):
             batch = torch.tensor(carray[idx:idx + conf.batch_size])
             if tta:
                 fliped = hflip_batch(batch)
                 emb_batch = self.idprehead(
                     self.backbone(batch.to(conf.device))) + self.idprehead(
                         self.backbone(fliped.to(conf.device)))
                 embeddings[idx:idx + conf.batch_size] = l2_norm(emb_batch)
             else:
                 embeddings[idx:idx + conf.batch_size] = self.idprehead(
                     self.backbone(batch.to(conf.device))).cpu()
             idx += conf.batch_size
         if idx < len(carray):
             batch = torch.tensor(carray[idx:])
             if tta:
                 fliped = hflip_batch(batch)
                 emb_batch = self.idprehead(
                     self.backbone(batch.to(conf.device))) + self.idprehead(
                         self.backbone(fliped.to(conf.device)))
                 embeddings[idx:] = l2_norm(emb_batch)
             else:
                 embeddings[idx:] = self.idprehead(
                     self.backbone(batch.to(conf.device))).cpu()
     tpr, fpr, accuracy, best_thresholds = evaluate(embeddings, issame,
                                                    nrof_folds)
     buf = gen_plot(fpr, tpr)
     roc_curve = Image.open(buf)
     roc_curve_tensor = trans.ToTensor()(roc_curve)
     return accuracy.mean(), best_thresholds.mean(), roc_curve_tensor
Beispiel #2
0
 def evaluate(self, cfg, carray, issame, nrof_folds=5, tta=False):
     self.model.eval()
     idx = 0
     embeddings = np.zeros([len(carray), cfg.MODEL.HEADS.EMBEDDING_DIM])
     batch_size = cfg.SOLVER.IMS_PER_BATCH
     with torch.no_grad():
         while idx + batch_size <= len(carray):
             batch = torch.tensor(carray[idx:idx + batch_size])
             if tta:
                 fliped = hflip_batch(batch)
                 emb_batch = self.model(batch.to(self.device)) + self.model(
                     fliped.to(self.device))
                 embeddings[idx:idx + batch_size] = l2_norm(emb_batch)
             else:
                 embeddings[idx:idx + batch_size] = self.model(
                     batch.to(self.device)).cpu()
             idx += batch_size
         if idx < len(carray):
             batch = torch.tensor(carray[idx:])
             if tta:
                 fliped = hflip_batch(batch)
                 emb_batch = self.model(batch.to(self.device)) + self.model(
                     fliped.to(self.device))
                 embeddings[idx:] = l2_norm(emb_batch)
             else:
                 embeddings[idx:] = self.model(batch.to(self.device)).cpu()
     tpr, fpr, accuracy, best_thresholds = scores(embeddings, issame,
                                                  nrof_folds)
     buf = gen_plot(fpr, tpr)
     roc_curve = Image.open(buf)
     roc_curve_tensor = trans.ToTensor()(roc_curve)
     return accuracy.mean(), best_thresholds.mean(), roc_curve_tensor
Beispiel #3
0
 def evaluate(self, conf, carray, issame, nrof_folds = 5, tta = False):
     self.model.eval()
     idx = 0
     embeddings = np.zeros([len(carray), conf.embedding_size])
     with torch.no_grad():
         while idx + conf.batch_size <= len(carray):
             batch = torch.tensor(carray[idx:idx + conf.batch_size])
             if tta:
                 fliped = hflip_batch(batch)
                 emb_batch = self.model(batch.to(conf.device)) + self.model(fliped.to(conf.device))
                 embeddings[idx:idx + conf.batch_size] = l2_norm(emb_batch)
             else:
                 embeddings[idx:idx + conf.batch_size] = self.model(batch.to(conf.device)).cpu()
             idx += conf.batch_size
         if idx < len(carray):
             batch = torch.tensor(carray[idx:])            
             if tta:
                 fliped = hflip_batch(batch)
                 emb_batch = self.model(batch.to(conf.device)) + self.model(fliped.to(conf.device))
                 embeddings[idx:] = l2_norm(emb_batch)
             else:
                 embeddings[idx:] = self.model(batch.to(conf.device)).cpu()
     tpr, fpr, accuracy, best_thresholds = evaluate(embeddings, issame, nrof_folds)
     try:
         tpr_val = tpr[np.less(fpr,0.0012)&np.greater(fpr,0.0008)][0]
         
     except:
         tpr_val = 0
     buf = gen_plot(fpr, tpr)
     roc_curve = Image.open(buf)
     roc_curve_tensor = trans.ToTensor()(roc_curve)
     return accuracy.mean(), best_thresholds.mean(), roc_curve_tensor,tpr_val
Beispiel #4
0
 def evaluate(self, conf, carray, issame, nrof_folds=5, tta=False):
     self.model.eval()
     idx = 0
     entry_num = carray.size()[0]
     embeddings = np.zeros([entry_num, conf.embedding_size])
     with torch.no_grad():
         while idx + conf.batch_size <= entry_num:
             batch = carray[idx:idx + conf.batch_size]
             if tta:
                 fliped = hflip_batch(batch)
                 emb_batch = self.model(batch.cuda()) + self.model(fliped.cuda())
                 embeddings[idx:idx + conf.batch_size] = l2_norm(emb_batch).cpu().detach().numpy()
             else:
                 embeddings[idx:idx + conf.batch_size] = self.model(batch.cuda()).cpu().detach().numpy()
             idx += conf.batch_size
         if idx < entry_num:
             batch = carray[idx:]
             if tta:
                 fliped = hflip_batch(batch)
                 emb_batch = self.model(batch.cuda()) + self.model(fliped.cuda())
                 embeddings[idx:] = l2_norm(emb_batch).cpu().detach().numpy()
             else:
                 embeddings[idx:] = self.model(batch.cuda()).cpu().detach().numpy()
     tpr, fpr, accuracy, best_thresholds = evaluate(embeddings, issame, nrof_folds)
     buf = gen_plot(fpr, tpr)
     roc_curve = Image.open(buf)
     roc_curve_tensor = trans.ToTensor()(roc_curve)
     return accuracy.mean(), best_thresholds.mean(), roc_curve_tensor
 def get_embeddings(self, conf, carray, tta=False):
     self.model.eval()
     idx = 0
     embeddings = np.zeros([len(carray), conf.embedding_size])
     with torch.no_grad():
         while idx + conf.batch_size <= len(carray):
             batch = torch.tensor(carray[idx:idx + conf.batch_size])
             if tta:
                 fliped = hflip_batch(batch)
                 emb_batch = self.model(batch.to(conf.device)) + self.model(
                     fliped.to(conf.device))
                 embeddings[idx:idx +
                            conf.batch_size] = l2_norm(emb_batch).cpu()
             else:
                 embeddings[idx:idx + conf.batch_size] = self.model(
                     batch.to(conf.device)).cpu()
             idx += conf.batch_size
         if idx < len(carray):
             batch = torch.tensor(carray[idx:])
             if tta:
                 fliped = hflip_batch(batch)
                 emb_batch = self.model(batch.to(conf.device)) + self.model(
                     fliped.to(conf.device))
                 embeddings[idx:] = l2_norm(emb_batch)
             else:
                 embeddings[idx:] = self.model(batch.to(conf.device)).cpu()
     return embeddings
Beispiel #6
0
    def evaluate(self, conf, val_loader, issame, nrof_folds=5, tta=False):
        self.model.model.eval()
        embeddings = torch.zeros(
            [len(val_loader.dataset), conf.embedding_size])
        with torch.no_grad():
            for idx, data in enumerate(
                    val_loader):  # data: batch_size * 3 * 112 * 112
                batch_size = data.size(0)
                if tta:
                    fliped = hflip_batch(data)
                    emb_batch = self.model.model(
                        data.to(conf.device)) + self.model.model(
                            fliped.to(conf.device))
                    embeddings[idx:idx + batch_size] = l2_norm(emb_batch)
                else:
                    embeddings[idx:idx + batch_size] = self.model.model(
                        data.to(conf.device))  # embeddings: batch_size * 512

        tpr, fpr, accuracy, best_thresholds = calculate_roc(
            self.thresholds, embeddings, issame, nrof_folds)
        buf = gen_plot(fpr, tpr)
        roc_curve = Image.open(buf)
        roc_curve_tensor = trans.ToTensor()(roc_curve)

        return accuracy.mean(), best_thresholds.mean(), roc_curve_tensor
Beispiel #7
0
 def evaluate(self, conf, carray, issame, nrof_folds=10, tta=False, n=1):
     self.model.eval()
     idx = 0
     embeddings = np.zeros([len(carray), conf.embedding_size // n])
     i = 0
     with torch.no_grad():
         while idx + conf.batch_size <= len(carray):
             batch = torch.tensor(carray[idx:idx + conf.batch_size])
             if tta:
                 fliped = hflip_batch(batch)
                 emb_batch = self.model(batch.to(conf.device)) + self.model(
                     fliped.to(conf.device))
                 embeddings[idx:idx + conf.batch_size] = l2_norm(
                     emb_batch).cpu()[:, i * conf.embedding_size //
                                      n:(i + 1) * conf.embedding_size // n]
             else:
                 embeddings[idx:idx + conf.batch_size] = self.model(
                     batch.to(conf.device)).cpu()[:,
                                                  i * conf.embedding_size //
                                                  n:(i + 1) *
                                                  conf.embedding_size // n]
             idx += conf.batch_size
         if idx < len(carray):
             batch = torch.tensor(carray[idx:])
             if tta:
                 fliped = hflip_batch(batch)
                 emb_batch = self.model(batch.to(conf.device)) + self.model(
                     fliped.to(conf.device))
                 embeddings[idx:] = l2_norm(
                     emb_batch).cpu()[:, i * conf.embedding_size //
                                      n:(i + 1) * conf.embedding_size // n]
             else:
                 embeddings[idx:] = self.model(batch.to(
                     conf.device)).cpu()[:, i * conf.embedding_size //
                                         n:(i + 1) * conf.embedding_size //
                                         n]
     tpr, fpr, accuracy, best_thresholds, angle_info = evaluate(
         embeddings, issame, nrof_folds)
     buf = gen_plot(fpr, tpr)
     roc_curve = Image.open(buf)
     roc_curve_tensor = trans.ToTensor()(roc_curve)
     return accuracy.mean(), best_thresholds.mean(
     ), roc_curve_tensor, angle_info
Beispiel #8
0
 def evaluate(self, conf, carray, issame, nrof_folds=10, tta=True):
     self.model.eval()
     self.growup.eval()
     self.discriminator.eval()
     idx = 0
     embeddings = np.zeros([len(carray), conf.embedding_size])
     with torch.no_grad():
         while idx + conf.batch_size <= len(carray):
             batch = torch.tensor(carray[idx:idx + conf.batch_size])
             if tta:
                 fliped = hflip_batch(batch)
                 emb_batch = self.model(
                     batch.to(conf.device)).cpu() + self.model(
                         fliped.to(conf.device)).cpu()
                 embeddings[idx:idx +
                            conf.batch_size] = l2_norm(emb_batch).cpu()
             else:
                 embeddings[idx:idx + conf.batch_size] = self.model(
                     batch.to(conf.device)).cpu()
             idx += conf.batch_size
         if idx < len(carray):
             batch = torch.tensor(carray[idx:])
             if tta:
                 fliped = hflip_batch(batch)
                 emb_batch = self.model(
                     batch.to(conf.device)).cpu() + self.model(
                         fliped.to(conf.device)).cpu()
                 embeddings[idx:] = l2_norm(emb_batch).cpu()
             else:
                 embeddings[idx:] = self.model(batch.to(conf.device)).cpu()
     tpr, fpr, accuracy, best_thresholds, dist = evaluate_dist(
         embeddings, issame, nrof_folds)
     buf = gen_plot(fpr, tpr)
     roc_curve = Image.open(buf)
     roc_curve_tensor = transforms.ToTensor()(roc_curve)
     return accuracy.mean(), best_thresholds.mean(), roc_curve_tensor, dist
Beispiel #9
0
def evaluate_ori(model, path, name, nrof_folds=10, tta=True):
    from utils import ccrop_batch, hflip_batch
    from models.model import l2_norm
    from verifacation import evaluate
    idx = 0
    from data.data_pipe import get_val_pair
    carray, issame = get_val_pair(path, name)
    carray = carray[:, ::-1, :, :]  # BGR 2 RGB!
    if use_mxnet:
        carray *= 0.5
        carray += 0.5
        carray *= 255.
    embeddings = np.zeros([len(carray), 512])
    if not use_mxnet:
        with torch.no_grad():
            while idx + bs <= len(carray):
                batch = torch.tensor(carray[idx:idx + bs])
                if tta:
                    # batch = ccrop_batch(batch)
                    fliped = hflip_batch(batch)
                    emb_batch = model(batch.cuda()) + model(fliped.cuda())
                    emb_batch = emb_batch.cpu()
                    embeddings[idx:idx + bs] = l2_norm(emb_batch)
                else:
                    embeddings[idx:idx + bs] = model(batch.cuda()).cpu()
                idx += bs
            if idx < len(carray):
                batch = torch.tensor(carray[idx:])
                if tta:
                    # batch = ccrop_batch(batch)
                    fliped = hflip_batch(batch)
                    emb_batch = model(batch.cuda()) + model(fliped.cuda())
                    emb_batch = emb_batch.cpu()
                    embeddings[idx:] = l2_norm(emb_batch)
                else:
                    embeddings[idx:] = model(batch.cuda()).cpu()
    else:
        from sklearn.preprocessing import normalize
        while idx + bs <= len(carray):
            batch = torch.tensor(carray[idx:idx + bs])
            if tta:
                fliped = hflip_batch(batch)
                emb_batch = model(batch) + model(fliped)
                embeddings[idx:idx + bs] = normalize(emb_batch)
            else:
                embeddings[idx:idx + bs] = model(batch)
            idx += bs
        if idx < len(carray):
            batch = torch.tensor(carray[idx:])
            if tta:
                fliped = hflip_batch(batch)
                emb_batch = model(batch) + model(fliped)
                embeddings[idx:] = normalize(emb_batch)
            else:
                embeddings[idx:] = model(batch)
    tpr, fpr, accuracy, best_thresholds = evaluate(embeddings, issame,
                                                   nrof_folds)
    roc_curve_tensor = None
    # buf = gen_plot(fpr, tpr)
    # roc_curve = Image.open(buf)
    # roc_curve_tensor = trans.ToTensor()(roc_curve)
    return accuracy.mean(), best_thresholds.mean(), roc_curve_tensor
for race in target:
    ir_se101_models[race] = model.ResNet()
    ir_se101_models[race].load_state_dict(
        torch.load('models/model_ir_se101_final_{}.pth'.format(race)))
    # ir_se101_models[race].load_state_dict(torch.load('models/model_ir_se101.pth'.format(race)))
    ir_se101_models[race].cuda()
    ir_se101_models[race].eval()

    input[race] = list(map(get_img, dir[race]))
    input[race] = torch.cat(input[race], 0)

    num = input[race].size()[0]

    with torch.no_grad():
        for i in tqdm(range(0, num, args.batch_size)):
            if i + args.batch_size > num:
                batch = input[race][i:].cuda()
                batch_f = hflip_batch(input[race][i:]).cuda()
            else:
                batch = input[race][i:i + args.batch_size].cuda()
                batch_f = hflip_batch(input[race][i:i +
                                                  args.batch_size]).cuda()
            emb = ir_se101_models[race](batch) + ir_se101_models[race](batch_f)
            output = model.l2_norm(emb).cpu().detach().numpy()
            if i == 0:
                res = output
            else:
                res = np.concatenate([res, output], 0)

    np.savetxt('output/' + race + '_output', res)