pooling.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. """
  2. Augmenters that apply pooling operations to images.
  3. List of augmenters:
  4. * :class:`AveragePooling`
  5. * :class:`MaxPooling`
  6. * :class:`MinPooling`
  7. * :class:`MedianPooling`
  8. """
  9. from __future__ import print_function, division, absolute_import
  10. from abc import ABCMeta, abstractmethod
  11. import functools
  12. import six
  13. import numpy as np
  14. import imgaug as ia
  15. from . import meta
  16. from .. import parameters as iap
  17. def _compute_shape_after_pooling(image_shape, ksize_h, ksize_w):
  18. if any([axis == 0 for axis in image_shape]):
  19. return image_shape
  20. height, width = image_shape[0:2]
  21. if height % ksize_h > 0:
  22. height += ksize_h - (height % ksize_h)
  23. if width % ksize_w > 0:
  24. width += ksize_w - (width % ksize_w)
  25. return tuple([
  26. height//ksize_h,
  27. width//ksize_w,
  28. ] + list(image_shape[2:]))
  29. @six.add_metaclass(ABCMeta)
  30. class _AbstractPoolingBase(meta.Augmenter):
  31. # TODO add floats as ksize denoting fractions of image sizes
  32. # (note possible overlap with fractional kernel sizes here)
  33. def __init__(self, kernel_size, keep_size=True,
  34. seed=None, name=None,
  35. random_state="deprecated", deterministic="deprecated"):
  36. super(_AbstractPoolingBase, self).__init__(
  37. seed=seed, name=name,
  38. random_state=random_state, deterministic=deterministic)
  39. self.kernel_size = iap.handle_discrete_kernel_size_param(
  40. kernel_size,
  41. "kernel_size",
  42. value_range=(0, None),
  43. allow_floats=False)
  44. self.keep_size = keep_size
  45. self._resize_hm_and_sm_arrays = True
  46. @abstractmethod
  47. def _pool_image(self, image, kernel_size_h, kernel_size_w):
  48. """Apply pooling method with given kernel height/width to an image."""
  49. def _draw_samples(self, nb_rows, random_state):
  50. rss = random_state.duplicate(2)
  51. mode = "single" if self.kernel_size[1] is None else "two"
  52. kernel_sizes_h = self.kernel_size[0].draw_samples(
  53. (nb_rows,),
  54. random_state=rss[0])
  55. if mode == "single":
  56. kernel_sizes_w = kernel_sizes_h
  57. else:
  58. kernel_sizes_w = self.kernel_size[1].draw_samples(
  59. (nb_rows,), random_state=rss[1])
  60. return (
  61. np.clip(kernel_sizes_h, 1, None),
  62. np.clip(kernel_sizes_w, 1, None)
  63. )
  64. # Added in 0.4.0.
  65. def _augment_batch_(self, batch, random_state, parents, hooks):
  66. if batch.images is None and self.keep_size:
  67. return batch
  68. samples = self._draw_samples(batch.nb_rows, random_state)
  69. for column in batch.columns:
  70. value_aug = getattr(
  71. self, "_augment_%s_by_samples" % (column.name,)
  72. )(column.value, samples)
  73. setattr(batch, column.attr_name, value_aug)
  74. return batch
  75. # Added in 0.4.0.
  76. def _augment_images_by_samples(self, images, samples):
  77. if not self.keep_size:
  78. images = list(images)
  79. kernel_sizes_h, kernel_sizes_w = samples
  80. gen = enumerate(zip(images, kernel_sizes_h, kernel_sizes_w))
  81. for i, (image, ksize_h, ksize_w) in gen:
  82. if ksize_h >= 2 or ksize_w >= 2:
  83. image_pooled = self._pool_image(
  84. image, ksize_h, ksize_w)
  85. if self.keep_size:
  86. image_pooled = ia.imresize_single_image(
  87. image_pooled, image.shape[0:2])
  88. images[i] = image_pooled
  89. return images
  90. # Added in 0.4.0.
  91. def _augment_heatmaps_by_samples(self, heatmaps, samples):
  92. return self._augment_hms_and_segmaps_by_samples(heatmaps, samples,
  93. "arr_0to1")
  94. # Added in 0.4.0.
  95. def _augment_segmentation_maps_by_samples(self, segmaps, samples):
  96. return self._augment_hms_and_segmaps_by_samples(segmaps, samples,
  97. "arr")
  98. # Added in 0.4.0.
  99. def _augment_hms_and_segmaps_by_samples(self, augmentables, samples,
  100. arr_attr_name):
  101. if self.keep_size:
  102. return augmentables
  103. kernel_sizes_h, kernel_sizes_w = samples
  104. gen = enumerate(zip(augmentables, kernel_sizes_h, kernel_sizes_w))
  105. for i, (augmentable, ksize_h, ksize_w) in gen:
  106. if ksize_h >= 2 or ksize_w >= 2:
  107. # We could also keep the size of the HM/SM array unchanged
  108. # here as the library can handle HMs/SMs that are larger
  109. # than the image. This might be inintuitive however and
  110. # could lead to unnecessary performance degredation.
  111. if self._resize_hm_and_sm_arrays:
  112. new_shape_arr = _compute_shape_after_pooling(
  113. getattr(augmentable, arr_attr_name).shape,
  114. ksize_h, ksize_w)
  115. augmentable = augmentable.resize(new_shape_arr[0:2])
  116. new_shape = _compute_shape_after_pooling(
  117. augmentable.shape, ksize_h, ksize_w)
  118. augmentable.shape = new_shape
  119. augmentables[i] = augmentable
  120. return augmentables
  121. # Added in 0.4.0.
  122. def _augment_keypoints_by_samples(self, keypoints_on_images, samples):
  123. if self.keep_size:
  124. return keypoints_on_images
  125. kernel_sizes_h, kernel_sizes_w = samples
  126. gen = enumerate(zip(keypoints_on_images, kernel_sizes_h,
  127. kernel_sizes_w))
  128. for i, (kpsoi, ksize_h, ksize_w) in gen:
  129. if ksize_h >= 2 or ksize_w >= 2:
  130. new_shape = _compute_shape_after_pooling(
  131. kpsoi.shape, ksize_h, ksize_w)
  132. keypoints_on_images[i] = kpsoi.on_(new_shape)
  133. return keypoints_on_images
  134. # Added in 0.4.0.
  135. def _augment_polygons_by_samples(self, polygons_on_images, samples):
  136. func = functools.partial(self._augment_keypoints_by_samples,
  137. samples=samples)
  138. return self._apply_to_polygons_as_keypoints(polygons_on_images, func,
  139. recoverer=None)
  140. # Added in 0.4.0.
  141. def _augment_line_strings_by_samples(self, line_strings_on_images, samples):
  142. func = functools.partial(self._augment_keypoints_by_samples,
  143. samples=samples)
  144. return self._apply_to_cbaois_as_keypoints(line_strings_on_images, func)
  145. # Added in 0.4.0.
  146. def _augment_bounding_boxes_by_samples(self, bounding_boxes_on_images,
  147. samples):
  148. func = functools.partial(self._augment_keypoints_by_samples,
  149. samples=samples)
  150. return self._apply_to_cbaois_as_keypoints(bounding_boxes_on_images,
  151. func)
  152. def get_parameters(self):
  153. """See :func:`~imgaug.augmenters.meta.Augmenter.get_parameters`."""
  154. return [self.kernel_size, self.keep_size]
  155. # TODO rename kernel size parameters in all augmenters to kernel_size
  156. # TODO add per_channel
  157. # TODO add upscaling interpolation mode?
  158. class AveragePooling(_AbstractPoolingBase):
  159. """
  160. Apply average pooling to images.
  161. This augmenter pools images with kernel sizes ``H x W`` by averaging the
  162. pixel values within these windows. For e.g. ``2 x 2`` this halves the image
  163. size. Optionally, the augmenter will automatically re-upscale the image
  164. to the input size (by default this is activated).
  165. Note that this augmenter is very similar to ``AverageBlur``.
  166. ``AverageBlur`` applies averaging within windows of given kernel size
  167. *without* striding, while ``AveragePooling`` applies striding corresponding
  168. to the kernel size, with optional upscaling afterwards. The upscaling
  169. is configured to create "pixelated"/"blocky" images by default.
  170. .. note::
  171. During heatmap or segmentation map augmentation, the respective
  172. arrays are not changed, only the shapes of the underlying images
  173. are updated. This is because imgaug can handle maps/maks that are
  174. larger/smaller than their corresponding image.
  175. **Supported dtypes**:
  176. See :func:`~imgaug.imgaug.avg_pool`.
  177. Attributes
  178. ----------
  179. kernel_size : int or tuple of int or list of int or imgaug.parameters.StochasticParameter or tuple of tuple of int or tuple of list of int or tuple of imgaug.parameters.StochasticParameter, optional
  180. The kernel size of the pooling operation.
  181. * If an int, then that value will be used for all images for both
  182. kernel height and width.
  183. * If a tuple ``(a, b)``, then a value from the discrete range
  184. ``[a..b]`` will be sampled per image.
  185. * If a list, then a random value will be sampled from that list per
  186. image and used for both kernel height and width.
  187. * If a StochasticParameter, then a value will be sampled per image
  188. from that parameter per image and used for both kernel height and
  189. width.
  190. * If a tuple of tuple of int given as ``((a, b), (c, d))``, then two
  191. values will be sampled independently from the discrete ranges
  192. ``[a..b]`` and ``[c..d]`` per image and used as the kernel height
  193. and width.
  194. * If a tuple of lists of int, then two values will be sampled
  195. independently per image, one from the first list and one from the
  196. second, and used as the kernel height and width.
  197. * If a tuple of StochasticParameter, then two values will be sampled
  198. indepdently per image, one from the first parameter and one from the
  199. second, and used as the kernel height and width.
  200. keep_size : bool, optional
  201. After pooling, the result image will usually have a different
  202. height/width compared to the original input image. If this
  203. parameter is set to True, then the pooled image will be resized
  204. to the input image's size, i.e. the augmenter's output shape is always
  205. identical to the input shape.
  206. seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
  207. See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.
  208. name : None or str, optional
  209. See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.
  210. random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
  211. Old name for parameter `seed`.
  212. Its usage will not yet cause a deprecation warning,
  213. but it is still recommended to use `seed` now.
  214. Outdated since 0.4.0.
  215. deterministic : bool, optional
  216. Deprecated since 0.4.0.
  217. See method ``to_deterministic()`` for an alternative and for
  218. details about what the "deterministic mode" actually does.
  219. Examples
  220. --------
  221. >>> import imgaug.augmenters as iaa
  222. >>> aug = iaa.AveragePooling(2)
  223. Create an augmenter that always pools with a kernel size of ``2 x 2``.
  224. >>> aug = iaa.AveragePooling(2, keep_size=False)
  225. Create an augmenter that always pools with a kernel size of ``2 x 2``
  226. and does *not* resize back to the input image size, i.e. the resulting
  227. images have half the resolution.
  228. >>> aug = iaa.AveragePooling([2, 8])
  229. Create an augmenter that always pools either with a kernel size
  230. of ``2 x 2`` or ``8 x 8``.
  231. >>> aug = iaa.AveragePooling((1, 7))
  232. Create an augmenter that always pools with a kernel size of
  233. ``1 x 1`` (does nothing) to ``7 x 7``. The kernel sizes are always
  234. symmetric.
  235. >>> aug = iaa.AveragePooling(((1, 7), (1, 7)))
  236. Create an augmenter that always pools with a kernel size of
  237. ``H x W`` where ``H`` and ``W`` are both sampled independently from the
  238. range ``[1..7]``. E.g. resulting kernel sizes could be ``3 x 7``
  239. or ``5 x 1``.
  240. """
  241. # TODO add floats as ksize denoting fractions of image sizes
  242. # (note possible overlap with fractional kernel sizes here)
  243. def __init__(self, kernel_size=(1, 5), keep_size=True,
  244. seed=None, name=None,
  245. random_state="deprecated", deterministic="deprecated"):
  246. super(AveragePooling, self).__init__(
  247. kernel_size=kernel_size, keep_size=keep_size,
  248. seed=seed, name=name,
  249. random_state=random_state, deterministic=deterministic)
  250. def _pool_image(self, image, kernel_size_h, kernel_size_w):
  251. return ia.avg_pool(
  252. image,
  253. (kernel_size_h, kernel_size_w)
  254. )
  255. class MaxPooling(_AbstractPoolingBase):
  256. """
  257. Apply max pooling to images.
  258. This augmenter pools images with kernel sizes ``H x W`` by taking the
  259. maximum pixel value over windows. For e.g. ``2 x 2`` this halves the image
  260. size. Optionally, the augmenter will automatically re-upscale the image
  261. to the input size (by default this is activated).
  262. The maximum within each pixel window is always taken channelwise..
  263. .. note::
  264. During heatmap or segmentation map augmentation, the respective
  265. arrays are not changed, only the shapes of the underlying images
  266. are updated. This is because imgaug can handle maps/maks that are
  267. larger/smaller than their corresponding image.
  268. **Supported dtypes**:
  269. See :func:`~imgaug.imgaug.max_pool`.
  270. Attributes
  271. ----------
  272. kernel_size : int or tuple of int or list of int or imgaug.parameters.StochasticParameter or tuple of tuple of int or tuple of list of int or tuple of imgaug.parameters.StochasticParameter, optional
  273. The kernel size of the pooling operation.
  274. * If an int, then that value will be used for all images for both
  275. kernel height and width.
  276. * If a tuple ``(a, b)``, then a value from the discrete range
  277. ``[a..b]`` will be sampled per image.
  278. * If a list, then a random value will be sampled from that list per
  279. image and used for both kernel height and width.
  280. * If a StochasticParameter, then a value will be sampled per image
  281. from that parameter per image and used for both kernel height and
  282. width.
  283. * If a tuple of tuple of int given as ``((a, b), (c, d))``, then two
  284. values will be sampled independently from the discrete ranges
  285. ``[a..b]`` and ``[c..d]`` per image and used as the kernel height
  286. and width.
  287. * If a tuple of lists of int, then two values will be sampled
  288. independently per image, one from the first list and one from the
  289. second, and used as the kernel height and width.
  290. * If a tuple of StochasticParameter, then two values will be sampled
  291. indepdently per image, one from the first parameter and one from the
  292. second, and used as the kernel height and width.
  293. keep_size : bool, optional
  294. After pooling, the result image will usually have a different
  295. height/width compared to the original input image. If this
  296. parameter is set to True, then the pooled image will be resized
  297. to the input image's size, i.e. the augmenter's output shape is always
  298. identical to the input shape.
  299. seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
  300. See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.
  301. name : None or str, optional
  302. See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.
  303. random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
  304. Old name for parameter `seed`.
  305. Its usage will not yet cause a deprecation warning,
  306. but it is still recommended to use `seed` now.
  307. Outdated since 0.4.0.
  308. deterministic : bool, optional
  309. Deprecated since 0.4.0.
  310. See method ``to_deterministic()`` for an alternative and for
  311. details about what the "deterministic mode" actually does.
  312. Examples
  313. --------
  314. >>> import imgaug.augmenters as iaa
  315. >>> aug = iaa.MaxPooling(2)
  316. Create an augmenter that always pools with a kernel size of ``2 x 2``.
  317. >>> aug = iaa.MaxPooling(2, keep_size=False)
  318. Create an augmenter that always pools with a kernel size of ``2 x 2``
  319. and does *not* resize back to the input image size, i.e. the resulting
  320. images have half the resolution.
  321. >>> aug = iaa.MaxPooling([2, 8])
  322. Create an augmenter that always pools either with a kernel size
  323. of ``2 x 2`` or ``8 x 8``.
  324. >>> aug = iaa.MaxPooling((1, 7))
  325. Create an augmenter that always pools with a kernel size of
  326. ``1 x 1`` (does nothing) to ``7 x 7``. The kernel sizes are always
  327. symmetric.
  328. >>> aug = iaa.MaxPooling(((1, 7), (1, 7)))
  329. Create an augmenter that always pools with a kernel size of
  330. ``H x W`` where ``H`` and ``W`` are both sampled independently from the
  331. range ``[1..7]``. E.g. resulting kernel sizes could be ``3 x 7``
  332. or ``5 x 1``.
  333. """
  334. # TODO add floats as ksize denoting fractions of image sizes
  335. # (note possible overlap with fractional kernel sizes here)
  336. def __init__(self, kernel_size=(1, 5), keep_size=True,
  337. seed=None, name=None,
  338. random_state="deprecated", deterministic="deprecated"):
  339. super(MaxPooling, self).__init__(
  340. kernel_size=kernel_size, keep_size=keep_size,
  341. seed=seed, name=name,
  342. random_state=random_state, deterministic=deterministic)
  343. def _pool_image(self, image, kernel_size_h, kernel_size_w):
  344. # TODO extend max_pool to support pad_mode and set it here
  345. # to reflection padding
  346. return ia.max_pool(
  347. image,
  348. (kernel_size_h, kernel_size_w)
  349. )
  350. class MinPooling(_AbstractPoolingBase):
  351. """
  352. Apply minimum pooling to images.
  353. This augmenter pools images with kernel sizes ``H x W`` by taking the
  354. minimum pixel value over windows. For e.g. ``2 x 2`` this halves the image
  355. size. Optionally, the augmenter will automatically re-upscale the image
  356. to the input size (by default this is activated).
  357. The minimum within each pixel window is always taken channelwise.
  358. .. note::
  359. During heatmap or segmentation map augmentation, the respective
  360. arrays are not changed, only the shapes of the underlying images
  361. are updated. This is because imgaug can handle maps/maks that are
  362. larger/smaller than their corresponding image.
  363. **Supported dtypes**:
  364. See :func:`~imgaug.imgaug.min_pool`.
  365. Attributes
  366. ----------
  367. kernel_size : int or tuple of int or list of int or imgaug.parameters.StochasticParameter or tuple of tuple of int or tuple of list of int or tuple of imgaug.parameters.StochasticParameter, optional
  368. The kernel size of the pooling operation.
  369. * If an int, then that value will be used for all images for both
  370. kernel height and width.
  371. * If a tuple ``(a, b)``, then a value from the discrete range
  372. ``[a..b]`` will be sampled per image.
  373. * If a list, then a random value will be sampled from that list per
  374. image and used for both kernel height and width.
  375. * If a StochasticParameter, then a value will be sampled per image
  376. from that parameter per image and used for both kernel height and
  377. width.
  378. * If a tuple of tuple of int given as ``((a, b), (c, d))``, then two
  379. values will be sampled independently from the discrete ranges
  380. ``[a..b]`` and ``[c..d]`` per image and used as the kernel height
  381. and width.
  382. * If a tuple of lists of int, then two values will be sampled
  383. independently per image, one from the first list and one from the
  384. second, and used as the kernel height and width.
  385. * If a tuple of StochasticParameter, then two values will be sampled
  386. indepdently per image, one from the first parameter and one from the
  387. second, and used as the kernel height and width.
  388. keep_size : bool, optional
  389. After pooling, the result image will usually have a different
  390. height/width compared to the original input image. If this
  391. parameter is set to True, then the pooled image will be resized
  392. to the input image's size, i.e. the augmenter's output shape is always
  393. identical to the input shape.
  394. seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
  395. See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.
  396. name : None or str, optional
  397. See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.
  398. random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
  399. Old name for parameter `seed`.
  400. Its usage will not yet cause a deprecation warning,
  401. but it is still recommended to use `seed` now.
  402. Outdated since 0.4.0.
  403. deterministic : bool, optional
  404. Deprecated since 0.4.0.
  405. See method ``to_deterministic()`` for an alternative and for
  406. details about what the "deterministic mode" actually does.
  407. Examples
  408. --------
  409. >>> import imgaug.augmenters as iaa
  410. >>> aug = iaa.MinPooling(2)
  411. Create an augmenter that always pools with a kernel size of ``2 x 2``.
  412. >>> aug = iaa.MinPooling(2, keep_size=False)
  413. Create an augmenter that always pools with a kernel size of ``2 x 2``
  414. and does *not* resize back to the input image size, i.e. the resulting
  415. images have half the resolution.
  416. >>> aug = iaa.MinPooling([2, 8])
  417. Create an augmenter that always pools either with a kernel size
  418. of ``2 x 2`` or ``8 x 8``.
  419. >>> aug = iaa.MinPooling((1, 7))
  420. Create an augmenter that always pools with a kernel size of
  421. ``1 x 1`` (does nothing) to ``7 x 7``. The kernel sizes are always
  422. symmetric.
  423. >>> aug = iaa.MinPooling(((1, 7), (1, 7)))
  424. Create an augmenter that always pools with a kernel size of
  425. ``H x W`` where ``H`` and ``W`` are both sampled independently from the
  426. range ``[1..7]``. E.g. resulting kernel sizes could be ``3 x 7``
  427. or ``5 x 1``.
  428. """
  429. # TODO add floats as ksize denoting fractions of image sizes
  430. # (note possible overlap with fractional kernel sizes here)
  431. def __init__(self, kernel_size=(1, 5), keep_size=True,
  432. seed=None, name=None,
  433. random_state="deprecated", deterministic="deprecated"):
  434. super(MinPooling, self).__init__(
  435. kernel_size=kernel_size, keep_size=keep_size,
  436. seed=seed, name=name,
  437. random_state=random_state, deterministic=deterministic)
  438. def _pool_image(self, image, kernel_size_h, kernel_size_w):
  439. # TODO extend pool to support pad_mode and set it here
  440. # to reflection padding
  441. return ia.min_pool(
  442. image,
  443. (kernel_size_h, kernel_size_w)
  444. )
  445. class MedianPooling(_AbstractPoolingBase):
  446. """
  447. Apply median pooling to images.
  448. This augmenter pools images with kernel sizes ``H x W`` by taking the
  449. median pixel value over windows. For e.g. ``2 x 2`` this halves the image
  450. size. Optionally, the augmenter will automatically re-upscale the image
  451. to the input size (by default this is activated).
  452. The median within each pixel window is always taken channelwise.
  453. .. note::
  454. During heatmap or segmentation map augmentation, the respective
  455. arrays are not changed, only the shapes of the underlying images
  456. are updated. This is because imgaug can handle maps/maks that are
  457. larger/smaller than their corresponding image.
  458. **Supported dtypes**:
  459. See :func:`~imgaug.imgaug.median_pool`.
  460. Attributes
  461. ----------
  462. kernel_size : int or tuple of int or list of int or imgaug.parameters.StochasticParameter or tuple of tuple of int or tuple of list of int or tuple of imgaug.parameters.StochasticParameter, optional
  463. The kernel size of the pooling operation.
  464. * If an int, then that value will be used for all images for both
  465. kernel height and width.
  466. * If a tuple ``(a, b)``, then a value from the discrete range
  467. ``[a..b]`` will be sampled per image.
  468. * If a list, then a random value will be sampled from that list per
  469. image and used for both kernel height and width.
  470. * If a StochasticParameter, then a value will be sampled per image
  471. from that parameter per image and used for both kernel height and
  472. width.
  473. * If a tuple of tuple of int given as ``((a, b), (c, d))``, then two
  474. values will be sampled independently from the discrete ranges
  475. ``[a..b]`` and ``[c..d]`` per image and used as the kernel height
  476. and width.
  477. * If a tuple of lists of int, then two values will be sampled
  478. independently per image, one from the first list and one from the
  479. second, and used as the kernel height and width.
  480. * If a tuple of StochasticParameter, then two values will be sampled
  481. indepdently per image, one from the first parameter and one from the
  482. second, and used as the kernel height and width.
  483. keep_size : bool, optional
  484. After pooling, the result image will usually have a different
  485. height/width compared to the original input image. If this
  486. parameter is set to True, then the pooled image will be resized
  487. to the input image's size, i.e. the augmenter's output shape is always
  488. identical to the input shape.
  489. seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
  490. See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.
  491. name : None or str, optional
  492. See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.
  493. random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
  494. Old name for parameter `seed`.
  495. Its usage will not yet cause a deprecation warning,
  496. but it is still recommended to use `seed` now.
  497. Outdated since 0.4.0.
  498. deterministic : bool, optional
  499. Deprecated since 0.4.0.
  500. See method ``to_deterministic()`` for an alternative and for
  501. details about what the "deterministic mode" actually does.
  502. Examples
  503. --------
  504. >>> import imgaug.augmenters as iaa
  505. >>> aug = iaa.MedianPooling(2)
  506. Create an augmenter that always pools with a kernel size of ``2 x 2``.
  507. >>> aug = iaa.MedianPooling(2, keep_size=False)
  508. Create an augmenter that always pools with a kernel size of ``2 x 2``
  509. and does *not* resize back to the input image size, i.e. the resulting
  510. images have half the resolution.
  511. >>> aug = iaa.MedianPooling([2, 8])
  512. Create an augmenter that always pools either with a kernel size
  513. of ``2 x 2`` or ``8 x 8``.
  514. >>> aug = iaa.MedianPooling((1, 7))
  515. Create an augmenter that always pools with a kernel size of
  516. ``1 x 1`` (does nothing) to ``7 x 7``. The kernel sizes are always
  517. symmetric.
  518. >>> aug = iaa.MedianPooling(((1, 7), (1, 7)))
  519. Create an augmenter that always pools with a kernel size of
  520. ``H x W`` where ``H`` and ``W`` are both sampled independently from the
  521. range ``[1..7]``. E.g. resulting kernel sizes could be ``3 x 7``
  522. or ``5 x 1``.
  523. """
  524. # TODO add floats as ksize denoting fractions of image sizes
  525. # (note possible overlap with fractional kernel sizes here)
  526. def __init__(self, kernel_size=(1, 5), keep_size=True,
  527. seed=None, name=None,
  528. random_state="deprecated", deterministic="deprecated"):
  529. super(MedianPooling, self).__init__(
  530. kernel_size=kernel_size, keep_size=keep_size,
  531. seed=seed, name=name,
  532. random_state=random_state, deterministic=deterministic)
  533. def _pool_image(self, image, kernel_size_h, kernel_size_w):
  534. # TODO extend pool to support pad_mode and set it here
  535. # to reflection padding
  536. return ia.median_pool(
  537. image,
  538. (kernel_size_h, kernel_size_w)
  539. )