pixelshuffle.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import torch.nn.functional as F
  2. from torch import Tensor
  3. from .module import Module
  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. """
  41. Runs the forward pass.
  42. """
  43. return F.pixel_shuffle(input, self.upscale_factor)
  44. def extra_repr(self) -> str:
  45. """
  46. Return the extra representation of the module.
  47. """
  48. return f"upscale_factor={self.upscale_factor}"
  49. class PixelUnshuffle(Module):
  50. r"""Reverse the PixelShuffle operation.
  51. Reverses the :class:`~torch.nn.PixelShuffle` operation by rearranging elements
  52. in a tensor of shape :math:`(*, C, H \times r, W \times r)` to a tensor of shape
  53. :math:`(*, C \times r^2, H, W)`, where r is a downscale factor.
  54. See the paper:
  55. `Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network`_
  56. by Shi et al. (2016) for more details.
  57. Args:
  58. downscale_factor (int): factor to decrease spatial resolution by
  59. Shape:
  60. - Input: :math:`(*, C_{in}, H_{in}, W_{in})`, where * is zero or more batch dimensions
  61. - Output: :math:`(*, C_{out}, H_{out}, W_{out})`, where
  62. .. math::
  63. C_{out} = C_{in} \times \text{downscale\_factor}^2
  64. .. math::
  65. H_{out} = H_{in} \div \text{downscale\_factor}
  66. .. math::
  67. W_{out} = W_{in} \div \text{downscale\_factor}
  68. Examples::
  69. >>> pixel_unshuffle = nn.PixelUnshuffle(3)
  70. >>> input = torch.randn(1, 1, 12, 12)
  71. >>> output = pixel_unshuffle(input)
  72. >>> print(output.size())
  73. torch.Size([1, 9, 4, 4])
  74. .. _Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network:
  75. https://arxiv.org/abs/1609.05158
  76. """
  77. __constants__ = ["downscale_factor"]
  78. downscale_factor: int
  79. def __init__(self, downscale_factor: int) -> None:
  80. super().__init__()
  81. self.downscale_factor = downscale_factor
  82. def forward(self, input: Tensor) -> Tensor:
  83. """
  84. Runs the forward pass.
  85. """
  86. return F.pixel_unshuffle(input, self.downscale_factor)
  87. def extra_repr(self) -> str:
  88. """
  89. Return the extra representation of the module.
  90. """
  91. return f"downscale_factor={self.downscale_factor}"