Skip to content

Commit

Permalink
Improve Performance of Segment Replacement (aleju#640)
Browse files Browse the repository at this point in the history
This patch improves the performance of segment
replacement (by average colors within the segments),
used in `Superpixels` and `segment_voronoi()`.
The new method is up to around 7x faster, more for
smaller images and more segments. It can be slightly
slower in some cases for large images (512x512 and
larger).

This change seems to improve the overall performance
of `Superpixels` by a factor of around 1.1x to 1.4x
(more for smaller images).
It improves the overall performance of
`segment_voronoi()` by about 1.1x to 2.0x and can
reach much higher improvements in the case of very few
segments that have to be replaced.

Note that `segment_voronoi()` is used in `Voronoi`.

Added functions:
* `imgaug.augmenters.segmentation.replace_segments_`

Added classes:
* `imgaug.testutils.temporary_constants` (context)
  • Loading branch information
aleju authored May 3, 2020
1 parent e4db0b0 commit 7b27fbe
Show file tree
Hide file tree
Showing 5 changed files with 426 additions and 239 deletions.
25 changes: 25 additions & 0 deletions changelogs/master/improved/20200315_segment_replacement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Improved Performance of Segment Replacement #640

This patch improves the performance of segment
replacement (by average colors within the segments),
used in `Superpixels` and `segment_voronoi()`.
The new method is up to around 7x faster, more for
smaller images and more segments. It can be slightly
slower in some cases for large images (512x512 and
larger).

This change seems to improve the overall performance
of `Superpixels` by a factor of around 1.1x to 1.4x
(more for smaller images).
It improves the overall performance of
`segment_voronoi()` by about 1.1x to 2.0x and can
reach much higher improvements in the case of very few
segments that have to be replaced.

Note that `segment_voronoi()` is used in `Voronoi`.

Added functions:
* `imgaug.augmenters.segmentation.replace_segments_`

Added classes:
* `imgaug.testutils.temporary_constants` (context)
28 changes: 15 additions & 13 deletions checks/check_voronoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ def main():
)
uniform_sampler = iaa.UniformPointsSampler(50*50)

augs = [
iaa.Voronoi(points_sampler=reggrid_sampler, p_replace=1.0,
max_size=128),
iaa.Voronoi(points_sampler=uniform_sampler, p_replace=1.0,
max_size=128),
iaa.UniformVoronoi(50*50, p_replace=1.0, max_size=128),
iaa.RegularGridVoronoi(50, 50, p_drop_points=0.4, p_replace=1.0,
max_size=128),
]

images = [aug(image=image) for aug in augs]

ia.imshow(np.hstack(images))
for p_replace in [1.0, 0.5, 0.1, 0.0]:
augs = [
iaa.Voronoi(points_sampler=reggrid_sampler, p_replace=p_replace,
max_size=128),
iaa.Voronoi(points_sampler=uniform_sampler, p_replace=p_replace,
max_size=128),
iaa.UniformVoronoi(50*50, p_replace=p_replace, max_size=128),
iaa.RegularGridVoronoi(50, 50, p_drop_points=0.4,
p_replace=p_replace, max_size=128),
iaa.RelativeRegularGridVoronoi(p_replace=p_replace, max_size=128)
]

images = [aug(image=image) for aug in augs]

ia.imshow(np.hstack(images))


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 7b27fbe

Please sign in to comment.