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.

sliding_window.py 2.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 typing import Tuple, Union
  10. from ..functional import sliding_window
  11. from .module import Module
  12. class SlidingWindow(Module):
  13. r"""
  14. Apply a sliding window to input tensor and copy content in the window to
  15. corresponding output location. Assume input shape is :math:`(N, C, IH, IW)`,
  16. then output shape would be :math:`(N, C, OH, OW, window_h, window_w)` where
  17. :math:`(OH, OW)` would be computed from padding, stride, window and
  18. :math:`(IH, IW)`, as in convolution. For each output location, we have;
  19. .. math::
  20. out_{n, c, oh, ow, wh, ww} &= src_{n, c, ih+wh, iw+ww} \\
  21. \text{where } & ih=-pad_h+oh \times stride_h + (wh-1) \times (dilation_h-1) \\
  22. & iw=-pad_w+ow \times stride_w + (ww-1) \times (dilation_w-1)
  23. :param kernel_size: the size of the window to take a max over.
  24. :param padding: implicit zero padding to be added on both sides. Default: 0
  25. :param stride: the stride of the window. Default: 1
  26. :param dilation: the dilation of the window. Default: 1
  27. Example:
  28. .. testcode::
  29. from megengine import tensor
  30. import megengine.module as M
  31. import numpy as np
  32. inp = tensor(np.arange(30).reshape(1,1,5,6))
  33. op = M.SlidingWindow(kernel_size=3, padding=1, stride=2, dilation=2)
  34. out = op(inp)
  35. print(out.numpy())
  36. Outputs:
  37. .. testoutput::
  38. [[[[[[ 0 0 0]
  39. [ 0 7 9]
  40. [ 0 19 21]]
  41. [[ 0 0 0]
  42. [ 7 9 11]
  43. [19 21 23]]]
  44. [[[ 0 7 9]
  45. [ 0 19 21]
  46. [ 0 0 0]]
  47. [[ 7 9 11]
  48. [19 21 23]
  49. [ 0 0 0]]]]]]
  50. """
  51. def __init__(
  52. self,
  53. kernel_size: Union[int, Tuple[int, int]],
  54. padding: Union[int, Tuple[int, int]] = 0,
  55. stride: Union[int, Tuple[int, int]] = 1,
  56. dilation: Union[int, Tuple[int, int]] = 1,
  57. **kwargs
  58. ):
  59. super(SlidingWindow, self).__init__(**kwargs)
  60. self.kernel_size = kernel_size
  61. self.padding = padding
  62. self.stride = stride
  63. self.dilation = dilation
  64. def forward(self, inp):
  65. return sliding_window(
  66. inp, self.kernel_size, self.padding, self.stride, self.dilation
  67. )

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