def test_sver_wrapper(): """ Test to ensure cpp and python agree and that cpp is faster CommandLine: python -m vtool.sver_c_wrapper --test-test_sver_wrapper python -m vtool.sver_c_wrapper --test-test_sver_wrapper --rebuild-sver python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --dummy python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --fname1=easy1.png --fname2=easy2.png python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --fname1=easy1.png --fname2=hard3.png python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --fname1=carl.jpg --fname2=hard3.png Example: >>> # ENABLE_DOCTEST >>> from vtool.sver_c_wrapper import * # NOQA >>> test_sver_wrapper() Ignore: %timeit call_python_version(*args) %timeit get_affine_inliers_cpp(*args) """ import vtool.spatial_verification as sver import vtool.tests.dummy as dummy xy_thresh_sqrd = ktool.KPTS_DTYPE(.4) scale_thresh_sqrd = ktool.KPTS_DTYPE(2.0) ori_thresh = ktool.KPTS_DTYPE(TAU / 4.0) keys = 'xy_thresh_sqrd, scale_thresh_sqrd, ori_thresh'.split(', ') print(ut.dict_str(ut.dict_subset(locals(), keys))) def report_errors(): pass if ut.get_argflag('--dummy'): testtup = dummy.testdata_dummy_matches() (kpts1, kpts2, fm_input, fs_input, rchip1, rchip2) = testtup fm_input = fm_input.astype(fm_dtype) #fm_input = fm_input[0:10].astype(fm_dtype) #fs_input = fs_input[0:10].astype(np.float32) else: fname1 = ut.get_argval('--fname1', type_=str, default='easy1.png') fname2 = ut.get_argval('--fname2', type_=str, default='easy2.png') testtup = dummy.testdata_ratio_matches(fname1, fname2) (kpts1, kpts2, fm_input, fs_input, rchip1, rchip2) = testtup # pack up call to aff hypothesis import vtool as vt import scipy.stats.mstats scales1 = vt.get_scales(kpts1.take(fm_input.T[0], axis=0)) scales2 = vt.get_scales(kpts2.take(fm_input.T[1], axis=0)) #fs_input = 1 / scipy.stats.mstats.gmean(np.vstack((scales1, scales2))) fs_input = scipy.stats.mstats.gmean(np.vstack((scales1, scales2))) print('fs_input = ' + ut.numpy_str(fs_input)) #fs_input[0:-9] = 0 #fs_input = np.ones(len(fm_input), dtype=fs_dtype) #ut.embed() #fs_input = scales1 * scales2 args = (kpts1, kpts2, fm_input, fs_input, xy_thresh_sqrd, scale_thresh_sqrd, ori_thresh) ex_list = [] try: with ut.Indenter('[TEST1] '): inlier_tup = vt.compare_implementations( sver.get_affine_inliers, get_affine_inliers_cpp, args, lbl1='py', lbl2='c', output_lbl=('aff_inliers_list', 'aff_errors_list', 'Aff_mats') ) out_inliers, out_errors, out_mats = inlier_tup except AssertionError as ex: ex_list.append(ex) raise try: import functools with ut.Indenter('[TEST2] '): bestinlier_tup = vt.compare_implementations( functools.partial(sver.get_best_affine_inliers, forcepy=True), get_best_affine_inliers_cpp, args, show_output=True, lbl1='py', lbl2='c', output_lbl=('bestinliers', 'besterror', 'bestmat') ) bestinliers, besterror, bestmat = bestinlier_tup except AssertionError as ex: ex_list.append(ex) raise if len(ex_list) > 0: raise AssertionError('some tests failed. see previous stdout') #num_inliers_list = np.array(map(len, out_inliers_c)) #best_argx = num_inliers_list.argmax() ##best_inliers_py = out_inliers_py[best_argx] #best_inliers_c = out_inliers_c[best_argx] if ut.show_was_requested(): import plottool as pt fm_output = fm_input.take(bestinliers, axis=0) fnum = pt.next_fnum() pt.figure(fnum=fnum, doclf=True, docla=True) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm_input, ell_linewidth=5, fnum=fnum, pnum=(2, 1, 1)) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm_output, ell_linewidth=5, fnum=fnum, pnum=(2, 1, 2)) pt.show_if_requested()
def test_sver_wrapper(): """ Test to ensure cpp and python agree and that cpp is faster CommandLine: python -m vtool.sver_c_wrapper --test-test_sver_wrapper python -m vtool.sver_c_wrapper --test-test_sver_wrapper --rebuild-sver python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --dummy python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --fname1=easy1.png --fname2=easy2.png python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --fname1=easy1.png --fname2=hard3.png python -m vtool.sver_c_wrapper --test-test_sver_wrapper --show --fname1=carl.jpg --fname2=hard3.png Example: >>> # ENABLE_DOCTEST >>> from vtool.sver_c_wrapper import * # NOQA >>> test_sver_wrapper() Ignore: %timeit call_python_version(*args) %timeit get_affine_inliers_cpp(*args) """ import vtool.spatial_verification as sver import vtool.tests.dummy as dummy xy_thresh_sqrd = ktool.KPTS_DTYPE(.4) scale_thresh_sqrd = ktool.KPTS_DTYPE(2.0) ori_thresh = ktool.KPTS_DTYPE(TAU / 4.0) keys = 'xy_thresh_sqrd, scale_thresh_sqrd, ori_thresh'.split(', ') print(ut.dict_str(ut.dict_subset(locals(), keys))) def report_errors(): pass if ut.get_argflag('--dummy'): testtup = dummy.testdata_dummy_matches() (kpts1, kpts2, fm_input, fs_input, rchip1, rchip2) = testtup fm_input = fm_input.astype(fm_dtype) #fm_input = fm_input[0:10].astype(fm_dtype) #fs_input = fs_input[0:10].astype(np.float32) else: fname1 = ut.get_argval('--fname1', type_=str, default='easy1.png') fname2 = ut.get_argval('--fname2', type_=str, default='easy2.png') testtup = dummy.testdata_ratio_matches(fname1, fname2) (kpts1, kpts2, fm_input, fs_input, rchip1, rchip2) = testtup # pack up call to aff hypothesis import vtool as vt import scipy.stats.mstats scales1 = vt.get_scales(kpts1.take(fm_input.T[0], axis=0)) scales2 = vt.get_scales(kpts2.take(fm_input.T[1], axis=0)) #fs_input = 1 / scipy.stats.mstats.gmean(np.vstack((scales1, scales2))) fs_input = scipy.stats.mstats.gmean(np.vstack((scales1, scales2))) print('fs_input = ' + ut.numpy_str(fs_input)) #fs_input[0:-9] = 0 #fs_input = np.ones(len(fm_input), dtype=fs_dtype) #ut.embed() #fs_input = scales1 * scales2 args = (kpts1, kpts2, fm_input, fs_input, xy_thresh_sqrd, scale_thresh_sqrd, ori_thresh) ex_list = [] try: with ut.Indenter('[TEST1] '): inlier_tup = vt.compare_implementations( sver.get_affine_inliers, get_affine_inliers_cpp, args, lbl1='py', lbl2='c', output_lbl=('aff_inliers_list', 'aff_errors_list', 'Aff_mats')) out_inliers, out_errors, out_mats = inlier_tup except AssertionError as ex: ex_list.append(ex) raise try: import functools with ut.Indenter('[TEST2] '): bestinlier_tup = vt.compare_implementations( functools.partial(sver.get_best_affine_inliers, forcepy=True), get_best_affine_inliers_cpp, args, show_output=True, lbl1='py', lbl2='c', output_lbl=('bestinliers', 'besterror', 'bestmat')) bestinliers, besterror, bestmat = bestinlier_tup except AssertionError as ex: ex_list.append(ex) raise if len(ex_list) > 0: raise AssertionError('some tests failed. see previous stdout') #num_inliers_list = np.array(map(len, out_inliers_c)) #best_argx = num_inliers_list.argmax() ##best_inliers_py = out_inliers_py[best_argx] #best_inliers_c = out_inliers_c[best_argx] if ut.show_was_requested(): import plottool as pt fm_output = fm_input.take(bestinliers, axis=0) fnum = pt.next_fnum() pt.figure(fnum=fnum, doclf=True, docla=True) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm_input, ell_linewidth=5, fnum=fnum, pnum=(2, 1, 1)) pt.show_chipmatch2(rchip1, rchip2, kpts1, kpts2, fm_output, ell_linewidth=5, fnum=fnum, pnum=(2, 1, 2)) pt.show_if_requested()
def understanding_pseudomax_props(mode=2): """ Function showing some properties of distances between normalized pseudomax vectors CommandLine: python -m vtool.distance --test-understanding_pseudomax_props Example: >>> # ENABLE_DOCTEST >>> from vtool.distance import * # NOQA >>> for mode in [0, 1, 2, 3]: ... print('+---') ... print('mode = %r' % (mode,)) ... result = understanding_pseudomax_props(mode) ... print('L___') >>> print(result) """ import vtool as vt pseudo_max = 512 rng = np.random.RandomState(0) num = 10 if mode == 0: dim = 2 p1_01 = (vt.normalize_rows(rng.rand(num, dim))) p2_01 = (vt.normalize_rows(rng.rand(num, dim))) elif mode == 1: p1_01 = vt.dummy.testdata_dummy_sift(num, rng) / pseudo_max p2_01 = vt.dummy.testdata_dummy_sift(num, rng) / pseudo_max elif mode == 2: # Build theoretically maximally distant normalized vectors (type 1) dim = 128 p1_01 = np.zeros((1, dim)) p2_01 = np.zeros((1, dim)) p2_01[:, 0::2] = 1 p1_01[:, 1::2] = 1 p1_01 = vt.normalize_rows(p1_01) p2_01 = vt.normalize_rows(p2_01) elif mode == 3: # Build theoretically maximally distant vectors (type 2) # This mode will clip if cast to uint8, thus failing the test dim = 128 p1_01 = np.zeros((1, dim)) p2_01 = np.zeros((1, dim)) p2_01[:, 0] = 1 p1_01[:, 1:] = 1 p1_01 = vt.normalize_rows(p1_01) p2_01 = vt.normalize_rows(p2_01) pass print('ndims = %r' % (p1_01.shape[1])) p1_01 = p1_01.astype(TEMP_VEC_DTYPE) p2_01 = p2_01.astype(TEMP_VEC_DTYPE) p1_256 = p1_01 * pseudo_max p2_256 = p2_01 * pseudo_max dist_sqrd_01 = vt.L2_sqrd(p1_01, p2_01) dist_sqrd_256 = vt.L2_sqrd(p1_256, p2_256) dist_01 = np.sqrt(dist_sqrd_01) dist_256 = np.sqrt(dist_sqrd_256) print('dist_sqrd_01 = %s' % (ut.numpy_str(dist_sqrd_01, precision=2), )) print('dist_sqrd_256 = %s' % (ut.numpy_str(dist_sqrd_256, precision=2), )) print('dist_01 = %s' % (ut.numpy_str(dist_01, precision=2), )) print('dist_256 = %s' % (ut.numpy_str(dist_256, precision=2), )) print('--') print('sqrt(2) = %f' % (np.sqrt(2))) print('--') assert np.all(dist_01 == vt.L2(p1_01, p2_01)) assert np.all(dist_256 == vt.L2(p1_256, p2_256)) const_sqrd = dist_sqrd_256 / dist_sqrd_01 const = dist_256 / dist_01 print('const = %r' % (const[0], )) print('const_sqrd = %r' % (const_sqrd[0], )) print('1 / const = %r' % (1 / const[0], )) print('1 / const_sqrd = %r' % (1 / const_sqrd[0], )) assert ut.allsame(const) assert ut.allsame(const_sqrd) assert np.all(const == np.sqrt(const_sqrd)) # Assert that distance conversions work assert np.all(dist_256 / const == dist_01) assert np.all(dist_sqrd_256 / const_sqrd == dist_sqrd_01) print('Conversions work') print('Maximal L2 distance between any two NON-NEGATIVE L2-NORMALIZED' ' vectors should always be sqrt(2)')
def understanding_pseudomax_props(mode=2): """ Function showing some properties of distances between normalized pseudomax vectors CommandLine: python -m vtool.distance --test-understanding_pseudomax_props Example: >>> # ENABLE_DOCTEST >>> from vtool.distance import * # NOQA >>> for mode in [0, 1, 2, 3]: ... print('+---') ... print('mode = %r' % (mode,)) ... result = understanding_pseudomax_props(mode) ... print('L___') >>> print(result) """ import vtool as vt pseudo_max = 512 rng = np.random.RandomState(0) num = 10 if mode == 0: dim = 2 p1_01 = (vt.normalize_rows(rng.rand(num, dim))) p2_01 = (vt.normalize_rows(rng.rand(num, dim))) elif mode == 1: p1_01 = vt.dummy.testdata_dummy_sift(num, rng) / pseudo_max p2_01 = vt.dummy.testdata_dummy_sift(num, rng) / pseudo_max elif mode == 2: # Build theoretically maximally distant normalized vectors (type 1) dim = 128 p1_01 = np.zeros((1, dim)) p2_01 = np.zeros((1, dim)) p2_01[:, 0::2] = 1 p1_01[:, 1::2] = 1 p1_01 = vt.normalize_rows(p1_01) p2_01 = vt.normalize_rows(p2_01) elif mode == 3: # Build theoretically maximally distant vectors (type 2) # This mode will clip if cast to uint8, thus failing the test dim = 128 p1_01 = np.zeros((1, dim)) p2_01 = np.zeros((1, dim)) p2_01[:, 0] = 1 p1_01[:, 1:] = 1 p1_01 = vt.normalize_rows(p1_01) p2_01 = vt.normalize_rows(p2_01) pass print('ndims = %r' % (p1_01.shape[1])) p1_01 = p1_01.astype(TEMP_VEC_DTYPE) p2_01 = p2_01.astype(TEMP_VEC_DTYPE) p1_256 = p1_01 * pseudo_max p2_256 = p2_01 * pseudo_max dist_sqrd_01 = vt.L2_sqrd(p1_01, p2_01) dist_sqrd_256 = vt.L2_sqrd(p1_256, p2_256) dist_01 = np.sqrt(dist_sqrd_01) dist_256 = np.sqrt(dist_sqrd_256) print('dist_sqrd_01 = %s' % (ut.numpy_str(dist_sqrd_01, precision=2),)) print('dist_sqrd_256 = %s' % (ut.numpy_str(dist_sqrd_256, precision=2),)) print('dist_01 = %s' % (ut.numpy_str(dist_01, precision=2),)) print('dist_256 = %s' % (ut.numpy_str(dist_256, precision=2),)) print('--') print('sqrt(2) = %f' % (np.sqrt(2))) print('--') assert np.all(dist_01 == vt.L2(p1_01, p2_01)) assert np.all(dist_256 == vt.L2(p1_256, p2_256)) const_sqrd = dist_sqrd_256 / dist_sqrd_01 const = dist_256 / dist_01 print('const = %r' % (const[0],)) print('const_sqrd = %r' % (const_sqrd[0],)) print('1 / const = %r' % (1 / const[0],)) print('1 / const_sqrd = %r' % (1 / const_sqrd[0],)) assert ut.allsame(const) assert ut.allsame(const_sqrd) assert np.all(const == np.sqrt(const_sqrd)) # Assert that distance conversions work assert np.all(dist_256 / const == dist_01) assert np.all(dist_sqrd_256 / const_sqrd == dist_sqrd_01) print('Conversions work') print('Maximal L2 distance between any two NON-NEGATIVE L2-NORMALIZED' ' vectors should always be sqrt(2)')