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.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. .. testcode::
  38. import numpy as np
  39. import megengine as mge
  40. import megengine.module as M
  41. m = M.AdaptiveMaxPool2d((2, 2))
  42. inp = mge.tensor(np.arange(0, 16).astype("float32").reshape(1, 1, 4, 4))
  43. oup = m(inp)
  44. print(oup.numpy())
  45. Outputs:
  46. .. testoutput::
  47. [[[[ 5. 7.]
  48. [13. 15.]]]]
  49. """
  50. def forward(self, inp):
  51. return adaptive_max_pool2d(inp, self.oshp)
  52. class AdaptiveAvgPool2d(_AdaptivePoolNd):
  53. r"""Applies a 2D average pooling over an input.
  54. For instance, given an input of the size :math:`(N, C, H, W)` and
  55. an output shape :math:`(OH, OW)`, this layer generates the output of
  56. the size :math:`(N, C, OH, OW)` through a process described as:
  57. .. math::
  58. out(N_i, C_j, h, w) = \frac{1}{kH * kW} \sum_{m=0}^{kH-1} \sum_{n=0}^{kW-1}
  59. input(N_i, C_j, stride[0] \times h + m, stride[1] \times w + n)
  60. ``kernel_size`` and ``stride`` can be inferred from input shape and out shape:
  61. * padding: (0, 0)
  62. * stride: (floor(IH / OH), floor(IW / OW))
  63. * kernel_size: (IH - (OH - 1) * stride_h, IW - (OW - 1) * stride_w)
  64. Examples:
  65. .. testcode::
  66. import numpy as np
  67. import megengine as mge
  68. import megengine.module as M
  69. m = M.AdaptiveAvgPool2d((2, 2))
  70. inp = mge.tensor(np.arange(0, 16).astype("float32").reshape(1, 1, 4, 4))
  71. oup = m(inp)
  72. print(oup.numpy())
  73. Outputs:
  74. .. testoutput::
  75. [[[[ 2.5 4.5]
  76. [10.5 12.5]]]]
  77. """
  78. def forward(self, inp):
  79. return adaptive_avg_pool2d(inp, self.oshp)