def load_test_data(short=False, n=0, use_cpp=False, **kwargs): if 'short' not in vars(): short = False # Read Image #ellipse.rrr() nScales = 4 nSamples = 16 img_fpath = get_test_image() imgBGR = cv2.imread(img_fpath) imgLAB = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2LAB) imgL = imgLAB[:, :, 0] detect_kwargs = { 'scale_min': 20, 'scale_max': 100 } detect_kwargs.update(kwargs) if not use_cpp: kpts, desc = pyhesaff.detect_feats(img_fpath, **detect_kwargs) else: # Try the new C++ code [kpts], [desc] = pyhesaff.detect_feats_list([img_fpath], **detect_kwargs) if short and n > 0: extra_fxs = [] if split(img_fpath)[1] == 'zebra.png': extra_fxs = [374, 520, 880][0:1] fxs = np.array(spaced_elements2(kpts, n).tolist() + extra_fxs) kpts = kpts[fxs] desc = desc[fxs] test_data = locals() return test_data
def load_test_data(short=False, n=0, use_cpp=False, **kwargs): if 'short' not in vars(): short = False # Read Image #ellipse.rrr() nScales = 4 nSamples = 16 img_fpath = get_test_image() imgBGR = cv2.imread(img_fpath) imgLAB = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2LAB) imgL = imgLAB[:, :, 0] detect_kwargs = {'scale_min': 20, 'scale_max': 100} detect_kwargs.update(kwargs) if not use_cpp: kpts, desc = pyhesaff.detect_feats(img_fpath, **detect_kwargs) else: # Try the new C++ code [kpts], [desc] = pyhesaff.detect_feats_list([img_fpath], **detect_kwargs) if short and n > 0: extra_fxs = [] if split(img_fpath)[1] == 'zebra.png': extra_fxs = [374, 520, 880][0:1] fxs = np.array(spaced_elements2(kpts, n).tolist() + extra_fxs) kpts = kpts[fxs] desc = desc[fxs] test_data = locals() return test_data
def test_simple_parallel(): r""" CommandLine: python -m pyhesaff.tests.test_pyhesaff_simple_parallel --test-test_simple_parallel --show Example: >>> # ENABLE_DOCTEST >>> from pyhesaff.tests.test_pyhesaff_simple_parallel import * # NOQA >>> import matplotlib as mpl >>> from matplotlib import pyplot as plt >>> img_fpaths, kpts_array, desc_array = test_simple_parallel() >>> ut.quit_if_noshow() >>> # Do not plot by default >>> fig = plt.figure() >>> for count, (img_fpath, kpts, desc) in enumerate(zip(img_fpaths, kpts_array, >>> desc_array)): >>> if count > 3: >>> break >>> ax = fig.add_subplot(2, 2, count + 1) >>> img = mpl.image.imread(img_fpath) >>> plt.imshow(img) >>> _xs, _ys = kpts.T[0:2] >>> ax.plot(_xs, _ys, 'ro', alpha=.5) >>> ut.show_if_requested() """ import pyhesaff test_fnames = ['carl.jpg', 'lena.png', 'zebra.png', 'ada.jpg', 'star.png'] img_fpaths = list(map(ut.grab_test_imgpath, test_fnames)) * 2 # Time parallel computation with ut.Timer('Timing Parallel'): kpts_array, desc_array = pyhesaff.detect_feats_list(img_fpaths) # Time serial computation kpts_list2 = [] desc_list2 = [] with ut.Timer('Timing Iterative'): for img_fpath in img_fpaths: kpts_, desc_ = pyhesaff.detect_feats(img_fpath) kpts_list2.append(kpts_) desc_list2.append(desc_) print('Checking for errors') for (kpts_, desc_, kpts, desc) in zip(kpts_list2, desc_list2, kpts_array, desc_array): print('shape(kpts, kpts_, desc, desc_) = %9r, %9r, %11r, %11r' % (kpts.shape, kpts_.shape, desc.shape, desc_.shape)) try: assert np.all(kpts_ == kpts), 'parallel computation inconsistent' assert np.all(desc_ == desc), 'parallel computation inconsistent' assert len(kpts_) > 0, 'no kpts detected' #assert False, 'deliberate triggering to see printouts' except Exception as ex: ut.printex(ex) raise print('Keypoints seem consistent') return img_fpaths, kpts_array, desc_array
def gen_feat_openmp(cid_list, chip_fpath_list, hesaff_params): r""" Compute features in parallel on the C++ side, return generator here """ print("Detecting %r features in parallel: " % len(cid_list)) kpts_list, desc_list = pyhesaff.detect_feats_list(chip_fpath_list, **hesaff_params) for cid, kpts, vecs in zip(cid_list, kpts_list, desc_list): nFeat = len(kpts) yield cid, nFeat, kpts, vecs
def gen_feat_openmp(cid_list, chip_fpath_list, hesaff_params): r""" Compute features in parallel on the C++ side, return generator here """ print('Detecting %r features in parallel: ' % len(cid_list)) kpts_list, desc_list = pyhesaff.detect_feats_list(chip_fpath_list, **hesaff_params) for cid, kpts, vecs in zip(cid_list, kpts_list, desc_list): nFeat = len(kpts) yield cid, nFeat, kpts, vecs
def TEST_PARALLEL(): gpath_list = grabdata.get_test_gpaths(ndata=10, names=['zebra', 'lena', 'jeff']) args_list = [(gpath, ) for gpath in gpath_list] @utool.argv_flag_dec def print_test_results(result_list): for kpts, desc in result_list: print('[test] kpts.shape=(%4d, %d), desc.sum=%8d' % (kpts.shape[0], kpts.shape[1], desc.sum())) hesaff_kwargs = {'scale_min': -1, 'scale_max': -1, 'nogravity_hack': False} with utool.Timer('c++ parallel'): kpts_list, desc_list = pyhesaff.detect_feats_list( gpath_list, **hesaff_kwargs) # Run parallel tasks @utool.indent_func('[test_task]') def run_parallel_task(num_procs=None): print('run_parallel_task. num_procs=%r' % None) if num_procs is not None: util_parallel.close_pool() util_parallel.init_pool(num_procs) else: num_procs = util_parallel.get_default_numprocs() msg = 'processing tasks in %s' % ('serial' if num_procs == 1 else str(num_procs) + '-parallel') with utool.Timer(msg): result_list = util_parallel.process(pyhesaff.detect_feats, args_list, hesaff_kwargs) print_test_results(result_list) return result_list run_parallel_task() # Compare to serial if needed @utool.argv_flag_dec def compare_serial(): print('compare_serial') run_parallel_task(1) compare_serial() return locals()
def test_cpp_rotinvar_main(): r""" CommandLine: python -m pyhesaff.tests.test_cpp_rotation_invariance --test-test_cpp_rotinvar_main python -m pyhesaff.tests.test_cpp_rotation_invariance --test-test_cpp_rotinvar_main --show Example: >>> # DISABLE_DOCTEST >>> from pyhesaff.tests.test_cpp_rotation_invariance import * # NOQA >>> # build test data >>> # execute function >>> result = test_cpp_rotinvar_main() >>> # verify results >>> print(result) """ # TODO; take visualization out of this test by default from pyhesaff.tests import pyhestest import pyhesaff # Read data print('[rotinvar] loading test data') img_fpath = pyhestest.get_test_image() [kpts1], [desc1] = pyhesaff.detect_feats_list([img_fpath], rotation_invariance=False) [kpts2], [desc2] = pyhesaff.detect_feats_list([img_fpath], rotation_invariance=True) np.set_printoptions(threshold=5000, linewidth=5000, precision=8, suppress=True) print('kpts1.shape = %r' % (kpts1.shape, )) print('kpts2.shape = %r' % (kpts2.shape, )) print('desc1.shape = %r' % (desc1.shape, )) print('desc2.shape = %r' % (desc2.shape, )) print('\n----\n'.join([ str(k1) + '\n' + str(k2) for k1, k2 in zip(kpts1[0:10], kpts2[0:10]) ])) n = 4 #clip = min(len(kpts1), n) # HACK FIXME fxs = np.array(pyhestest.spaced_elements2(kpts2, n).tolist()[0:3]) print('fxs=%r' % fxs) kpts1 = kpts1[fxs] kpts2 = kpts2[fxs] desc1 = desc1[fxs] desc2 = desc2[fxs] print('\n----\n'.join( [str(k1) + '\n' + str(k2) for k1, k2 in zip(kpts1, kpts2)])) imgBGR = pyhestest.cv2.imread(img_fpath) sel = min(len(kpts1) - 1, 3) TEST_keypoint(imgBGR, img_fpath, kpts1, desc1, sel, fnum=1, figtitle='Downward Rotation') TEST_keypoint(imgBGR, img_fpath, kpts2, desc2, sel, fnum=9001, figtitle='Adapted Rotation') #locals_ = TEST_keypoint(imgBGR, img_fpath, kpts1, desc1, sel) #exec(utool.execstr_dict(locals_, 'locals_')) #exec(utool.execstr_dict(f1_loc, 'f1_loc')) # NOQA #pinteract.interact_keypoints(imgBGR, kpts2, desc, arrow=True, rect=True) if ut.show_was_requested(): exec(df2.present())
def test_cpp_rotinvar_main(): r""" CommandLine: python -m pyhesaff.tests.test_cpp_rotation_invariance --test-test_cpp_rotinvar_main python -m pyhesaff.tests.test_cpp_rotation_invariance --test-test_cpp_rotinvar_main --show Example: >>> # DISABLE_DOCTEST >>> from pyhesaff.tests.test_cpp_rotation_invariance import * # NOQA >>> # build test data >>> # execute function >>> result = test_cpp_rotinvar_main() >>> # verify results >>> print(result) """ # TODO; take visualization out of this test by default from pyhesaff.tests import pyhestest import pyhesaff # Read data print('[rotinvar] loading test data') img_fpath = pyhestest.get_test_image() [kpts1], [desc1] = pyhesaff.detect_feats_list([img_fpath], rotation_invariance=False) [kpts2], [desc2] = pyhesaff.detect_feats_list([img_fpath], rotation_invariance=True) np.set_printoptions(threshold=5000, linewidth=5000, precision=8, suppress=True) print('kpts1.shape = %r' % (kpts1.shape,)) print('kpts2.shape = %r' % (kpts2.shape,)) print('desc1.shape = %r' % (desc1.shape,)) print('desc2.shape = %r' % (desc2.shape,)) print('\n----\n'.join([str(k1) + '\n' + str(k2) for k1, k2 in zip(kpts1[0:10], kpts2[0:10])])) n = 4 #clip = min(len(kpts1), n) # HACK FIXME fxs = np.array(pyhestest.spaced_elements2(kpts2, n).tolist()[0:3]) print('fxs=%r' % fxs) kpts1 = kpts1[fxs] kpts2 = kpts2[fxs] desc1 = desc1[fxs] desc2 = desc2[fxs] print('\n----\n'.join([str(k1) + '\n' + str(k2) for k1, k2 in zip(kpts1, kpts2)])) imgBGR = pyhestest.cv2.imread(img_fpath) sel = min(len(kpts1) - 1, 3) TEST_keypoint(imgBGR, img_fpath, kpts1, desc1, sel, fnum=1, figtitle='Downward Rotation') TEST_keypoint(imgBGR, img_fpath, kpts2, desc2, sel, fnum=9001, figtitle='Adapted Rotation') #locals_ = TEST_keypoint(imgBGR, img_fpath, kpts1, desc1, sel) #exec(utool.execstr_dict(locals_, 'locals_')) #exec(utool.execstr_dict(f1_loc, 'f1_loc')) # NOQA #pinteract.interact_keypoints(imgBGR, kpts2, desc, arrow=True, rect=True) if ut.show_was_requested(): exec(df2.present())