def get_chipscores(hsA, hsB, count2rr_AB): ''' Input: Two database handles and queries from A to B Output: Matrix of chip scores. As well as ''' num_results = -1 # ensure there are N top results num_queries = len(count2rr_AB) # Get top scores of result chipscore_list = [[] for _ in xrange(num_queries)] # Indicator as to if chipscore was a true positive isscoreTP_list = [[] for _ in xrange(num_queries)] for count in xrange(num_queries): rr = count2rr_AB[count] res = QueryResult(hsB, rr, hsA) res.force_num_top(-1) top_scores = res.top_scores() gtpos_full = res.get_groundtruth_ranks() gtpos_full = [] if gtpos_full is None else gtpos_full gtpos_list = [] for gt_pos in gtpos_full: if num_results == -1 or gt_pos < num_results: gtpos_list.append(gt_pos) isscoreTP_list[count] = gtpos_list chipscore_list[count] = top_scores num_results = len(chipscore_list[0]) assert np.all(num_results == np.array([len(_) for _ in chipscore_list])), \ 'There must be the same number of results' chipscore_data = np.array(chipscore_list) ischipscore_TP = np.zeros((num_queries, num_results), dtype=np.bool) for count in xrange(num_queries): gtpos_list = [isscoreTP_list[count]] ischipscore_TP[count][gtpos_list] = 1 return chipscore_data, ischipscore_TP
def viz_threshold_matchings(hsA, hsB, count2rr_AB, thresh_out_dir): 'returns database, cx, database cx' import numpy as np valid_cxsB = hsB.cm.get_valid_cxs() num_matching = 0 MATCH_THRESHOLD = __method_2_matchthresh__.get(__METHOD__, 10) results_name = get_results_name(hsA, hsB) print(' * Visualizing threshold matchings ' + results_name + ' give it some time to plot...') threshdb_out_dir = join_mkdir(thresh_out_dir, results_name) # For each query run in hsA vs hsB for count in xrange(len(count2rr_AB)): rr = count2rr_AB[count] qcx = rr.qcx res = QueryResult(hsB, rr, hsA) qname = res.qhs.cm.cx2_name(qcx) # Set matching threshold res.top_thresh = MATCH_THRESHOLD res.num_top_min = 0 res.num_top_max = 5 res.num_extra_return = 0 # Check to see if any matched over the threshold top_cxs = res.top_cx() top_names = res.hs.cm.cx2_name(top_cxs) top_scores = res.scores()[top_cxs] if len(top_cxs) > 0: # Visualize the result # Stupid segfaults #if qcx == 113: #41: #import IPython #IPython.embed() # Get the scores num_matching += 1 res.visualize() # Create a filename showing dataset, score, cx, match names matchname_set = \ set([name.replace('Lionfish','') for name in (top_names+[qname])]) matchnames = '-'.join(list(matchname_set)) scorestr = str(int(round(top_scores[0]))) fig_fname = '-'.join([ results_name, 'score' + scorestr, 'cx' + str(qcx), 'MATCHES' + matchnames ]) + '.jpg' fig = myfigure(0) fig_fpath = join(threshdb_out_dir, fig_fname) sys.stdout.write('.') safe_savefig(fig, fig_fpath) print(' * Visualized %d above thresh: %f from expt: %s ' % (num_matching, MATCH_THRESHOLD, results_name))
def write_all_chipscore_results(hsA, hsB, count2rr_AB, txt_output_fpath): txt_header = '''#<COMMENTED_XML_HEADER> # <filename> # ''' + txt_output_fpath + ''' # </filename> # # <description> # Sorted Query Results. The results are stored in the format: # The first line gives the query image name, the next lines are # a sorted list of result image names and scores. Queries are # separated by two new lines. # </description> # # <data_format> # queryA # resultA1 scoreA1 # resultA2 scoreA2 # resultA3 scoreA3 # ... # # queryB-gname # resultB1 scoreB1 # resultB2 scoreB2 # resultB3 scoreB3 # ... # # ... # </data_format> #</COMMENTED_XML_HEADER> ''' txt_body = '' for count in range(len(count2rr_AB)): rr = count2rr_AB[count] res = QueryResult(hsB, rr, hsA) txt_body += hsA.cm.cx2_gname(rr.qcx) + '\n' top_cxs = res.cx_sort() scores = res.scores() output_tup_list = [] for cx in top_cxs: output_tup_list.append((hsB.cm.cx2_gname(cx), scores[cx])) output_tup_list.sort(key=lambda tup: tup[1]) output_tup_list = output_tup_list[::-1] for tup in output_tup_list: txt_body += tup[0] + ' ' + str(tup[1]) + '\n' txt_body += '\n' with open(txt_output_fpath, 'w') as file: file.write(txt_body)
def write_all_chipscore_results(hsA, hsB, count2rr_AB, txt_output_fpath): txt_header = '''#<COMMENTED_XML_HEADER> # <filename> # '''+txt_output_fpath+''' # </filename> # # <description> # Sorted Query Results. The results are stored in the format: # The first line gives the query image name, the next lines are # a sorted list of result image names and scores. Queries are # separated by two new lines. # </description> # # <data_format> # queryA # resultA1 scoreA1 # resultA2 scoreA2 # resultA3 scoreA3 # ... # # queryB-gname # resultB1 scoreB1 # resultB2 scoreB2 # resultB3 scoreB3 # ... # # ... # </data_format> #</COMMENTED_XML_HEADER> ''' txt_body = '' for count in range(len(count2rr_AB)): rr = count2rr_AB[count] res = QueryResult(hsB, rr, hsA) txt_body += hsA.cm.cx2_gname(rr.qcx)+'\n' top_cxs = res.cx_sort() scores = res.scores() output_tup_list = [] for cx in top_cxs: output_tup_list.append((hsB.cm.cx2_gname(cx), scores[cx])) output_tup_list.sort(key=lambda tup: tup[1]) output_tup_list = output_tup_list[::-1] for tup in output_tup_list: txt_body += tup[0]+' '+str(tup[1])+'\n' txt_body+='\n' with open(txt_output_fpath,'w') as file: file.write(txt_body)
def run_all_queries(em): cm, vm = em.hs.get_managers('cm', 'vm') all_cxs = cm.get_valid_cxs() cx2_rr = em.batch_query(force_recomp=em.recompute_bit, test_cxs=all_cxs) em.cx2_res = np.array([ [] if rr == [] else\ QueryResult(em.hs,rr) for rr in cx2_rr]) pass
def query(hs, qcid, qhs=None): ''' Runs query against this database with cid. If hsother is specified uses the chip from that database ''' qhs = hs if qhs is None else qhs qcx = qhs.cm.cx(qcid) hs.ensure_model() rawres = hs.qm.cx2_rr(qcx, qhs) return QueryResult(hs, rawres, qhs)
def viz_threshold_matchings(hsA, hsB, count2rr_AB, thresh_out_dir): 'returns database, cx, database cx' import numpy as np valid_cxsB = hsB.cm.get_valid_cxs() num_matching = 0 MATCH_THRESHOLD = __method_2_matchthresh__.get(__METHOD__, 10) results_name = get_results_name(hsA, hsB) print(' * Visualizing threshold matchings '+results_name+' give it some time to plot...') threshdb_out_dir = join_mkdir(thresh_out_dir, results_name) # For each query run in hsA vs hsB for count in xrange(len(count2rr_AB)): rr = count2rr_AB[count] qcx = rr.qcx res = QueryResult(hsB, rr, hsA) qname = res.qhs.cm.cx2_name(qcx) # Set matching threshold res.top_thresh = MATCH_THRESHOLD res.num_top_min = 0 res.num_top_max = 5 res.num_extra_return = 0 # Check to see if any matched over the threshold top_cxs = res.top_cx() top_names = res.hs.cm.cx2_name(top_cxs) top_scores = res.scores()[top_cxs] if len(top_cxs) > 0: # Visualize the result # Stupid segfaults #if qcx == 113: #41: #import IPython #IPython.embed() # Get the scores num_matching += 1 res.visualize() # Create a filename showing dataset, score, cx, match names matchname_set = \ set([name.replace('Lionfish','') for name in (top_names+[qname])]) matchnames = '-'.join(list(matchname_set)) scorestr = str(int(round(top_scores[0]))) fig_fname = '-'.join([results_name, 'score'+scorestr, 'cx'+str(qcx), 'MATCHES'+matchnames])+'.jpg' fig = myfigure(0) fig_fpath = join(threshdb_out_dir, fig_fname) sys.stdout.write('.') safe_savefig(fig, fig_fpath) print(' * Visualized %d above thresh: %f from expt: %s ' % (num_matching, MATCH_THRESHOLD, results_name))
def run_singleton_queries(em): '''Quick experiment: Query each chip with a duplicate against whole database Do not remove anyone from ANN matching''' cm, vm = em.hs.get_managers('cm', 'vm') valid_cx = cm.get_valid_cxs() cx2_num_other = cm.cx2_num_other_chips(valid_cx) singleton_cx = valid_cx[cx2_num_other == 1] # find singletons duplicate_cx = valid_cx[cx2_num_other > 1] # find matchables cx2_rr = em.batch_query(force_recomp=em.recompute_bit, test_cxs=duplicate_cx) em.cx2_res = np.array([ [] if rr == [] else\ QueryResult(em.hs,rr) for rr in cx2_rr])
def run_matching_experiment(em, expt_name='MatchingExperiment', with_images=True): '''Quick experiment: Query each chip with a duplicate against whole database Do not remove anyone from ANN matching''' import os logmsg('Running Quick Experiment: ' + expt_name) hs = em.hs am, cm, vm, qm, iom, dm = hs.get_managers('am', 'cm', 'vm', 'qm', 'iom', 'dm') with_ellipses = True with_points = False with_full_results = False with_ellipse_and_points = False # Create an Experiment Directory in .hs_internals/computed timestamp = iom.get_timestamp() expt_dpath = iom.ensure_directory( iom.get_user_fpath('_'.join(['expt', expt_name, timestamp]))) expt_img_dpath = iom.ensure_directory( os.path.join(expt_dpath, 'result_images')) expt_rr_dpath = iom.ensure_directory( os.path.join(expt_dpath, 'raw_results')) # Write Algorithm Settings to the file algo_prefs_text = am.get_algo_name('all') algo_prefs_fpath = os.path.join(expt_dpath, 'algo_prefs.txt') iom.write(algo_prefs_fpath, algo_prefs_text) vm.build_model() # Defaults to building model of all prev_ell = dm.draw_prefs.ellipse_bit prev_pts = dm.draw_prefs.points_bit dm.fignum = 1 # Create Matches File To Append to with open(os.path.join(expt_dpath, 'matches.txt'), 'a') as file: for cx in iter(cm.get_valid_cxs()): # Preform Query res = QueryResult(qm.hs, qm.cx2_rr(cx)) # Get Query Info qcid, gname = cm.cx2_(res.rr.qcx, 'cid', 'gname') # Get Result Info (tcid, tgname, tscore) = res.tcid2_('cid', 'gname', 'score') # Print Query Info logmsg('---QUERY---') outstr = 'QUERY, gname=%s, cid=%4d' % (gname, qcid) print outstr file.write(outstr + '\n') # Print Result Info if len(tscore) == 0: maxsim = 0 # Best Score if len(tscore) > 0: maxsim = tscore[0] # Best Score for (rank, tup) in enumerate( zip(*[x.tolist() for x in (tgname, tcid, tscore)])): outstr = ' rank=%d, gname=%s, cid=%4d, score=%7.2f' % tuple( [rank + 1] + list(tup)) print outstr file.write(outstr + '\n') print '' file.write('\n\n') # Output Images query_name = 'sim=%07.2f-qcid=%d.png' % (maxsim, qcid) if with_full_results: #import shelve #shelve.open(os.path.join(expt_rr_dpath, 'rr_'+query_name+'.shelf') #import cPickle #rr = res.rr #cPickle.dump(, rr) pass if with_images: dm.draw_prefs.ellipse_bit = False dm.draw_prefs.points_bit = False dm.draw_prefs.bbox_bit = False dm.show_query(res) dm.save_fig( os.path.join(expt_img_dpath, 'img_' + query_name)) if with_ellipses: dm.draw_prefs.ellipse_bit = True dm.draw_prefs.points_bit = False dm.show_query(res) dm.save_fig( os.path.join(expt_img_dpath, 'ellipse_' + query_name)) if with_points: dm.draw_prefs.ellipse_bit = False dm.draw_prefs.points_bit = True dm.show_query(res) dm.save_fig( os.path.join(expt_img_dpath, 'point_' + query_name)) if with_ellipse_and_points: dm.draw_prefs.ellipse_bit = True dm.draw_prefs.points_bit = True dm.show_query(res) dm.save_fig( os.path.join(expt_img_dpath, 'both_' + query_name)) logmsg('Finished Matching Experiment: ' + expt_name) timestamp = iom.get_timestamp() iom.write(os.path.join(expt_dpath, 'Finished_' + str(timestamp)), timestamp + '\n' + em.hs.get_database_stat_str()) prev_ell = dm.draw_prefs.ellipse_bit prev_pts = dm.draw_prefs.points_bit
def run_matching_experiment(em, expt_name='MatchingExperiment', with_images=True): '''Quick experiment: Query each chip with a duplicate against whole database Do not remove anyone from ANN matching''' import os logmsg('Running Quick Experiment: '+expt_name) hs = em.hs am, cm, vm, qm, iom, dm = hs.get_managers('am', 'cm','vm', 'qm', 'iom', 'dm') with_ellipses = True with_points = False with_full_results = False with_ellipse_and_points = False # Create an Experiment Directory in .hs_internals/computed timestamp = iom.get_timestamp() expt_dpath = iom.ensure_directory(iom.get_user_fpath('_'.join(['expt',expt_name,timestamp]))) expt_img_dpath = iom.ensure_directory(os.path.join(expt_dpath, 'result_images')) expt_rr_dpath = iom.ensure_directory(os.path.join(expt_dpath, 'raw_results')) # Write Algorithm Settings to the file algo_prefs_text = am.get_algo_name('all') algo_prefs_fpath = os.path.join(expt_dpath, 'algo_prefs.txt') iom.write(algo_prefs_fpath, algo_prefs_text) vm.build_model() # Defaults to building model of all prev_ell = dm.draw_prefs.ellipse_bit prev_pts = dm.draw_prefs.points_bit dm.fignum = 1 # Create Matches File To Append to with open(os.path.join(expt_dpath,'matches.txt'), 'a') as file: for cx in iter(cm.get_valid_cxs()): # Preform Query res = QueryResult(qm.hs, qm.cx2_rr(cx)) # Get Query Info qcid, gname = cm.cx2_(res.rr.qcx, 'cid', 'gname') # Get Result Info (tcid , tgname , tscore ) = res.tcid2_('cid','gname','score') # Print Query Info logmsg('---QUERY---') outstr = 'QUERY, gname=%s, cid=%4d' % (gname, qcid) print outstr file.write(outstr+'\n') # Print Result Info if len(tscore) == 0: maxsim = 0 # Best Score if len(tscore) > 0: maxsim = tscore[0] # Best Score for (rank, tup) in enumerate(zip(*[x.tolist() for x in (tgname, tcid, tscore )])): outstr = ' rank=%d, gname=%s, cid=%4d, score=%7.2f' % tuple([rank+1]+list(tup)) print outstr file.write(outstr+'\n') print '' file.write('\n\n') # Output Images query_name = 'sim=%07.2f-qcid=%d.png' % (maxsim, qcid) if with_full_results: #import shelve #shelve.open(os.path.join(expt_rr_dpath, 'rr_'+query_name+'.shelf') #import cPickle #rr = res.rr #cPickle.dump(, rr) pass if with_images: dm.draw_prefs.ellipse_bit = False dm.draw_prefs.points_bit = False dm.draw_prefs.bbox_bit = False dm.show_query(res) dm.save_fig(os.path.join(expt_img_dpath, 'img_'+query_name)) if with_ellipses: dm.draw_prefs.ellipse_bit = True dm.draw_prefs.points_bit = False dm.show_query(res) dm.save_fig(os.path.join(expt_img_dpath, 'ellipse_'+query_name)) if with_points: dm.draw_prefs.ellipse_bit = False dm.draw_prefs.points_bit = True dm.show_query(res) dm.save_fig(os.path.join(expt_img_dpath, 'point_'+query_name)) if with_ellipse_and_points: dm.draw_prefs.ellipse_bit = True dm.draw_prefs.points_bit = True dm.show_query(res) dm.save_fig(os.path.join(expt_img_dpath, 'both_'+query_name)) logmsg('Finished Matching Experiment: '+expt_name) timestamp = iom.get_timestamp() iom.write(os.path.join(expt_dpath, 'Finished_'+str(timestamp)), timestamp+'\n'+em.hs.get_database_stat_str()) prev_ell = dm.draw_prefs.ellipse_bit prev_pts = dm.draw_prefs.points_bit