num_g = 1000 distmat = np.random.rand(num_q, num_g) * 20 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) end = time.time() cmc, mAP = eval_market1501_wrap(distmat, q_pids, g_pids, q_camids, g_camids, 10) elapsed_cython = time.time() - end print("=> Cython evaluation") print("consume time {:.5f} \n mAP is {} \n cmc is {}".format(elapsed_cython, mAP, cmc)) end = time.time() cmc, mAP = eval_market1501(distmat, q_pids, g_pids, q_camids, g_camids, 10) elapsed_python = time.time() - end print("=> Python evaluation") print("consume time {:.5f} \n mAP is {} \n cmc is {}".format(elapsed_python, mAP, cmc)) xtimes = elapsed_python / elapsed_cython print("=> Conclusion: cython is {:.2f}x faster than python".format(xtimes))
from eval_metrics import eval_market1501 import numpy as np import time num_q = 100 num_g = 1000 distmat = np.random.rand(num_q, num_g) * 20 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) end = time.time() cmc, mAP = eval_market1501_wrap(distmat, q_pids, g_pids, q_camids, g_camids, 10) elapsed_cython = time.time() - end print("=> Cython evaluation") print("consume time {:.5f} \n mAP is {} \n cmc is {}".format( elapsed_cython, mAP, cmc)) end = time.time() cmc, mAP = eval_market1501(distmat, q_pids, g_pids, q_camids, g_camids, 10) elapsed_python = time.time() - end print("=> Python evaluation") print("consume time {:.5f} \n mAP is {} \n cmc is {}".format( elapsed_python, mAP, cmc)) xtimes = elapsed_python / elapsed_cython print("=> Conclusion: cython is {:.2f}x faster than python".format(xtimes))
def test(model, queryloader, galleryloader, ranks=[1, 5, 10, 20]): # 设定模型为测试模式 model.eval() # with torch.no_grad():使with中的语句不会计算梯度,计算过程不会在反向传播中被记录 with torch.no_grad(): # 以下都是列表。qf存储全局特征;q_pids存储行人ID;q_camids存储摄像机ID;lqf存储局部特征 qf, q_pids, q_camids, lqf = [], [], [], [] for batch_idx, (imgs, pids, camids) in enumerate(queryloader): features, local_features = model(imgs) # 计算全局特征与局部特征 qf.append(features) lqf.append(local_features) q_pids.extend(pids) q_camids.extend(camids) qf = torch.cat(qf, 0) # 将全局特征按列形成一个张量 lqf = torch.cat(lqf, 0) # 将局部特征按列形成一个张量 q_pids = np.asarray(q_pids) # 将行人ID列表转成np数组 q_camids = np.asarray(q_camids) # 将摄像机ID列表转成np数组 print("Extracted features for query set, obtained {}-by-{} matrix". format(qf.size(0), qf.size(1))) # 以下都是列表。gf存储全局特征;g_pids存储行人ID;g_camids存储摄像机ID;lgf存储局部特征 gf, g_pids, g_camids, lgf = [], [], [], [] for batch_idx, (imgs, pids, camids) in enumerate(galleryloader): features, local_features = model(imgs) # 计算全局特征与局部特征 gf.append(features) lgf.append(local_features) g_pids.extend(pids) g_camids.extend(camids) gf = torch.cat(gf, 0) # 将全局特征按列形成一个张量 lgf = torch.cat(lgf, 0) # 将局部特征按列形成一个张量 g_pids = np.asarray(g_pids) # 将行人ID列表转成np数组 g_camids = np.asarray(g_camids) # 将摄像机ID列表转成np数组 print("Extracted features for gallery set, obtained {}-by-{} matrix". format(gf.size(0), gf.size(1))) # 特征标准化 qf = 1. * qf / (torch.norm(qf, 2, dim=-1, keepdim=True).expand_as(qf) + 1e-12) gf = 1. * gf / (torch.norm(gf, 2, dim=-1, keepdim=True).expand_as(gf) + 1e-12) """ 计算query特征与gallery特征的距离矩阵 全局特征计算欧氏距离,矩阵A,B欧氏距离等于√(A^2 + (B^T)^2 - 2A(B^T)) 局部特征使用局部对齐最小距离算法计算距离 """ m, n = qf.size(0), gf.size(0) # 计算A^2 + (B^T)^2 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() # 计算A^2 + (B^T)^2 - 2A(B^T) distmat.addmm_(1, -2, qf, gf.t()) # 计算√(A^2 + (B^T)^2 - 2A(B^T)) distance = distmat.clamp(min=1e-12).sqrt() # 转换成numpy数组 global_distmat = distmat.numpy() lqf = lqf.permute(0, 2, 1) lgf = lgf.permute(0, 2, 1) # 计算局部特征距离矩阵 local_distmat = low_memory_local_dist(lqf.numpy(), lgf.numpy(), aligned=aligned) # 得到包含全局特征与局部特征的总距离矩阵 print("Using global and local branches") distmat = local_distmat + global_distmat print("Computing CMC and mAP") # 使用评估函数计算cmc曲线中的Rank-N与mAP平均准确率均值 cmc, mAP = eval_market1501(distmat, q_pids, g_pids, q_camids, g_camids) # 打印相关性能测试结果 print("Results ----------") print("mAP: {:.1%}".format(mAP)) print("CMC curve") for r in ranks: print("Rank-{:<3}: {:.1%}".format(r, cmc[r - 1])) print("-------------------") return 0