You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

adaptive_pooling.py 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. from abc import abstractmethod
  10. from typing import Tuple, Union
  11. from ..functional import adaptive_avg_pool2d, adaptive_max_pool2d
  12. from ..tensor import Parameter, Tensor
  13. from .module import Module
  14. class _AdaptivePoolNd(Module):
  15. def __init__(self, oshp: Union[Tuple[int, int], int, Tensor], **kwargs):
  16. super(_AdaptivePoolNd, self).__init__(**kwargs)
  17. self.oshp = oshp
  18. @abstractmethod
  19. def forward(self, inp):
  20. pass
  21. class AdaptiveMaxPool2d(_AdaptivePoolNd):
  22. r"""Applies a 2D max adaptive pooling over an input.
  23. For instance, given an input of the size :math:`(N, C, H, W)` and
  24. an output shape :math:`(OH, OW)`, this layer generates the output of
  25. the size :math:`(N, C, OH, OW)` through a process described as:
  26. .. math::
  27. \begin{aligned}
  28. out(N_i, C_j, h, w) ={} & \max_{m=0, \ldots, kH-1} \max_{n=0, \ldots, kW-1}
  29. \text{input}(N_i, C_j, \text{stride[0]} \times h + m,
  30. \text{stride[1]} \times w + n)
  31. \end{aligned}
  32. ``kernel_size`` and ``stride`` can be inferred from input shape and out shape:
  33. * padding: (0, 0)
  34. * stride: (floor(IH / OH), floor(IW / OW))
  35. * kernel_size: (IH - (OH - 1) * stride_h, IW - (OW - 1) * stride_w)
  36. Examples:
  37. >>> import numpy as np
  38. >>> m = M.AdaptiveMaxPool2d((2, 2))
  39. >>> inp = mge.tensor(np.arange(0, 16).astype("float32").reshape(1, 1, 4, 4))
  40. >>> oup = m(inp)
  41. >>> oup.numpy()
  42. array([[[[ 5., 7.],
  43. [13., 15.]]]], dtype=float32)
  44. """
  45. def forward(self, inp):
  46. return adaptive_max_pool2d(inp, self.oshp)
  47. class AdaptiveAvgPool2d(_AdaptivePoolNd):
  48. r"""Applies a 2D average pooling over an input.
  49. For instance, given an input of the size :math:`(N, C, H, W)` and
  50. an output shape :math:`(OH, OW)`, this layer generates the output of
  51. the size :math:`(N, C, OH, OW)` through a process described as:
  52. .. math::
  53. out(N_i, C_j, h, w) = \frac{1}{kH * kW} \sum_{m=0}^{kH-1} \sum_{n=0}^{kW-1}
  54. input(N_i, C_j, stride[0] \times h + m, stride[1] \times w + n)
  55. ``kernel_size`` and ``stride`` can be inferred from input shape and out shape:
  56. * padding: (0, 0)
  57. * stride: (floor(IH / OH), floor(IW / OW))
  58. * kernel_size: (IH - (OH - 1) * stride_h, IW - (OW - 1) * stride_w)
  59. Examples:
  60. >>> import numpy as np
  61. >>> m = M.AdaptiveAvgPool2d((2, 2))
  62. >>> inp = mge.tensor(np.arange(0, 16).astype("float32").reshape(1, 1, 4, 4))
  63. >>> oup = m(inp)
  64. >>> oup.numpy()
  65. array([[[[ 2.5, 4.5],
  66. [10.5, 12.5]]]], dtype=float32)
  67. """
  68. def forward(self, inp):
  69. return adaptive_avg_pool2d(inp, self.oshp)