• Unet网络结构搭建


    1. from typing import Dict
    2. import torch
    3. import torch.nn as nn
    4. class DoubleConv(nn.Sequential): #定义两个串联卷积模块
    5. def __init__(self, in_channels, out_channels, mid_channels=None):
    6. if mid_channels is None: #如果没有设置mid_channels,则mid_channels = out_channels
    7. mid_channels = out_channels
    8. super(DoubleConv, self).__init__(
    9. nn.Conv2d(in_channels, mid_channels, kernel_size=3, padding=1, bias=False),
    10. nn.BatchNorm2d(mid_channels),
    11. nn.ReLU(inplace=True),
    12. nn.Conv2d(mid_channels, out_channels, kernel_size=3, padding=1, bias=False),
    13. nn.BatchNorm2d(out_channels),
    14. nn.ReLU(inplace=True)
    15. )
    16. class Down(nn.Sequential): #定义下采样模块,最大池化+两个串联卷积模块
    17. def __init__(self, in_channels, out_channels):
    18. super(Down, self).__init__(
    19. nn.MaxPool2d(2, stride=2),
    20. DoubleConv(in_channels, out_channels)
    21. )
    22. class Up(nn.Module):
    23. def __init__(self, in_channels, out_channels, bilinear=True):
    24. super(Up, self).__init__()
    25. if bilinear: #双线性插值
    26. self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
    27. self.conv = DoubleConv(in_channels, out_channels, in_channels // 2)
    28. else: #转置卷积
    29. self.up = nn.ConvTranspose2d(in_channels, in_channels // 2, kernel_size=2, stride=2)
    30. self.conv = DoubleConv(in_channels, out_channels)
    31. # Up正向传播过程,先进行上采样,在进行拼接,拼接之后在经过DoubleConv
    32. def forward(self, x1: torch.Tensor, x2: torch.Tensor) -> torch.Tensor:
    33. x1 = self.up(x1) #此处只经过Upsample或ConvTranspose2d
    34. x = torch.cat([x2, x1], dim=1) #x2, x1进行拼接
    35. x = self.conv(x) #拼接之后在经过DoubleConv
    36. return x
    37. class OutConv(nn.Sequential):
    38. def __init__(self, in_channels, num_classes):
    39. super(OutConv, self).__init__(
    40. nn.Conv2d(in_channels, num_classes, kernel_size=1) #1*1卷积调整最后的通道数
    41. )
    42. class UNet(nn.Module):
    43. def __init__(self,
    44. in_channels: int = 3,
    45. num_classes: int = 2,
    46. bilinear: bool = True,
    47. base_c: int = 64):
    48. super(UNet, self).__init__()
    49. self.in_channels = in_channels
    50. self.num_classes = num_classes
    51. self.bilinear = bilinear
    52. self.in_conv = DoubleConv(in_channels, base_c)
    53. self.down1 = Down(base_c, base_c * 2)
    54. self.down2 = Down(base_c * 2, base_c * 4)
    55. self.down3 = Down(base_c * 4, base_c * 8)
    56. factor = 2 if bilinear else 1
    57. self.down4 = Down(base_c * 8, base_c * 16 // factor)
    58. self.up1 = Up(base_c * 16, base_c * 8 // factor, bilinear)
    59. self.up2 = Up(base_c * 8, base_c * 4 // factor, bilinear)
    60. self.up3 = Up(base_c * 4, base_c * 2 // factor, bilinear)
    61. self.up4 = Up(base_c * 2, base_c, bilinear)
    62. self.out_conv = OutConv(base_c, num_classes)
    63. def forward(self, x: torch.Tensor) -> Dict[str, torch.Tensor]:
    64. x1 = self.in_conv(x) # 480*480*3 -> 480*480*64 -> 480*480*64
    65. x2 = self.down1(x1) # 240*240*64 -> 240*240*128 -> 240*240*128
    66. x3 = self.down2(x2) # 120*120*128 -> 120*120*256 -> 120*120*256
    67. x4 = self.down3(x3) # 60*60*256 -> 60*60*512 -> 60*60*512
    68. x5 = self.down4(x4) # 30*30*512 -> 30*30*512 -> 30*30*512
    69. x = self.up1(x5, x4) # 60*60*512 -> 60*60*1024 -> 60*60*512 -> 60*60*256
    70. x = self.up2(x, x3) # 120*120*256 -> 120*120*512 -> 120*120*256 -> 120*120*128
    71. x = self.up3(x, x2) # 240*240*128 -> 240*240*256 -> 240*240*128 -> 240*240*64
    72. x = self.up4(x, x1) # 480*480*64 -> 480*480*128 -> 480*480*64 -> 480*480*64
    73. logits = self.out_conv(x) # 480*480*64-> 480*480*num_classes
    74. return {"out": logits}

    reference

    【使用Pytorch搭建U-Net网络并基于DRIVE数据集训练(语义分割)】 https://www.bilibili.com/video/BV1rq4y1w7xM?share_source=copy_web&vd_source=95705b32f23f70b32dfa1721628d5874

  • 相关阅读:
    Spring Boot源码分析一:启动流程
    【ESP 保姆级教程】疯狂Node.js服务器篇 ——本地部署MQTT服务器以及剖析MQTT过程
    解决docker容器: bash: ping: command not found, 并制作镜像
    python:list和dict的基本操作实例
    设计模式基础
    【gitee】本地建仓托管
    【华为OD机试python】返回矩阵中非1的元素个数【2023 B卷|200分】
    HashMap
    SQL server数据库单用户模式如何退出
    小程序框架->框架,视图层,生命周期(逻辑层)
  • 原文地址:https://blog.csdn.net/m0_56247038/article/details/126992011