pixelshuffle.py 3.5 KB

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