-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_network.py
599 lines (484 loc) · 20.3 KB
/
block_network.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import torch
from torch import nn
from torch.autograd import Function
import torch.nn.functional as F
import math
import numpy as np
import random
class EcgClassifier(nn.Module):
"""Feature classifier class for MNIST -> MNIST-M experiment in ATDA."""
def __init__(self, dropout_keep=None, num_classes=5):
"""Init classifier."""
super(EcgClassifier, self).__init__()
self.dropout_keep = dropout_keep
self.classifier = nn.Sequential(
nn.Linear(256 * 10, 256 * 5),
# nn.Linear(256 *5, 256 * 1),
# nn.BatchNorm1d(256 * 5),
nn.ReLU(inplace=True),
nn.Dropout(self.dropout_keep), # 0.5
nn.Linear(256 * 5, num_classes),
)
def forward(self, x):
"""Forward classifier."""
out = self.classifier(x)
return out
class AlexNetforEcg_Single_Model(nn.Module):
'''input tensor size:(None,1,3,128)'''
def __init__(self):
super(AlexNetforEcg_Single_Model, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(1, 64, kernel_size=(3, 5), padding=(0, 0)),
# nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2)),#(N,64,1,62)
nn.Conv2d(64, 192, kernel_size=(1, 5), padding=(0, 2)),
# nn.BatchNorm2d(192),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2)),#(N,192,1,30)
nn.Conv2d(192, 256, kernel_size=(1, 5), padding=(0, 2)),#(N,_,1,30)
# nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2)),
)
self.fc = nn.Sequential(
nn.Linear(256 * 15, 256 * 10),
# nn.Linear(256 * 14, 256 * 10),
nn.ReLU(inplace=True),
nn.Dropout(0.5),#0.3
nn.BatchNorm1d(256 * 10),
)
self.classifier = nn.Sequential(
nn.Linear(256 * 10, 256 * 5),
nn.ReLU(inplace=True),
nn.Dropout(0.5), # 0.5
nn.Linear(256 * 5, 4)
)
def forward(self, x):
x = self.features(x)
# print("feature size:", x.size())
# x = x.view(x.size(0), self.num_flat_features(x))
# print(x.size())
x = x.view(x.size(0), -1)
x = self.fc(x)
y = self.classifier(x)
return x, y
class ResClassifier(nn.Module):
def __init__(self, num_classes=5, dropout_keep=0.5):
super(ResClassifier, self).__init__()
self.fc1 = nn.Sequential(
nn.Linear(256 * 10, 256 * 5),
# nn.BatchNorm1d(1000, affine=True),
nn.ReLU(inplace=True),
nn.Dropout(p=dropout_keep)
)
self.fc2 = nn.Linear(256 * 5, num_classes)
# self.extract = extract
self.dropout_p = dropout_keep
def forward(self, x, extract=False):
fc1_emb = self.fc1(x)
if self.training:
fc1_emb.mul_(math.sqrt(1 - self.dropout_p))
logit = self.fc2(fc1_emb)
if extract:
return fc1_emb, logit
return logit
class AlexNetforEcg_DS1_to_DS2(nn.Module):
'''input tensor size:(None,1,3,128)'''
def __init__(self):
super(AlexNetforEcg_DS1_to_DS2, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(1, 64, kernel_size=(3, 5), padding=(0, 0)),
nn.ReLU(inplace=True),
# nn.BatchNorm2d(96),
nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2)),#(N,64,1,62)
nn.Conv2d(64, 192, kernel_size=(1, 5), padding=(0, 2)),
nn.ReLU(inplace=True),
# nn.BatchNorm2d(256),
nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2)),#(N,192,1,30)
nn.Conv2d(192, 256, kernel_size=(1, 5), padding=(0, 2)),#(N,_,1,30)
# nn.Conv2d(192, 256, kernel_size=(1, 3), padding=(0, 1)),
# # nn.BatchNorm2d(384),
# nn.LeakyReLU(0.2, inplace=True),
nn.ReLU(inplace=True),
# nn.Conv2d(384, 384, kernel_size=(1, 1), padding=(0, 0)),
# nn.ReLU(inplace=True),
# nn.Conv2d(384, 256, kernel_size=(1, 1), padding=(0, 0)),
# nn.ReLU(inplace=True),
# nn.Conv2d(384, 256, kernel_size=(1, 1), padding=(0, 0)),#(N,256,1,32)
# nn.BatchNorm2d(256),
# nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2)),
)
self.fc = nn.Sequential(
nn.Linear(256 * 15, 256 * 10),
# nn.Linear(256 * 15, 256 * 5),
# nn.Linear(256 * 14, 256 * 10),
nn.ReLU(inplace=True),
nn.Dropout(0.3),#0.3
nn.BatchNorm1d(256 * 10),
)
def forward(self, x):
x = self.features(x)
# print("feature size:", x.size())
# x = x.view(x.size(0), self.num_flat_features(x))
# print(x.size())
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
class AlexNetforEcg_DS1_to_DS2_each_patient(nn.Module):
'''input tensor size:(None,1,3,128)'''
def __init__(self):
super(AlexNetforEcg_DS1_to_DS2_each_patient, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(1, 64, kernel_size=(3, 5), padding=(0, 0)),
# nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2)),#(N,64,1,62)
nn.Conv2d(64, 192, kernel_size=(1, 5), padding=(0, 2)),
# nn.BatchNorm2d(192),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2)),#(N,192,1,30)
nn.Conv2d(192, 256, kernel_size=(1, 5), padding=(0, 2)),#(N,_,1,30)
# nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2)),
)
self.fc = nn.Sequential(
nn.Linear(256 * 15, 256 * 10),
# nn.Linear(256 * 14, 256 * 10),
nn.ReLU(inplace=True),
nn.Dropout(0.5),#0.3
nn.BatchNorm1d(256 * 10),
)
def forward(self, x):
x = self.features(x)
# print("feature size:", x.size())
# x = x.view(x.size(0), self.num_flat_features(x))
# print(x.size())
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
class AlexNetforEcg_mitdb_to_incart(nn.Module):
'''input tensor size:(None,1,3,128)'''
def __init__(self):
super(AlexNetforEcg_mitdb_to_incart, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(1, 64, kernel_size=(3, 5), padding=(0, 0)),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2)),#(N,64,1,62)
nn.Conv2d(64, 192, kernel_size=(1, 5), padding=(0, 2)),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2)),#(N,192,1,30)
nn.Conv2d(192, 256, kernel_size=(1, 5), padding=(0, 2)),#(N,_,1,30)
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2)),
####DS1->SVDB、DS1->INCARTDB实验多加了一个卷积层
nn.Conv2d(256, 256, kernel_size=(1, 5), padding=(0, 2)),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=(1, 5), padding=(0, 2)),
nn.ReLU(inplace=True),
)
self.fc = nn.Sequential(
nn.Linear(256 * 15, 256 * 10),
# nn.Linear(256 * 14, 256 * 10),
nn.ReLU(inplace=True),
nn.Dropout(0.3),#0.3
nn.BatchNorm1d(256 * 10),
)
def forward(self, x):
x = self.features(x)
# print("feature size:", x.size())
# x = x.view(x.size(0), self.num_flat_features(x))
# print(x.size())
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
class AlexNetforEcg_mitdb_to_svdb(nn.Module):
'''input tensor size:(None,1,3,128)'''
def __init__(self):
super(AlexNetforEcg_mitdb_to_svdb, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(1, 64, kernel_size=(3, 5), padding=(0, 0)),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2)), # (N,64,1,62)
nn.Conv2d(64, 192, kernel_size=(1, 5), padding=(0, 2)),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2)), # (N,192,1,30)
nn.Conv2d(192, 256, kernel_size=(1, 5), padding=(0, 2)), # (N,_,1,30)
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2)),
####DS1->SVDB、DS1->INCARTDB实验多加了一个卷积层
nn.Conv2d(256, 256, kernel_size=(1, 5), padding=(0, 2)),
nn.ReLU(inplace=True),
)
self.fc = nn.Sequential(
nn.Linear(256 * 15, 256 * 10),
# nn.Linear(256 * 14, 256 * 10),
nn.ReLU(inplace=True),
nn.Dropout(0.3), # 0.3
nn.BatchNorm1d(256 * 10),
)
def forward(self, x):
x = self.features(x)
# print("feature size:", x.size())
# x = x.view(x.size(0), self.num_flat_features(x))
# print(x.size())
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
class GradReverse(Function):
def __init__(self, lambd):
self.lambd = lambd
def forward(self, x):
return x.view_as(x)
def backward(self, grad_output):
return (grad_output * -self.lambd)
def grad_reverse(x, lambd=1.0):
return GradReverse(lambd)(x)
class AdversarialLayer(torch.autograd.Function):
def __init__(self, high_value=1.0, max_iter=10000):
self.iter_num = 0
self.alpha = 10
self.low = 0.0
self.high = high_value
self.max_iter = 10000.0
def forward(self, input):
self.iter_num += 1
output = input * 1.0
return output
def backward(self, gradOutput):
self.coeff = np.float(
2.0 * (self.high - self.low) / (1.0 + np.exp(-self.alpha * self.iter_num / self.max_iter)) - (
self.high - self.low) + self.low)
# self.coeff = 1
return -self.coeff * gradOutput
class AdversarialNetwork(nn.Module):
def __init__(self):
super(AdversarialNetwork, self).__init__()
self.ad_layer1 = nn.Linear(2560, 1024)
self.ad_layer2 = nn.Linear(1024,1024)
self.ad_layer3 = nn.Linear(1024, 1)
# self.ad_layer1.weight.data.normal_(0, 0.01)
# self.ad_layer2.weight.data.normal_(0, 0.01)
# self.ad_layer3.weight.data.normal_(0, 0.3)
# self.ad_layer1.bias.data.fill_(0.0)
# self.ad_layer2.bias.data.fill_(0.0)
# self.ad_layer3.bias.data.fill_(0.0)
self.relu1 = nn.ReLU()
self.relu2 = nn.ReLU()
self.dropout1 = nn.Dropout(0.5)
self.dropout2 = nn.Dropout(0.5)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.ad_layer1(x)
x = self.relu1(x)
x = self.dropout1(x)
x = self.ad_layer2(x)
x = self.relu2(x)
x = self.dropout2(x)
x = self.ad_layer3(x)
x = self.sigmoid(x)
return x
def output_num(self):
return 1
class DomainClassifier(nn.Module):
"""Feature classifier class for MNIST -> MNIST-M experiment in ATDA."""
def __init__(self, num_classes=5):
"""Init classifier."""
super(DomainClassifier, self).__init__()
self.classifier_d = nn.Sequential(
nn.Linear(256 * 10, 256 * 2),
nn.ReLU(inplace=True),
nn.Linear(256 * 2, 256 * 2),
nn.ReLU(inplace=True),
nn.Linear(256 * 2, num_classes),
)
def forward(self, x):
"""Forward classifier."""
# x = grad_reverse(x)
out = self.classifier_d(x)
return out
class Generator(nn.Module):
'''Dense Convolutional Networks With Focal Loss and Image Generation for
Electrocardiogram Classification
Convert beat to image'''
def __init__(self):
super(Generator, self).__init__()
self.fc = nn.Sequential(
nn.Linear(128, 1024),
nn.BatchNorm1d(1024),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(1024, 1568),
)
self.upsample = nn.Sequential(
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.Conv2d(32, 64, kernel_size=(1, 1), padding=(0, 0)),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.Conv2d(64, 64, kernel_size=(1, 1), padding=(0, 0)),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Conv2d(64, 3, kernel_size=(1, 1), padding=(0, 0)),
)
def forward(self, x):
x = self.fc(x)
# print("feature size:", x.size())
x = x.view(-1, 32, 7, 7)
# print(x.size())
# x = x.view(x.size(0), -1)
x = self.upsample(x)
return x
class Bottleneck(nn.Module):
'''
the above mentioned bottleneck, including two conv layer, one's kernel size is 1×1, another's is 3×3
in_planes可以理解成channel
after non-linear operation, concatenate the input to the output
DenseNet的非线性变换H采用了Bottleneck结构BN-ReLU-Conv(1×1)-BN-ReLU-Conv(3×3),1×1的卷积用于降低维度,将channels数降
低至4 * Growth_rate
Bottleneck是这样一种网络,其输入输出channel差距较大,就像一个瓶颈一样,上窄下宽亦或上宽下窄,
特征图的大小会因为最后一步的cat从N×in_planes×H×W变成N×(in_planes+growth_rate)×H×W
'''
def __init__(self, in_planes, growth_rate):
super(Bottleneck, self).__init__()
self.bn1 = nn.BatchNorm2d(in_planes)
self.conv1 = nn.Conv2d(in_planes, 4 * growth_rate, kernel_size=1, bias=False)
self.bn2 = nn.BatchNorm2d(4 * growth_rate)
self.conv2 = nn.Conv2d(4 * growth_rate, growth_rate, kernel_size=3, padding=1, bias=False)
# self.conv2 = nn.Conv2d(in_planes, growth_rate, kernel_size=3, padding=1, bias=False)
self.dropout = nn.Dropout(0.5)
def forward(self, x):
out = self.conv1(F.relu(self.bn1(x)))
out = self.conv2(F.relu(self.bn2(out)))
# out = self.conv2(F.relu(self.bn1(out)))
out = self.dropout(out)
# input and output are concatenated here
out = torch.cat([out, x], 1)
return out
class Transition(nn.Module):
'''
transition layer is used for down sampling the feature
when compress rate is 0.5, out_planes is a half of in_planes
'''
def __init__(self, in_planes, out_planes):
super(Transition, self).__init__()
self.bn = nn.BatchNorm2d(in_planes)
self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=1, bias=False)
def forward(self, x):
out = self.conv(F.relu(self.bn(x)))
# use average pooling change the size of feature map here
out = F.avg_pool2d(out, 2)
return out
class DenseNet(nn.Module):
def __init__(self, block, nblocks, growth_rate=8, reduction=1, num_classes=5):
super(DenseNet, self).__init__()
'''
Args:
block: bottleneck
nblock: a list, the elements is number of bottleneck in each denseblock
growth_rate: channel size of bottleneck's output
reduction:
'''
self.generator = Generator()
self.growth_rate = growth_rate
# num_planes = 2 * growth_rate
num_planes = 16
self.conv1 = nn.Conv2d(3, num_planes, kernel_size=32, padding=15, bias=False)
# a DenseBlock and a transition layer
self.dense1 = self._make_dense_layers(block, num_planes, nblocks[0])
num_planes += nblocks[0] * growth_rate
# the channel size is superposed, mutiply by reduction to cut it down here, the reduction is also known as compress rate
out_planes = int(math.floor(num_planes * reduction))
self.trans1 = Transition(num_planes, out_planes)
num_planes = out_planes
# a DenseBlock and a transition layer
self.dense2 = self._make_dense_layers(block, num_planes, nblocks[1])
num_planes += nblocks[1] * growth_rate
# the channel size is superposed, mutiply by reduction to cut it down here, the reduction is also known as compress rate
out_planes = int(math.floor(num_planes * reduction))
self.trans2 = Transition(num_planes, out_planes)
num_planes = out_planes
# a DenseBlock and a transition layer
self.dense3 = self._make_dense_layers(block, num_planes, nblocks[2])
num_planes += nblocks[2] * growth_rate
# the channel size is superposed, mutiply by reduction to cut it down here, the reduction is also known as compress rate
# out_planes = int(math.floor(num_planes * reduction))
# self.trans3 = Transition(num_planes, out_planes)
# num_planes = out_planes
# only one DenseBlock
# self.dense4 = self._make_dense_layers(block, num_planes, nblocks[3])
# num_planes += nblocks[3] * growth_rate
# the last part is a linear layer as a classifier
self.bn = nn.BatchNorm2d(num_planes)
self.linear = nn.Linear(num_planes, num_classes)
def _make_dense_layers(self, block, in_planes, nblock):
layers = []
# number of non-linear transformations in one DenseBlock depends on the parameter you set
for i in range(nblock):
layers.append(block(in_planes, self.growth_rate))
in_planes += self.growth_rate
return nn.Sequential(*layers)
def forward(self, x):
img = self.generator(x)
out = self.conv1(img)
out = self.trans1(self.dense1(out))
out = self.trans2(self.dense2(out))
out = self.dense3(out)
out = nn.AdaptiveAvgPool2d((1,1))(F.relu(self.bn(out)))
out = out.view(out.size(0), -1)
# out = self.linear(out)
return out
def densenet():
return DenseNet(Bottleneck, [2, 2, 2])
class AlexNet(nn.Module):
def __init__(self,num_classes=5):
super(AlexNet,self).__init__()
self.generator = Generator()
self.layer1 = nn.Sequential( # 输入1*28*28
nn.Conv2d(3, 32, kernel_size=3, padding=1), # 32*28*28
nn.MaxPool2d(kernel_size=2, stride=2), # 32*14*14
nn.ReLU(inplace=True),
)
self.layer2 = nn.Sequential(
nn.Conv2d(32, 64, kernel_size=3, padding=1), # 64*14*14
nn.MaxPool2d(kernel_size=2, stride=2), # 64*7*7
nn.ReLU(inplace=True),
)
self.layer3 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, padding=1), # 128*7*7
)
self.layer4 = nn.Sequential(
nn.Conv2d(128, 256, kernel_size=3, padding=1), # 256*7*7
)
self.layer5 = nn.Sequential(
nn.Conv2d(256, 256, kernel_size=3, padding=1), # 256*7*7
nn.MaxPool2d(kernel_size=3, stride=2), # 256*3*3
nn.ReLU(inplace=True),
)
self.classifier = nn.Sequential(
nn.Linear(256 * 9, 256 * 4),
# nn.Linear(256 * 14, 256 * 10),
nn.ReLU(inplace=True),
nn.Dropout(0.3), # 0.3
nn.BatchNorm1d(256 * 4),
)
# self.fc1 = nn.Linear(256 * 3 * 3, 1024)
# self.fc2 = nn.Linear(1024, 512)
# self.fc3 = nn.Linear(512, 10)
def forward(self, x):
x = self.generator(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.layer5(x)
x = x.view(-1, 256 * 3 * 3)
x = self.classifier(x)
return x