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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. >>> import numpy as np
  29. >>> inp = Tensor(np.arange(30).reshape(1,1,5,6))
  30. >>> op = M.SlidingWindow(kernel_size=3, padding=1, stride=2, dilation=2)
  31. >>> out = op(inp)
  32. >>> print(out.numpy())
  33. [[[[[[ 0 0 0]
  34. [ 0 7 9]
  35. [ 0 19 21]]
  36. <BLANKLINE>
  37. [[ 0 0 0]
  38. [ 7 9 11]
  39. [19 21 23]]]
  40. <BLANKLINE>
  41. <BLANKLINE>
  42. [[[ 0 7 9]
  43. [ 0 19 21]
  44. [ 0 0 0]]
  45. <BLANKLINE>
  46. [[ 7 9 11]
  47. [19 21 23]
  48. [ 0 0 0]]]]]]
  49. """
  50. def __init__(
  51. self,
  52. kernel_size: Union[int, Tuple[int, int]],
  53. padding: Union[int, Tuple[int, int]] = 0,
  54. stride: Union[int, Tuple[int, int]] = 1,
  55. dilation: Union[int, Tuple[int, int]] = 1,
  56. **kwargs
  57. ):
  58. super(SlidingWindow, self).__init__(**kwargs)
  59. self.kernel_size = kernel_size
  60. self.padding = padding
  61. self.stride = stride
  62. self.dilation = dilation
  63. def forward(self, inp):
  64. return sliding_window(
  65. inp, self.kernel_size, self.padding, self.stride, self.dilation
  66. )
  67. class SlidingWindowTranspose(Module):
  68. r"""Opposite opration of SlidingWindow, sum over the sliding windows on the
  69. corresponding input location. Given an input of the size
  70. :math:`(N, C, IH, IW, window_h, window_w)` and :attr:`output_size`, the
  71. output shape would be :math:`(N, C, output\_size_{h}, output\_size_{w})` and the
  72. arguments must satisfy
  73. .. math::
  74. \text{IH} = \lfloor \frac{\text{output_size}_{h} + 2 * \text{padding}_{h} -
  75. \text{dilation}_{h} * (\text{kernel_size}_{h} - 1) - 1}{\text{stride}_{h}} + 1 \rfloor
  76. .. math::
  77. \text{IW} = \lfloor \frac{\text{output_size}_{w} + 2 * \text{padding}_{w} -
  78. \text{dilation}_{w} * (\text{kernel_size}_{w} - 1) - 1}{\text{stride}_{w}} + 1 \rfloor
  79. For each output location, we have:
  80. .. math::
  81. \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}
  82. .. math::
  83. \text{location}(n, c, ih, iw, wh, ww) &= (n, c, oh+wh, ow+ww) \\
  84. \text{where } & oh=-pad_h+ih \times stride_h + (wh-1) \times (dilation_h-1) \\
  85. & ow=-pad_w+iw \times stride_w + (ww-1) \times (dilation_w-1)
  86. Args:
  87. output_size: the size of the output tensor.
  88. kernel_size: the size of the window to take a max over.
  89. padding: implicit zero padding to be added on both sides. Default: 0
  90. stride: the stride of the window. Default: 1
  91. dilation: the dilation of the window. Default: 1
  92. """
  93. def __init__(
  94. self,
  95. output_size: Union[int, Tuple[int, int]],
  96. kernel_size: Union[int, Tuple[int, int]],
  97. padding: Union[int, Tuple[int, int]] = 0,
  98. stride: Union[int, Tuple[int, int]] = 1,
  99. dilation: Union[int, Tuple[int, int]] = 1,
  100. **kwargs
  101. ):
  102. super(SlidingWindowTranspose, self).__init__(**kwargs)
  103. self.output_size = output_size
  104. self.kernel_size = kernel_size
  105. self.padding = padding
  106. self.stride = stride
  107. self.dilation = dilation
  108. def forward(self, inp):
  109. return sliding_window_transpose(
  110. inp,
  111. self.output_size,
  112. self.kernel_size,
  113. self.padding,
  114. self.stride,
  115. self.dilation,
  116. )