def evaluate_all(distmat,
                 query=None,
                 gallery=None,
                 query_ids=None,
                 gallery_ids=None,
                 query_cams=None,
                 gallery_cams=None,
                 cmc_topk=(1, 5, 10)):
    if query is not None and gallery is not None:
        query_ids = [pid for _, pid, _ in query]
        gallery_ids = [pid for _, pid, _ in gallery]
        query_cams = [cam for _, _, cam in query]
        gallery_cams = [cam for _, _, cam in gallery]
    else:
        assert (query_ids is not None and gallery_ids is not None
                and query_cams is not None and gallery_cams is not None)

    # Compute mean AP
    mAP = mean_ap(distmat, query_ids, gallery_ids, query_cams, gallery_cams)
    print('Mean AP: {:4.1%}'.format(mAP))

    # Compute all kinds of CMC scores
    cmc_configs = {
        # 'allshots': dict(separate_camera_set=False,
        #                  single_gallery_shot=False,
        #                  first_match_break=False),
        # 'cuhk03': dict(separate_camera_set=True,
        #                single_gallery_shot=True,
        #                first_match_break=False),
        'market1501':
        dict(separate_camera_set=False,
             single_gallery_shot=False,
             first_match_break=True)
    }
    cmc_scores = {
        name: cmc(distmat, query_ids, gallery_ids, query_cams, gallery_cams,
                  **params)
        for name, params in cmc_configs.items()
    }

    # print('CMC Scores{:>12}{:>12}{:>12}'
    #       .format('allshots', 'cuhk03', 'market1501'))
    # for k in cmc_topk:
    #     print('  top-{:<4}{:12.1%}{:12.1%}{:12.1%}'
    #           .format(k, cmc_scores['allshots'][k - 1],
    #                   cmc_scores['cuhk03'][k - 1],
    #                   cmc_scores['market1501'][k - 1]))

    # Use the allshots cmc top-1 score for validation criterion
    return [cmc_scores['market1501'][k] for k in [0, 4, 9, 19]]
def evaluate_video(distmat,
                   query=None,
                   gallery=None,
                   query_ids=None,
                   gallery_ids=None,
                   query_cams=None,
                   gallery_cams=None,
                   cmc_topk=(1, 5, 10, 20),
                   dataset=None):
    if query is not None and gallery is not None:
        query_ids = [pid for _, pid, _ in query]
        gallery_ids = [pid for _, pid, _ in gallery]
        query_cams = [cam for _, _, cam in query]
        gallery_cams = [cam for _, _, cam in gallery]
    else:
        assert (query_ids is not None and gallery_ids is not None
                and query_cams is not None and gallery_cams is not None)

    mAP = mean_ap(distmat, query_ids, gallery_ids, query_cams, gallery_cams)
    print('Mean AP: {:4.1%}'.format(mAP))

    # Compute all kinds of CMC scores
    if True:
        cmc_configs = {
            'market1501':
            dict(separate_camera_set=False,
                 single_gallery_shot=False,
                 first_match_break=True)
        }
        cmc_scores = {
            name: cmc(distmat, query_ids, gallery_ids, query_cams,
                      gallery_cams, **params)
            for name, params in cmc_configs.items()
        }

        print('CMC Scores{:>12}'.format('market1501'))
        for k in cmc_topk:
            print('  top-{:<4}{:12.1%}'.format(k, cmc_scores['market1501'][k -
                                                                           1]))
        return cmc_scores['market1501'][0], mAP
    def evaluate_1st(self, distmat, query=None, gallery=None):
        if query is not None and gallery is not None:
            query_ids = [pid for _, pid, _ in query]
            gallery_ids = [pid for _, pid, _ in gallery]
            query_cams = [cam for _, _, cam in query]
            gallery_cams = [cam for _, _, cam in gallery]
        else:
            raise RuntimeError(
                'please provide the query and gallery information')
        distmat = to_numpy(distmat)
        mAP = mean_ap(distmat, query_ids, gallery_ids, query_cams,
                      gallery_cams)
        print('Mean AP: {:4.1%}'.format(mAP))

        indices = np.argsort(distmat, axis=1)
        indices = np.argsort(indices, axis=1)
        mask = (indices < self.select_size).astype(float)

        cmc_configs = {
            # 'allshots': dict(separate_camera_set=False,
            #                  single_gallery_shot=False,
            #                  first_match_break=False),
            # 'cuhk03': dict(separate_camera_set=True,
            #                single_gallery_shot=True,
            #                first_match_break=False),
            'market1501':
            dict(separate_camera_set=False,
                 single_gallery_shot=False,
                 first_match_break=True)
        }
        cmc_scores = {
            name: cmc(distmat, query_ids, gallery_ids, query_cams,
                      gallery_cams, **params)
            for name, params in cmc_configs.items()
        }

        return [cmc_scores['market1501'][k] for k in [0, 4, 9, 19]], mask
def evaluate_all(distmat,
                 query=None,
                 gallery=None,
                 query_ids=None,
                 gallery_ids=None,
                 query_cams=None,
                 gallery_cams=None,
                 cmc_topk=(1, 5, 10)):
    if query is not None and gallery is not None:
        query_ids = [pid for _, pid, _ in query]
        gallery_ids = [pid for _, pid, _ in gallery]
        query_cams = [cam for _, _, cam in query]
        gallery_cams = [cam for _, _, cam in gallery]
    else:
        assert (query_ids is not None and gallery_ids is not None
                and query_cams is not None and gallery_cams is not None)

    # Compute mean AP
    print(distmat)
    distnp = distmat.cpu().numpy()
    np.save('dist_demo', distnp)

    mAP = mean_ap(distmat, query_ids, gallery_ids, query_cams, gallery_cams)
    print('Mean AP: {:4.1%}'.format(mAP))

    # Compute all kinds of CMC scores
    cmc_configs = {
        # 'allshots': dict(separate_camera_set=False,
        #                  single_gallery_shot=False,
        #                  first_match_break=False),
        # 'cuhk03': dict(separate_camera_set=True,
        #                single_gallery_shot=True,
        #                first_match_break=False),
        'market1501':
        dict(separate_camera_set=False,
             single_gallery_shot=False,
             first_match_break=True)
    }
    cmc_scores = {
        name: cmc(distmat, query_ids, gallery_ids, query_cams, gallery_cams,
                  **params)
        for name, params in cmc_configs.items()
    }

    # print('CMC Scores{:>12}{:>12}{:>12}'
    #       .format('allshots', 'cuhk03', 'market1501'))
    # for k in cmc_topk:
    #     print('  top-{:<4}{:12.1%}{:12.1%}{:12.1%}'
    #           .format(k, cmc_scores['allshots'][k - 1],
    #                   cmc_scores['cuhk03'][k - 1],
    #                   cmc_scores['market1501'][k - 1]))

    # Use the allshots cmc top-1 score for validation criterion
    print([cmc_scores['market1501'][k] for k in [0, 4, 9, 19]])

    ############### multiple query-images #######################

    joint_query = list(zip(query_ids, query_cams))
    summarize_index = list(set(joint_query))

    query_num = len(summarize_index)
    gallery_num = len(gallery_ids)
    sum_distmat = torch.zeros(query_num, gallery_num).cuda()
    for newind, sumind in enumerate(summarize_index):
        sum_indpos = [
            posx for posx, x in enumerate(joint_query) if x == sumind
        ]
        ### mean results
        select_results = torch.index_select(
            distmat, 0,
            torch.LongTensor(sum_indpos).cuda())
        select_mean = torch.mean(select_results, 0)
        sum_distmat[newind] = select_mean

    ## QUERY CAMERA

    multiquery_ids, multiquery_cams = zip(*summarize_index)
    multiquery_ids = list(multiquery_ids)
    multiquery_cams = list(multiquery_cams)

    multimAP = mean_ap(sum_distmat, multiquery_ids, gallery_ids,
                       multiquery_cams, gallery_cams)
    print('Mean AP: {:4.1%}'.format(multimAP))

    # Compute all kinds of CMC scores
    multicmc_configs = {
        # 'allshots': dict(separate_camera_set=False,
        #                  single_gallery_shot=False,
        #                  first_match_break=False),
        # 'cuhk03': dict(separate_camera_set=True,
        #                single_gallery_shot=True,
        #                first_match_break=False),
        'market1501':
        dict(separate_camera_set=False,
             single_gallery_shot=False,
             first_match_break=True)
    }
    multicmc_scores = {
        name: cmc(sum_distmat, multiquery_ids, gallery_ids, multiquery_cams,
                  gallery_cams, **params)
        for name, params in multicmc_configs.items()
    }

    # print('CMC Scores{:>12}{:>12}{:>12}'
    #       .format('allshots', 'cuhk03', 'market1501'))
    # for k in cmc_topk:
    #     print('  top-{:<4}{:12.1%}{:12.1%}{:12.1%}'
    #           .format(k, cmc_scores['allshots'][k - 1],
    #                   cmc_scores['cuhk03'][k - 1],
    #                   cmc_scores['market1501'][k - 1]))

    # Use the allshots cmc top-1 score for validation criterion
    print([multicmc_scores['market1501'][k] for k in [0, 4, 9, 19]])

    return [cmc_scores['market1501'][k] for k in [0, 4, 9, 19]]