-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathTRUNet.py
167 lines (146 loc) · 6.07 KB
/
TRUNet.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
""" Yangang Cao 2021.4.24 1:22am"""
import torch
import torch.nn as nn
import torch.nn.functional as F
class StandardConv1d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(StandardConv1d, self).__init__()
self.StandardConv1d = nn.Sequential(
nn.Conv1d(in_channels = in_channels,
out_channels = out_channels,
kernel_size = kernel_size,
stride = stride,
padding = stride //2),
nn.ReLU(inplace=True))
def forward(self, x):
return self.StandardConv1d(x)
class DepthwiseSeparableConv1d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(DepthwiseSeparableConv1d, self).__init__()
self.DepthwiseSeparableConv1d = nn.Sequential(
nn.Conv1d(in_channels = in_channels,
out_channels = out_channels,
kernel_size = 1),
nn.BatchNorm1d(out_channels),
nn.ReLU(inplace=True),
nn.Conv1d(in_channels = out_channels,
out_channels = out_channels,
kernel_size = kernel_size,
stride = stride,
padding = kernel_size // 2,
groups = out_channels),
nn.BatchNorm1d(out_channels),
nn.ReLU(inplace = True))
def forward (self, x):
return self.DepthwiseSeparableConv1d(x)
class GRUBlock(nn.Module):
def __init__(self, in_channels, hidden_size, out_channels, bidirectional):
super(GRUBlock, self).__init__()
self.GRU = nn.GRU(in_channels, hidden_size, batch_first=True, bidirectional=bidirectional)
self.conv = nn.Sequential(nn.Conv1d(hidden_size * (2 if bidirectional==True else 1), out_channels, kernel_size = 1),
nn.BatchNorm1d(out_channels),
nn.ReLU(inplace=True))
def forward(self, x):
output,h = self.GRU(x)
output = output.transpose(1,2)
output = self.conv(output)
return output
class FirstTrCNN(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(FirstTrCNN, self).__init__()
self.FirstTrCNN = nn.Sequential(
nn.Conv1d(in_channels, out_channels, kernel_size=1),
nn.BatchNorm1d(out_channels),
nn.ReLU(inplace=True),
nn.ConvTranspose1d(in_channels = out_channels,
out_channels = out_channels,
kernel_size = kernel_size,
stride = stride,
padding = stride//2),
nn.BatchNorm1d(out_channels),
nn.ReLU(inplace=True)
)
def forward(self,x):
return self.FirstTrCNN(x)
class TrCNN(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(TrCNN, self).__init__()
self.TrCNN = nn.Sequential(
nn.Conv1d(in_channels, out_channels, kernel_size=1),
nn.BatchNorm1d(out_channels),
nn.ReLU(inplace=True),
nn.ConvTranspose1d(in_channels = out_channels,
out_channels = out_channels,
kernel_size = kernel_size,
stride = stride,
padding = stride//2),
nn.BatchNorm1d(out_channels),
nn.ReLU(inplace=True)
)
def forward(self,x1,x2):
diffY = x2.size()[2] - x1.size()[2]
x1 = F.pad(x1, [diffY // 2, diffY - diffY // 2, 0, 0])
x = torch.cat((x1,x2),1)
output = self.TrCNN(x)
return output
class LastTrCNN(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(LastTrCNN, self).__init__()
self.LastTrCNN = nn.Sequential(
nn.Conv1d(in_channels, out_channels, kernel_size=1),
nn.BatchNorm1d(out_channels),
nn.ReLU(inplace=True),
nn.ConvTranspose1d(in_channels = out_channels,
out_channels = out_channels,
kernel_size = kernel_size,
stride = stride,
padding=stride//2))
def forward(self,x1,x2):
diffY = x2.size()[2] - x1.size()[2]
x1 = F.pad(x1, [diffY // 2, diffY - diffY // 2, 0, 0])
x = torch.cat((x1,x2),1)
output = self.LastTrCNN(x)
return output
class TRUNet(nn.Module):
def __init__(self):
super(TRUNet, self).__init__()
self.down1 = StandardConv1d(4,64,5,2)
self.down2 = DepthwiseSeparableConv1d(64, 128, 3, 1)
self.down3 = DepthwiseSeparableConv1d(128, 128, 5, 2)
self.down4 = DepthwiseSeparableConv1d(128, 128, 3, 1)
self.down5 = DepthwiseSeparableConv1d(128, 128, 5, 2)
self.down6 = DepthwiseSeparableConv1d(128, 128, 3, 2)
self.FGRU = GRUBlock(128, 64, 64, bidirectional=True)
self.TGRU = GRUBlock(64, 128, 64, bidirectional=False)
self.up1 = FirstTrCNN(64, 64, 3, 2)
self.up2 = TrCNN(192, 64, 5, 2)
self.up3 = TrCNN(192, 64, 3, 1)
self.up4 = TrCNN(192, 64, 5, 2)
self.up5 = TrCNN(192, 64, 3, 1)
self.up6 = LastTrCNN(128, 5, 5, 2)
def forward(self, x):
x1 = self.down1(x)
x2 = self.down2(x1)
x3 = self.down3(x2)
x4 = self.down4(x3)
x5 = self.down5(x4)
x6 = self.down6(x5)
x7 = x6.transpose(1,2)
x8 = self.FGRU(x7)
x9 = x8.transpose(1,2)
x10 = self.TGRU(x9)
x11 = self.up1(x10)
x12 = self.up2(x11,x5)
x13 = self.up3(x12,x4)
x14 = self.up4(x13,x3)
x15 = self.up5(x14,x2)
x16 = self.up6(x15,x1)
return x16
if __name__=='__main__':
TRU = TRUNet()
total_params = sum(p.numel() for p in TRU.parameters())
print("total params:",total_params)
x = torch.randn(1, 4, 257)
print("input_shape:",x.shape)
y = TRU(x)
print("output_shape:",y.shape)