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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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"""Apply a sliding window to input tensor and copy content in the window to
  14. corresponding output location. Assume input shape is :math:`(N, C, IH, IW)`,
  15. then output shape would be :math:`(N, C, OH, OW, window_h, window_w)` where
  16. :math:`(OH, OW)` would be computed from padding, stride, window and
  17. :math:`(IH, IW)`, as in convolution. For each output location, we have;
  18. .. math::
  19. out_{n, c, oh, ow, wh, ww} &= src_{n, c, ih+wh, iw+ww} \\
  20. \text{where } & ih=-pad_h+oh \times stride_h + (wh-1) \times (dilation_h-1) \\
  21. & iw=-pad_w+ow \times stride_w + (ww-1) \times (dilation_w-1)
  22. Args:
  23. kernel_size: the size of the window to take a max over.
  24. padding: implicit zero padding to be added on both sides. Default: 0
  25. stride: the stride of the window. Default: 1
  26. 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"""Opposite opration of SlidingWindow, sum over the sliding windows on the
  70. corresponding input location. Given an input of the size
  71. :math:`(N, C, IH, IW, window_h, window_w)` and :attr:`output_size`, the
  72. output shape would be :math:`(N, C, output\_size_{h}, output\_size_{w})` and the
  73. arguments must satisfy
  74. .. math::
  75. \text{IH} = \lfloor \frac{\text{output_size}_{h} + 2 * \text{padding}_{h} -
  76. \text{dilation}_{h} * (\text{kernel_size}_{h} - 1) - 1}{\text{stride}_{h}} + 1 \rfloor
  77. .. math::
  78. \text{IW} = \lfloor \frac{\text{output_size}_{w} + 2 * \text{padding}_{w} -
  79. \text{dilation}_{w} * (\text{kernel_size}_{w} - 1) - 1}{\text{stride}_{w}} + 1 \rfloor
  80. For each output location, we have:
  81. .. math::
  82. \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}
  83. .. math::
  84. \text{location}(n, c, ih, iw, wh, ww) &= (n, c, oh+wh, ow+ww) \\
  85. \text{where } & oh=-pad_h+ih \times stride_h + (wh-1) \times (dilation_h-1) \\
  86. & ow=-pad_w+iw \times stride_w + (ww-1) \times (dilation_w-1)
  87. Args:
  88. output_size: the size of the output tensor.
  89. kernel_size: the size of the window to take a max over.
  90. padding: implicit zero padding to be added on both sides. Default: 0
  91. stride: the stride of the window. Default: 1
  92. dilation: the dilation of the window. Default: 1
  93. """
  94. def __init__(
  95. self,
  96. output_size: Union[int, Tuple[int, int]],
  97. kernel_size: Union[int, Tuple[int, int]],
  98. padding: Union[int, Tuple[int, int]] = 0,
  99. stride: Union[int, Tuple[int, int]] = 1,
  100. dilation: Union[int, Tuple[int, int]] = 1,
  101. **kwargs
  102. ):
  103. super(SlidingWindowTranspose, self).__init__(**kwargs)
  104. self.output_size = output_size
  105. self.kernel_size = kernel_size
  106. self.padding = padding
  107. self.stride = stride
  108. self.dilation = dilation
  109. def forward(self, inp):
  110. return sliding_window_transpose(
  111. inp,
  112. self.output_size,
  113. self.kernel_size,
  114. self.padding,
  115. self.stride,
  116. self.dilation,
  117. )