def evaluate(self): features = torch.cat(self.features, dim=0) spatial_features = np.vstack(self.spatial_features) scores = torch.cat(self.scores, dim=0) # query feature, person ids and camera ids query_features = features[:self._num_query] query_pids = np.asarray(self.pids[:self._num_query]) query_camids = np.asarray(self.camids[:self._num_query]) # gallery features, person ids and camera ids gallery_features = features[self._num_query:] gallery_pids = np.asarray(self.pids[self._num_query:]) gallery_camids = np.asarray(self.camids[self._num_query:]) dist = 1 - torch.mm(query_features, gallery_features.t()).numpy() logger.info("Testing without DSR setting") self._results = OrderedDict() if self.cfg.TEST.DSR.ENABLED: topk = self.cfg.TEST.DSR.TOPK dist = compute_dsr_dist(spatial_features[:self._num_query], spatial_features[self._num_query:], dist, scores[:self._num_query], topk) logger.info("Testing with DSR setting") cmc, all_AP, all_INP = evaluate_rank(dist, query_pids, gallery_pids, query_camids, gallery_camids) mAP = np.mean(all_AP) mINP = np.mean(all_INP) self._results['R-1'] = cmc[0] self._results['mAP'] = mAP self._results['mINP'] = mINP return copy.deepcopy(self._results)
def evaluate(self): features = torch.cat(self.features, dim=0) spatial_features = np.vstack(self.spatial_features) scores = torch.cat(self.scores, dim=0) # query feature, person ids and camera ids query_features = features[:self._num_query] query_pids = np.asarray(self.pids[:self._num_query]) query_camids = np.asarray(self.camids[:self._num_query]) # gallery features, person ids and camera ids gallery_features = features[self._num_query:] gallery_pids = np.asarray(self.pids[self._num_query:]) gallery_camids = np.asarray(self.camids[self._num_query:]) self._results = OrderedDict() query_features = F.normalize(query_features, dim=1) gallery_features = F.normalize(gallery_features, dim=1) dist = 1 - torch.mm(query_features, gallery_features.t()).numpy() if self.cfg.TEST.DSR.ENABLED: dsr_dist = get_dsr_dist(spatial_features[:self._num_query], spatial_features[self._num_query:], dist, scores[:self._num_query]) logger.info("Test with DSR setting") lamb = self.cfg.TEST.DSR.LAMB dist = (1 - lamb) * dist + lamb * dsr_dist cmc, all_AP, all_INP = evaluate_rank(dist, query_pids, gallery_pids, query_camids, gallery_camids) mAP = np.mean(all_AP) mINP = np.mean(all_INP) for r in [1, 5, 10]: self._results['Rank-{}'.format(r)] = cmc[r - 1] self._results['mAP'] = mAP self._results['mINP'] = mINP tprs = evaluate_roc(dist, query_pids, gallery_pids, query_camids, gallery_camids) fprs = [1e-4, 1e-3, 1e-2] for i in range(len(fprs)): self._results["TPR@FPR={}".format(fprs[i])] = tprs[i] return copy.deepcopy(self._results)
def evaluate(self): if comm.get_world_size() > 1: comm.synchronize() features = comm.gather(self.features) features = sum(features, []) spatial_features = comm.gather(self.spatial_features) spatial_features = sum(spatial_features, []) scores = comm.gather(self.scores) scores = sum(scores, []) pids = comm.gather(self.pids) pids = sum(pids, []) camids = comm.gather(self.camids) camids = sum(camids, []) # fmt: off if not comm.is_main_process(): return {} # fmt: on else: features = self.features spatial_features = self.spatial_features scores = self.scores pids = self.pids camids = self.camids features = torch.cat(features, dim=0) spatial_features = torch.cat(spatial_features, dim=0).numpy() scores = torch.cat(scores, dim=0) # query feature, person ids and camera ids query_features = features[:self._num_query] query_pids = np.asarray(pids[:self._num_query]) query_camids = np.asarray(camids[:self._num_query]) # gallery features, person ids and camera ids gallery_features = features[self._num_query:] gallery_pids = np.asarray(pids[self._num_query:]) gallery_camids = np.asarray(camids[self._num_query:]) if self.cfg.TEST.METRIC == "cosine": query_features = F.normalize(query_features, dim=1) gallery_features = F.normalize(gallery_features, dim=1) dist = 1 - torch.mm(query_features, gallery_features.t()).numpy() self._results = OrderedDict() query_features = query_features.numpy() gallery_features = gallery_features.numpy() if self.cfg.TEST.DSR.ENABLED: logger.info("Testing with DSR setting") dsr_dist = compute_dsr_dist(spatial_features[:self._num_query], spatial_features[self._num_query:], dist, scores[:self._num_query]) max_value = 0 k = 0 for i in range(0, 101): lamb = 0.01 * i dist1 = (1 - lamb) * dist + lamb * dsr_dist cmc, all_AP, all_INP = evaluate_rank(dist1, query_pids, gallery_pids, query_camids, gallery_camids) if (cmc[0] > max_value): k = lamb max_value = cmc[0] dist1 = (1 - k) * dist + k * dsr_dist cmc, all_AP, all_INP = evaluate_rank(dist1, query_pids, gallery_pids, query_camids, gallery_camids) else: cmc, all_AP, all_INP = evaluate_rank(dist, query_pids, gallery_pids, query_camids, gallery_camids) mAP = np.mean(all_AP) mINP = np.mean(all_INP) for r in [1, 5, 10]: self._results['Rank-{}'.format(r)] = cmc[r - 1] * 100 self._results['mAP'] = mAP * 100 self._results['mINP'] = mINP * 100 return copy.deepcopy(self._results)
def evaluate(self): if comm.get_world_size() > 1: comm.synchronize() features = comm.gather(self.features) features = sum(features, []) spatial_features = comm.gather(self.spatial_features) spatial_features = sum(spatial_features, []) scores = comm.gather(self.scores) scores = sum(scores, []) pids = comm.gather(self.pids) pids = sum(pids, []) camids = comm.gather(self.camids) camids = sum(camids, []) # fmt: off if not comm.is_main_process(): return {} # fmt: on else: features = self.features spatial_features = self.spatial_features scores = self.scores pids = self.pids camids = self.camids features = torch.cat(features, dim=0) spatial_features = torch.cat(spatial_features, dim=0).numpy() scores = torch.cat(scores, dim=0) # query feature, person ids and camera ids query_features = features[:self._num_query] query_pids = np.asarray(pids[:self._num_query]) query_camids = np.asarray(camids[:self._num_query]) # gallery features, person ids and camera ids gallery_features = features[self._num_query:] gallery_pids = np.asarray(pids[self._num_query:]) gallery_camids = np.asarray(camids[self._num_query:]) if self.cfg.TEST.METRIC == "cosine": query_features = F.normalize(query_features, dim=1) gallery_features = F.normalize(gallery_features, dim=1) dist = 1 - torch.mm(query_features, gallery_features.t()).numpy() self._results = OrderedDict() query_features = query_features.numpy() gallery_features = gallery_features.numpy() if self.cfg.TEST.DSR.ENABLED: logger.info("Testing with DSR setting") dist = compute_dsr_dist(spatial_features[:self._num_query], spatial_features[self._num_query:], dist, scores[:self._num_query]) cmc, all_AP, all_INP = evaluate_rank(dist, query_features, gallery_features, query_pids, gallery_pids, query_camids, gallery_camids, use_distmat=True) else: cmc, all_AP, all_INP = evaluate_rank(dist, query_features, gallery_features, query_pids, gallery_pids, query_camids, gallery_camids, use_distmat=False) mAP = np.mean(all_AP) mINP = np.mean(all_INP) for r in [1, 5, 10]: self._results['Rank-{}'.format(r)] = cmc[r - 1] self._results['mAP'] = mAP self._results['mINP'] = mINP if self.cfg.TEST.ROC_ENABLED: scores, labels = evaluate_roc(dist, query_features, gallery_features, query_pids, gallery_pids, query_camids, gallery_camids) fprs, tprs, thres = metrics.roc_curve(labels, scores) for fpr in [1e-4, 1e-3, 1e-2]: ind = np.argmin(np.abs(fprs - fpr)) self._results["TPR@FPR={:.0e}".format(fpr)] = tprs[ind] return copy.deepcopy(self._results)
print("=> Check precision") num_q = 30 num_g = 300 dim = 512 max_rank = 5 q_feats = np.random.rand(num_q, dim).astype(np.float32) * 20 q_feats = q_feats / np.linalg.norm(q_feats, ord=2, axis=1, keepdims=True) g_feats = np.random.rand(num_g, dim).astype(np.float32) * 20 g_feats = g_feats / np.linalg.norm(g_feats, ord=2, axis=1, keepdims=True) distmat = 1 - np.dot(q_feats, g_feats.transpose()) q_pids = np.random.randint(0, num_q, size=num_q) g_pids = np.random.randint(0, num_g, size=num_g) q_camids = np.random.randint(0, 5, size=num_q) g_camids = np.random.randint(0, 5, size=num_g) cmc_py, mAP_py, mINP_py = evaluate_rank(distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_cython=False) cmc_cy, mAP_cy, mINP_cy = evaluate_rank(distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_cython=True) np.testing.assert_allclose(cmc_py, cmc_cy, rtol=1e-3, atol=1e-6) np.testing.assert_allclose(mAP_py, mAP_cy, rtol=1e-3, atol=1e-6) np.testing.assert_allclose(mINP_py, mINP_cy, rtol=1e-3, atol=1e-6) print('Rank results between python and cython are the same!') scores_cy, labels_cy = evaluate_roc(distmat, q_pids, g_pids, q_camids, g_camids, use_cython=True) scores_py, labels_py = evaluate_roc(distmat, q_pids, g_pids, q_camids, g_camids, use_cython=False) np.testing.assert_allclose(scores_cy, scores_py, rtol=1e-3, atol=1e-6) np.testing.assert_allclose(labels_cy, labels_py, rtol=1e-3, atol=1e-6) print('ROC results between python and cython are the same!\n')