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.

pooling.py 2.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 avg_pool2d, max_pool2d
  12. from .module import Module
  13. class _PoolNd(Module):
  14. def __init__(
  15. self,
  16. kernel_size: Union[int, Tuple[int, int]],
  17. stride: Union[int, Tuple[int, int]] = None,
  18. padding: Union[int, Tuple[int, int]] = 0,
  19. ):
  20. super(_PoolNd, self).__init__()
  21. self.kernel_size = kernel_size
  22. self.stride = stride or kernel_size
  23. self.padding = padding
  24. @abstractmethod
  25. def forward(self, inp):
  26. pass
  27. class MaxPool2d(_PoolNd):
  28. r"""Applies a 2D max pooling over an input.
  29. For instance, given an input of the size :math:`(N, C, H, W)` and
  30. :attr:`kernel_size` :math:`(kH, kW)`, this layer generates the output of
  31. the size :math:`(N, C, H_{out}, W_{out})` through a process described as:
  32. .. math::
  33. \begin{aligned}
  34. out(N_i, C_j, h, w) ={} & \max_{m=0, \ldots, kH-1} \max_{n=0, \ldots, kW-1}
  35. \text{input}(N_i, C_j, \text{stride[0]} \times h + m,
  36. \text{stride[1]} \times w + n)
  37. \end{aligned}
  38. If :attr:`padding` is non-zero, then the input is implicitly zero-padded on
  39. both sides for :attr:`padding` number of points.
  40. :param kernel_size: the size of the window to take a max over.
  41. :param stride: the stride of the window. Default value is ``kernel_size``.
  42. :param padding: implicit zero padding to be added on both sides.
  43. """
  44. def forward(self, inp):
  45. return max_pool2d(inp, self.kernel_size, self.stride, self.padding)
  46. class AvgPool2d(_PoolNd):
  47. r"""Applies a 2D average pooling over an input.
  48. For instance, given an input of the size :math:`(N, C, H, W)` and
  49. :attr:`kernel_size` :math:`(kH, kW)`, this layer generates the output of
  50. the size :math:`(N, C, H_{out}, W_{out})` through a process described as:
  51. .. math::
  52. out(N_i, C_j, h, w) = \frac{1}{kH * kW} \sum_{m=0}^{kH-1} \sum_{n=0}^{kW-1}
  53. input(N_i, C_j, stride[0] \times h + m, stride[1] \times w + n)
  54. If :attr:`padding` is non-zero, then the input is implicitly zero-padded on
  55. both sides for :attr:`padding` number of points.
  56. :param kernel_size: the size of the window.
  57. :param stride: the stride of the window. Default value is ``kernel_size``.
  58. :param padding: implicit zero padding to be added on both sides.
  59. """
  60. def forward(self, inp):
  61. return avg_pool2d(inp, self.kernel_size, self.stride, self.padding)

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