Esempio n. 1
0
def test_vib(model,
             queryloader,
             galleryloader,
             use_gpu,
             args,
             writer,
             epoch,
             ranks=[1, 5, 10, 20],
             return_distmat=False,
             use_cosine=False,
             draw_tsne=False,
             tsne_clusters=3):

    batch_time = AverageMeter()

    model.eval()

    with torch.no_grad():
        q_pids, q_camids = [], [], []
        for batch_idx, (imgs, pids, camids) in enumerate(queryloader):
            if use_gpu: imgs = imgs.cuda()

            end = time.time()
            features = model(imgs)
            batch_time.update(time.time() - end)

            features = features.data.cpu()
            qf_stat.append(features)
            q_pids.extend(pids)
            q_camids.extend(camids)
        qf_stat = torch.cat(qf_stat, 0)
        q_pids = np.asarray(q_pids)
        q_camids = np.asarray(q_camids)

        print("Extracted features for query set, obtained {}-by-{} matrix".
              format(qf_stat.size(0), qf_stat.size(1)))

        g_pids, g_camids = [], [], []
        end = time.time()
        for batch_idx, (imgs, pids, camids) in enumerate(galleryloader):
            if use_gpu: imgs = imgs.cuda()

            end = time.time()
            features = model(imgs)
            batch_time.update(time.time() - end)

            features = features.data.cpu()
            gf_stat.append(features)
            g_pids.extend(pids)
            g_camids.extend(camids)
        gf_stat = torch.cat(gf_stat, 0)
        g_pids = np.asarray(g_pids)
        g_camids = np.asarray(g_camids)

        print("Extracted features for gallery set, obtained {}-by-{} matrix".
              format(gf.size(0), gf.size(1)))

    print("==> BatchTime(s)/BatchSize(img): {:.3f}/{}".format(
        batch_time.avg, args.test_batch))

    pdb.set_trace()

    m, n = qf_stat.size(0), gf_stat.size(0)
    score_board = torch.zeros((m, n, n), dtype=torch.int16)
    qf = torch.zeros(m, 512)  #,torch.zeros(m,512)
    for _ in range(args.sampling_count):
        qf_sample = model.reparametrize_n(qf_stat[:, 0], qf_stat[:, 1])
        qf = qf + qf_sample

    qf = qf / args.sampling_count

    for _ in range(args.sampling_count):

        #qf = model.reparametrize_n(qf_stat[:,0],qf_stat[:,1])
        gf = model.reparametrize_n(gf_stat[:, 0], gf_stat[:, 1])
        if not use_cosine:
            distmat = torch.pow(qf, 2).sum(dim=1, keepdim=True).expand(m, n) + \
                      torch.pow(gf, 2).sum(dim=1, keepdim=True).expand(n, m).t()
            distmat.addmm_(1, -2, qf, gf.t())
            distmat = distmat.numpy()
        else:
            qf_norm = qf / qf.norm(dim=1)[:, None]
            gf_norm = gf / gf.norm(dim=1)[:, None]
            distmat = torch.addmm(1, torch.ones((m, n)), -1, qf_norm,
                                  gf_norm.transpose(0, 1))
            distmat = distmat.numpy()

        indices = np.argsort(distmat, axis=1)

        for indx in range(m):
            score_board[m, indices[indx], list(range(n))] += 1

    print("Computing CMC and mAP")
    cmc, mAP = evaluate(distmat,
                        q_pids,
                        g_pids,
                        q_camids,
                        g_camids,
                        use_metric_cuhk03=args.use_metric_cuhk03)

    print("Results ----------")
    print("mAP: {:.1%}".format(mAP))
    print("CMC curve")
    for r in ranks:
        print("Rank-{:<3}: {:.1%}".format(r, cmc[r - 1]))
    print("------------------")

    if draw_tsne:
        drawTSNE(qf, gf, q_pids, g_pids, q_camids, g_camids, tsne_clusters,
                 args.save_dir)
    if return_distmat:
        return distmat

    if writer != None:
        writer.add_scalars('Testing',
                           dict(rank_1=cmc[0], rank_5=cmc[4], mAP=mAP),
                           epoch + 1)
    return cmc[0]
Esempio n. 2
0
def test(model,
         queryloader,
         galleryloader,
         use_gpu,
         args,
         writer,
         epoch,
         ranks=[1, 5, 10, 20],
         return_distmat=False,
         tsne_clusters=3):

    batch_time = AverageMeter()

    model.eval()

    with torch.no_grad():
        qf, q_pids, q_camids = [], [], []
        q_imgPath = []
        for batch_idx, (input) in enumerate(queryloader):
            if not args.draw_tsne:
                imgs, pids, camids = input
            else:
                imgs, pids, camids, img_path = input
                q_imgPath.extend(img_path)
            if use_gpu: imgs = imgs.cuda()

            end = time.time()
            features = model(imgs)
            batch_time.update(time.time() - end)

            features = features.data.cpu()
            qf.append(features)
            q_pids.extend(pids)
            q_camids.extend(camids)
        qf = torch.cat(qf, 0)
        q_pids = np.asarray(q_pids)
        q_camids = np.asarray(q_camids)
        q_imgPath = np.asarray(q_imgPath)

        print("Extracted features for query set, obtained {}-by-{} matrix".
              format(qf.size(0), qf.size(1)))

        gf, g_pids, g_camids = [], [], []
        g_imgPath = []
        end = time.time()
        for batch_idx, (input) in enumerate(galleryloader):
            if not args.draw_tsne:
                imgs, pids, camids = input
            else:
                imgs, pids, camids, img_path = input
                g_imgPath.extend(img_path)
            if use_gpu: imgs = imgs.cuda()

            end = time.time()
            features = model(imgs)
            batch_time.update(time.time() - end)

            features = features.data.cpu()
            gf.append(features)
            g_pids.extend(pids)
            g_camids.extend(camids)
        gf = torch.cat(gf, 0)
        g_pids = np.asarray(g_pids)
        g_camids = np.asarray(g_camids)
        g_imgPath = np.asarray(q_imgPath)

        print("Extracted features for gallery set, obtained {}-by-{} matrix".
              format(gf.size(0), gf.size(1)))

    print("==> BatchTime(s)/BatchSize(img): {:.3f}/{}".format(
        batch_time.avg, args.test_batch))

    if args.use_ecn:
        distmat = (ECN_custom(qf,
                              gf,
                              k=25,
                              t=3,
                              q=8,
                              method='rankdist',
                              use_cosine=args.use_cosine)).transpose()
    elif not args.use_cosine:
        m, n = qf.size(0), gf.size(0)
        distmat = torch.pow(qf, 2).sum(dim=1, keepdim=True).expand(m, n) + \
                  torch.pow(gf, 2).sum(dim=1, keepdim=True).expand(n, m).t()
        distmat.addmm_(1, -2, qf, gf.t())
        distmat = distmat.numpy()

        if args.re_ranking:
            distmat_q_q = torch.pow(qf, 2).sum(dim=1, keepdim=True).expand(m, m) + \
                      torch.pow(qf, 2).sum(dim=1, keepdim=True).expand(m, m).t()
            distmat_q_q.addmm_(1, -2, qf, qf.t())
            distmat_q_q = distmat_q_q.numpy()

            distmat_g_g = torch.pow(gf, 2).sum(dim=1, keepdim=True).expand(n, n) + \
                      torch.pow(gf, 2).sum(dim=1, keepdim=True).expand(n, n).t()
            distmat_g_g.addmm_(1, -2, gf, gf.t())
            distmat_g_g = distmat_g_g.numpy()

            distmat = re_ranking(distmat,
                                 distmat_q_q,
                                 distmat_g_g,
                                 k1=20,
                                 k2=6,
                                 lambda_value=0.3)

    else:
        m, n = qf.size(0), gf.size(0)
        qf_norm = qf / qf.norm(dim=1)[:, None]
        gf_norm = gf / gf.norm(dim=1)[:, None]
        distmat = torch.addmm(1, torch.ones((m, n)), -1, qf_norm,
                              gf_norm.transpose(0, 1))
        distmat = distmat.numpy()

        if args.re_ranking:
            distmat_q_q = torch.addmm(1, torch.ones((m, m)), -1, qf_norm,
                                      qf_norm.transpose(0, 1))
            distmat_q_q = distmat_q_q.numpy()

            distmat_g_g = torch.addmm(1, torch.ones((n, n)), -1, gf_norm,
                                      gf_norm.transpose(0, 1))
            distmat_g_g = distmat_g_g.numpy()

            distmat = re_ranking(distmat,
                                 distmat_q_q,
                                 distmat_g_g,
                                 k1=20,
                                 k2=6,
                                 lambda_value=0.3)

    print("Computing CMC and mAP")
    cmc, mAP = evaluate(distmat,
                        q_pids,
                        g_pids,
                        q_camids,
                        g_camids,
                        use_metric_cuhk03=args.use_metric_cuhk03)

    print("Results ----------")
    print("mAP: {:.1%}".format(mAP))
    print("CMC curve")
    for r in ranks:
        print("Rank-{:<3}: {:.1%}".format(r, cmc[r - 1]))
    print("------------------")

    if args.draw_tsne:
        drawTSNE(qf, gf, q_pids, g_pids, q_camids, g_camids, q_imgPath,
                 g_imgPath, tsne_clusters, args.save_dir)
    if return_distmat:
        return distmat

    if writer != None:
        writer.add_scalars('Testing',
                           dict(rank_1=cmc[0], rank_5=cmc[4], mAP=mAP),
                           epoch + 1)
    return cmc[0]
Esempio n. 3
0
def test(model, queryloader, galleryloader, use_gpu, args,writer,epoch, ranks=[1, 5, 10, 20], return_distmat=False,use_cosine = False,draw_tsne=False,tsne_clusters=3):

    batch_time = AverageMeter()

    model.eval()

    with torch.no_grad():
        qf, q_pids, q_camids = [], [], []
        qf_std = []
        q_imgPath = []
        for batch_idx, (input) in enumerate(queryloader):
            if not args.draw_tsne:
                imgs, pids, camids = input
            else:
                imgs, pids, camids,img_path = input
                q_imgPath.extend(img_path)
            if use_gpu: imgs = imgs.cuda()

            end = time.time()
            features,std = model(imgs)
            batch_time.update(time.time() - end)

            features = features.data.cpu()
            std = std.data.cpu()
            qf.append(features)
            qf_std.append(std)
            q_pids.extend(pids)
            q_camids.extend(camids)
        qf = torch.cat(qf, 0)
        qf_std = torch.cat(qf_std, 0)
        q_pids = np.asarray(q_pids)
        q_camids = np.asarray(q_camids)
        q_imgPath = np.asarray(q_imgPath)

        print("Extracted features for query set, obtained {}-by-{} matrix".format(qf.size(0), qf.size(1)))

        gf, g_pids, g_camids = [], [], []
        #gf_std = []
        g_imgPath = []
        end = time.time()
        for batch_idx, (input) in enumerate(galleryloader):
            if not args.draw_tsne:
                imgs, pids, camids = input
            else:
                imgs, pids, camids,img_path = input
                g_imgPath.extend(img_path)
            if use_gpu: imgs = imgs.cuda()

            end = time.time()
            features,_ = model(imgs)
            batch_time.update(time.time() - end)

            features = features.data.cpu()
            #std = std.data.cpu()
            gf.append(features)
            #gf_std.append(std)
            g_pids.extend(pids)
            g_camids.extend(camids)
        gf = torch.cat(gf, 0)
        #gf_std = torch.cat(gf_std, 0)
        g_pids = np.asarray(g_pids)
        g_camids = np.asarray(g_camids)
        g_imgPath = np.asarray(q_imgPath)

        print("Extracted features for gallery set, obtained {}-by-{} matrix".format(gf.size(0), gf.size(1)))

    print("==> BatchTime(s)/BatchSize(img): {:.3f}/{}".format(batch_time.avg, args.test_batch))
    m, n = qf.size(0), gf.size(0)

    if args.use_ecn:
        distmat= (ECN(qf.numpy(),gf.numpy(),k=25,t=3,q=8,method='rankdist')).transpose()
    elif args.mahalanobis:
        print("Using STD for Mahalanobis distance")
        distmat = torch.zeros((m,n))
        # #pdb.set_trace()
        # qf = qf.data.numpy()
        # gf= gf.data.numpy()
        # qf_std = qf_std.data.numpy()
        # for q_indx in range(int(m)):
        #     distmat[q_indx]= pw(np.expand_dims(qf[q_indx],axis=0),gf,metric='mahalanobis',n_jobs=8, VI=(np.eye(qf_std[q_indx].shape[0])*(1/qf_std[q_indx]).transpose()))
        #     print(q_indx)
        # pdb.set_trace()
        qf = qf / qf_std
        for q_indx in range(int(m)):
            gf_norm = gf * 1/qf_std[q_indx]
            distmat[q_indx] = torch.pow(qf[q_indx], 2).sum(dim=0, keepdim=True).expand(n) + \
                      torch.pow(gf_norm, 2).sum(dim=1, keepdim=True).squeeze()
            distmat[q_indx].unsqueeze(0).addmm_(1, -2, qf[q_indx].unsqueeze(0), gf_norm.t())
        distmat = distmat.numpy()
    elif not (use_cosine or args.use_cosine):

        distmat = torch.pow(qf, 2).sum(dim=1, keepdim=True).expand(m, n) + \
                  torch.pow(gf, 2).sum(dim=1, keepdim=True).expand(n, m).t()
        distmat.addmm_(1, -2, qf, gf.t())
        distmat = distmat.numpy()

        if args.re_ranking:
            distmat_q_q = torch.pow(qf, 2).sum(dim=1, keepdim=True).expand(m, m) + \
                      torch.pow(qf, 2).sum(dim=1, keepdim=True).expand(m, m).t()
            distmat_q_q.addmm_(1, -2, qf, qf.t())
            distmat_q_q = distmat_q_q.numpy()

            distmat_g_g = torch.pow(gf, 2).sum(dim=1, keepdim=True).expand(n, n) + \
                      torch.pow(gf, 2).sum(dim=1, keepdim=True).expand(n, n).t()
            distmat_g_g.addmm_(1, -2, gf, gf.t())
            distmat_g_g = distmat_g_g.numpy()

            print("Normal Re-Ranking")
            distmat = re_ranking(distmat, distmat_q_q, distmat_g_g, k1=20, k2=6, lambda_value=0.3)
    else:
        qf_norm = qf/qf.norm(dim=1)[:,None]
        gf_norm = gf/gf.norm(dim=1)[:,None]
        distmat = torch.addmm(1,torch.ones((m,n)),-1,qf_norm,gf_norm.transpose(0,1))
        distmat = distmat.numpy()

        if args.re_ranking:
            distmat_q_q = torch.addmm(1,torch.ones((m,m)),-1,qf_norm,qf_norm.transpose(0,1))
            distmat_q_q = distmat_q_q.numpy()

            distmat_g_g = torch.addmm(1,torch.ones((n,n)),-1,gf_norm,gf_norm.transpose(0,1))
            distmat_g_g = distmat_g_g.numpy()

            print("Re-Ranking with Cosine")
            distmat = re_ranking(distmat, distmat_q_q, distmat_g_g, k1=20, k2=6, lambda_value=0.3)

    print("Computing CMC and mAP")
    cmc, mAP = evaluate(distmat, q_pids, g_pids, q_camids, g_camids, use_metric_cuhk03=args.use_metric_cuhk03)

    print("Results ----------")
    print("mAP: {:.1%}".format(mAP))
    print("CMC curve")
    for r in ranks:
        print("Rank-{:<3}: {:.1%}".format(r, cmc[r-1]))
    print("------------------")

    if draw_tsne:
        drawTSNE(qf,gf,q_pids, g_pids, q_camids, g_camids,q_imgPath, g_imgPath,tsne_clusters,args.save_dir)
    if return_distmat:
        return distmat


    if writer != None:
        writer.add_scalars(
          'Testing',
          dict(rank_1=cmc[0],
               rank_5 = cmc[4],
               mAP=mAP),
          epoch + 1)
    return cmc[0]