Skip to content

Commit

Permalink
Improve Performance of ElasticTransformation (aleju#624)
Browse files Browse the repository at this point in the history
This patch applies various performance-related changes to
`ElasticTransformation`. These cover: (a) the re-use of
generated random samples for multiple images in the same
batch (with some adjustments so that they are not identical),
(b) the caching of generated and re-useable arrays,
(c) a performance-optimized smoothing method for the
underlying displacement maps and (d) the use of nearest
neighbour interpolation (`order=0`) instead of cubic
interpolation (`order=3`) as the new default parameter
for `order`.

These changes lead to a speedup of about 3x to 4x (more
for larger images) at a slight loss of visual
quality (mainly from `order=0`) and variety (due to the
re-use of random samples within each batch).
The new smoothing method leads to slightly stronger
displacements for larger `sigma` values.
  • Loading branch information
aleju authored May 3, 2020
1 parent e67fc6c commit 18d3f0b
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 139 deletions.
19 changes: 19 additions & 0 deletions changelogs/master/improved/20200223_faster_elastic_tf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Improved Performance of `ElasticTransformation` #624

This patch applies various performance-related changes to
`ElasticTransformation`. These cover: (a) the re-use of
generated random samples for multiple images in the same
batch (with some adjustments so that they are not identical),
(b) the caching of generated and re-useable arrays,
(c) a performance-optimized smoothing method for the
underlying displacement maps and (d) the use of nearest
neighbour interpolation (`order=0`) instead of cubic
interpolation (`order=3`) as the new default parameter
for `order`.

These changes lead to a speedup of about 3x to 4x (more
for larger images) at a slight loss of visual
quality (mainly from `order=0`) and variety (due to the
re-use of random samples within each batch).
The new smoothing method leads to slightly stronger
displacements for larger `sigma` values.
7 changes: 7 additions & 0 deletions checks/check_elastic_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def main():
augs_cv2 = aug_cv2.augment_images([image] * 8)
ia.imshow(ia.draw_grid(augs_scipy + augs_cv2, rows=2))

# check behaviour for multiple consecutive batches
aug = iaa.ElasticTransformation(alpha=(5, 100), sigma=(3, 5))
images1 = aug(images=[np.copy(image) for _ in range(10)])
images2 = aug(images=[np.copy(image) for _ in range(10)])
images3 = aug(images=[np.copy(image) for _ in range(10)])
ia.imshow(ia.draw_grid(images1 + images2 + images3, rows=3))

print("alpha=vary, sigma=0.25")
augs = [iaa.ElasticTransformation(alpha=alpha, sigma=0.25) for alpha in np.arange(0.0, 50.0, 0.1)]
images_aug = [aug.augment_image(image) for aug in augs]
Expand Down
15 changes: 15 additions & 0 deletions checks/check_segmentation_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ def main():
])
)

print("ElasticTransformation alpha=200, sig=20...")
aug = iaa.ElasticTransformation(alpha=200.0, sigma=20.0)
aug_det = aug.to_deterministic()
quokka_aug = aug_det.augment_image(quokka)
segmaps_aug = aug_det.augment_segmentation_maps(segmap)
segmaps_drawn = segmap.draw_on_image(quokka)[0]
segmaps_aug_drawn = segmaps_aug.draw_on_image(quokka_aug)[0]

ia.imshow(
np.hstack([
segmaps_drawn,
segmaps_aug_drawn
])
)

print("CopAndPad mode=constant...")
aug = iaa.CropAndPad(px=(-10, 10, 15, -15), pad_mode="constant", pad_cval=128)
aug_det = aug.to_deterministic()
Expand Down
Loading

0 comments on commit 18d3f0b

Please sign in to comment.