-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
141 lines (110 loc) · 5.06 KB
/
models.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
import torch
from torch import nn
class ecgTransForm(nn.Module):
def __init__(self, configs, hparams):
super(ecgTransForm, self).__init__()
filter_sizes = [5, 9, 11]
self.conv1 = nn.Conv1d(configs.input_channels, configs.mid_channels, kernel_size=filter_sizes[0],
stride=configs.stride, bias=False, padding=(filter_sizes[0] // 2))
self.conv2 = nn.Conv1d(configs.input_channels, configs.mid_channels, kernel_size=filter_sizes[1],
stride=configs.stride, bias=False, padding=(filter_sizes[1] // 2))
self.conv3 = nn.Conv1d(configs.input_channels, configs.mid_channels, kernel_size=filter_sizes[2],
stride=configs.stride, bias=False, padding=(filter_sizes[2] // 2))
self.bn = nn.BatchNorm1d(configs.mid_channels)
self.relu = nn.ReLU()
self.mp = nn.MaxPool1d(kernel_size=2, stride=2, padding=1)
self.do = nn.Dropout(configs.dropout)
self.conv_block2 = nn.Sequential(
nn.Conv1d(configs.mid_channels, configs.mid_channels * 2, kernel_size=8, stride=1, bias=False, padding=4),
nn.BatchNorm1d(configs.mid_channels * 2),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2, stride=2, padding=1)
)
self.conv_block3 = nn.Sequential(
nn.Conv1d(configs.mid_channels * 2, configs.final_out_channels, kernel_size=8, stride=1, bias=False,
padding=4),
nn.BatchNorm1d(configs.final_out_channels),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2, stride=2, padding=1),
)
self.inplanes = 128
self.crm = self._make_layer(SEBasicBlock, 128, 3)
self.encoder_layer = nn.TransformerEncoderLayer(d_model=configs.trans_dim, nhead=configs.num_heads, batch_first=True)
self.transformer_encoder = nn.TransformerEncoder(self.encoder_layer, num_layers=3)
self.aap = nn.AdaptiveAvgPool1d(1)
self.clf = nn.Linear(hparams["feature_dim"], configs.num_classes)
def _make_layer(self, block, planes, blocks, stride=1): # makes residual SE block
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv1d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm1d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def forward(self, x_in):
# Multi-scale Convolutions
x1 = self.conv1(x_in)
x2 = self.conv2(x_in)
x3 = self.conv3(x_in)
x_concat = torch.mean(torch.stack([x1, x2, x3],2), 2)
x_concat = self.do(self.mp(self.relu(self.bn(x_concat))))
x = self.conv_block2(x_concat)
x = self.conv_block3(x)
# Channel Recalibration Module
x = self.crm(x)
# Bi-directional Transformer
x1 = self.transformer_encoder(x)
x2 = self.transformer_encoder(torch.flip(x,[2]))
x = x1+x2
x = self.aap(x)
x_flat = x.reshape(x.shape[0], -1)
x_out = self.clf(x_flat)
return x_out
class SELayer(nn.Module):
def __init__(self, channel, reduction=4):
super(SELayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool1d(1)
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction, bias=False),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel, bias=False),
nn.Sigmoid()
)
def forward(self, x):
b, c, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1)
return x * y.expand_as(x)
class SEBasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
base_width=64, dilation=1, norm_layer=None,
*, reduction=4):
super(SEBasicBlock, self).__init__()
self.conv1 = nn.Conv1d(inplanes, planes, stride)
self.bn1 = nn.BatchNorm1d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv1d(planes, planes, 1)
self.bn2 = nn.BatchNorm1d(planes)
self.se = SELayer(planes, reduction)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.se(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out