-
Notifications
You must be signed in to change notification settings - Fork 1
/
unet_utils.py
85 lines (68 loc) · 2.6 KB
/
unet_utils.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
import torch
from torch import nn
class DoubleConvLayer(nn.Module):
"""
Implementation of the Double Convolutional Layer part of the U-NET Architecture.
It consists of two convolutional layers:
- First: in_channels -> out_channels with kernel size = 3
- Second: out_channels -> out_channels with kernel size = 3
Parameters
----------
in_channels : int
No of channels in the input
out_channels : int
No of channels in the output
"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.double_conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size = 3, padding = 1),
nn.ReLU(inplace = True),
nn.Conv2d(out_channels, out_channels, kernel_size = 3, padding = 1),
nn.ReLU(inplace = True),
)
def forward(self, x):
return self.double_conv(x)
class DownSampleLayer(nn.Module):
"""
Implementation of the Downsampling Layer part of the U-NET Architecture.
It consists of a convolutional layer with a max pool layer:
- First: in_channels -> out_channels with kernel size = 3
- Second: maxpool layer with kernel_size = 2 and stride = 2
Parameters
----------
in_channels : int
No of channels in the input
out_channels : int
No of channels in the output
"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.double_conv = DoubleConvLayer(in_channels, out_channels)
self.pooling = nn.MaxPool2d(kernel_size = 2, stride = 2)
def forward(self, x):
down = self.double_conv(x)
p = self.pooling(down)
return down, p
class UpSampleLayer(nn.Module):
"""
Implementation of the Upsampling Layer part of the U-NET Architecture.
It consists of a convolutional layer with a DoubleConvLayer:
- First: in_channels -> in_channels // 2 with kernel size = 2 and stride = 2
- Second: DoubleConvLayer with in_channels -> out_channels
Parameters
----------
in_channels : int
No of channels in the input
out_channels : int
No of channels in the output
"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.upsample = nn.ConvTranspose2d(in_channels, in_channels // 2, kernel_size = 2, stride = 2)
self.double_conv = DoubleConvLayer(in_channels, out_channels)
def forward(self, x1, x2):
x1 = self.upsample(x1)
x = torch.cat([x1, x2], 1) # Output from DownSampleLayer is concatenated here.
x = self.double_conv(x)
return x