MCPcopy Create free account
hub / github.com/alinlab/SelfPatch / compute_map

Function compute_map

utils.py:723–792  ·  view source on GitHub ↗

Computes the mAP for a given set of returned results. Usage: map = compute_map (ranks, gnd) computes mean average precsion (map) only map, aps, pr, prs = compute_map (ranks, gnd, kappas) computes mean average precision (map), aver

(ranks, gnd, kappas=[])

Source from the content-addressed store, hash-verified

721
722
723def compute_map(ranks, gnd, kappas=[]):
724 """
725 Computes the mAP for a given set of returned results.
726 Usage:
727 map = compute_map (ranks, gnd)
728 computes mean average precsion (map) only
729 map, aps, pr, prs = compute_map (ranks, gnd, kappas)
730 computes mean average precision (map), average precision (aps) for each query
731 computes mean precision at kappas (pr), precision at kappas (prs) for each query
732 Notes:
733 1) ranks starts from 0, ranks.shape = db_size X #queries
734 2) The junk results (e.g., the query itself) should be declared in the gnd stuct array
735 3) If there are no positive images for some query, that query is excluded from the evaluation
736 """
737
738 map = 0.
739 nq = len(gnd) # number of queries
740 aps = np.zeros(nq)
741 pr = np.zeros(len(kappas))
742 prs = np.zeros((nq, len(kappas)))
743 nempty = 0
744
745 for i in np.arange(nq):
746 qgnd = np.array(gnd[i]['ok'])
747
748 # no positive images, skip from the average
749 if qgnd.shape[0] == 0:
750 aps[i] = float('nan')
751 prs[i, :] = float('nan')
752 nempty += 1
753 continue
754
755 try:
756 qgndj = np.array(gnd[i]['junk'])
757 except:
758 qgndj = np.empty(0)
759
760 # sorted positions of positive and junk images (0 based)
761 pos = np.arange(ranks.shape[0])[np.in1d(ranks[:,i], qgnd)]
762 junk = np.arange(ranks.shape[0])[np.in1d(ranks[:,i], qgndj)]
763
764 k = 0;
765 ij = 0;
766 if len(junk):
767 # decrease positions of positives based on the number of
768 # junk images appearing before them
769 ip = 0
770 while (ip < len(pos)):
771 while (ij < len(junk) and pos[ip] > junk[ij]):
772 k += 1
773 ij += 1
774 pos[ip] = pos[ip] - k
775 ip += 1
776
777 # compute ap
778 ap = compute_ap(pos, len(qgnd))
779 map = map + ap
780 aps[i] = ap

Callers

nothing calls this directly

Calls 1

compute_apFunction · 0.85

Tested by

no test coverage detected