1. # coding: utf-8
    2. """
    3. File Name: main_1
    4. Author: Ma
    5. Date: 2021/8/26
    6. """
    7. import torch
    8. import torch.nn as nn
    9. from torch.nn import functional as F
    10. class DoubleConv(nn.Module):
    11. """(convolution => [BN] => ReLU) * 2"""
    12. def __init__(self, in_channels, out_channels, mid_channels=None):
    13. super().__init__()
    14. if not mid_channels:
    15. mid_channels = out_channels
    16. self.double_conv = nn.Sequential(
    17. nn.Conv2d(in_channels, mid_channels, kernel_size=3, padding=1),
    18. nn.BatchNorm2d(mid_channels),
    19. nn.ReLU(inplace=True),
    20. nn.Conv2d(mid_channels, out_channels, kernel_size=3, padding=1),
    21. nn.BatchNorm2d(out_channels),
    22. nn.ReLU(inplace=True)
    23. )
    24. def forward(self, x):
    25. return self.double_conv(x)
    26. class Down(nn.Module):
    27. """Downscaling with maxpool then double conv"""
    28. def __init__(self, in_channels, out_channels):
    29. super().__init__()
    30. self.maxpool_conv = nn.Sequential(
    31. nn.MaxPool2d(2),
    32. DoubleConv(in_channels, out_channels)
    33. )
    34. def forward(self, x):
    35. return self.maxpool_conv(x)
    36. class Up(nn.Module):
    37. """Upscaling then double conv"""
    38. def __init__(self, in_channels, out_channels, bilinear=True):
    39. super().__init__()
    40. # if bilinear, use the normal convolutions to reduce the number of channels
    41. if bilinear:
    42. self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
    43. self.conv = DoubleConv(in_channels, out_channels, in_channels // 2)
    44. else:
    45. self.up = nn.ConvTranspose2d(in_channels, in_channels // 2, kernel_size=2, stride=2)
    46. self.conv = DoubleConv(in_channels, out_channels)
    47. def forward(self, x1, x2):
    48. x1 = self.up(x1)
    49. # input is CHW
    50. diffy = x2.size()[2] - x1.size()[2]
    51. diffx = x2.size()[3] - x1.size()[3]
    52. x1 = F.pad(x1, [diffx // 2, diffx - diffx // 2,
    53. diffy // 2, diffy - diffy // 2])
    54. # if you have padding issues, see
    55. # https://github.com/HaiyongJiang/U-Net-Pytorch-Unstructured-Buggy/commit/0e854509c2cea854e247a9c615f175f76fbb2e3a
    56. # https://github.com/xiaopeng-liao/Pytorch-UNet/commit/8ebac70e633bac59fc22bb5195e513d5832fb3bd
    57. x = torch.cat([x2, x1], dim=1)
    58. return self.conv(x)
    59. class OutConv(nn.Module):
    60. def __init__(self, in_channels, out_channels):
    61. super(OutConv, self).__init__()
    62. self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1)
    63. def forward(self, x):
    64. return self.conv(x)
    65. class UNet(nn.Module):
    66. def __init__(self, n_channels, n_classes, bilinear=True):
    67. super(UNet, self).__init__()
    68. self.n_channels = n_channels
    69. self.n_classes = n_classes
    70. self.bilinear = bilinear
    71. self.inc = DoubleConv(n_channels, 64)
    72. self.down1 = Down(64, 128)
    73. self.down2 = Down(128, 256)
    74. self.down3 = Down(256, 512)
    75. factor = 2 if bilinear else 1
    76. self.down4 = Down(512, 1024 // factor)
    77. self.up1 = Up(1024, 512 // factor, bilinear)
    78. self.up2 = Up(512, 256 // factor, bilinear)
    79. self.up3 = Up(256, 128 // factor, bilinear)
    80. self.up4 = Up(128, 64, bilinear)
    81. self.outc = OutConv(64, n_classes)
    82. def forward(self, x):
    83. x1 = self.inc(x)
    84. x2 = self.down1(x1)
    85. x3 = self.down2(x2)
    86. x4 = self.down3(x3)
    87. x5 = self.down4(x4)
    88. x = self.up1(x5, x4)
    89. x = self.up2(x, x3)
    90. x = self.up3(x, x2)
    91. x = self.up4(x, x1)
    92. logits = self.outc(x)
    93. return logits
    94. if __name__ == "__main__":
    95. image = torch.randn(1, 1, 256, 128)
    96. model = UNet(1, 4)
    97. pred = model(image)
    98. print(pred.shape)