model_s.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import torch
  2. from torch import nn
  3. import torch.nn.functional as F
  4. from torchinfo import summary
  5. class ResidualBlock(nn.Module):
  6. def __init__(self, channels):
  7. super(ResidualBlock, self).__init__()
  8. self.conv1 = nn.Conv2d(channels, channels, kernel_size=3, padding=1, bias=False)
  9. self.bn1 = nn.BatchNorm2d(channels)
  10. self.relu6_1 = nn.ReLU6(inplace=True)
  11. self.conv2 = nn.Conv2d(channels, channels, kernel_size=3, padding=1, bias=False)
  12. self.bn2 = nn.BatchNorm2d(channels)
  13. self.relu6_2 = nn.ReLU6(inplace=True)
  14. self.relu6_latest = nn.ReLU6(inplace=True)
  15. def forward(self, x):
  16. residual = self.conv1(x)
  17. residual = self.bn1(residual)
  18. residual = self.relu6_1(residual)
  19. residual = self.conv2(residual)
  20. residual = self.bn2(residual)
  21. residual = self.relu6_2(residual)
  22. add = x + residual
  23. return self.relu6_latest(add)
  24. class M64ColorNet(nn.Module):
  25. def __init__(self):
  26. super(M64ColorNet, self).__init__()
  27. self.block1 = nn.Sequential(
  28. nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, padding=1, bias=False, stride=1),
  29. nn.BatchNorm2d(16),
  30. nn.ReLU6(inplace=True)
  31. )
  32. self.block2 = nn.Sequential(
  33. nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=1, bias=False, stride=2),
  34. nn.BatchNorm2d(32),
  35. nn.ReLU6(inplace=True)
  36. )
  37. self.block3 = nn.Sequential(
  38. nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, padding=1, bias=False, stride=2),
  39. nn.BatchNorm2d(32),
  40. nn.ReLU6(inplace=True)
  41. )
  42. self.block4 = ResidualBlock(32)
  43. # self.block5 = ResidualBlock(32)
  44. # self.block6 = nn.Sequential(
  45. # nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1, bias=False),
  46. # nn.BatchNorm2d(128), nn.ReLU6(inplace=True))
  47. # self.block6 = ResidualBlock(64)
  48. # self.block6_1 = nn.Sequential(
  49. # nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1, bias=False, stride=2),
  50. # nn.BatchNorm2d(128),
  51. # nn.ReLU6(inplace=True)
  52. # )
  53. # self.block6_2 = ResidualBlock(128)
  54. # self.block6_3 = ResidualBlock(128)
  55. # self.block6_3 = nn.Sequential(
  56. # nn.Conv2d(in_channels=128, out_channels=64, kernel_size=3, padding=1, bias=False),
  57. # nn.BatchNorm2d(64),
  58. # nn.ReLU6(inplace=True))
  59. # self.block6_4 = nn.Sequential(
  60. # # nn.Conv2d(in_channels=64, out_channels=32, kernel_size=3, padding=1, bias=False),
  61. # nn.ConvTranspose2d(in_channels=128, out_channels=64, kernel_size=3, padding=1, bias=False, stride=2,output_padding=1),
  62. # nn.BatchNorm2d(64),
  63. # nn.ReLU6(inplace=True))
  64. # self.block6_3 = ResidualBlock(64)
  65. # self.block6_5 = ResidualBlock(64)
  66. self.block7 = ResidualBlock(32)
  67. self.block8 = ResidualBlock(32)
  68. # self.block9 = nn.Sequential(
  69. # nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1, bias=False),
  70. # nn.BatchNorm2d(64),
  71. # nn.ReLU6(inplace=True))
  72. self.block10 = nn.Sequential(
  73. # nn.Conv2d(in_channels=64, out_channels=32, kernel_size=3, padding=1, bias=False),
  74. nn.ConvTranspose2d(in_channels=32, out_channels=32, kernel_size=3, padding=1, bias=False, stride=2, output_padding=1),
  75. nn.BatchNorm2d(32),
  76. nn.ReLU6(inplace=True)
  77. )
  78. self.block11 = nn.Sequential(
  79. # nn.Conv2d(in_channels=32, out_channels=16, kernel_size=3, padding=1, bias=False),
  80. nn.ConvTranspose2d(in_channels=32, out_channels=16, kernel_size=3, padding=1, bias=False, stride=2, output_padding=1),
  81. nn.BatchNorm2d(16),
  82. nn.ReLU6(inplace=True)
  83. )
  84. self.block12 = nn.Sequential(
  85. nn.Conv2d(in_channels=16, out_channels=3, kernel_size=3, padding=1, bias=False),
  86. nn.BatchNorm2d(3),
  87. nn.ReLU6(inplace=True)
  88. )
  89. # self.dropout = nn.Dropout(0.4)
  90. def forward(self, x):
  91. input = x
  92. x = self.block1(x)
  93. input2 = x
  94. x = self.block2(x)
  95. input3 = x
  96. x = self.block3(x)
  97. input4 = x
  98. x = self.block4(x)
  99. # x = self.block5(x)
  100. # x = self.block6(x)
  101. # x = self.block6_1(x)
  102. # input4_1 = x
  103. # x = self.block6_2(x)
  104. # x = self.block6_3(x)
  105. # x = input4_1 + x
  106. # x = self.block6_4(x)
  107. # x = self.block6_5(x)
  108. x = self.block7(x)
  109. x = self.block8(x)
  110. x = input4 + x
  111. # x = self.block9(x)
  112. x = self.block10(x)
  113. x = input3 + x
  114. x = self.block11(x)
  115. x = input2 + x
  116. x = self.block12(x)
  117. x = input + x
  118. x = torch.sigmoid(x)
  119. return x
  120. @staticmethod
  121. def load_trained_model(ckpt_path):
  122. ckpt_dict = torch.load(ckpt_path, map_location=torch.device('cpu'))
  123. model = M64ColorNet()
  124. model.load_state_dict(ckpt_dict["model_state_dict"])
  125. model.eval()
  126. return model, ckpt_dict["mean"], ckpt_dict["std"], ckpt_dict["loss"], ckpt_dict["ssim_score"], ckpt_dict["psnr_score"]
  127. if __name__ == "__main__":
  128. model = M64ColorNet()
  129. summary(model, input_size=(16, 3, 256, 256))