def oxford_vsmany(): params.__MATCH_TYPE__ = 'vsmany' params.__CHIP_SQRT_AREA__ = None db_dir = ld2.OXFORD if not 'hs' in vars() or hs is None: hs = ld2.HotSpotter() hs.load_tables(ld2.OXFORD) hs.load_chips() hs.load_features(load_desc=False) hs.set_sample_range(55, None) # Use only database images hs = ld2.HotSpotter(db_dir, samples_range=(55, None)) expt_locals = run_experiment(hs, pprefix='[ox-vsmany]', free_mem=True, oxford=False, stem=False, matrix=False) return locals()
def run_experiment(hs=None, free_mem=False, pprefix='[run_expt]', **kwargs): 'Runs experiment and dumps results. Returns locals={qcx2_res, hs}' ''' import experiments as expt from experiments import * ''' print('** Changing print function with pprefix=%r' % (pprefix, )) def prefix_print(msg): helpers.println(helpers.indent(str(msg), pprefix)) ld2.print = prefix_print df2.print = prefix_print mc2.print = prefix_print rr2.print = prefix_print viz.print = prefix_print # Load a HotSpotter object with just tables if not 'hs' in vars() or hs is None: hs = ld2.HotSpotter() hs.load_tables(ld2.DEFAULT) # because we probably plan to at least draw something hs.load_chips() hs.load_features(load_desc=False) hs.set_samples() # default samples print('======================') print('[expt] Running Experiment on hs:\n' + str(hs.db_name())) #print('[expt] Params: \n'+ helpers.indent(params.param_string())) print('======================') # First load cached results qcx2_res, dirty_samp = mc2.load_cached_matches(hs) if len(dirty_samp) > 0: # Run matching of cached results arent working if hs.matcher is None: print('[expt] !! %d dirty queries force the loading of data.' % len(dirty_samp)) hs.load_chips() hs.load_features(load_desc=True) hs.load_matcher() # HACK: I need to do this because matcher changes the match_uid # This is really bad and needs to be fixed. No changing the damn # match_uid!!! qcx2_res, dirty_samp = mc2.load_cached_matches(hs) qcx2_res = mc2.run_matching(hs, qcx2_res, dirty_samp) #if free_mem: # Try to free memory before reporting results #hs.free_some_memory() allres = rr2.report_all(hs, qcx2_res, **kwargs) return locals()
def oxford_bow(): params.__MATCH_TYPE__ = 'bagofwords' params.__CHIP_SQRT_AREA__ = None params.__BOW_NUM_WORDS__ = [1e4, 2e4, 5e4, 1e6, 1.25e6][3] db_dir = ld2.OXFORD if not 'hs' in vars() or hs is None: hs = ld2.HotSpotter() hs.load_tables(ld2.OXFORD) hs.load_chips() hs.load_features(load_desc=False) hs.set_sample_range(55, None) # Use only database images assert min(hs.test_sample_cx) == 55 and max(hs.test_sample_cx) == 5117 expt_locals = run_experiment(hs, pprefix='[ox-bow]', free_mem=True, oxford=False, stem=False, matrix=False) return expt_locals
def oxford_philbin07(hs=None): # philbin params params.__MATCH_TYPE__ = 'bagofwords' params.__BOW_NUM_WORDS__ = [1e4, 2e4, 5e4, 1e6, 1.25e6][3] params.__NUM_RERANK__ = [100, 200, 400, 800, 1000][4] params.__CHIP_SQRT_AREA__ = None params.__XY_THRESH__ = 0.01 #unsure about checks params.BOW_AKMEANS_FLANN_PARAMS = dict(algorithm='kdtree', trees=8, checks=64) if not 'hs' in vars() or hs is None: hs = ld2.HotSpotter() hs.load_tables(ld2.OXFORD) hs.load_chips() hs.load_features(load_desc=False) hs.set_sample_split_pos(55) # Use the 55 cannonical test cases expt_locals = run_experiment(hs, pprefix='[philbin]', oxford=True) return expt_locals
import report_results2 as rr2 import db_info import params if __name__ == '__main__': from multiprocessing import freeze_support import draw_func2 as df2 import vizualizations as viz freeze_support() #db_dir = params.PZ_DanExt_Test #db_dir = params.PZ_DanExt_All #db_dir = params.Wildebeast db_dir = params.DEFAULT if not db_info.has_internal_tables(db_dir): print('initial load shows no tables. Creating them') from convert_db import init_database_from_images init_database_from_images(db_dir) hs = ld2.HotSpotter() hs.load_all(db_dir, matcher=False) # #info_locals = db_info.db_info(hs) #print info_locals['info_str'] # #mc2.run_matching2(hs) viz.DUMP = True viz.BROWSE = False print('Dumping all queries') rr2.dump_all_queries2(hs) print('Finished dump')
def print_result_summaries_list(topnum=5): print('\n<(^_^<)\n') # Print out some summary of all results you have hs = ld2.HotSpotter() hs.load_tables(ld2.DEFAULT) result_file_list = os.listdir(hs.dirs.result_dir) sorted_rankres = [] for result_fname in iter(result_file_list): if fnmatch.fnmatch(result_fname, 'rankres_str*.csv'): print(result_fname) with open(join(hs.dirs.result_dir, result_fname), 'r') as file: metaline = file.readline() toprint = metaline # skip 4 metalines [file.readline() for _ in xrange(4)] top5line = file.readline() top1line = file.readline() toprint += top5line + top1line line = read_until(file, '# NumData') num_data = int(line.replace('# NumData', '')) file.readline() # header res_data_lines = [file.readline() for _ in xrange(num_data)] res_data_str = np.array( [line.split(',') for line in res_data_lines]) tt_scores = np.array(res_data_str[:, 5], dtype=np.float) bt_scores = np.array(res_data_str[:, 6], dtype=np.float) tf_scores = np.array(res_data_str[:, 7], dtype=np.float) tt_score_sum = sum([score for score in tt_scores if score > 0]) bt_score_sum = sum([score for score in bt_scores if score > 0]) tf_score_sum = sum([score for score in tf_scores if score > 0]) toprint += ('tt_scores = %r; ' % tt_score_sum) toprint += ('bt_scores = %r; ' % bt_score_sum) toprint += ('tf_scores = %r; ' % tf_score_sum) if topnum == 5: sorted_rankres.append(top5line + metaline) else: sorted_rankres.append(top1line + metaline) print(toprint + '\n') print('\n(>^_^)>\n') sorted_mapscore = [] for result_fname in iter(result_file_list): if fnmatch.fnmatch(result_fname, 'oxsty_map_csv*.csv'): print(result_fname) with open(join(hs.dirs.result_dir, result_fname), 'r') as file: metaline = file.readline() scoreline = file.readline() toprint = metaline + scoreline sorted_mapscore.append(scoreline + metaline) print(toprint) print('\n'.join(sorted(sorted_rankres))) print('\n'.join(sorted(sorted_mapscore))) print('\n^(^_^)^\n')
def test_realdata2(): from helpers import printWARN, printINFO import warnings import numpy.linalg as linalg import numpy as np import scipy.sparse as sparse import scipy.sparse.linalg as sparse_linalg import load_data2 import params import draw_func2 as df2 import helpers import spatial_verification #params.reload_module() #load_data2.reload_module() #df2.reload_module() db_dir = load_data2.MOTHERS hs = load_data2.HotSpotter(db_dir) assign_matches = hs.matcher.assign_matches qcx = 0 cx = hs.get_other_cxs(qcx)[0] fm, fs, score = hs.get_assigned_matches_to(qcx, cx) # Get chips rchip1 = hs.get_chip(qcx) rchip2 = hs.get_chip(cx) # Get keypoints kpts1 = hs.get_kpts(qcx) kpts2 = hs.get_kpts(cx) # Get feature matches kpts1_m = kpts1[fm[:, 0], :].T kpts2_m = kpts2[fm[:, 1], :].T title='(qx%r v cx%r)\n #match=%r' % (qcx, cx, len(fm)) df2.show_matches2(rchip1, rchip2, kpts1, kpts2, fm, fs, title=title) np.random.seed(6) subst = helpers.random_indexes(len(fm),len(fm)) kpts1_m = kpts1[fm[subst, 0], :].T kpts2_m = kpts2[fm[subst, 1], :].T df2.reload_module() df2.SHOW_LINES = True df2.ELL_LINEWIDTH = 2 df2.LINE_ALPHA = .5 df2.ELL_ALPHA = 1 df2.reset() df2.show_keypoints(rchip1, kpts1_m.T, fignum=0, plotnum=121) df2.show_keypoints(rchip2, kpts2_m.T, fignum=0, plotnum=122) df2.show_matches2(rchip1, rchip2, kpts1_m.T, kpts2_m.T, title=title, fignum=1, vert=True) spatial_verification.reload_module() with helpers.Timer(): aff_inliers1 = spatial_verification.aff_inliers_from_ellshape2(kpts1_m, kpts2_m, xy_thresh_sqrd) with helpers.Timer(): aff_inliers2 = spatial_verification.aff_inliers_from_ellshape(kpts1_m, kpts2_m, xy_thresh_sqrd) # Homogonize+Normalize xy1_m = kpts1_m[0:2,:] xy2_m = kpts2_m[0:2,:] (xyz_norm1, T1) = spatial_verification.homogo_normalize_pts(xy1_m[:,aff_inliers1]) (xyz_norm2, T2) = spatial_verification.homogo_normalize_pts(xy2_m[:,aff_inliers1]) H_prime = spatial_verification.compute_homog(xyz_norm1, xyz_norm2) H = linalg.solve(T2, H_prime).dot(T1) # Unnormalize Hdet = linalg.det(H) # Estimate final inliers acd1_m = kpts1_m[2:5,:] # keypoint shape matrix [a 0; c d] matches acd2_m = kpts2_m[2:5,:] # Precompute the determinant of lower triangular matrix (a*d - b*c); b = 0 det1_m = acd1_m[0] * acd1_m[2] det2_m = acd2_m[0] * acd2_m[2] # Matrix Multiply xyacd matrix by H # [[A, B, X], # [C, D, Y], # [E, F, Z]] # dot # [(a, 0, x), # (c, d, y), # (0, 0, 1)] # = # [(a*A + c*B + 0*E, 0*A + d*B + 0*X, x*A + y*B + 1*X), # (a*C + c*D + 0*Y, 0*C + d*D + 0*Y, x*C + y*D + 1*Y), # (a*E + c*F + 0*Z, 0*E + d*F + 0*Z, x*E + y*F + 1*Z)] # = # [(a*A + c*B, d*B, x*A + y*B + X), # (a*C + c*D, d*D, x*C + y*D + Y), # (a*E + c*F, d*F, x*E + y*F + Z)] # # IF x=0 and y=0 # = # [(a*A + c*B, d*B, 0*A + 0*B + X), # (a*C + c*D, d*D, 0*C + 0*D + Y), # (a*E + c*F, d*F, 0*E + 0*F + Z)] # = # [(a*A + c*B, d*B, X), # (a*C + c*D, d*D, Y), # (a*E + c*F, d*F, Z)] # --- # A11 = a*A + c*B # A21 = a*C + c*D # A31 = a*E + c*F # A12 = d*B # A22 = d*D # A32 = d*F # A31 = X # A32 = Y # A33 = Z # # det(A) = A11*(A22*A33 - A23*A32) - A12*(A21*A33 - A23*A31) + A13*(A21*A32 - A22*A31) det1_mAt = det1_m * Hdet # Check Error in position and scale xy_sqrd_err = (x1_mAt - x2_m)**2 + (y1_mAt - y2_m)**2 scale_sqrd_err = det1_mAt / det2_m # Check to see if outliers are within bounds xy_inliers = xy_sqrd_err < xy_thresh_sqrd s1_inliers = scale_sqrd_err > scale_thresh_low s2_inliers = scale_sqrd_err < scale_thresh_high _inliers, = np.where(np.logical_and(np.logical_and(xy_inliers, s1_inliers), s2_inliers)) xy1_mHt = transform_xy(H, xy1_m) # Transform Kpts1 to Kpts2-space sqrd_dist_error = np.sum( (xy1_mHt - xy2_m)**2, axis=0) # Final Inlier Errors inliers = sqrd_dist_error < xy_thresh_sqrd df2.show_matches2(rchip1, rchip2, kpts1_m.T[best_inliers1], kpts2_m.T[aff_inliers1], title=title, fignum=2, vert=False) df2.show_matches2(rchip1, rchip2, kpts1_m.T[best_inliers2], kpts2_m.T[aff_inliers2], title=title, fignum=3, vert=False) df2.present(wh=(600,400))
__VIEW_TOP__ = 10 __SHOW_PLAIN_CHIPS__ = True __SHOW_KPTS_CHIPS__ = True __SHOW_ASSIGNED_FEATURE_MATCHES__ = False __SHOW_INLIER_MATCHES__ = False __SHOW_WARP__ = False __WARP_FEATURE_TYPE__ = 'HESAFF' __oldfeattype = None # INITIALIZE print('----------------------') print('Initializing warp test') # reload data if hs was deleted if not 'hs' in vars(): print('hs is not in vars... reloading') hs = load_data2.HotSpotter(load_data2.DEFAULT) cx2_nx = hs.tables.cx2_nx nx2_name = hs.tables.nx2_name cx2_rchip_path = hs.cpaths.cx2_rchip_path # rerun query if feature type has changed if __oldfeattype != params.__FEAT_TYPE__: print('The feature type is new or has changed') cx2_kpts = hs.feats.cx2_kpts cx2_desc = hs.feats.cx2_desc vsmany_index = mc2.precompute_index_vsmany(hs) cx2_fm, cx2_fs, _ = mc2.assign_matches_vsmany(qcx, cx2_desc, vsmany_index) __oldfeattype = params.__FEAT_TYPE__ cx2_fm_V, cx2_fs_V, _ = mc2.spatially_verify_matches(qcx, cx2_kpts, cx2_fm, cx2_fs)
#df2.figure(fignum=fnum) #df2.show_matches_annote_res(res, hs, cx, draw_pts=False, plotnum=(2,2,1), SV=False) #df2.show_matches_annote_res(res, hs, cx, draw_pts=False, plotnum=(2,2,2), SV=True) #df2.show_matches_annote_res(res2, hs, cx, draw_pts=False, plotnum=(2,2,3), SV=False) #df2.show_matches_annote_res(res2, hs, cx, draw_pts=False, plotnum=(2,2,4), SV=True) #fnum += 1 return fnum if __name__ == '__main__': from multiprocessing import freeze_support import load_data2 import chip_compute2 import params freeze_support() print('[mc2] __main__ = match_chips2.py') # --- CHOOSE DATABASE --- # db_dir = params.DEFAULT hs = load_data2.HotSpotter() hs.load_tables(db_dir) hs.load_chips() hs.set_samples() hs.load_features() hs.load_matcher() #qcx = 111 #cx = 305 qcx = helpers.get_arg_after('--qcx', type_=int) if qcx is None: qcx = 0 matcher_test(hs, qcx) exec(df2.present())
def leave_out(expt_func=None, split_test=False, **kwargs): ''' do with TF-IDF on the zebra data set. Let M be the total number of *animals* (not images and not chips) in an experimental data set. Do a series of leave-M-out (M >= 1) experiments on the TF-IDF scoring, where the "left out" M are M different zebras, so that there are no images of these zebras in the images used to form the vocabulary. The vocabulary is formed from the remaining N-M animals. Test how well TF-IDF recognition does with these M animals. Repeat for different subsets of M animals. import experiments as expt from experiments import * ''' # --- # Testing should have animals I have seen and animals I haven't seen. # Make sure num descriptors -per- word is about the same as Oxford # --- # Notes from Monday: # 1) Larger training set (see how animals in training do vs animals out of training) # 2) More detailed analysis of failures # 3) Aggregate scores across different pictures of the same animal if not 'expt_func' in vars() or expt_func is None: expt_func = run_experiment # Load tables hs = ld2.HotSpotter(ld2.DEFAULT, load_basic=True) # Grab names db_names_info = db_info.get_db_names_info(hs) nx2_cxs = db_names_info['nx2_cxs'] valid_nxs = db_names_info['valid_nxs'] multiton_nxs = db_names_info['multiton_nxs'] # How to generate samples/splits for names num_nsplits = 5 nsplit_size = (db_names_info['num_names_with_gt'] // num_nsplits) # How to generate samples/splits for chips csplit_size = 1 # number of indexed chips per Jth experiment # Generate name splits kx2_name_split = far_appart_splits(multiton_nxs, nsplit_size, num_nsplits) result_map = {} kx = 0 # run K experiments all_cxs = np.hstack(nx2_cxs[list(valid_nxs)]) for kx in xrange(num_nsplits): print('***************') print('[expt] Leave M=%r names out iteration: %r/%r' % (nsplit_size, kx + 1, num_nsplits)) print('***************') # Get name splits (test_nxs, train_nxs) = kx2_name_split[kx] # Lock in TRAIN sample train_cxs_list = nx2_cxs[list(train_nxs)] train_samp = np.hstack(train_cxs_list) # Generate chip splits test_cxs_list = nx2_cxs[list(test_nxs)] test_nChip = map(len, test_cxs_list) print('[expt] testnames #cxs stats: %r' % helpers.printable_mystats(test_nChip)) test_cx_splits = [] if not split_test: # Chucks version of the test (much simplier and better) indx_samp = all_cxs train_samp = train_samp test_samp = all_cxs hs.set_samples(test_samp, train_samp, indx_samp) m_label = '[LNO: %r/%r]' % (kx + 1, num_nsplits) expt_locals = expt_func(hs, pprefix=m_label, **kwargs) #result_map[kx] = expt_locals['allres'] return locals()