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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 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__(
  16. self, oshp: Union[Tuple[int, int], int, Tensor],
  17. ):
  18. super(_AdaptivePoolNd, self).__init__()
  19. self.oshp = oshp
  20. @abstractmethod
  21. def forward(self, inp):
  22. pass
  23. class AdaptiveMaxPool2d(_AdaptivePoolNd):
  24. r"""
  25. Applies a 2D max adaptive pooling over an input.
  26. For instance, given an input of the size :math:`(N, C, H, W)` and
  27. an output shape :math:`(OH, OW)`, this layer generates the output of
  28. the size :math:`(N, C, OH, OW)` through a process described as:
  29. .. math::
  30. \begin{aligned}
  31. out(N_i, C_j, h, w) ={} & \max_{m=0, \ldots, kH-1} \max_{n=0, \ldots, kW-1}
  32. \text{input}(N_i, C_j, \text{stride[0]} \times h + m,
  33. \text{stride[1]} \times w + n)
  34. \end{aligned}
  35. ``kernel_size`` and ``stride`` can be inferred from input shape and out shape:
  36. * padding: (0, 0)
  37. * stride: (floor(IH / OH), floor(IW / OW))
  38. * kernel_size: (IH - (OH - 1) * stride_h, IW - (OW - 1) * stride_w)
  39. Examples:
  40. .. testcode::
  41. import numpy as np
  42. import megengine as mge
  43. import megengine.module as M
  44. m = M.AdaptiveMaxPool2d((2, 2))
  45. inp = mge.tensor(np.arange(0, 16).astype("float32").reshape(1, 1, 4, 4))
  46. oup = m(inp)
  47. print(oup.numpy())
  48. Outputs:
  49. .. testoutput::
  50. [[[[ 5. 7.]
  51. [13. 15.]]]]
  52. """
  53. def forward(self, inp):
  54. return adaptive_max_pool2d(inp, self.oshp)
  55. class AdaptiveAvgPool2d(_AdaptivePoolNd):
  56. r"""
  57. Applies a 2D average pooling over an input.
  58. For instance, given an input of the size :math:`(N, C, H, W)` and
  59. an output shape :math:`(OH, OW)`, this layer generates the output of
  60. the size :math:`(N, C, OH, OW)` through a process described as:
  61. .. math::
  62. out(N_i, C_j, h, w) = \frac{1}{kH * kW} \sum_{m=0}^{kH-1} \sum_{n=0}^{kW-1}
  63. input(N_i, C_j, stride[0] \times h + m, stride[1] \times w + n)
  64. ``kernel_size`` and ``stride`` can be inferred from input shape and out shape:
  65. * padding: (0, 0)
  66. * stride: (floor(IH / OH), floor(IW / OW))
  67. * kernel_size: (IH - (OH - 1) * stride_h, IW - (OW - 1) * stride_w)
  68. Examples:
  69. .. testcode::
  70. import numpy as np
  71. import megengine as mge
  72. import megengine.module as M
  73. m = M.AdaptiveAvgPool2d((2, 2))
  74. inp = mge.tensor(np.arange(0, 16).astype("float32").reshape(1, 1, 4, 4))
  75. oup = m(inp)
  76. print(oup.numpy())
  77. Outputs:
  78. .. testoutput::
  79. [[[[ 2.5 4.5]
  80. [10.5 12.5]]]]
  81. """
  82. def forward(self, inp):
  83. return adaptive_avg_pool2d(inp, self.oshp)

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台