-
Notifications
You must be signed in to change notification settings - Fork 0
/
UNet.py
97 lines (78 loc) · 3.47 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
85
86
87
88
89
90
91
92
93
94
95
96
97
import torch
import torch.nn as nn
# Define the module: Conv + BN + ReLU + Conv + BN + ReLU
class conv_step(nn.Module):
def __init__(self, input_dim, num_features, kernel_size):
super(conv_step, self).__init__()
padding = int((kernel_size-1)/2)
self.conv_1 = nn.Conv1d(input_dim, num_features, kernel_size, padding=padding)
self.bn = nn.BatchNorm1d(num_features)
self.conv_2 = nn.Conv1d(num_features, num_features, kernel_size, padding=padding)
self.relu = nn.ReLU()
def forward(self, x):
x = self.conv_1(x)
x = self.bn(x)
x = self.relu(x)
x = self.conv_2(x)
x = self.bn(x)
x = self.relu(x)
return x
# Define up-sampling
class up_conv(nn.Module):
def __init__(self, input_dim):
super(up_conv, self).__init__()
self.upsample = nn.Upsample(scale_factor=2)
self.conv = nn.Conv1d(input_dim, input_dim//2, kernel_size=3, padding=1)
self.bn = nn.BatchNorm1d(input_dim//2)
def forward(self, x):
x = self.upsample(x)
x = self.conv(x)
x = self.bn(x)
return x
# Define the U_Net network architecture
class UNET(nn.Module):
def __init__(self, input_dim, num_features, kernel_size):
super(UNET, self).__init__()
self.down_layer_1 = conv_step(input_dim, num_features, kernel_size)
self.down_layer_2 = conv_step(num_features, num_features*2, kernel_size)
self.down_layer_3 = conv_step(num_features*2, num_features*4, kernel_size)
self.down_layer_4 = conv_step(num_features*4, num_features*8, kernel_size)
self.down_layer_5 = conv_step(num_features*8, num_features*16, kernel_size)
self.up_conv_1 = up_conv(num_features*16)
self.up_conv_2 = up_conv(num_features*8)
self.up_conv_3 = up_conv(num_features*4)
self.up_conv_4 = up_conv(num_features*2)
self.up_layer_1 = conv_step(num_features*16, num_features*8, kernel_size)
self.up_layer_2 = conv_step(num_features*8, num_features*4, kernel_size)
self.up_layer_3 = conv_step(num_features*4, num_features*2, kernel_size)
self.up_layer_4 = conv_step(num_features*2, num_features, kernel_size)
self.maxpool = nn.MaxPool1d(kernel_size=2, stride=2, padding=0)
self.softmax = nn.LogSoftmax(dim=1)
self.final = nn.Conv1d(in_channels=num_features, out_channels=3, kernel_size=1)
self.bn = nn.BatchNorm1d(input_dim)
def forward(self,x):
""" Contracting """
out_1 = self.down_layer_1(x)
x = self.maxpool(out_1)
out_2 = self.down_layer_2(x)
x = self.maxpool(out_2)
out_3 = self.down_layer_3(x)
x = self.maxpool(out_3)
out_4 = self.down_layer_4(x)
x = self.maxpool(out_4)
end = self.down_layer_5(x)
""" Expanding """
x = self.up_conv_1(end)
x = torch.cat([out_4, x], dim=1)
x = self.up_layer_1(x)
x = self.up_conv_2(x)
x = torch.cat([out_3, x], dim=1)
x = self.up_layer_2(x)
x = self.up_conv_3(x)
x = torch.cat([out_2, x], dim=1)
x = self.up_layer_3(x)
x = self.up_conv_4(x)
x = torch.cat([out_1, x], dim=1)
x = self.up_layer_4(x)
x = self.final(x)
return x