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 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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, sliding_window_transpose
  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. )
  68. class SlidingWindowTranspose(Module):
  69. r"""
  70. Opposite opration of SlidingWindow, sum over the sliding windows on the
  71. corresponding input location. Given an input of the size
  72. :math:`(N, C, IH, IW, window_h, window_w)` and :attr:`output_size`, the
  73. output shape would be :math:`(N, C, output\_size_{h}, output\_size_{w})` and the
  74. arguments must satisfy
  75. .. math::
  76. \text{IH} = \lfloor \frac{\text{output_size}_{h} + 2 * \text{padding}_{h} -
  77. \text{dilation}_{h} * (\text{kernel_size}_{h} - 1) - 1}{\text{stride}_{h}} + 1 \rfloor
  78. .. math::
  79. \text{IW} = \lfloor \frac{\text{output_size}_{w} + 2 * \text{padding}_{w} -
  80. \text{dilation}_{w} * (\text{kernel_size}_{w} - 1) - 1}{\text{stride}_{w}} + 1 \rfloor
  81. For each output location, we have:
  82. .. math::
  83. \text{out}_{n, c, oh, ow} = \sum_{n,c,oh,ow=location(n, c, ih, iw, wh, ww)}\text{src}_{n, c, ih, iw, wh, ww}
  84. .. math::
  85. \text{location}(n, c, ih, iw, wh, ww) &= (n, c, oh+wh, ow+ww) \\
  86. \text{where } & oh=-pad_h+ih \times stride_h + (wh-1) \times (dilation_h-1) \\
  87. & ow=-pad_w+iw \times stride_w + (ww-1) \times (dilation_w-1)
  88. :param output_size: the size of the output tensor.
  89. :param kernel_size: the size of the window to take a max over.
  90. :param padding: implicit zero padding to be added on both sides. Default: 0
  91. :param stride: the stride of the window. Default: 1
  92. :param dilation: the dilation of the window. Default: 1
  93. Example:
  94. .. testcode::
  95. from megengine import tensor
  96. import megengine.module as M
  97. import numpy as np
  98. inp = tensor(np.arange(20).reshape(1,1,4,5))
  99. unfold = M.SlidingWindow(kernel_size=3, padding=0, stride=1, dilation=1)
  100. fold = M.SlidingWindowTranspose((4,5), kernel_size=3, padding=0, stride=1, dilation=1)
  101. out = fold(unfold(inp))
  102. print(out.numpy())
  103. Outputs:
  104. .. testoutput::
  105. [[[[ 0 2 6 6 4]
  106. [10 24 42 32 18]
  107. [20 44 72 52 28]
  108. [15 32 51 36 19]]]]
  109. """
  110. def __init__(
  111. self,
  112. output_size: Union[int, Tuple[int, int]],
  113. kernel_size: Union[int, Tuple[int, int]],
  114. padding: Union[int, Tuple[int, int]] = 0,
  115. stride: Union[int, Tuple[int, int]] = 1,
  116. dilation: Union[int, Tuple[int, int]] = 1,
  117. **kwargs
  118. ):
  119. super(SlidingWindowTranspose, self).__init__(**kwargs)
  120. self.output_size = output_size
  121. self.kernel_size = kernel_size
  122. self.padding = padding
  123. self.stride = stride
  124. self.dilation = dilation
  125. def forward(self, inp):
  126. return sliding_window_transpose(
  127. inp,
  128. self.output_size,
  129. self.kernel_size,
  130. self.padding,
  131. self.stride,
  132. self.dilation,
  133. )

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