from rankutils.utilities import safe_create_dir import numpy as np ranks_to_convert = OrderedDict(places365=[1, 2], vggfaces=[1, 2], imagenet=[1, 3], unicamp=[1, 2, 4]) if __name__ == "__main__": fvname = sys.argv[1] outfvname = sys.argv[2] nk = int(sys.argv[3]) pathcfg = cfgloader("../source/path_2.cfg") for key, values in ranks_to_convert.items(): for v in values: expkey = "{0:s}_{1:03d}".format(key, v) print("converting: {0:s} - {1:s}".format(expkey, fvname)) sfvdir = pathcfg.get(expkey, 'seqfeature') # pdb.set_trace() in_fvpath = glob.glob("{0:s}*{1:s}*".format(sfvdir, fvname))[0] out_sfvpath = "{0:s}{1:s}.{2:s}".format(sfvdir, expkey, outfvname)
#!/usr/bin/env python #-*- coding: utf-8 -*- import sys, os sys.path.append('../source/') import glob from rankutils.cfgloader import cfgloader import argparse import numpy as np completedir = lambda d: d if d[-1] == '/' else d+'/' pathtcfg = cfgloader('../source/path_2.cfg') def gen_fold_assign(n): idx = np.arange(n) fold_idx = np.zeros((n, 1), dtype=np.uint8) np.random.shuffle(idx) aux = idx[0:(n//2)] fold_idx[aux] = 1 return fold_idx for section in sys.argv[1:]:
def merge_topk(dataset, desc_a, desc_b, method, outdir): print(rk_dtype) round = 0 pathcfg = cfgloader("path_2.cfg") key_a = "{0:s}_desc{1:d}".format(dataset, desc_a) key_b = "{0:s}_desc{1:d}".format(dataset, desc_b) rkdir_a = pathcfg['rank'][key_a] rkdir_b = pathcfg['rank'][key_b] folds_a = np.load(glob.glob(rkdir_a + "*.folds.npy")[0])[:, round] folds_b = np.load(glob.glob(rkdir_b + "*.folds.npy")[0])[:, round] #print('\n' + pathcfg['output'][key_a] + method + "/rel-prediction/*r{0:03d}*".format(round)) #print('\n' + pathcfg['output'][key_b] + method + "/rel-prediction/*r{0:03d}*".format(round)) ppath_a = glob.glob(pathcfg['output'][key_a] + method + "/rel-prediction/*r{0:03d}*.npy".format(round)) ppath_a.sort() ppath_b = glob.glob(pathcfg['output'][key_b] + method + "/rel-prediction/*r{0:03d}*.npy".format(round)) ppath_b.sort() assert len(ppath_a) == 2, "Could not find predictions for {0:s}".format( key_a) assert len(ppath_b) == 2, "Could not find predictions for {0:s}".format( key_b) pred_pair_a = [np.load(ppath_a[0]), np.load(ppath_a[1])] pred_pair_b = [np.load(ppath_b[0]), np.load(ppath_b[1])] fullpred_a = [] fullpred_b = [] n0 = 0 n1 = 0 for fa in folds_a: if fa == 0: fullpred_a.append(pred_pair_a[fa][n0, :]) n0 += 1 if fa == 1: fullpred_a.append(pred_pair_a[fa][n1, :]) n1 += 1 n0 = 0 n1 = 0 for fb in folds_b: if fb == 0: fullpred_b.append(pred_pair_b[fb][n0:n0 + 1, :]) n0 += 1 if fb == 1: fullpred_b.append(pred_pair_b[fb][n1:n1 + 1, :]) n1 += 1 rkfiles_a = glob.glob(rkdir_a + "/*.rk") rkfiles_a.sort() rkfiles_b = glob.glob(rkdir_b + "/*.rk") rkfiles_b.sort() assert len(rkfiles_a) == len(rkfiles_b), "Inconsistent number of ranks for rank directory a <{0:d}> and b <{1:d}>"\ .format(len(rkfiles_a), len(rkfiles_b)) pos = 0 for rkfpath_a, rkfpath_b in zip(rkfiles_a, rkfiles_b): assert os.path.basename(rkfpath_a) == os.path.basename(rkfpath_b),\ "Rank files <{0:s}> and <{1:s}> diverge!".format(os.path.basename(rkfpath_a), os.path.basename(rkfpath_b)) print("->" + os.path.basename(rkfpath_a)) rank_a = read_rank(rkfpath_a) rank_b = read_rank(rkfpath_b) pred_a = fullpred_a[pos] pred_b = fullpred_b[pos] rank_merge = merge(rank_a, pred_a, rank_b, pred_b) rankoutname = "{0:s}{1:s}".format(outdir, os.path.basename(rkfpath_a)) write_rank(rankoutname, rank_merge)