discriminator_arch.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from basicsr.utils.registry import ARCH_REGISTRY
  2. from torch import nn as nn
  3. from torch.nn import functional as F
  4. from torch.nn.utils import spectral_norm
  5. @ARCH_REGISTRY.register()
  6. class UNetDiscriminatorSN(nn.Module):
  7. """Defines a U-Net discriminator with spectral normalization (SN)
  8. It is used in Real-ESRGAN: Training Real-World Blind Super-Resolution with Pure Synthetic Data.
  9. Arg:
  10. num_in_ch (int): Channel number of inputs. Default: 3.
  11. num_feat (int): Channel number of base intermediate features. Default: 64.
  12. skip_connection (bool): Whether to use skip connections between U-Net. Default: True.
  13. """
  14. def __init__(self, num_in_ch, num_feat=64, skip_connection=True):
  15. super(UNetDiscriminatorSN, self).__init__()
  16. self.skip_connection = skip_connection
  17. norm = spectral_norm
  18. # the first convolution
  19. self.conv0 = nn.Conv2d(num_in_ch, num_feat, kernel_size=3, stride=1, padding=1)
  20. # downsample
  21. self.conv1 = norm(nn.Conv2d(num_feat, num_feat * 2, 4, 2, 1, bias=False))
  22. self.conv2 = norm(nn.Conv2d(num_feat * 2, num_feat * 4, 4, 2, 1, bias=False))
  23. self.conv3 = norm(nn.Conv2d(num_feat * 4, num_feat * 8, 4, 2, 1, bias=False))
  24. # upsample
  25. self.conv4 = norm(nn.Conv2d(num_feat * 8, num_feat * 4, 3, 1, 1, bias=False))
  26. self.conv5 = norm(nn.Conv2d(num_feat * 4, num_feat * 2, 3, 1, 1, bias=False))
  27. self.conv6 = norm(nn.Conv2d(num_feat * 2, num_feat, 3, 1, 1, bias=False))
  28. # extra convolutions
  29. self.conv7 = norm(nn.Conv2d(num_feat, num_feat, 3, 1, 1, bias=False))
  30. self.conv8 = norm(nn.Conv2d(num_feat, num_feat, 3, 1, 1, bias=False))
  31. self.conv9 = nn.Conv2d(num_feat, 1, 3, 1, 1)
  32. def forward(self, x):
  33. # downsample
  34. x0 = F.leaky_relu(self.conv0(x), negative_slope=0.2, inplace=True)
  35. x1 = F.leaky_relu(self.conv1(x0), negative_slope=0.2, inplace=True)
  36. x2 = F.leaky_relu(self.conv2(x1), negative_slope=0.2, inplace=True)
  37. x3 = F.leaky_relu(self.conv3(x2), negative_slope=0.2, inplace=True)
  38. # upsample
  39. x3 = F.interpolate(x3, scale_factor=2, mode='bilinear', align_corners=False)
  40. x4 = F.leaky_relu(self.conv4(x3), negative_slope=0.2, inplace=True)
  41. if self.skip_connection:
  42. x4 = x4 + x2
  43. x4 = F.interpolate(x4, scale_factor=2, mode='bilinear', align_corners=False)
  44. x5 = F.leaky_relu(self.conv5(x4), negative_slope=0.2, inplace=True)
  45. if self.skip_connection:
  46. x5 = x5 + x1
  47. x5 = F.interpolate(x5, scale_factor=2, mode='bilinear', align_corners=False)
  48. x6 = F.leaky_relu(self.conv6(x5), negative_slope=0.2, inplace=True)
  49. if self.skip_connection:
  50. x6 = x6 + x0
  51. # extra convolutions
  52. out = F.leaky_relu(self.conv7(x6), negative_slope=0.2, inplace=True)
  53. out = F.leaky_relu(self.conv8(out), negative_slope=0.2, inplace=True)
  54. out = self.conv9(out)
  55. return out