-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
173 lines (143 loc) · 5.1 KB
/
model.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
168
169
170
171
172
173
import torch.nn as nn
import torch.nn.functional as F
from functools import partial
import torch
nonlinearity = partial(F.relu, inplace=True)
class double_conv(nn.Module):
def __init__(self, in_ch, out_ch):
super(double_conv, self).__init__()
self.conv = nn.Sequential(
nn.Conv1d(in_ch, out_ch, kernel_size=5, padding=2),
nn.BatchNorm1d(out_ch),
nn.ReLU(inplace=True),
nn.Conv1d(out_ch, out_ch, kernel_size=5, padding=2),
nn.BatchNorm1d(out_ch),
nn.ReLU(inplace=True),
)
def forward(self, x):
x = self.conv(x)
return x
class inconv(nn.Module):
def __init__(self, in_ch, out_ch):
super(inconv, self).__init__()
self.conv = double_conv(in_ch, out_ch)
def forward(self, x):
x = self.conv(x)
return x
class outconv(nn.Module):
def __init__(self, in_ch, out_ch):
super(outconv, self).__init__()
self.conv = nn.Conv1d(in_ch, out_ch, kernel_size=1)
def forward(self, x):
x = self.conv(x)
return x
class down(nn.Module):
def __init__(self, in_ch, out_ch):
super(down, self).__init__()
self.max_pool_conv = nn.Sequential(
nn.MaxPool1d(2), double_conv(in_ch, out_ch))
def forward(self, x):
x = self.max_pool_conv(x)
return x
class up(nn.Module):
"""Upscaling then double conv"""
def __init__(self, in_channels, out_channels, bilinear=True):
super().__init__()
if bilinear:
self.up = nn.Upsample(
scale_factor=2, mode="linear", align_corners=True)
else:
self.up = nn.ConvTranspose1d(
in_channels // 2, in_channels // 2, kernel_size=2, stride=2
)
self.conv = double_conv(in_channels, out_channels)
def forward(self, x1, x2):
x1 = self.up(x1)
# input is CHW
diff = torch.tensor([x2.size()[2] - x1.size()[2]])
x1 = F.pad(x1, [diff // 2, diff - diff // 2])
x = torch.cat([x2, x1], dim=1)
return self.conv(x)
class TPPblock(nn.Module):
def __init__(self, in_channels):
super(TPPblock, self).__init__()
self.pool1 = nn.MaxPool1d(kernel_size=2, stride=2)
self.pool2 = nn.MaxPool1d(kernel_size=3, stride=3)
self.pool3 = nn.MaxPool1d(kernel_size=5, stride=5)
self.pool4 = nn.MaxPool1d(kernel_size=6, stride=6)
self.conv = nn.Conv1d(
in_channels=in_channels, out_channels=1, kernel_size=1, padding=0
)
def forward(self, x):
self.in_channels, t = x.size(1), x.size(2)
self.layer1 = F.upsample(
self.conv(self.pool1(x)), size=t, mode="linear", align_corners=True
)
self.layer2 = F.upsample(
self.conv(self.pool2(x)), size=t, mode="linear", align_corners=True
)
self.layer3 = F.upsample(
self.conv(self.pool3(x)), size=t, mode="linear", align_corners=True
)
self.layer4 = F.upsample(
self.conv(self.pool4(x)), size=t, mode="linear", align_corners=True
)
out = torch.cat([self.layer1, self.layer2,
self.layer3, self.layer4, x], 1)
return out
class C2F_TCN(nn.Module):
'''
Features are extracted at the last layer of decoder.
'''
def __init__(self, n_channels, n_classes):
super(C2F_TCN, self).__init__()
self.inc = inconv(n_channels, 256)
self.down1 = down(256, 256)
self.down2 = down(256, 256)
self.down3 = down(256, 128)
self.down4 = down(128, 128)
self.down5 = down(128, 128)
self.down6 = down(128, 128)
self.up = up(260, 128)
self.outcc0 = outconv(128, n_classes)
self.up0 = up(256, 128)
self.outcc1 = outconv(128, n_classes)
self.up1 = up(256, 128)
self.outcc2 = outconv(128, n_classes)
self.up2 = up(384, 128)
self.outcc3 = outconv(128, n_classes)
self.up3 = up(384, 128)
self.outcc4 = outconv(128, n_classes)
self.up4 = up(384, 128)
self.outcc = outconv(128, n_classes)
self.tpp = TPPblock(128)
self.weights = torch.nn.Parameter(torch.ones(6))
def forward(self, x):
x1 = self.inc(x)
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
x6 = self.down5(x5)
x7 = self.down6(x6)
# x7 = self.dac(x7)
x7 = self.tpp(x7)
x = self.up(x7, x6)
y1 = self.outcc0(F.relu(x))
# print("y1.shape=", y1.shape)
x = self.up0(x, x5)
y2 = self.outcc1(F.relu(x))
# print("y2.shape=", y2.shape)
x = self.up1(x, x4)
y3 = self.outcc2(F.relu(x))
# print("y3.shape=", y3.shape)
x = self.up2(x, x3)
y4 = self.outcc3(F.relu(x))
# print("y4.shape=", y4.shape)
x = self.up3(x, x2)
y5 = self.outcc4(F.relu(x))
# print("y5.shape=", y5.shape)
x = self.up4(x, x1)
y = self.outcc(x)
# print("y.shape=", y.shape)
return y, [y5, y4, y3, y2, y1], x