-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunet_layers.py
66 lines (40 loc) · 1.79 KB
/
unet_layers.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
import torch.nn as nn
class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, padding=1, stride=1, name=None):
super(ConvBlock, self).__init__()
block = []
# first conv layer
block.append(nn.Conv2d(in_channels, out_channels, kernel_size, \
padding=padding, stride=stride))
block.append(nn.ReLU())
block.append(nn.BatchNorm2d(out_channels))
# second conv layer
block.append(nn.Conv2d(out_channels, out_channels, kernel_size, \
padding=padding, stride=stride))
block.append(nn.ReLU())
block.append(nn.BatchNorm2d(out_channels))
# make sequential
self.conv_block = nn.Sequential(*block)
def forward(self, x):
output = self.conv_block(x)
return output
class DownSampling(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, name=None):
super(DownSampling, self).__init__()
self.conv = ConvBlock(in_channels, out_channels, kernel_size)
self.max_pool = nn.MaxPool2d(kernel_size=2, stride=2)
def forward(self, x):
conv_out = self.conv(x)
output = self.max_pool(conv_out)
return output, conv_out
class UpSampling(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, name=None):
super(UpSampling, self).__init__()
self.conv = ConvBlock(in_channels, out_channels, kernel_size)
self.conv_t = nn.ConvTranspose2d(out_channels, out_channels, kernel_size, \
padding=1, stride=2, output_padding=1)
def forward(self, x, skip):
conv_out = self.conv(x)
output = self.conv_t(conv_out)
output += skip
return output