|
| 1 | +# Copyright (c) 2022 Carl Zeiss AG – All Rights Reserved. |
| 2 | +# ZEISS, ZEISS.com are registered trademarks of Carl Zeiss AG |
| 3 | + |
| 4 | +import torch |
| 5 | +import torch.nn as nn |
| 6 | +import torch.nn.functional as F |
| 7 | + |
| 8 | +__all__ = ['UNet'] |
| 9 | + |
| 10 | +class UNet(nn.Module): |
| 11 | + def __init__( |
| 12 | + self, num_channels, num_classes, depth=4, initial_filter_count=64, bilinear=True |
| 13 | + ): |
| 14 | + super(UNet, self).__init__() |
| 15 | + |
| 16 | + self.num_channels = num_channels |
| 17 | + self.num_classes = num_classes |
| 18 | + self.depth = depth |
| 19 | + self.initial_filter_count = initial_filter_count |
| 20 | + self.bilinear = bilinear |
| 21 | + |
| 22 | + factor = 2 if bilinear else 1 |
| 23 | + |
| 24 | + filter_count = initial_filter_count |
| 25 | + |
| 26 | + encoder_blocks = [] |
| 27 | + encoder_blocks.append(DoubleConv(num_channels, filter_count)) |
| 28 | + for d in range(depth): |
| 29 | + if d < depth - 1: |
| 30 | + encoder_blocks.append(Down(filter_count, 2 * filter_count)) |
| 31 | + else: |
| 32 | + encoder_blocks.append(Down(filter_count, (2 * filter_count) // factor)) |
| 33 | + filter_count *= 2 |
| 34 | + self.encoder_blocks = nn.Sequential(*encoder_blocks) |
| 35 | + |
| 36 | + decoder_blocks = [] |
| 37 | + for d in range(depth): |
| 38 | + if d < depth - 1: |
| 39 | + decoder_blocks.append( |
| 40 | + Up(filter_count, filter_count // 2 // factor, bilinear) |
| 41 | + ) |
| 42 | + else: |
| 43 | + decoder_blocks.append(Up(filter_count, filter_count // 2, bilinear)) |
| 44 | + filter_count //= 2 |
| 45 | + self.decoder_blocks = nn.Sequential(*decoder_blocks) |
| 46 | + |
| 47 | + self.outc = OutputConvolution(filter_count, num_classes) |
| 48 | + |
| 49 | + def forward(self, x): |
| 50 | + xs = [] |
| 51 | + for encoder_block in self.encoder_blocks: |
| 52 | + x = encoder_block(x) |
| 53 | + xs.append(x) |
| 54 | + |
| 55 | + xs.reverse() |
| 56 | + xs = xs[1:] |
| 57 | + |
| 58 | + for decoder_block, x_skip in zip(self.decoder_blocks, xs): |
| 59 | + x = decoder_block(x, x_skip) |
| 60 | + |
| 61 | + logits = self.outc(x) |
| 62 | + |
| 63 | + return logits |
| 64 | + |
| 65 | + |
| 66 | +class DoubleConv(nn.Module): |
| 67 | + """Module combining Conv -> BN -> ReLU -> Conv -> BN -> ReLU.""" |
| 68 | + |
| 69 | + def __init__( |
| 70 | + self, num_input_channels, num_output_channels, num_middle_channels=None |
| 71 | + ): |
| 72 | + super().__init__() |
| 73 | + |
| 74 | + if not num_middle_channels: |
| 75 | + num_middle_channels = num_output_channels |
| 76 | + |
| 77 | + self.double_conv = nn.Sequential( |
| 78 | + nn.Conv2d( |
| 79 | + num_input_channels, |
| 80 | + num_middle_channels, |
| 81 | + kernel_size=3, |
| 82 | + padding=1, |
| 83 | + bias=False, |
| 84 | + ), |
| 85 | + nn.BatchNorm2d(num_middle_channels), |
| 86 | + nn.ReLU(inplace=True), |
| 87 | + nn.Conv2d( |
| 88 | + num_middle_channels, |
| 89 | + num_output_channels, |
| 90 | + kernel_size=3, |
| 91 | + padding=1, |
| 92 | + bias=False, |
| 93 | + ), |
| 94 | + nn.BatchNorm2d(num_output_channels), |
| 95 | + nn.ReLU(inplace=True), |
| 96 | + ) |
| 97 | + |
| 98 | + def forward(self, x): |
| 99 | + return self.double_conv(x) |
| 100 | + |
| 101 | + |
| 102 | +class Down(nn.Module): |
| 103 | + """Module combining downscaling and DoubleConvolution.""" |
| 104 | + |
| 105 | + def __init__(self, num_input_channels, num_output_channels): |
| 106 | + super().__init__() |
| 107 | + |
| 108 | + self.maxpool_conv = nn.Sequential( |
| 109 | + nn.MaxPool2d(2), DoubleConv(num_input_channels, num_output_channels) |
| 110 | + ) |
| 111 | + |
| 112 | + def forward(self, x): |
| 113 | + return self.maxpool_conv(x) |
| 114 | + |
| 115 | + |
| 116 | +class Up(nn.Module): |
| 117 | + """Module combining upscaling and DoubleConvolution.""" |
| 118 | + |
| 119 | + def __init__(self, num_input_channels, num_output_channels, bilinear=True): |
| 120 | + super().__init__() |
| 121 | + |
| 122 | + # if bilinear, use the normal convolutions to reduce the number of channels |
| 123 | + if bilinear: |
| 124 | + self.up = nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True) |
| 125 | + self.conv = DoubleConv( |
| 126 | + num_input_channels, num_output_channels, num_input_channels // 2 |
| 127 | + ) |
| 128 | + else: |
| 129 | + self.up = nn.ConvTranspose2d( |
| 130 | + num_input_channels, num_input_channels // 2, kernel_size=2, stride=2 |
| 131 | + ) |
| 132 | + self.conv = DoubleConv(num_input_channels, num_output_channels) |
| 133 | + |
| 134 | + def forward(self, x1, x2): |
| 135 | + x1 = self.up(x1) |
| 136 | + x = torch.cat([x2, x1], dim=1) |
| 137 | + |
| 138 | + return self.conv(x) |
| 139 | + |
| 140 | + |
| 141 | +class OutputConvolution(nn.Module): |
| 142 | + def __init__(self, num_input_channels, num_output_channels): |
| 143 | + super(OutputConvolution, self).__init__() |
| 144 | + |
| 145 | + self.conv = nn.Conv2d(num_input_channels, num_output_channels, kernel_size=1) |
| 146 | + |
| 147 | + def forward(self, x): |
| 148 | + return self.conv(x) |
0 commit comments