예제 #1
0
def test(model, queryloader, galleryloader):
    since = time.time()
    model.eval()
    # Extract features for query set
    qf, q_pids, q_camids = extract_feature(model, queryloader)
    print("Extracted features for query set, obtained {} matrix".format(
        qf.shape))
    # Extract features for gallery set
    gf, g_pids, g_camids = extract_feature(model, galleryloader)
    print("Extracted features for gallery set, obtained {} matrix".format(
        gf.shape))
    time_elapsed = time.time() - since
    print('Extracting features complete in {:.0f}m {:.0f}s'.format(
        time_elapsed // 60, time_elapsed % 60))
    # Compute distance matrix between query and gallery
    m, n = qf.size(0), gf.size(0)
    distmat = torch.zeros((m, n))
    if config.TEST.DISTANCE == 'euclidean':
        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()
        for i in range(m):
            distmat[i:i + 1].addmm_(1, -2, qf[i:i + 1], gf.t())
    else:
        # Cosine similarity
        qf = F.normalize(qf, p=2, dim=1)
        gf = F.normalize(gf, p=2, dim=1)
        for i in range(m):
            distmat[i] = -torch.mm(qf[i:i + 1], gf.t())
    distmat = distmat.numpy()

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

    print("Results ----------------------------------------")
    print('top1:{:.1%} top5:{:.1%} top10:{:.1%} mAP:{:.1%}'.format(
        cmc[0], cmc[4], cmc[9], mAP))
    print("------------------------------------------------")

    return cmc[0]
def test_rerank(model, queryloader, galleryloader, pool, use_gpu, ranks=[1, 5, 10, 20], lamb=0.3, parameters=None):
    model.eval()
    qf, q_pids, q_camids = [], [], []
    for batch_idx, (imgs, pids, camids) in enumerate(queryloader):
        if use_gpu:
            imgs = imgs.cuda()
        imgs = Variable(imgs, volatile=True)
        b, n, s, c, h, w = imgs.size()
        assert(b==1)
        imgs = imgs.view(b*n, s, c, h, w)
        features = model(imgs)
        features = features.view(n, -1)
        features = torch.mean(features, 0)
        features = features.data.cpu()
        qf.append(features)
        q_pids.extend(pids)
        q_camids.extend(camids)
    qf = torch.stack(qf)
    q_pids = np.asarray(q_pids)
    q_camids = np.asarray(q_camids)
    print("Extracted features for query set, obtained {}-by-{} matrix".format(qf.size(0), qf.size(1)))
    gf, g_pids, g_camids = [], [], []
    for batch_idx, (imgs, pids, camids) in enumerate(galleryloader):
        if use_gpu:
            imgs = imgs.cuda()
        imgs = Variable(imgs, volatile=True)
        b, n, s, c, h, w = imgs.size()
        imgs = imgs.view(b*n, s , c, h, w)
        assert(b==1)
        features = model(imgs)
        features = features.view(n, -1)
        if pool == 'avg':
            features = torch.mean(features, 0)
        else:
            features, _ = torch.max(features, 0)
        features = features.data.cpu()
        gf.append(features)
        g_pids.extend(pids)
        g_camids.extend(camids)
    gf = torch.stack(gf)
    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("Computing distance matrix")
    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()
    gf = gf.numpy()
    qf = qf.numpy()
    distmat_rerank = re_ranking(qf,gf , lambda_value=lamb)
    print("Original Computing CMC and mAP")
    cmc, mAP = evaluate(distmat, q_pids, g_pids, q_camids, g_camids)
    print("Rerank Computing CMC and mAP")
    re_rank_cmc, re_rank_mAP = evaluate(distmat_rerank, q_pids, g_pids, q_camids, g_camids)
    # print("Results ---------- {:.1%} ".format(distmat_rerank))
    print (parameters)
    print("Results ---------- ")
    if 'mars' in args.dataset :
        print("mAP: {:.1%} vs {:.1%}".format(mAP, re_rank_mAP))
    print("CMC curve")
    for r in ranks:
        print("Rank-{:<3}: {:.1%} vs {:.1%}".format(r, cmc[r-1], re_rank_cmc[r-1]))
    print("------------------")
    del qf, q_pids, q_camids
    del gf, g_pids, g_camids
    del distmat , distmat_rerank
    if 'mars' not in args.dataset :
        print("Dataset not MARS : instead", args.dataset)
        return cmc[0]
    else:
        if args.focus == "map":
            print("returning map")
            return mAP
        else:
            print("returning re-rank")
            return re_rank_mAP
예제 #3
0
def test(queryset, galleryset, dataset,ranks=[1, 5, 10, 20]):
    since=time.time()
    qf, q_pids, q_camids = [], [], []
    batch_size=args.test_batch
    query_img, query_id, query_cam_id = map(list, zip(*dataset.query))
    indicies=np.arange(len(query_id))
    for i in range(len(indicies) // batch_size):
        try:
            test_batch = queryset.__getbatch__(indicies[i * batch_size:(i + 1) * batch_size])
        except:
            test_batch = queryset.__getbatch__(indicies[-batch_size:])
        feat=query_job(test_batch[0])
        qf.append(feat)
        q_pids.extend(test_batch[1].astype(np.float32))
        q_camids.extend(test_batch[2])
    qf=np.concatenate(qf,0)
    q_pids=np.asarray(q_pids)
    q_camids=np.asarray(q_camids)
    print("Extracted features for query set, obtained {} matrix".format(qf.shape))
    
    gf, g_pids, g_camids = [], [], []
    gallery_img, gallery_id, gallery_cam_id = map(list, zip(*dataset.gallery))
    indicies=np.arange(len(gallery_id))
    for i in range(len(indicies) // batch_size):
        try:
            gallery_batch = galleryset.__getbatch__(indicies[i * batch_size:(i + 1) * batch_size])
        except:
            gallery_batch = galleryset.__getbatch__(indicies[-batch_size:])
        feat=query_job(gallery_batch[0])
        gf.append(feat)
        g_pids.extend(gallery_batch[1].astype(np.float32))
        g_camids.extend(gallery_batch[2])
    gf=np.concatenate(gf,0)
    g_pids = np.asarray(g_pids)
    g_camids = np.asarray(g_camids)
    if args.dataset == 'mars':
        gf=np.concatenate((qf,gf),0)
        g_pids = np.append(q_pids, g_pids)
        g_camids = np.append(q_camids, g_camids)

    print("Extracted features for gallery set, obtained {} matrix".format(gf.shape))
    time_elapsed = time.time() - since
    print('Extracting features complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))

    print("Computing distance matrix")
    m, n = qf.shape[0], gf.shape[0]
    distmat = np.zeros((m,n))
    if args.distance== 'euclidean':
        distmat1=np.power(qf,2)
        distmat2=np.power(gf,2)
        distmat1=np.sum(distmat1,axis=1,keepdims=True)
        distmat2=np.sum(distmat2,axis=1,keepdims=True)
        distmat1=np.broadcast_to(distmat1,(m,n))
        distmat2=np.broadcast_to(distmat2,(n,m))
        distmat2=np.transpose(distmat2)
        distmat=distmat2+distmat1
        tempgf=np.transpose(gf)

        for i in range(m):
            distmat[i:i+1]=addmm(
                distmat[i:i+1],qf[i:i+1],tempgf,1,-2
            )
    else:
        q_norm=np.linalg.norm(qf,ord=2,axis=1,keepdims=True)
        g_norm=np.linalg.norm(gf,ord=2,axis=1,keepdims=True)
        q_norm=np.broadcast_to(q_norm,qf.shape)
        g_norm=np.broadcast_to(g_norm,gf.shape)
        gf=np.divide(gf,g_norm)
        qf=np.divide(qf,q_norm)
        tempgf=np.transpose(gf)
        for i in range(m):
            distmat[i] = - np.matmul(qf[i:i+1],tempgf)
    print("Computing CMC and mAP")
    cmc, mAP = evaluate(distmat, q_pids, g_pids, q_camids, g_camids)

    print("Results ----------")
    print('top1:{:.1%} top5:{:.1%} top10:{:.1%} mAP:{:.1%}'.format(cmc[0],cmc[4],cmc[9],mAP))
    print("------------------")
    return cmc[0]
예제 #4
0
def test(model, queryloader, galleryloader, use_gpu):
    since = time.time()
    model.eval()

    qf, q_pids, q_camids = [], [], []
    for batch_idx, (vids, pids, camids) in enumerate(queryloader):
        if (batch_idx + 1) % 1000 == 0:
            print("{}/{}".format(batch_idx + 1, len(queryloader)))

        qf.append(extract(model, vids, use_gpu).squeeze())
        q_pids.extend(pids)
        q_camids.extend(camids)

    qf = torch.stack(qf)
    q_pids = np.asarray(q_pids)
    q_camids = np.asarray(q_camids)
    print("Extracted features for query set, obtained {} matrix".format(
        qf.shape))

    gf, g_pids, g_camids = [], [], []
    for batch_idx, (vids, pids, camids) in enumerate(galleryloader):
        if (batch_idx + 1) % 1000 == 0:
            print("{}/{}".format(batch_idx + 1, len(galleryloader)))

        gf.append(extract(model, vids, use_gpu).squeeze())
        g_pids.extend(pids)
        g_camids.extend(camids)
    gf = torch.stack(gf)
    g_pids = np.asarray(g_pids)
    g_camids = np.asarray(g_camids)

    if args.dataset == 'mars':
        # gallery set must contain query set, otherwise 140 query imgs will not have ground truth.
        gf = torch.cat((qf, gf), 0)
        g_pids = np.append(q_pids, g_pids)
        g_camids = np.append(q_camids, g_camids)

    print("Extracted features for gallery set, obtained {} matrix".format(
        gf.shape))

    time_elapsed = time.time() - since
    print('Extracting features complete in {:.0f}m {:.0f}s'.format(
        time_elapsed // 60, time_elapsed % 60))

    print("Computing distance matrix")
    m, n = qf.size(0), gf.size(0)
    distmat = torch.zeros((m, n))

    if args.distance == 'euclidean':
        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()
        for i in range(m):
            distmat[i:i + 1].addmm_(1, -2, qf[i:i + 1], gf.t())
    else:
        q_norm = torch.norm(qf, p=2, dim=1, keepdim=True)
        g_norm = torch.norm(gf, p=2, dim=1, keepdim=True)
        qf = qf.div(q_norm.expand_as(qf))
        gf = gf.div(g_norm.expand_as(gf))
        for i in range(m):
            distmat[i] = -torch.mm(qf[i:i + 1], gf.t())
    distmat = distmat.numpy()

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

    print("Results ----------")
    print('top1:{:.1%} top5:{:.1%} top10:{:.1%} mAP:{:.1%}'.format(
        cmc[0], cmc[4], cmc[9], mAP))
    print("------------------")

    return cmc[0]