예제 #1
0
def fix_qcx2_res_types(qcx2_res):
    ' Changes data types of cx2_fm_V and cx2_fs_V '
    total_qcx = len(qcx2_res)
    fmt_str = helpers.make_progress_fmt_str(total_qcx)
    for qcx in xrange(total_qcx):
        helpers.print_(fmt_str % (qcx))
        res = qcx2_res[qcx]
        fix_res_types(res)
예제 #2
0
def get_oxsty_mAP_score_from_res(hs, res, SV, oxsty_qres_dpath,
                                 compute_ap_exe, oxford_gt_dir):
    # find oxford ground truth directory
    cwd = os.getcwd()
    # build groundtruth query
    qcx = res.qcx
    qnx = hs.tables.cx2_nx[qcx]
    cx2_oxnum = hs.tables.prop_dict['oxnum']
    qoxnum = cx2_oxnum[qcx]
    qname  = hs.tables.nx2_name[qnx]
    # build ranked list
    cx2_score = res.cx2_score_V if SV else res.cx2_score
    top_cx = cx2_score.argsort()[::-1]
    top_gx = hs.tables.cx2_gx[top_cx]
    top_gname = hs.tables.gx2_gname[top_gx]
    # build mAP args
    if qoxnum == '':
        print("HACK: Adding a dummy qoxynum")
        qoxnum = '1'
    ground_truth_query = qname+'_'+qoxnum
    # build ranked list of gnames (remove duplicates)
    seen = set([])
    ranked_list = []
    for gname in iter(top_gname):
        gname_ = gname.replace('.jpg','')
        if not gname_ in seen:
            seen.add(gname_)
            ranked_list.append(gname_)
    ranked_list2 = [gname.replace('.jpg','') for gname in top_gname]
    # Write the ranked list of images names
    cx_aug = 'qcx_'+str(qcx)
    ranked_list_fname = 'ranked_list_' + cx_aug + ground_truth_query + '.txt'
    ranked_list_fpath = join(oxsty_qres_dpath, ranked_list_fname)
    helpers.write_to(ranked_list_fpath, '\n'.join(ranked_list))
    # execute external mAP code:
    # ./compute_ap [GROUND_TRUTH] [RANKED_LIST]
    os.chdir(oxford_gt_dir)

    def filename(path):
        return os.path.split(path)[1]

    if OXSTY_VERBOSE:
        printable_cmd = ' '.join((filename(compute_ap_exe),
                                ground_truth_query,
                                filename(ranked_list_fpath)))
        print('Executing: %r' % printable_cmd)
    else:
        helpers.print_('.')
    args = (compute_ap_exe, ground_truth_query, ranked_list_fpath)

    cmdstr  = ' '.join(args)

    try:
        proc_out = run_process(args)
        out = proc_out.out
    except OSError as ex:
        out = -1
        if OXSTY_VERBOSE:
            print(repr(ex))
        if repr(ex) == "OSError(12, 'Cannot allocate memory')":
            args_hash = helpers.hashstr(args)
            proc_err_fname = 'proc_err'+args_hash
            proc_err_cmd = proc_err_fname+'.cmd'
            proc_err_out = proc_err_fname+'.out'
            helpers.write_to(proc_err_cmd, repr(args))
            if helpers.checkpath(proc_err_out):
                out = helpers.read_from(proc_err_out)
    mAP = float(out.strip())
    os.chdir(cwd)
    return mAP