-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unet.py
84 lines (62 loc) · 2.8 KB
/
Unet.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
import torch
import torch.nn as nn
import torchvision.transforms.functional as TF
# [(i-k+2p)/s] + 1
class Conv_op(nn.Module):
def __init__(self, input_channels, output_channels):
super(Conv_op, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels=input_channels, out_channels=output_channels,
kernel_size=(3, 3), padding=1, stride=(1, 1), bias=False),
nn.BatchNorm2d(output_channels),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=output_channels, out_channels=output_channels,
kernel_size=(3, 3), padding=1, stride=(1, 1), bias=False),
nn.BatchNorm2d(output_channels),
nn.ReLU(inplace=True)
)
def forward(self, x):
return self.conv(x)
# a = torch.rand(size=(32, 3, 100, 100))
# ob = Conv_op(input_channels=3, output_channels=10)
# b = ob(a)
# print(b.shape)
class UNET(nn.Module):
def __init__(self, input_channels=3, output_channels=1, features=[64, 128, 256, 512]):
super(UNET, self).__init__()
self.features = features
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.up_layers = nn.ModuleList()
self.down_layers = nn.ModuleList()
# Down scaling part of U-Net
for feature in features:
self.down_layers.append(Conv_op(input_channels=input_channels, output_channels=feature))
input_channels = feature
# Shape after Conv transpose: [(i-1)*s + k - 2p]
for feature in reversed(features):
self.up_layers.append(
nn.ConvTranspose2d(in_channels=feature*2, out_channels=feature, kernel_size=(2, 2), stride=(2, 2))
)
self.up_layers.append(Conv_op(input_channels=feature*2, output_channels=feature))
self.bottle_neck = Conv_op(features[-1], features[-1]*2)
self.conv_last = nn.Conv2d(in_channels=features[0], out_channels=output_channels, kernel_size=(1, 1))
def forward(self, x):
skip_connections = []
for down in self.down_layers:
x = down(x)
skip_connections.append(x)
x = self.pool(x)
x = self.bottle_neck(x)
skip_connections = skip_connections[::-1]
for i in range(0, len(self.up_layers), 2):
x = self.up_layers[i](x)
skip_connection = skip_connections[i//2]
if x.shape != skip_connection.shape:
x = TF.resize(x, size=skip_connection.shape[2:])
concat_skip = torch.cat((skip_connection, x), dim=1)
x = self.up_layers[i+1](concat_skip)
return self.conv_last(x)
# img = torch.rand((1, 3, 100, 100))
# model = UNET()
# a = model(img)
# print(a.shape)