-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathECGNet1.py
210 lines (174 loc) · 7.61 KB
/
ECGNet1.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# ECGNet_201911091150
import torch.nn as nn
import math
import torch.utils.model_zoo as model_zoo
import torch
def conv_2d(in_planes, out_planes, stride=(1,1), size=3):
"""3x3 convolution with padding"""
return nn.Conv2d(in_planes, out_planes, kernel_size=(1,size), stride=stride,
padding=(0,(size-1)//2), bias=False)
def conv_1d(in_planes, out_planes, stride=1, size=3):
"""3x3 convolution with padding"""
return nn.Conv1d(in_planes, out_planes, kernel_size=size, stride=stride,
padding=(size-1)//2, bias=False)
class BasicBlock1d(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None, size=3, res=True):
super(BasicBlock1d, self).__init__()
self.conv1 = conv_1d(inplanes, planes, stride, size=size)
self.bn1 = nn.BatchNorm1d(inplanes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv_1d(planes, planes, size=size)
self.bn2 = nn.BatchNorm1d(planes)
self.conv3 = conv_1d(planes, planes, size=size)
self.bn3 = nn.BatchNorm1d(planes)
self.dropout = nn.Dropout(.2)
self.downsample = downsample
self.stride = stride
self.res = res
def forward(self, x):
residual = x
out = self.bn1(x)
out = self.relu(out)
out = self.conv1(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn3(out)
out = self.relu(out)
out = self.conv3(out)
if self.res:
if self.downsample is not None:
residual = self.downsample(x)
out += residual
#out = self.relu(out)
return out
class BasicBlock2d(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=(1,1), downsample=None, size=3, res=True):
super(BasicBlock2d, self).__init__()
self.conv1 = conv_2d(inplanes, planes, stride, size=size)
self.bn1 = nn.BatchNorm2d(inplanes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv_2d(planes, planes, size=size)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = conv_2d(planes, planes, size=size)
self.bn3 = nn.BatchNorm2d(planes)
self.dropout = nn.Dropout(.2)
self.downsample = downsample
self.stride = stride
self.res = res
def forward(self, x):
residual = x
out = self.bn1(x)
out = self.relu(out)
out = self.conv1(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn3(out)
out = self.relu(out)
out = self.conv3(out)
if self.res:
if self.downsample is not None:
residual = self.downsample(x)
out += residual
#out = self.relu(out)
return out
class ECGNet(nn.Module):
def __init__(self, input_channel=1, num_classes=20):#, layers=[2, 2, 2, 2, 2, 2]
sizes = [
[3,3,3,3,3,3],
[5,5,5,5,3,3],
[7,7,7,7,3,3],
]
self.sizes = sizes
layers = [
[3,3,2,2,2,2],
[3,2,2,2,2,2],
[2,2,2,2,2,2]
]
super(ECGNet, self).__init__()
self.relu = nn.ReLU(inplace=True)
self.conv1 = nn.Conv2d(input_channel, 32, kernel_size=(1,50), stride=(1,2), padding=(0,0),
bias=False)
self.bn1 = nn.BatchNorm2d(32)
self.conv2 = nn.Conv2d(32, 32, kernel_size=(1,16), stride=(1,2), padding=(0,0),
bias=False)
self.bn2 = nn.BatchNorm2d(32)
#self.conv3 = nn.Conv2d(32, 32, kernel_size=(1,16), stride=(1,2), padding=(0,0),
# bias=False)
#print(self.conv2)
#self.bn3 = nn.BatchNorm2d(32)
#self.dropout = nn.Dropout(.2)
self.maxpool = nn.MaxPool2d(kernel_size=(1,3), stride=(1,2), padding=(0,0))
self.avgpool = nn.AdaptiveAvgPool1d(1)
self.layers1_list = nn.ModuleList()
self.layers2_list = nn.ModuleList()
for i,size in enumerate(sizes):
self.inplanes = 32
self.layers1 = nn.Sequential()
self.layers2 = nn.Sequential()
self.layers1.add_module('layer{}_1_1'.format(size), self._make_layer2d(BasicBlock2d, 32, layers[i][0], stride=(1,1), size=sizes[i][0]))
self.layers1.add_module('layer{}_1_2'.format(size), self._make_layer2d(BasicBlock2d, 32, layers[i][1], stride=(1,1), size=sizes[i][1]))
self.inplanes *= 8
self.layers2.add_module('layer{}_2_1'.format(size), self._make_layer1d(BasicBlock1d, 256, layers[i][2], stride=2, size=sizes[i][2]))
self.layers2.add_module('layer{}_2_2'.format(size), self._make_layer1d(BasicBlock1d, 256, layers[i][3], stride=2, size=sizes[i][3]))
self.layers2.add_module('layer{}_2_3'.format(size), self._make_layer1d(BasicBlock1d, 256, layers[i][4], stride=2, size=sizes[i][4]))
self.layers2.add_module('layer{}_2_4'.format(size), self._make_layer1d(BasicBlock1d, 256, layers[i][5], stride=2, size=sizes[i][5]))
self.layers1_list.append(self.layers1)
self.layers2_list.append(self.layers2)
# self.drop = nn.Dropout(p=0.2)
self.fc = nn.Linear(256*len(sizes)+2, num_classes)
def _make_layer1d(self, block, planes, blocks, stride=2, size=3, res=True):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv1d(self.inplanes, planes * block.expansion,
kernel_size=1, padding=0, stride=stride, bias=False),
nn.BatchNorm1d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample, size=size, res=res))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes, size=size, res=res))
return nn.Sequential(*layers)
def _make_layer2d(self, block, planes, blocks, stride=(1,2), size=3, res=True):
downsample = None
if stride != (1,1) or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=(1,1), padding=(0,0), stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample, size=size, res=res))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes, size=size, res=res))
return nn.Sequential(*layers)
def forward(self, x0, fr):
x0 = x0.unsqueeze(1)
x0 = self.conv1(x0)
x0 = self.bn1(x0)
x0 = self.relu(x0)
x0 = self.maxpool(x0)
x0 = self.conv2(x0)
#x0 = self.bn2(x0)
#x0 = self.relu(x0)
x0 = self.maxpool(x0)
#x0 = self.dropout(x0)
xs = []
for i in range(len(self.sizes)):
#print(self.layers1_list[i])
x = self.layers1_list[i](x0)
x = torch.flatten(x, start_dim=1,end_dim=2)
x = self.layers2_list[i](x)
x = self.avgpool(x)
xs.append(x)
out = torch.cat(xs, dim=2)
out = out.view(out.size(0), -1)
out = torch.cat([out, fr], dim=1)
out = self.fc(out)
return out