From a930cc9ce68082764223aa094cae5efba69a0971 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 3 Jan 2022 22:43:01 +0100 Subject: [PATCH] random synth image deform (#66) * random synth image deform * pre-commit suggestions Co-authored-by: Jirka Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .github/workflows/code-format.yml | 4 +-- .pre-commit-config.yaml | 6 ++-- birl/utilities/registration.py | 5 ++-- bm_experiments/bm_comp_perform.py | 47 ++++++++++++++++++++++--------- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/.github/workflows/code-format.yml b/.github/workflows/code-format.yml index d651f1d8..7099a4d5 100644 --- a/.github/workflows/code-format.yml +++ b/.github/workflows/code-format.yml @@ -38,8 +38,8 @@ jobs: - uses: pre-commit/action@v2.0.2 # this action also provides an additional behaviour when used in private repositories # when configured with a github token, the action will push back fixes to the pull request branch - # with: - # token: ${{ secrets.GITHUB_TOKEN }} + with: + token: ${{ secrets.GITHUB_TOKEN }} #typing-check-mypy: # runs-on: ubuntu-20.04 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 63487182..fa1d6f7d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,7 +9,7 @@ ci: repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.0.1 + rev: v4.1.0 hooks: - id: end-of-file-fixer - id: trailing-whitespace @@ -30,7 +30,7 @@ repos: require_serial: false - repo: https://github.com/pre-commit/mirrors-yapf - rev: v0.31.0 + rev: v0.32.0 hooks: - id: yapf name: formatting @@ -38,7 +38,7 @@ repos: require_serial: false - repo: https://github.com/executablebooks/mdformat - rev: 0.7.10 + rev: 0.7.11 hooks: - id: mdformat additional_dependencies: diff --git a/birl/utilities/registration.py b/birl/utilities/registration.py index bc6236d5..fc1f6f61 100644 --- a/birl/utilities/registration.py +++ b/birl/utilities/registration.py @@ -76,8 +76,9 @@ def get_affine_components(matrix): >>> mtx = np.array([[ -0.95, 0.1, 65.], [ 0.1, 0.95, -60.], [ 0., 0., 1.]]) >>> import pandas as pd - >>> aff = pd.Series(get_affine_components(mtx)).sort_index() - >>> aff # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS + >>> aff = get_affine_components(mtx) + >>> aff["scale"] = tuple(aff["scale"]) # fix for version compatibility + >>> pd.Series(aff).sort_index() # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS rotation 173.9... scale (0.95..., 0.95...) shear -3.14... diff --git a/bm_experiments/bm_comp_perform.py b/bm_experiments/bm_comp_perform.py index aba86af3..5e981bce 100644 --- a/bm_experiments/bm_comp_perform.py +++ b/bm_experiments/bm_comp_perform.py @@ -75,6 +75,10 @@ def arg_parse_params(): return args +def _rand_float(low, high): + return np.random.random() * (high - low) + low + + def _prepare_images(path_out, im_size=IMAGE_SIZE): """ generate and prepare synth. images for registration @@ -88,7 +92,11 @@ def _prepare_images(path_out, im_size=IMAGE_SIZE): io.imsave(path_img_target, img_target) # warp synthetic image - tform = AffineTransform(scale=(0.9, 0.9), rotation=0.2, translation=(200, -50)) + tform = AffineTransform( + scale=(_rand_float(0.9, 1.1), _rand_float(0.9, 1.1)), + rotation=_rand_float(-0.1, 0.1), + translation=np.random.randint(-150, 150, size=2).tolist(), + ) img_source = warp(image, tform.inverse, output_shape=im_size) img_source = random_noise(img_source, var=IMAGE_NOISE) path_img_source = os.path.join(path_out, NAME_IMAGE_SOURCE) @@ -102,6 +110,8 @@ def _clean_images(image_paths): :param str image_paths: path to images """ for p_img in image_paths: + if not p_img: + continue os.remove(p_img) @@ -137,16 +147,20 @@ def register_image_pair(idx, path_img_target, path_img_source, path_out): AffineTransform, min_samples=25, max_trials=500, - residual_threshold=0.95, + residual_threshold=0.9, ) # warping source image with estimated transformations - img_warped = warp(img_target, model.inverse, output_shape=img_target.shape[:2]) path_img_warped = os.path.join(path_out, NAME_IMAGE_WARPED % idx) - try: - io.imsave(path_img_warped, img_warped) - except Exception: - traceback.print_exc() + if model: + img_warped = warp(img_target, model.inverse, output_shape=img_target.shape[:2]) + try: + io.imsave(path_img_warped, img_warped) + except Exception: + traceback.print_exc() + else: + warnings.warn("Image registration failed.", RuntimeWarning) + path_img_warped = None # summarise experiment execution_time = time.time() - start return path_img_warped, execution_time @@ -162,15 +176,15 @@ def measure_registration_single(path_out, nb_iter=5): path_img_target, path_img_source = _prepare_images(path_out, IMAGE_SIZE) paths = [path_img_target, path_img_source] - execution_times = [] + exec_times = [] for i in tqdm.tqdm(range(nb_iter), desc='using single-thread'): path_img_warped, t = register_image_pair(i, path_img_target, path_img_source, path_out) paths.append(path_img_warped) - execution_times.append(t) + exec_times.append(t) _clean_images(set(paths)) - logging.info('registration @1-thread: %f +/- %f', np.mean(execution_times), np.std(execution_times)) - res = {'registration @1-thread': np.mean(execution_times)} + logging.info('registration @1-thread: %f +/- %f', np.mean(exec_times), np.std(exec_times)) + res = {'registration @1-thread': np.mean(exec_times)} return res @@ -186,7 +200,7 @@ def measure_registration_parallel(path_out, nb_iter=3, nb_workers=CPU_COUNT): paths = [path_img_target, path_img_source] execution_times = [] - _regist = partial( + _register = partial( register_image_pair, path_img_target=path_img_target, path_img_source=path_img_source, @@ -197,7 +211,7 @@ def measure_registration_parallel(path_out, nb_iter=3, nb_workers=CPU_COUNT): tqdm_bar = tqdm.tqdm(total=nb_tasks, desc='parallel @ %i threads' % nb_workers) pool = mproc.Pool(nb_workers) - for path_img_warped, t in pool.map(_regist, (range(nb_tasks))): + for path_img_warped, t in pool.map(_register, (range(nb_tasks))): paths.append(path_img_warped) execution_times.append(t) tqdm_bar.update() @@ -206,7 +220,12 @@ def measure_registration_parallel(path_out, nb_iter=3, nb_workers=CPU_COUNT): tqdm_bar.close() _clean_images(set(paths)) - logging.info('registration @%i-thread: %f +/- %f', nb_workers, np.mean(execution_times), np.std(execution_times)) + logging.info( + 'registration @%i-thread: %f +/- %f', + nb_workers, + np.mean(execution_times), + np.std(execution_times), + ) res = {'registration @n-thread': np.mean(execution_times)} return res