pixelshuffle.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from .module import Module
  2. from .. import functional as F
  3. from torch import Tensor
  4. __all__ = ['PixelShuffle', 'PixelUnshuffle']
  5. class PixelShuffle(Module):
  6. r"""Rearrange elements in a tensor according to an upscaling factor.
  7. Rearranges elements in a tensor of shape :math:`(*, C \times r^2, H, W)`
  8. to a tensor of shape :math:`(*, C, H \times r, W \times r)`, where r is an upscale factor.
  9. This is useful for implementing efficient sub-pixel convolution
  10. with a stride of :math:`1/r`.
  11. See the paper:
  12. `Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network`_
  13. by Shi et al. (2016) for more details.
  14. Args:
  15. upscale_factor (int): factor to increase spatial resolution by
  16. Shape:
  17. - Input: :math:`(*, C_{in}, H_{in}, W_{in})`, where * is zero or more batch dimensions
  18. - Output: :math:`(*, C_{out}, H_{out}, W_{out})`, where
  19. .. math::
  20. C_{out} = C_{in} \div \text{upscale\_factor}^2
  21. .. math::
  22. H_{out} = H_{in} \times \text{upscale\_factor}
  23. .. math::
  24. W_{out} = W_{in} \times \text{upscale\_factor}
  25. Examples::
  26. >>> pixel_shuffle = nn.PixelShuffle(3)
  27. >>> input = torch.randn(1, 9, 4, 4)
  28. >>> output = pixel_shuffle(input)
  29. >>> print(output.size())
  30. torch.Size([1, 1, 12, 12])
  31. .. _Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network:
  32. https://arxiv.org/abs/1609.05158
  33. """
  34. __constants__ = ['upscale_factor']
  35. upscale_factor: int
  36. def __init__(self, upscale_factor: int) -> None:
  37. super().__init__()
  38. self.upscale_factor = upscale_factor
  39. def forward(self, input: Tensor) -> Tensor:
  40. return F.pixel_shuffle(input, self.upscale_factor)
  41. def extra_repr(self) -> str:
  42. return f'upscale_factor={self.upscale_factor}'
  43. class PixelUnshuffle(Module):
  44. r"""Reverse the PixelShuffle operation.
  45. Reverses the :class:`~torch.nn.PixelShuffle` operation by rearranging elements
  46. in a tensor of shape :math:`(*, C, H \times r, W \times r)` to a tensor of shape
  47. :math:`(*, C \times r^2, H, W)`, where r is a downscale factor.
  48. See the paper:
  49. `Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network`_
  50. by Shi et al. (2016) for more details.
  51. Args:
  52. downscale_factor (int): factor to decrease spatial resolution by
  53. Shape:
  54. - Input: :math:`(*, C_{in}, H_{in}, W_{in})`, where * is zero or more batch dimensions
  55. - Output: :math:`(*, C_{out}, H_{out}, W_{out})`, where
  56. .. math::
  57. C_{out} = C_{in} \times \text{downscale\_factor}^2
  58. .. math::
  59. H_{out} = H_{in} \div \text{downscale\_factor}
  60. .. math::
  61. W_{out} = W_{in} \div \text{downscale\_factor}
  62. Examples::
  63. >>> pixel_unshuffle = nn.PixelUnshuffle(3)
  64. >>> input = torch.randn(1, 1, 12, 12)
  65. >>> output = pixel_unshuffle(input)
  66. >>> print(output.size())
  67. torch.Size([1, 9, 4, 4])
  68. .. _Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network:
  69. https://arxiv.org/abs/1609.05158
  70. """
  71. __constants__ = ['downscale_factor']
  72. downscale_factor: int
  73. def __init__(self, downscale_factor: int) -> None:
  74. super().__init__()
  75. self.downscale_factor = downscale_factor
  76. def forward(self, input: Tensor) -> Tensor:
  77. return F.pixel_unshuffle(input, self.downscale_factor)
  78. def extra_repr(self) -> str:
  79. return f'downscale_factor={self.downscale_factor}'