-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
174 lines (137 loc) · 5.62 KB
/
model.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import torch
import torch.nn as nn
import torchvision.transforms.functional as tf
from torchsummary import summary
class DoubleConv(nn.Module):
def __init__(self, in_channels: int, out_channels: int,
kernel_size_1: int = 3, kernel_size_2: int = 3):
"""
Initialize the DoubleConv module.
Args:
in_channels (int): Number of input channels.
out_channels (int): Number of output channels.
kernel_size_1 (int, Optional): Size of first convolution kernel.
kernel_size_2 (int, Optional): Size of second convolution kernel.
"""
super(DoubleConv, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size_1, stride=1, padding=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=kernel_size_2, stride=1, padding=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
)
def forward(self, tensor: torch.Tensor) -> torch.Tensor:
"""
Forward pass of the DoubleConv module.
Args:
tensor (torch.Tensor): Input tensor.
Returns:
torch.Tensor: Output tensor.
"""
return self.conv(tensor)
class DownBlock(nn.Module):
"""Encoder block module."""
def __init__(self, in_channels: int, out_channels: int,
kernel_size_1: int = 3, kernel_size_2: int = 3):
"""
Initialize the Encoder module.
Args:
in_channels (int): Number of input channels.
out_channels (int): Number of output channels.
"""
super(DownBlock, self).__init__()
self.conv = DoubleConv(in_channels, out_channels, kernel_size_1, kernel_size_2)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
def forward(self, tensor: torch.Tensor) -> (torch.Tensor, torch.Tensor):
"""
Forward pass of the Encoder module.
Args:
tensor (torch.Tensor): Input tensor.
Returns:
(torch.Tensor, torch.Tensor): Tuple containing encoded tensor and skip connection tensor.
"""
tensor = self.conv(tensor)
skip = tensor
tensor = self.pool(tensor)
return tensor, skip
class UpBlock(nn.Module):
def __init__(self, in_channels: int, out_channels: int,
kernel_size_1: int = 3, kernel_size_2: int = 3):
"""
Initialize the Decoder module.
Args:
in_channels (int): Number of input channels.
out_channels (int): Number of output channels.
"""
super(UpBlock, self).__init__()
self.up = nn.ConvTranspose2d(in_channels, out_channels, kernel_size=2, stride=2)
self.conv = DoubleConv(in_channels, out_channels, kernel_size_1, kernel_size_2)
def forward(self, tensor: torch.Tensor, skip: torch.Tensor) -> torch.Tensor:
"""
Forward pass of the Decoder module.
Args:
tensor (torch.Tensor): Input tensor.
skip (torch.Tensor): Skip connection tensor.
Returns:
torch.Tensor: Output tensor.
"""
tensor = self.up(tensor)
if tensor.shape != skip.shape:
tensor = tf.resize(tensor, size=skip.shape[2:], antialias=False)
tensor = torch.cat((skip, tensor), dim=1)
tensor = self.conv(tensor)
return tensor
class UNET(nn.Module):
def __init__(self, in_channels: int = 3, out_channels: int = 1,
min_feature: int = 64, num_features: int = 4):
"""
Initialize the UNET model.
Args:
in_channels (int): Number of input channels.
out_channels (int): Number of output channels.
min_feature (int): Minimum number of features.
num_features (int): Number of scale levels.
"""
super(UNET, self).__init__()
features = self._calculate_features(min_feature, num_features)
self.downs = nn.ModuleList()
self.ups = nn.ModuleList()
self.bottleneck = DoubleConv(features[-1], 2 * features[-1])
self.final = nn.Conv2d(features[0], out_channels, kernel_size=1)
for feature in features:
self.downs.append(DownBlock(in_channels, feature))
in_channels = feature
for feature in reversed(features):
self.ups.append(UpBlock(2 * feature, feature))
@staticmethod
def _calculate_features(min_feature: int = 64, num_features: int = 4) -> list:
"""
Calculate the number of features at each scale level.
Args:
min_feature (int): Minimum number of features.
num_features (int): Number of scale levels.
Returns:
list: List containing the number of features at each scale level.
"""
return [min_feature * (2 ** idx) for idx in range(num_features)]
def forward(self, tensor: torch.Tensor) -> torch.Tensor:
"""
Forward pass of the UNET model.
Args:
tensor (torch.Tensor): Input tensor.
Returns:
torch.Tensor: Output tensor.
"""
skip_connections = []
for down in self.downs:
tensor, skip = down(tensor)
skip_connections.insert(0, skip)
tensor = self.bottleneck(tensor)
for up, skip_connection in zip(self.ups, skip_connections):
tensor = up(tensor, skip_connection)
return self.final(tensor)
if __name__ == "__main__":
model = UNET().to('cpu')
summary(model, (3, 112, 160), device='cpu')