-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
299 lines (263 loc) · 13.3 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
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
import torch;
import torch.nn as nn;
import numpy as np;
import random;
#Reproducibility
seed = 42;
random.seed(seed);
np.random.seed(seed);
torch.manual_seed(seed);
if torch.cuda.is_available():
torch.cuda.manual_seed(seed);
torch.backends.cudnn.deterministic = True;
torch.backends.cudnn.benchmark = False;
###########################################
class ACDNet(nn.Module):
def __init__(self, input_length, n_class, sr, ch_conf=None):
super(ACDNet, self).__init__();
self.input_length = input_length;
self.ch_config = ch_conf;
stride1 = 2;
stride2 = 2;
channels = 8;
k_size = (3, 3);
n_frames = (sr/1000)*10; #No of frames per 10ms
sfeb_pool_size = int(n_frames/(stride1*stride2));
tfeb_pool_size = (2,2);
if self.ch_config is None:
self.ch_config = [channels, channels*8, channels*4, channels*8, channels*8, channels*16, channels*16, channels*32, channels*32, channels*64, channels*64, n_class];
avg_pool_kernel_size = (1,4) if self.ch_config[1] < 64 else (2,4);
fcn_no_of_inputs = self.ch_config[-1];
conv1, bn1 = self.make_layers(1, self.ch_config[0], (1, 9), (1, stride1));
conv2, bn2 = self.make_layers(self.ch_config[0], self.ch_config[1], (1, 5), (1, stride2));
conv3, bn3 = self.make_layers(1, self.ch_config[2], k_size, padding=1);
conv4, bn4 = self.make_layers(self.ch_config[2], self.ch_config[3], k_size, padding=1);
conv5, bn5 = self.make_layers(self.ch_config[3], self.ch_config[4], k_size, padding=1);
conv6, bn6 = self.make_layers(self.ch_config[4], self.ch_config[5], k_size, padding=1);
conv7, bn7 = self.make_layers(self.ch_config[5], self.ch_config[6], k_size, padding=1);
conv8, bn8 = self.make_layers(self.ch_config[6], self.ch_config[7], k_size, padding=1);
conv9, bn9 = self.make_layers(self.ch_config[7], self.ch_config[8], k_size, padding=1);
conv10, bn10 = self.make_layers(self.ch_config[8], self.ch_config[9], k_size, padding=1);
conv11, bn11 = self.make_layers(self.ch_config[9], self.ch_config[10], k_size, padding=1);
conv12, bn12 = self.make_layers(self.ch_config[10], self.ch_config[11], (1, 1));
fcn = nn.Linear(fcn_no_of_inputs, n_class);
nn.init.kaiming_normal_(fcn.weight, nonlinearity='sigmoid') # kaiming with sigoid is equivalent to lecun_normal in keras
self.sfeb = nn.Sequential(
#Start: Filter bank
conv1, bn1, nn.ReLU(),\
conv2, bn2, nn.ReLU(),\
nn.MaxPool2d(kernel_size=(1, sfeb_pool_size))
);
self.tfeb = nn.Sequential(
conv3, bn3, nn.ReLU(), nn.MaxPool2d(kernel_size=tfeb_pool_size),\
conv4, bn4, nn.ReLU(),\
conv5, bn5, nn.ReLU(), nn.MaxPool2d(kernel_size=tfeb_pool_size),\
conv6, bn6, nn.ReLU(),\
conv7, bn7, nn.ReLU(), nn.MaxPool2d(kernel_size=tfeb_pool_size),\
conv8, bn8, nn.ReLU(),\
conv9, bn9, nn.ReLU(), nn.MaxPool2d(kernel_size=tfeb_pool_size),\
conv10, bn10, nn.ReLU(),\
conv11, bn11, nn.ReLU(), nn.MaxPool2d(kernel_size=tfeb_pool_size),\
nn.Dropout(0.2),\
conv12, bn12, nn.ReLU(), nn.AvgPool2d(kernel_size = avg_pool_kernel_size),\
nn.Flatten(),\
fcn
)
self.output = nn.Sequential(
nn.Softmax(dim=1)
);
def forward(self, x):
x = self.sfeb(x);
#swapaxes
x = x.permute((0, 2, 1, 3));
x = self.tfeb(x);
y = self.output[0](x);
return y;
def make_layers(self, in_channels, out_channels, kernel_size, stride=(1,1), padding=0, bias=False):
conv = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=bias);
nn.init.kaiming_normal_(conv.weight, nonlinearity='relu'); # kaiming with relu is equivalent to he_normal in keras
bn = nn.BatchNorm2d(out_channels);
return conv, bn;
def GetACDNetModel(input_len=66650, nclass=50, sr=44100, channel_config=None):
net = ACDNet(input_len, nclass, sr, ch_conf=channel_config);
return net;
#quantization:
from torch.quantization import QuantStub, DeQuantStub
class ACDNetQuant(nn.Module):
def __init__(self, input_length, n_class, sr, ch_conf=None):
super(ACDNetQuant, self).__init__();
self.input_length = input_length;
self.ch_config = ch_conf;
stride1 = 2;
stride2 = 2;
channels = 8;
k_size = (3, 3);
n_frames = (sr/1000)*10; #No of frames per 10ms
sfeb_pool_size = int(n_frames/(stride1*stride2));
pool_size = (2,2);
if self.ch_config is None:
self.ch_config = [channels, channels*8, channels*4, channels*8, channels*8, channels*16, channels*16, channels*32, channels*32, channels*64, channels*64, n_class];
avg_pool_kernel_size = (1,4) if self.ch_config[1] < 64 else (2,4);
fcn_no_of_inputs = self.ch_config[-1];
conv1, bn1 = self.make_layers(1, self.ch_config[0], (1, 9), (1, stride1));
conv2, bn2 = self.make_layers(self.ch_config[0], self.ch_config[1], (1, 5), (1, stride2));
conv3, bn3 = self.make_layers(1, self.ch_config[2], k_size, padding=1);
conv4, bn4 = self.make_layers(self.ch_config[2], self.ch_config[3], k_size, padding=1);
conv5, bn5 = self.make_layers(self.ch_config[3], self.ch_config[4], k_size, padding=1);
conv6, bn6 = self.make_layers(self.ch_config[4], self.ch_config[5], k_size, padding=1);
conv7, bn7 = self.make_layers(self.ch_config[5], self.ch_config[6], k_size, padding=1);
conv8, bn8 = self.make_layers(self.ch_config[6], self.ch_config[7], k_size, padding=1);
conv9, bn9 = self.make_layers(self.ch_config[7], self.ch_config[8], k_size, padding=1);
conv10, bn10 = self.make_layers(self.ch_config[8], self.ch_config[9], k_size, padding=1);
conv11, bn11 = self.make_layers(self.ch_config[9], self.ch_config[10], k_size, padding=1);
conv12, bn12 = self.make_layers(self.ch_config[10], self.ch_config[11], (1, 1));
fcn = nn.Linear(fcn_no_of_inputs, n_class);
nn.init.kaiming_normal_(fcn.weight, nonlinearity='sigmoid') # kaiming with sigoid is equivalent to lecun_normal in keras
self.sfeb = nn.Sequential(
#Start: Filter bank
conv1, bn1, nn.ReLU(),\
conv2, bn2, nn.ReLU(),\
nn.MaxPool2d(kernel_size=(1, sfeb_pool_size))
);
self.tfeb = nn.Sequential(
conv3, bn3, nn.ReLU(), nn.MaxPool2d(kernel_size=(2,2)),\
conv4, bn4, nn.ReLU(),\
conv5, bn5, nn.ReLU(), nn.MaxPool2d(kernel_size=(2,2)),\
conv6, bn6, nn.ReLU(),\
conv7, bn7, nn.ReLU(), nn.MaxPool2d(kernel_size=(2,2)),\
conv8, bn8, nn.ReLU(),\
conv9, bn9, nn.ReLU(), nn.MaxPool2d(kernel_size=(2,2)),\
conv10, bn10, nn.ReLU(),\
conv11, bn11, nn.ReLU(), nn.MaxPool2d(kernel_size=(2,2)),\
nn.Dropout(0.2),\
conv12, bn12, nn.ReLU(), nn.AvgPool2d(kernel_size = avg_pool_kernel_size),\
nn.Flatten(),\
fcn
)
self.output = nn.Sequential(
nn.Softmax(dim=1)
);
self.quant = QuantStub();
self.dequant = DeQuantStub();
def forward(self, x):
#Quantize input
x = self.quant(x);
x = self.sfeb(x);
#swapaxes
x = x.permute((0, 2, 1, 3));
x = self.tfeb(x);
#DeQuantize features before feeding to softmax
x = self.dequant(x);
y = self.output[0](x);
return y;
def make_layers(self, in_channels, out_channels, kernel_size, stride=(1,1), padding=0, bias=False):
conv = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=bias);
nn.init.kaiming_normal_(conv.weight, nonlinearity='relu'); # kaiming with relu is equivalent to he_normal in keras
bn = nn.BatchNorm2d(out_channels);
return conv, bn;
def GetACDNetQuantModel(input_len=66650, nclass=50, sr=44100, channel_config=None):
net = ACDNetQuant(input_len, nclass, sr, ch_conf=channel_config);
return net;
class ACDNetV2(nn.Module):
def __init__(self, input_length, n_class, sr, ch_conf=None):
super(ACDNetV2, self).__init__();
self.input_length = input_length;
self.ch_config = ch_conf;
stride1 = 2;
stride2 = 2;
channels = 8;
k_size = (3, 3);
n_frames = (sr/1000)*10; #No of frames per 10ms
sfeb_pool_size = int(n_frames/(stride1*stride2));
tfeb_pool_size = (2,2);
if self.ch_config is None:
self.ch_config = [channels, channels*8, channels*4, channels*8, channels*8, channels*16, channels*16, channels*32, channels*32, channels*64, channels*64, n_class];
avg_pool_kernel_size = (1,4) if self.ch_config[1] < 64 else (2,4);
fcn_no_of_inputs = self.ch_config[-1];
conv1, bn1 = self.make_layers(1, self.ch_config[0], (1, 9), (1, stride1));
conv2, bn2 = self.make_layers(self.ch_config[0], self.ch_config[1], (1, 5), (1, stride2));
conv3, bn3 = self.make_layers(1, self.ch_config[2], k_size, padding=1);
conv4, bn4 = self.make_layers(self.ch_config[2], self.ch_config[3], k_size, padding=1);
conv5, bn5 = self.make_layers(self.ch_config[3], self.ch_config[4], k_size, padding=1);
conv6, bn6 = self.make_layers(self.ch_config[4], self.ch_config[5], k_size, padding=1);
conv7, bn7 = self.make_layers(self.ch_config[5], self.ch_config[6], k_size, padding=1);
conv8, bn8 = self.make_layers(self.ch_config[6], self.ch_config[7], k_size, padding=1);
conv9, bn9 = self.make_layers(self.ch_config[7], self.ch_config[8], k_size, padding=1);
conv10, bn10 = self.make_layers(self.ch_config[8], self.ch_config[9], k_size, padding=1);
conv11, bn11 = self.make_layers(self.ch_config[9], self.ch_config[10], k_size, padding=1);
conv12, bn12 = self.make_layers(self.ch_config[10], self.ch_config[11], (1, 1));
fcn = nn.Linear(fcn_no_of_inputs, n_class);
nn.init.kaiming_normal_(fcn.weight, nonlinearity='sigmoid') # kaiming with sigoid is equivalent to lecun_normal in keras
self.sfeb = nn.Sequential(
#Start: Filter bank
conv1, bn1, nn.ReLU(),\
conv2, bn2, nn.ReLU(),\
nn.MaxPool2d(kernel_size=(1, sfeb_pool_size))
);
tfeb_modules = [];
tfeb_w = int(((self.input_length / sr)*1000)/10); # 10ms frames of audio length in seconds
tfeb_pool_sizes = self.get_tfeb_pool_sizes(self.ch_config[1], tfeb_w);
print(tfeb_pool_sizes)
p_index = 0;
for i in [3,4,6,8,10]:
tfeb_modules.extend([eval('conv{}'.format(i)), eval('bn{}'.format(i)), nn.ReLU()]);
if i != 3:
tfeb_modules.extend([eval('conv{}'.format(i+1)), eval('bn{}'.format(i+1)), nn.ReLU()]);
h, w = tfeb_pool_sizes[p_index];
if h>1 or w>1:
tfeb_modules.append(nn.MaxPool2d(kernel_size = (h,w)));
p_index += 1;
tfeb_modules.append(nn.Dropout(0.2));
tfeb_modules.extend([conv12, bn12, nn.ReLU()]);
h, w = tfeb_pool_sizes[-1];
if h>1 or w>1:
tfeb_modules.append(nn.AvgPool2d(kernel_size = (h,w)));
tfeb_modules.extend([nn.Flatten(), fcn]);
self.tfeb = nn.Sequential(*tfeb_modules);
self.output = nn.Sequential(
nn.Softmax(dim=1)
);
def forward(self, x):
x = self.sfeb(x);
#swapaxes
x = x.permute((0, 2, 1, 3));
x = self.tfeb(x);
y = self.output[0](x);
return y;
def make_layers(self, in_channels, out_channels, kernel_size, stride=(1,1), padding=0, bias=False):
conv = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=bias);
nn.init.kaiming_normal_(conv.weight, nonlinearity='relu'); # kaiming with relu is equivalent to he_normal in keras
bn = nn.BatchNorm2d(out_channels);
return conv, bn;
def get_tfeb_pool_sizes(self, con2_ch, width):
h = self.get_tfeb_pool_size_component(con2_ch);
w = self.get_tfeb_pool_size_component(width);
pool_size = [];
for (h1, w1) in zip(h, w):
pool_size.append((h1, w1));
return pool_size;
def get_tfeb_pool_size_component(self, length):
c = [];
index = 1;
while index <= 6:
if length >= 2:
if index == 6:
c.append(length);
else:
c.append(2);
length = length // 2;
else:
c.append(1);
index += 1;
return c;
def GetACDNetModelV2(input_len=66650, nclass=50, sr=44100, channel_config=None):
net = ACDNetV2(input_len, nclass, sr, ch_conf=channel_config);
return net;
# if __name__ == '__main__':
# ch_config = [8, 4, 32, 64, 64, 128, 128, 256, 256, 512, 512, 50];
# input_len = 4000;
# sr = 16000;
# net = GetACDNetModelV2(input_len, 50, sr, ch_config);
# import calculator as calc;
# calc.summary(net, (1,1,input_len));
# print(net.modules);