-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
152 lines (117 loc) · 4.51 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
"""
@FileName: model.py
@Author: Chenghong Xiao
"""
import math
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from utils import ComplexConv1d, ComplexBatchNorm, CReLU, ComplexAvgPool2d, ComplexLinear, ComplexMaxPool1d
from CDSC import CDSC1d
class Block(nn.Module):
def __init__(self, in_filters, out_filters, reps, strides=1, start_with_relu=True, grow_first=True):
super(Block, self).__init__()
if out_filters != in_filters or strides != 1:
self.skip = ComplexConv1d(in_filters, out_filters, 1, stride=strides, bias=False)
self.skipbn = ComplexBatchNorm(out_filters)
else:
self.skip = None
self.relu = CReLU()
rep = []
filters = in_filters
if grow_first:
rep.append(self.relu)
rep.append(CDSC1d(in_filters, out_filters, 3, stride=1, padding=1, bias=False))
rep.append(ComplexBatchNorm(out_filters))
filters = out_filters
for i in range(reps - 1):
rep.append(self.relu)
rep.append(CDSC1d(filters, filters, 3, stride=1, padding=1, bias=False))
rep.append(ComplexBatchNorm(filters))
if not grow_first:
rep.append(self.relu)
rep.append(CDSC1d(in_filters, out_filters, 3, stride=1, padding=1, bias=False))
rep.append(ComplexBatchNorm(out_filters))
if not start_with_relu:
rep = rep[1:]
else:
rep[0] = CReLU()
if strides != 1:
rep.append(ComplexMaxPool1d(3, strides, 1))
self.rep = nn.Sequential(*rep)
def forward(self, inp):
x = self.rep(inp)
x = x.cuda()
if self.skip is not None:
skip = self.skip(inp)
skip = self.skipbn(skip)
else:
skip = inp
skip = skip.cuda()
x += skip
return x
class CDSCNN(nn.Module):
"""
Complex-Valued Depthwise Separable Convolutional Neural Network (CDSCNN)
"""
def __init__(self, num_classes):
""" Initialize a CDSCNN
Args:
num_classes (int): the number of classes
"""
super(CDSCNN, self).__init__()
self.num_classes = num_classes
self.conv1 = ComplexConv1d(2, 16, 3, 2, 0, bias=False)
self.bn1 = ComplexBatchNorm(16)
self.relu = CReLU()
self.conv2 = ComplexConv1d(16, 32, 3, bias=False)
self.bn2 = ComplexBatchNorm(32)
self.block1 = Block(32, 64, 2, 2, start_with_relu=False, grow_first=True)
self.block2 = Block(64, 128, 2, 2, start_with_relu=True, grow_first=True)
self.block3 = Block(128, 128, 3, 1, start_with_relu=True, grow_first=True)
self.block4 = Block(128, 128, 3, 1, start_with_relu=True, grow_first=True)
self.block5 = Block(128, 128, 3, 1, start_with_relu=True, grow_first=True)
self.block6 = Block(128, 128, 3, 1, start_with_relu=True, grow_first=True)
self.block7 = Block(128, 128, 3, 1, start_with_relu=True, grow_first=True)
self.block8 = Block(128, 128, 3, 1, start_with_relu=True, grow_first=True)
self.block9 = Block(128, 128, 3, 1, start_with_relu=True, grow_first=True)
self.block10 = Block(128, 256, 2, 2, start_with_relu=True, grow_first=False)
self.conv3 = CDSC1d(256, 512, 3, 1, 1)
self.bn3 = ComplexBatchNorm(512)
self.avgpool = ComplexAvgPool2d((1, 1))
self.fc = ComplexLinear(512, self.num_classes)
# ------- init weights --------
for m in self.modules():
# print(m, flush=True)
if isinstance(m, nn.Conv1d):
n = m.kernel_size[0] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm1d):
m.weight.data.fill_(1)
m.bias.data.zero_()
# -----------------------------
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.conv2(x)
x = self.bn2(x)
x = self.relu(x)
x = self.block1(x)
x = self.block2(x)
x = self.block3(x)
x = self.block4(x)
x = self.block5(x)
x = self.block6(x)
x = self.block7(x)
x = self.block8(x)
x = self.block9(x)
x = self.block10(x)
x = self.conv3(x)
x = self.bn3(x)
x = self.relu(x)
x = x.view(x.size(0), x.size(1), x.size(2), 1)
x = self.avgpool(x)
x = self.fc(x)
return x