-
Notifications
You must be signed in to change notification settings - Fork 0
/
win9_1d.py
223 lines (182 loc) · 7.08 KB
/
win9_1d.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
211
212
213
214
215
216
217
218
219
220
221
222
223
import pickle
import numpy as np
import torch.optim as optim
from torch.utils.data import TensorDataset, DataLoader
from CBAM import CBAM
import torch
import torch.nn as nn
import torch.nn.functional as F
import time
import os
class Self_Attention(nn.Module):
def __init__(self, dim, dk, dv,init_weights=True):
super(Self_Attention, self).__init__()
self.scale = dk ** -0.5
self.q = nn.Linear(dim, dk)
self.k = nn.Linear(dim, dk)
self.v = nn.Linear(dim, dv)
if init_weights:
self._initialize_weights()
def _initialize_weights(self):
for m in self.modules():
if isinstance(m,nn.Conv2d):
m.weight.data.normal_(0.0, 0.01)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.normal_(1.0, 0.01)
m.bias.data.fill_(0)
elif isinstance(m, nn.Linear):
m.weight.data.normal_(0.0, 0.01)
m.bias.data.fill_(0)
def forward(self, x):
q = self.q(x)
k = self.k(x)
v = self.v(x)
attn = (q @ k.transpose(-2, -1)) * self.scale
attn = attn.softmax(dim=-1)
x = attn @ v
return x
class BasicConv1d(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride, padding=0,init_weights=True):
super(BasicConv1d, self).__init__()
if init_weights:
self._initialize_weights()
self.conv = nn.Conv1d(in_planes, out_planes,
kernel_size=kernel_size, stride=stride,
padding=padding, bias=False) # verify bias false
self.bn = nn.BatchNorm1d(out_planes,
eps=0.0001, # value found in tensorflow
momentum=0.1, # default pytorch value
affine=True)
self.relu = nn.ReLU(inplace=False)
def _initialize_weights(self):
for m in self.modules():
if isinstance(m,nn.Conv1d):
m.weight.data.normal_(0.0, 0.01)
elif isinstance(m, nn.BatchNorm1d):
m.weight.data.normal_(1.0, 0.01)
m.bias.data.fill_(0)
elif isinstance(m, nn.Linear):
m.weight.data.normal_(0.0, 0.01)
m.bias.data.fill_(0)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
# x = self.relu(x)
return x
class Inception_A(nn.Module):
def __init__(self,in_channel):
super(Inception_A, self).__init__()
self.branch0 = BasicConv1d(in_channel, 64, kernel_size=1, stride=1)
self.branch1 = nn.Sequential(
BasicConv1d(in_channel, 32, kernel_size=1, stride=1),
BasicConv1d(32, 64, kernel_size=3, stride=1, padding=1)
)
self.branch2 = nn.Sequential(
BasicConv1d(in_channel, 32, kernel_size=1, stride=1),
BasicConv1d(32, 64, kernel_size=3, stride=1, padding=1),
BasicConv1d(64, 64, kernel_size=3, stride=1, padding=1)
)
# self.branch3 = nn.Sequential(
# nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False),
# BasicConv2d(in_channel, 32, kernel_size=1, stride=1)
# )
def forward(self, x):
x0 = self.branch0(x)
x1 = self.branch1(x)
x2 = self.branch2(x)
# x3 = self.branch3(x)
out = torch.cat((x0, x1, x2), 1)
return out
class Reduction_A(nn.Module):
def __init__(self,in_channel):
super(Reduction_A, self).__init__()
self.branch0 = BasicConv1d(in_channel, 64, kernel_size=3, stride=2)
self.branch1 = nn.Sequential(
BasicConv1d(in_channel, 32, kernel_size=1, stride=1),
BasicConv1d(32, 64, kernel_size=3, stride=1, padding=1),
BasicConv1d(64, 64, kernel_size=3, stride=2)
)
self.branch2 = BasicConv1d(in_channel, 64, kernel_size=3, stride=2)
def forward(self, x):
x0 = self.branch0(x)
x1 = self.branch1(x)
x2 = self.branch2(x)
out = torch.cat((x0, x1, x2), 1)
return out
class ResidualBlock(nn.Module):
def __init__(self, in_channel):
super(ResidualBlock, self).__init__()
self.branch = nn.Sequential(
BasicConv1d(in_channel, 64, kernel_size=1, stride=1),
nn.ReLU(inplace=False),
BasicConv1d(64, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(inplace=False),
BasicConv1d(64, 64, kernel_size=3, stride=1, padding=1),
)
self.relu = nn.ReLU(inplace=False)
def forward(self, x):
residual = x
out = self.branch(x)
# out = self.cbam(out)
out =out+residual
out = self.relu(out)
return out
class Dense(nn.Module):
def __init__(self, input_size, hidden_size, num_classes, dropout_rate,init_weights=True):
super(Dense, self).__init__()
self.dense1 = nn.Linear(input_size, hidden_size)
self.dropout1 = nn.Dropout(dropout_rate)
self.dense2 = nn.Linear(hidden_size, num_classes)
# self.dropout2 = nn.Dropout(dropout_rate)
# self.dense3 = nn.Linear(hidden_size, num_classes)
if init_weights:
self._initialize_weights()
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
m.weight.data.normal_(0.0, 0.01)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.normal_(1.0, 0.01)
m.bias.data.fill_(0)
elif isinstance(m, nn.Linear):
m.weight.data.normal_(0.0, 0.01)
m.bias.data.fill_(0)
def forward(self, x):
x = self.dense1(x)
x = self.dropout1(x)
x = torch.relu(x)
x = self.dense2(x)
# x = self.dropout2(x)
# x = torch.relu(x)
# x = self.dense3(x)
return x
class Conv_att(nn.Module):
def __init__(self,in_channel):#,dim, dk, dv
super(Conv_att, self).__init__()
self.conv=BasicConv1d(in_channel,64,kernel_size=1,stride=1)
self.res_block = nn.Sequential(
ResidualBlock(64),
ResidualBlock(64),
# ResidualBlock(64),
# ResidualBlock(64),
Self_Attention(109*9, 109*9, 128),
ResidualBlock(64),
ResidualBlock(64),
# ResidualBlock(64),
# ResidualBlock(64),
Self_Attention(128, 128, 15),
ResidualBlock(64),
ResidualBlock(64),
# ResidualBlock(64),
# ResidualBlock(64),
Self_Attention(15, 15, 7),
)
self.fc=Dense(64*7,64,2,0.5)
self.sigmoid = nn.Sigmoid()
# self.relu=nn.ReLU(inplace=False)
def forward(self, x):
x=self.conv(x)
x = self.res_block(x)
x = x.view(x.size(0), -1)
x=self.fc(x)
return x