-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
513 lines (461 loc) · 21.3 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
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
from enum import Enum
import hashlib
import math
import os
import random
import re
from chainmap import ChainMap
from torch.autograd import Variable
import librosa
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data as data
from manage_audio import AudioPreprocessor
class SimpleCache(dict):
def __init__(self, limit):
super().__init__()
self.limit = limit
self.n_keys = 0
def __setitem__(self, key, value):
if key in self.keys():
super().__setitem__(key, value)
elif self.n_keys < self.limit:
self.n_keys += 1
super().__setitem__(key, value)
return value
class ConfigType(Enum):
CNN_TRAD_POOL2 = "cnn-trad-pool2" # default full model (TF variant)
CNN_ONE_STRIDE1 = "cnn-one-stride1" # default compact model (TF variant)
CNN_ONE_FPOOL3 = "cnn-one-fpool3"
CNN_ONE_FSTRIDE4 = "cnn-one-fstride4"
CNN_ONE_FSTRIDE8 = "cnn-one-fstride8"
CNN_TPOOL2 = "cnn-tpool2"
CNN_TPOOL3 = "cnn-tpool3"
CNN_TSTRIDE2 = "cnn-tstride2"
CNN_TSTRIDE4 = "cnn-tstride4"
CNN_TSTRIDE8 = "cnn-tstride8"
RES15 = "res15"
RES26 = "res26"
RES8 = "res8"
RES15_NARROW = "res15-narrow"
RES8_NARROW = "res8-narrow"
RES26_NARROW = "res26-narrow"
CUSTOMIZED_MODEL = 'customized-model'
def find_model(conf):
if isinstance(conf, ConfigType):
conf = conf.value
if conf.startswith("res"):
return SpeechResModel
else:
return CustomizedModel
def find_config(conf):
if isinstance(conf, ConfigType):
conf = conf.value
return _configs[conf]
def truncated_normal(tensor, std_dev=0.01):
tensor.zero_()
tensor.normal_(std=std_dev)
while torch.sum(torch.abs(tensor) > 2 * std_dev) > 0:
t = tensor[torch.abs(tensor) > 2 * std_dev]
t.zero_()
tensor[torch.abs(tensor) > 2 * std_dev] = torch.normal(t, std=std_dev)
class SerializableModule(nn.Module):
def __init__(self):
super().__init__()
def save(self, filename):
torch.save(self.state_dict(), filename)
def load(self, filename):
self.load_state_dict(torch.load(filename, map_location=lambda storage, loc: storage))
class LMCL(nn.Module):
def __init__(self, embedding_size, num_classes, s=20, m=0.2):
super(LMCL, self).__init__()
self.embedding_size = embedding_size
self.num_classes = num_classes
self.s = s
self.m = m
self.weights = nn.Parameter(torch.Tensor(num_classes, embedding_size))
nn.init.kaiming_normal_(self.weights) # 权值初始化
def forward(self, embedding, label):
assert embedding.size(1) == self.embedding_size, 'embedding size wrong'
logits = F.linear(F.normalize(embedding), F.normalize(self.weights)) # 全连接层
margin = torch.zeros_like(logits) # size = [-1, num_classes]
margin.scatter_(1, label.view(-1, 1), self.m) # 0向量对应label的位置加上m
m_logits = self.s * (logits - margin) # 减去m,之后再做softmax
return logits, m_logits, self.s * F.normalize(embedding), F.normalize(self.weights) # 原始输出,lmcl输出,scaling后的input向量,标准化的权重矩阵
class CustomizedModel(SerializableModule):
def __init__(self, config):
super().__init__()
self.conv_1 = nn.Sequential(nn.Conv2d(in_channels=1, out_channels=19, kernel_size=(10, 5), stride=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(19))
self.max_pool2d_1 = nn.Sequential(nn.MaxPool2d(kernel_size=2, stride=2, padding=0))
self.conv_2 = nn.Sequential(nn.Conv2d(in_channels=19, out_channels=19, kernel_size=(5, 5), stride=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(19))
self.max_pool2d_2 = nn.Sequential(nn.MaxPool2d(kernel_size=2, stride=2, padding=0))
self.fc_1 = nn.Sequential(nn.Linear(in_features=2793, out_features=128),
nn.ReLU(inplace=True),
nn.BatchNorm1d(128))
self.lmcl = LMCL(128, 12)
def forward(self, x, grd_truth=None):
x = x.unsqueeze(1)
x = self.conv_1(x)
x = self.max_pool2d_1(x)
x = self.conv_2(x)
x = self.max_pool2d_2(x)
x = x.view(x.size(0), -1)
x = self.fc_1(x)
if grd_truth is None:
x = F.softmax(x)
return x
else:
x = self.lmcl(x)
x = F.softmax(x)
return x
class SpeechResModel(SerializableModule):
def __init__(self, config):
# 继承SerializableModule的属性
super().__init__()
n_labels = config["n_labels"]
n_maps = config["n_feature_maps"]
self.conv0 = nn.Conv2d(1, n_maps, (3, 3), padding=(1, 1), bias=False)
if "res_pool" in config:
self.pool = nn.AvgPool2d(config["res_pool"])
self.n_layers = n_layers = config["n_layers"]
dilation = config["use_dilation"]
if dilation:
self.convs = [nn.Conv2d(n_maps, n_maps, (3, 3), padding=int(2**(i // 3)), dilation=int(2**(i // 3)),
bias=False) for i in range(n_layers)]
else:
self.convs = [nn.Conv2d(n_maps, n_maps, (3, 3), padding=1, dilation=1,
bias=False) for _ in range(n_layers)]
for i, conv in enumerate(self.convs):
self.add_module("bn{}".format(i + 1), nn.BatchNorm2d(n_maps, affine=False))
self.add_module("conv{}".format(i + 1), conv)
self.output = nn.Sequential(nn.Linear(n_maps, n_labels))
def forward(self, x):
x = x.unsqueeze(1)
for i in range(self.n_layers + 1):
y = F.relu(getattr(self, "conv{}".format(i))(x))
if i == 0:
if hasattr(self, "pool"):
y = self.pool(y)
old_x = y
if i > 0 and i % 2 == 0:
x = y + old_x
old_x = x
else:
x = y
if i > 0:
x = getattr(self, "bn{}".format(i))(x)
# view()相当于reshape
x = x.view(x.size(0), x.size(1), -1) # shape: (batch, feats, o3)
# 在时间上取平均
x = torch.mean(x, 2)
# self.embedding = x
x = self.output(x)
return x
class SpeechModel(SerializableModule):
def __init__(self, config):
super().__init__()
n_labels = config["n_labels"]
n_featmaps1 = config["n_feature_maps1"]
conv1_size = config["conv1_size"] # (time, frequency)
conv1_pool = config["conv1_pool"]
conv1_stride = tuple(config["conv1_stride"])
dropout_prob = config["dropout_prob"]
width = config["width"]
height = config["height"]
self.conv1 = nn.Conv2d(1, n_featmaps1, conv1_size, stride=conv1_stride)
tf_variant = config.get("tf_variant")
self.tf_variant = tf_variant
if tf_variant:
truncated_normal(self.conv1.weight.data)
self.conv1.bias.data.zero_()
self.pool1 = nn.MaxPool2d(conv1_pool)
x = Variable(torch.zeros(1, 1, height, width), volatile=True)
x = self.pool1(self.conv1(x))
conv_net_size = x.view(1, -1).size(1)
last_size = conv_net_size
if "conv2_size" in config:
conv2_size = config["conv2_size"]
conv2_pool = config["conv2_pool"]
conv2_stride = tuple(config["conv2_stride"])
n_featmaps2 = config["n_feature_maps2"]
self.conv2 = nn.Conv2d(n_featmaps1, n_featmaps2, conv2_size, stride=conv2_stride)
if tf_variant:
truncated_normal(self.conv2.weight.data)
self.conv2.bias.data.zero_()
self.pool2 = nn.MaxPool2d(conv2_pool)
x = self.pool2(self.conv2(x))
conv_net_size = x.view(1, -1).size(1)
last_size = conv_net_size
if not tf_variant:
self.lin = nn.Linear(conv_net_size, 32)
if "dnn1_size" in config:
dnn1_size = config["dnn1_size"]
last_size = dnn1_size
if tf_variant:
self.dnn1 = nn.Linear(conv_net_size, dnn1_size)
truncated_normal(self.dnn1.weight.data)
self.dnn1.bias.data.zero_()
else:
self.dnn1 = nn.Linear(32, dnn1_size)
if "dnn2_size" in config:
dnn2_size = config["dnn2_size"]
last_size = dnn2_size
self.dnn2 = nn.Linear(dnn1_size, dnn2_size)
if tf_variant:
truncated_normal(self.dnn2.weight.data)
self.dnn2.bias.data.zero_()
self.output = nn.Linear(last_size, n_labels)
if tf_variant:
truncated_normal(self.output.weight.data)
self.output.bias.data.zero_()
self.dropout = nn.Dropout(dropout_prob)
def forward(self, x):
x = F.relu(self.conv1(x.unsqueeze(1))) # shape: (batch, channels, i1, o1)
x = self.dropout(x)
x = self.pool1(x)
if hasattr(self, "conv2"):
x = F.relu(self.conv2(x)) # shape: (batch, o1, i2, o2)
x = self.dropout(x)
x = self.pool2(x)
x = x.view(x.size(0), -1) # shape: (batch, o3)
if hasattr(self, "lin"):
x = self.lin(x)
if hasattr(self, "dnn1"):
x = self.dnn1(x)
if not self.tf_variant:
x = F.relu(x)
x = self.dropout(x)
if hasattr(self, "dnn2"):
x = self.dnn2(x)
x = self.dropout(x)
return self.output(x)
class DatasetType(Enum):
TRAIN = 0
DEV = 1
TEST = 2
class SpeechDataset(data.Dataset):
LABEL_SILENCE = "__silence__"
LABEL_UNKNOWN = "__unknown__"
def __init__(self, data, set_type, config):
super().__init__()
self.sample_rate = config['sample_rate']
self.audio_files = list(data.keys())
self.set_type = set_type
self.audio_labels = list(data.values())
config["bg_noise_files"] = list(filter(lambda x: x.endswith("wav"), config.get("bg_noise_files", [])))
self.bg_noise_audio = [librosa.core.load(file, sr=self.sample_rate)[0] for file in config["bg_noise_files"]]
self.unknown_prob = config["unknown_prob"]
self.silence_prob = config["silence_prob"]
self.noise_prob = config["noise_prob"]
self.input_length = config["input_length"]
self.timeshift_ms = config["timeshift_ms"]
self._audio_cache = SimpleCache(config["cache_size"])
self._file_cache = SimpleCache(config["cache_size"])
n_unk = len(list(filter(lambda x: x == 1, self.audio_labels)))
self.n_silence = int(self.silence_prob * (len(self.audio_labels) - n_unk))
self.audio_processor = AudioPreprocessor(n_mels=config["n_mels"], n_dct_filters=config["n_dct_filters"], hop_ms=10)
self.audio_preprocess_type = config["audio_preprocess_type"]
@staticmethod
def default_config():
config = {}
config["group_speakers_by_id"] = True
config["silence_prob"] = 0.1
config["noise_prob"] = 0.8
config["n_dct_filters"] = 40
config["input_length"] = 16000
config['sample_rate'] = 16000
config["n_mels"] = 40
config["timeshift_ms"] = 100
config["unknown_prob"] = 0.1
config["train_pct"] = 80
config["dev_pct"] = 10
config["test_pct"] = 10
config["wanted_words"] = ["yes","no","up","down","left","right","on","off","stop","go"]
config["data_folder"] = "/users/liuli/database/speech_commands/audio"
config["audio_preprocess_type"] = "MFCCs"
return config
def collate_fn(self, data):
x = None
y = []
for audio_data, label in data:
if self.audio_preprocess_type == "MFCCs": # 注意是MFCCs
audio_tensor = torch.from_numpy(self.audio_processor.compute_mfccs(audio_data).reshape(1, 101, 40))
# 将数据从numpy转换成tensor的形式,然后按列堆叠
x = audio_tensor if x is None else torch.cat((x, audio_tensor), 0)
elif self.audio_preprocess_type == "PCEN":
audio_tensor = torch.from_numpy(np.expand_dims(audio_data, axis=0))
audio_tensor = self.audio_processor.compute_pcen(audio_tensor)
x = audio_tensor if x is None else torch.cat((x, audio_tensor), 0)
else:
raise ValueError('Unknown preprocess mode "%s" (should be "MFCCs" or "PCEN")' % (self.audio_preprocess_type))
# y是一个label的list
y.append(label)
return x, torch.tensor(y)
def _timeshift_audio(self, data):
"""
语音data进行时间上的偏移
:param data:
:return:
"""
# 设置最大偏移量
shift = (16000 * self.timeshift_ms) // 1000
shift = random.randint(-shift, shift)
a = -min(0, shift)
b = max(0, shift)
data = np.pad(data, (a, b), "constant")
# 裁剪
return data[:len(data) - a] if a else data[b:]
def load_audio(self, example, silence=False):
"""
读取音频文件,执行偏移加噪
:param example: 缓存区的文件名
:param silence:
:return:
"""
if silence:
example = "__silence__"
if random.random() < 0.7:
try:
return self._audio_cache[example]
except KeyError:
pass
in_len = self.input_length # 16000
if self.bg_noise_audio:
bg_noise = random.choice(self.bg_noise_audio)
a = random.randint(0, len(bg_noise) - in_len - 1)
# 噪声偏移
bg_noise = bg_noise[a:a + in_len]
else:
bg_noise = np.zeros(in_len)
if silence:
data = np.zeros(in_len, dtype=np.float32)
else:
file_data = self._file_cache.get(example)
data = librosa.core.load(example, sr=self.sample_rate)[0] if file_data is None else file_data
self._file_cache[example] = data
# 给不足16000个点的data padding
data = np.pad(data, (0, max(0, in_len - len(data))), "constant")
if self.set_type == DatasetType.TRAIN:
data = self._timeshift_audio(data)
# 通过概率值来确定加噪声的文件比例
if random.random() < self.noise_prob or silence:
a = random.random() * 0.1
# 将信号值固定在[-1, 1]之间
data = np.clip(a * bg_noise + data, -1, 1)
self._audio_cache[example] = data
return data
@classmethod
def splits(cls, config):
folder = config["data_folder"]
wanted_words = config["wanted_words"]
unknown_prob = config["unknown_prob"]
train_pct = config["train_pct"]
dev_pct = config["dev_pct"]
test_pct = config["test_pct"]
words = {word: i + 2 for i, word in enumerate(wanted_words)}
words.update({cls.LABEL_SILENCE:0, cls.LABEL_UNKNOWN:1})
sets = [{}, {}, {}]
unknowns = [0] * 3 # [0, 0, 0]
bg_noise_files = []
unknown_files = []
for folder_name in os.listdir(folder):
# keyword目录
path_name = os.path.join(folder, folder_name)
is_bg_noise = False
# 如果是个文件则跳出循环
if os.path.isfile(path_name):
continue
# 不是文件,根据文件夹名字打上label
if folder_name in words:
label = words[folder_name]
elif folder_name == "_background_noise_":
is_bg_noise = True
else:
label = words[cls.LABEL_UNKNOWN]
# 遍历文件夹中的文件
for filename in os.listdir(path_name):
wav_name = os.path.join(path_name, filename)
# 将文件名的路径添加进对应的list中
if is_bg_noise and os.path.isfile(wav_name):
bg_noise_files.append(wav_name)
continue
elif label == words[cls.LABEL_UNKNOWN]:
unknown_files.append(wav_name)
continue
if config["group_speakers_by_id"]:
# 删去'_nohash_'在内和之后的str
hashname = re.sub(r"_nohash_.*$", "", filename)
max_no_wavs = 2**27 - 1 # 134217727
bucket = int(hashlib.sha1(hashname.encode()).hexdigest(), 16)
# 确定百分比
bucket = (bucket % (max_no_wavs + 1)) * (100. / max_no_wavs)
if bucket < dev_pct:
tag = DatasetType.DEV
elif bucket < test_pct + dev_pct:
tag = DatasetType.TEST
else:
tag = DatasetType.TRAIN
#
sets[tag.value][wav_name] = label # sets[0][wav_name] = label
# 确定每个集合中unknown的数量,占关键词数量的unknown_prob(10%)
for i in range(len(sets)):
unknowns[i] = int(unknown_prob * len(sets[i]))
random.shuffle(unknown_files)
a = 0
for i, dataset in enumerate(sets):
b = a + unknowns[i]
unk_dict = {u: words[cls.LABEL_UNKNOWN] for u in unknown_files[a:b]}
# 将unk_dict加入sets中
dataset.update(unk_dict)
a = b
train_cfg = ChainMap(dict(bg_noise_files=bg_noise_files), config)
test_cfg = ChainMap(dict(bg_noise_files=bg_noise_files, noise_prob=0), config)
datasets = (cls(sets[0], DatasetType.TRAIN, train_cfg), cls(sets[1], DatasetType.DEV, test_cfg),
cls(sets[2], DatasetType.TEST, test_cfg))
return datasets
def __getitem__(self, index):
if index >= len(self.audio_labels):
return self.load_audio(None, silence=True), 0
return self.load_audio(self.audio_files[index]), self.audio_labels[index]
def __len__(self):
return len(self.audio_labels) + self.n_silence
_configs = {
ConfigType.CNN_TRAD_POOL2.value: dict(dropout_prob=0.5, height=101, width=40, n_labels=4, n_feature_maps1=64,
n_feature_maps2=64, conv1_size=(20, 8), conv2_size=(10, 4), conv1_pool=(2, 2), conv1_stride=(1, 1),
conv2_stride=(1, 1), conv2_pool=(1, 1), tf_variant=True),
ConfigType.CNN_ONE_STRIDE1.value: dict(dropout_prob=0.5, height=101, width=40, n_labels=4, n_feature_maps1=186,
conv1_size=(101, 8), conv1_pool=(1, 1), conv1_stride=(1, 1), dnn1_size=128, dnn2_size=128, tf_variant=True),
ConfigType.CNN_TSTRIDE2.value: dict(dropout_prob=0.5, height=101, width=40, n_labels=4, n_feature_maps1=78,
n_feature_maps2=78, conv1_size=(16, 8), conv2_size=(9, 4), conv1_pool=(1, 3), conv1_stride=(2, 1),
conv2_stride=(1, 1), conv2_pool=(1, 1), dnn1_size=128, dnn2_size=128),
ConfigType.CNN_TSTRIDE4.value: dict(dropout_prob=0.5, height=101, width=40, n_labels=4, n_feature_maps1=100,
n_feature_maps2=78, conv1_size=(16, 8), conv2_size=(5, 4), conv1_pool=(1, 3), conv1_stride=(4, 1),
conv2_stride=(1, 1), conv2_pool=(1, 1), dnn1_size=128, dnn2_size=128),
ConfigType.CNN_TSTRIDE8.value: dict(dropout_prob=0.5, height=101, width=40, n_labels=4, n_feature_maps1=126,
n_feature_maps2=78, conv1_size=(16, 8), conv2_size=(5, 4), conv1_pool=(1, 3), conv1_stride=(8, 1),
conv2_stride=(1, 1), conv2_pool=(1, 1), dnn1_size=128, dnn2_size=128),
ConfigType.CNN_TPOOL2.value: dict(dropout_prob=0.5, height=101, width=40, n_labels=4, n_feature_maps1=94,
n_feature_maps2=94, conv1_size=(21, 8), conv2_size=(6, 4), conv1_pool=(2, 3), conv1_stride=(1, 1),
conv2_stride=(1, 1), conv2_pool=(1, 1), dnn1_size=128, dnn2_size=128),
ConfigType.CNN_TPOOL3.value: dict(dropout_prob=0.5, height=101, width=40, n_labels=4, n_feature_maps1=94,
n_feature_maps2=94, conv1_size=(15, 8), conv2_size=(6, 4), conv1_pool=(3, 3), conv1_stride=(1, 1),
conv2_stride=(1, 1), conv2_pool=(1, 1), dnn1_size=128, dnn2_size=128),
ConfigType.CNN_ONE_FPOOL3.value: dict(dropout_prob=0.5, height=101, width=40, n_labels=4, n_feature_maps1=54,
conv1_size=(101, 8), conv1_pool=(1, 3), conv1_stride=(1, 1), dnn1_size=128, dnn2_size=128),
ConfigType.CNN_ONE_FSTRIDE4.value: dict(dropout_prob=0.5, height=101, width=40, n_labels=4, n_feature_maps1=186,
conv1_size=(101, 8), conv1_pool=(1, 1), conv1_stride=(1, 4), dnn1_size=128, dnn2_size=128),
ConfigType.CNN_ONE_FSTRIDE8.value: dict(dropout_prob=0.5, height=101, width=40, n_labels=4, n_feature_maps1=336,
conv1_size=(101, 8), conv1_pool=(1, 1), conv1_stride=(1, 8), dnn1_size=128, dnn2_size=128),
ConfigType.RES15.value: dict(n_labels=12, use_dilation=True, n_layers=13, n_feature_maps=45),
ConfigType.RES8.value: dict(n_labels=12, n_layers=6, n_feature_maps=45, res_pool=(4, 3), use_dilation=False),
ConfigType.RES26.value: dict(n_labels=12, n_layers=24, n_feature_maps=45, res_pool=(2, 2), use_dilation=False),
ConfigType.RES15_NARROW.value: dict(n_labels=12, use_dilation=True, n_layers=13, n_feature_maps=19),
ConfigType.RES8_NARROW.value: dict(n_labels=12, n_layers=6, n_feature_maps=19, res_pool=(4, 3), use_dilation=False),
ConfigType.RES26_NARROW.value: dict(n_labels=12, n_layers=24, n_feature_maps=19, res_pool=(2, 2), use_dilation=False)}
print(_configs)