def pre(self): json_file = 'results/result_' + time.strftime("%Y%m%d%H%M%S") + '.json' json_file_rk = 'results/result_' + time.strftime( "%Y%m%d%H%M%S") + '_rk.json' self.model.eval() t1 = time.time() print('extract features, this may take a few minutes') feats = extract_feature(self.model, tqdm(self.test_loader)) print('feats:', feats.shape) qf = feats[:self.num_query] gf = feats[self.num_query:] print('qf,gf:', qf.shape, gf.shape) m, n = qf.shape[0], gf.shape[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.cpu().numpy() self.writeResult(distmat, json_file) print('re_ranking ...') distmat_rk = reRanking(qf, gf, 7, 3, 0.85) self.writeResult(distmat_rk, json_file_rk) print('Time cost is: {:.2f} s'.format(time.time() - t1)) print('over!')
def ensemble(models): feats = [] for index, model in enumerate(models): feat = get_feat(model) if index == 0: feats = feat else: feats = torch.cat((feats, feat), 1) print('feats:', feats.shape) # query_paths = data.query_paths num_query = len(data.query_paths) qf = feats[:num_query] gf = feats[num_query:] print('qf,gf:', qf.shape, gf.shape) m, n = qf.shape[0], gf.shape[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.cpu().numpy() print('re_ranking ...') distmat_rk = reRanking(qf, gf, 7, 3, 0.85) writeResult(data, distmat_rk, 'results/result_{}_200.json'.format(time_str), 200) writeResult(data, distmat_rk, 'results/result_{}_300.json'.format(time_str), 300)
def pre(self): json_file = 'results/result_' + time.strftime("%Y%m%d%H%M%S") + '.json' json_file_rk = 'results/result_' + time.strftime( "%Y%m%d%H%M%S") + '_rk.json' self.model.eval() t1 = time.time() print('extract features, this may take a few minutes') feats = extract_feature(self.model, tqdm(self.test_loader)) print('feats:', feats.shape) qf = feats[:self.num_query] gf = feats[self.num_query:] print('qf,gf:', qf.shape, gf.shape) m, n = qf.shape[0], gf.shape[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.cpu().numpy() self.writeResult(distmat, json_file) print('re_ranking ...') distmat_rk = reRanking(qf, gf, 7, 3, 0.85) self.writeResult(distmat_rk, json_file_rk) # print('distmat:', distmat.shape) # index = np.argsort(distmat) # from small to large # max_index = index[:, :200] # print(max_index.shape) # # # Visualize the rank result # results = {} # json_file = 'results/result_' + time.strftime("%Y%m%d%H%M%S") + '.json' # json_file_rk = 'results/result_' + time.strftime("%Y%m%d%H%M%S") + '_rk.json' # for i in range(len(self.query_paths)): # query_name = self.query_paths[i].split('/')[-1] # index_mask = max_index[i] # gallery_name = [self.gallery_paths[k].split('/')[-1] for k in index_mask] # #print(index_mask) # # results[query_name] = gallery_name # #print(res) # # with open(json_file, 'w', encoding='utf-8') as fp: # json.dump(results, fp) print('Time cost is: {:.2f} s'.format(time.time() - t1)) print('over!')
def writeResult_rk(distmat, qf, gf, k1, k2, lambda_value, num=100, part=5, dis_th=0.5, json_file='part_res.json'): results = {} st0 = time.time() print('start part reranking, ini distmat:', distmat.shape) print('distmat mean:', np.mean(distmat)) print('selet num:', num) print('selet part:', part) print('selet dis_th:', dis_th) index = np.argsort(distmat) # from small to large print('argsort cost time:{} s'.format(time.time() - st0)) st0 = time.time() max_index = index[:, :num] print(max_index.shape) for ii in range(part): num_ = qf.size(0) // part start = ii * num_ end = (ii + 1) * num_ gf_indexs = [] for jj in range(start, end): gf_indexs.extend(max_index[jj, :].tolist()) # temp_inds = [dis_ind for dis_ind in max_index[jj, :] if distmat[jj, dis_ind] < dis_th] # gf_indexs.extend(temp_inds) gf_indexs = list(set(gf_indexs)) gf_indexs.sort() gf_indexs = np.array(gf_indexs) if ii != part - 1: qf_ = qf[start:end, :] query_paths_ = data.query_paths[start:end] gallery_paths_ = [data.gallery_paths[k] for k in gf_indexs] else: qf_ = qf[start:, :] query_paths_ = data.query_paths[start:] gallery_paths_ = [data.gallery_paths[k] for k in gf_indexs] gf_ = gf[gf_indexs, :] print('part-({}/{}), qf gf size:{} {}'.format(ii + 1, part, qf_.size(), gf_.size())) distmat_rk_ = reRanking(qf_, gf_, k1, k2, lambda_value) writeResult_part(results, distmat_rk_, query_paths_, gallery_paths_, num=200) print('part-({}/{}), cost time:{} s'.format(ii + 1, part, time.time() - st0)) st0 = time.time() with open(json_file, 'w', encoding='utf-8') as fp: json.dump(results, fp) print('save result :', json_file)