Skip to content
This repository has been archived by the owner on Feb 11, 2023. It is now read-only.

Commit

Permalink
random synth image deform (#66)
Browse files Browse the repository at this point in the history
* random synth image deform
* pre-commit suggestions

Co-authored-by: Jirka <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
pre-commit-ci[bot] and Borda authored Jan 3, 2022
1 parent 16daffe commit a930cc9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/code-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ jobs:
- uses: pre-commit/[email protected]
# 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
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,15 +30,15 @@ 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
language: python
require_serial: false

- repo: https://github.com/executablebooks/mdformat
rev: 0.7.10
rev: 0.7.11
hooks:
- id: mdformat
additional_dependencies:
Expand Down
5 changes: 3 additions & 2 deletions birl/utilities/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down
47 changes: 33 additions & 14 deletions bm_experiments/bm_comp_perform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)


Expand Down Expand Up @@ -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
Expand All @@ -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


Expand All @@ -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,
Expand All @@ -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()
Expand All @@ -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

Expand Down

0 comments on commit a930cc9

Please sign in to comment.