[748954]: / model.py

Download this file

155 lines (139 with data), 4.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
Code adapted from https://medium.com/mlearning-ai/semantic-segmentation-with-pytorch-u-net-from-scratch-502d6565910a
"""
import torch
import torch.nn as nn
import torchvision.transforms.functional as TF
class CNNBlock(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int = 3,
stride: int = 1,
padding: int = 0
):
super().__init__()
self.block = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
def forward(self, img):
return self.block(img)
class CNNBlocks(nn.Module):
def __init__(
self,
in_channels: int, # the first in_channels
out_channels: int,
num_layers: int = 2, # number of CNN blocks
kernel_size: int = 3,
stride: int = 1,
padding: int = 0
):
super().__init__()
self.blocks = nn.ModuleList()
for _ in range(num_layers):
self.blocks.append(CNNBlock(in_channels, out_channels, kernel_size, stride, padding))
in_channels = out_channels
def forward(self, img):
for block in self.blocks:
img = block(img)
return img
class Encoder(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
downhills: int = 4, # number of (CNNBlocks + MaxPool2d)
num_layers: int = 2, # number of CNN blocks per CNNBlocks
kernel_size: int = 3,
stride: int = 1,
padding: int = 0
):
super().__init__()
self.down_blocks = nn.ModuleList()
for _ in range(downhills):
self.down_blocks.extend(
[
CNNBlocks(in_channels, out_channels, num_layers, kernel_size, stride, padding),
nn.MaxPool2d(kernel_size=2) # stride default to 2
]
)
in_channels = out_channels
out_channels *= 2
self.bottom_block = CNNBlocks(in_channels, out_channels, num_layers, kernel_size, stride, padding)
def forward(self, img):
skip_connections = []
for module in self.down_blocks:
img = module(img)
if isinstance(module, CNNBlocks):
skip_connections.append(img)
return self.bottom_block(img), skip_connections
class Decoder(nn.Module):
def __init__(
self,
in_channels: int,
n_classes: int, # final out_channels
uphills: int = 4, # number of (ConvTranpose2D + CNNBlocks)
num_layers: int = 2, # number of CNN blocks per CNNBlocks
kernel_size: int = 3,
stride: int = 1,
padding: int = 0
):
super().__init__()
self.up_blocks = nn.ModuleList()
for _ in range(uphills):
out_channels = in_channels // 2
self.up_blocks.extend(
[
nn.ConvTranspose2d(in_channels, out_channels, kernel_size=2, stride=2),
CNNBlocks(in_channels, out_channels, num_layers, kernel_size, stride, padding)
]
)
in_channels //= 2
self.segmentation_layer = nn.Conv2d(in_channels, out_channels=n_classes, kernel_size=1, padding=padding)
def forward(self, img, skip_connections: list):
for module in self.up_blocks:
if isinstance(module, CNNBlocks):
connection = skip_connections.pop(-1)
cropped_connection = TF.center_crop(connection, img.shape[2])
img = torch.cat([cropped_connection, img], dim=1)
img = module(img)
return self.segmentation_layer(img)
class UNet(nn.Module):
def __init__(
self,
n_classes: int,
first_in_channels: int = 1,
first_out_channels: int = 64,
downhills: int = 4,
uphills: int = 4,
num_layers: int = 2,
kernel_size: int = 3,
stride: int = 1,
padding: int = 0
):
super().__init__()
self.encoder = Encoder(
in_channels=first_in_channels,
out_channels=first_out_channels,
downhills=downhills,
num_layers=num_layers,
kernel_size=kernel_size,
stride=stride,
padding=padding
)
self.decoder = Decoder(
in_channels=first_out_channels * 2 ** downhills,
n_classes=n_classes,
uphills=uphills,
num_layers=num_layers,
kernel_size=kernel_size,
stride=stride,
padding=padding
)
def forward(self, img):
img, skip_connections = self.encoder(img)
segmentations = self.decoder(img, skip_connections)
return segmentations