rrdbnet_arch.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import torch
  2. from torch import nn as nn
  3. from torch.nn import functional as F
  4. from .arch_util import default_init_weights, make_layer, pixel_unshuffle
  5. class ResidualDenseBlock(nn.Module):
  6. """Residual Dense Block.
  7. Used in RRDB block in ESRGAN.
  8. Args:
  9. num_feat (int): Channel number of intermediate features.
  10. num_grow_ch (int): Channels for each growth.
  11. """
  12. def __init__(self, num_feat: int = 64, num_grow_ch: int = 32) -> None:
  13. super(ResidualDenseBlock, self).__init__()
  14. self.conv1 = nn.Conv2d(num_feat, num_grow_ch, 3, 1, 1)
  15. self.conv2 = nn.Conv2d(num_feat + num_grow_ch, num_grow_ch, 3, 1, 1)
  16. self.conv3 = nn.Conv2d(num_feat + 2 * num_grow_ch, num_grow_ch, 3, 1, 1)
  17. self.conv4 = nn.Conv2d(num_feat + 3 * num_grow_ch, num_grow_ch, 3, 1, 1)
  18. self.conv5 = nn.Conv2d(num_feat + 4 * num_grow_ch, num_feat, 3, 1, 1)
  19. self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
  20. # initialization
  21. default_init_weights(
  22. [self.conv1, self.conv2, self.conv3, self.conv4, self.conv5], 0.1
  23. )
  24. def forward(self, x: torch.Tensor) -> torch.Tensor:
  25. x1 = self.lrelu(self.conv1(x))
  26. x2 = self.lrelu(self.conv2(torch.cat((x, x1), 1)))
  27. x3 = self.lrelu(self.conv3(torch.cat((x, x1, x2), 1)))
  28. x4 = self.lrelu(self.conv4(torch.cat((x, x1, x2, x3), 1)))
  29. x5 = self.conv5(torch.cat((x, x1, x2, x3, x4), 1))
  30. # Empirically, we use 0.2 to scale the residual for better performance
  31. return x5 * 0.2 + x
  32. class RRDB(nn.Module):
  33. """Residual in Residual Dense Block.
  34. Used in RRDB-Net in ESRGAN.
  35. Args:
  36. num_feat (int): Channel number of intermediate features.
  37. num_grow_ch (int): Channels for each growth.
  38. """
  39. def __init__(self, num_feat: int, num_grow_ch: int = 32) -> None:
  40. super(RRDB, self).__init__()
  41. self.rdb1 = ResidualDenseBlock(num_feat, num_grow_ch)
  42. self.rdb2 = ResidualDenseBlock(num_feat, num_grow_ch)
  43. self.rdb3 = ResidualDenseBlock(num_feat, num_grow_ch)
  44. def forward(self, x: torch.Tensor) -> torch.Tensor:
  45. out = self.rdb1(x)
  46. out = self.rdb2(out)
  47. out = self.rdb3(out)
  48. # Empirically, we use 0.2 to scale the residual for better performance
  49. return out * 0.2 + x
  50. class RRDBNet(nn.Module):
  51. """Networks consisting of Residual in Residual Dense Block, which is used
  52. in ESRGAN.
  53. ESRGAN: Enhanced Super-Resolution Generative Adversarial Networks.
  54. We extend ESRGAN for scale x2 and scale x1.
  55. Note: This is one option for scale 1, scale 2 in RRDBNet.
  56. We first employ the pixel-unshuffle (an inverse operation of pixelshuffle to reduce the spatial size
  57. and enlarge the channel size before feeding inputs into the main ESRGAN architecture.
  58. Args:
  59. num_in_ch (int): Channel number of inputs.
  60. num_out_ch (int): Channel number of outputs.
  61. num_feat (int): Channel number of intermediate features.
  62. Default: 64
  63. num_block (int): Block number in the trunk network. Defaults: 23
  64. num_grow_ch (int): Channels for each growth. Default: 32.
  65. """
  66. def __init__(
  67. self,
  68. num_in_ch: int,
  69. num_out_ch: int,
  70. scale: int = 4,
  71. num_feat: int = 64,
  72. num_block: int = 23,
  73. num_grow_ch: int = 32,
  74. ) -> None:
  75. super(RRDBNet, self).__init__()
  76. self.scale = scale
  77. if scale == 2:
  78. num_in_ch = num_in_ch * 4
  79. elif scale == 1:
  80. num_in_ch = num_in_ch * 16
  81. self.conv_first = nn.Conv2d(num_in_ch, num_feat, 3, 1, 1)
  82. self.body = make_layer(
  83. RRDB, num_block, num_feat=num_feat, num_grow_ch=num_grow_ch
  84. )
  85. self.conv_body = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
  86. # upsample
  87. self.conv_up1 = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
  88. self.conv_up2 = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
  89. self.conv_hr = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
  90. self.conv_last = nn.Conv2d(num_feat, num_out_ch, 3, 1, 1)
  91. self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
  92. def forward(self, x: torch.Tensor) -> torch.Tensor:
  93. if self.scale == 2:
  94. feat = pixel_unshuffle(x, scale=2)
  95. elif self.scale == 1:
  96. feat = pixel_unshuffle(x, scale=4)
  97. else:
  98. feat = x
  99. feat = self.conv_first(feat)
  100. body_feat = self.conv_body(self.body(feat))
  101. feat = feat + body_feat
  102. # upsample
  103. feat = self.lrelu(
  104. self.conv_up1(F.interpolate(feat, scale_factor=2, mode="nearest"))
  105. )
  106. feat = self.lrelu(
  107. self.conv_up2(F.interpolate(feat, scale_factor=2, mode="nearest"))
  108. )
  109. out = self.conv_last(self.lrelu(self.conv_hr(feat)))
  110. return out