-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Part three
245 lines (203 loc) · 8.99 KB
/
Part three
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
~.~.~.~.~.~.~.~.~.~.~.~.~.~
requierment that we need in first step are:
asteroid-filterbanks >=0.4,<0.5
backports.cached_property
einops >=0.3,<0.4.0
hmmlearn >=0.2.7,<0.3
huggingface_hub >= 0.8.1
networkx >= 2.6,<3.0
omegaconf >=2.1,<3.0
pyannote.core >=4.4,<5.0
pyannote.database >=4.1.1,<5.0
pyannote.metrics >=3.2,<4.0
pyannote.pipeline >=2.3,<3.0
pytorch_lightning >=1.5.4,<1.7
pytorch_metric_learning >=1.0.0,<2.0
rich >= 12.0.0
semver >=2.10.2,<3.0
singledispatchmethod
soundfile >=0.10.2,<0.12
speechbrain >=0.5.12,<0.6
torch >=1.9
torch_audiomentations >= 0.11.0
torchaudio >=0.10,<1.0
torchmetrics >=0.11,<1.0
typing_extensions
######## Change source code:
import pytorch-lightning
import Prodigy
import torch-audiomentations
# -*- coding: utf-8 -*-
# !/usr/bin/env python
import tensorflow as tf
from tensorpack.graph_builder.model_desc import ModelDesc, InputDesc
from tensorpack.tfutils import (
get_current_tower_context, optimizer, gradproc)
from tensorpack.tfutils.scope_utils import auto_reuse_variable_scope
import tensorpack_extension
from data_load import phns
from hparam import hparam as hp
from modules import prenet, cbhg, normalize
class Net1(ModelDesc):
def __init__(self):
pass
def _get_inputs(self):
return [InputDesc(tf.float32, (None, None, hp.default.n_mfcc), 'x_mfccs'),
InputDesc(tf.int32, (None, None,), 'y_ppgs')]
def _build_graph(self, inputs):
self.x_mfccs, self.y_ppgs = inputs
is_training = get_current_tower_context().is_training
with tf.variable_scope('net1'):
self.ppgs, self.preds, self.logits = self.network(self.x_mfccs, is_training)
self.cost = self.loss()
acc = self.acc()
# summaries
tf.summary.scalar('net1/train/loss', self.cost)
tf.summary.scalar('net1/train/acc', acc)
if not is_training:
# summaries
tf.summary.scalar('net1/eval/summ_loss', self.cost)
tf.summary.scalar('net1/eval/summ_acc', acc)
# for confusion matrix
tf.reshape(self.y_ppgs, shape=(tf.size(self.y_ppgs),), name='net1/eval/y_ppg_1d')
tf.reshape(self.preds, shape=(tf.size(self.preds),), name='net1/eval/pred_ppg_1d')
def _get_optimizer(self):
lr = tf.get_variable('learning_rate', initializer=hp.train1.lr, trainable=False)
return tf.train.AdamOptimizer(lr)
@auto_reuse_variable_scope
def network(self, x_mfcc, is_training):
# Pre-net
prenet_out = prenet(x_mfcc,
num_units=[hp.train1.hidden_units, hp.train1.hidden_units // 2],
dropout_rate=hp.train1.dropout_rate,
is_training=is_training) # (N, T, E/2)
# CBHG
out = cbhg(prenet_out, hp.train1.num_banks, hp.train1.hidden_units // 2,
hp.train1.num_highway_blocks, hp.train1.norm_type, is_training)
# Final linear projection
logits = tf.layers.dense(out, len(phns)) # (N, T, V)
ppgs = tf.nn.softmax(logits / hp.train1.t, name='ppgs') # (N, T, V)
preds = tf.to_int32(tf.argmax(logits, axis=-1)) # (N, T)
return ppgs, preds, logits
def loss(self):
istarget = tf.sign(tf.abs(tf.reduce_sum(self.x_mfccs, -1))) # indicator: (N, T)
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.logits / hp.train1.t,
labels=self.y_ppgs)
loss *= istarget
loss = tf.reduce_mean(loss)
return loss
def acc(self):
istarget = tf.sign(tf.abs(tf.reduce_sum(self.x_mfccs, -1))) # indicator: (N, T)
num_hits = tf.reduce_sum(tf.to_float(tf.equal(self.preds, self.y_ppgs)) * istarget)
num_targets = tf.reduce_sum(istarget)
acc = num_hits / num_targets
return acc
class Net2(ModelDesc):
def _get_inputs(self):
n_timesteps = (hp.default.duration * hp.default.sr) // hp.default.hop_length + 1
return [InputDesc(tf.float32, (None, n_timesteps, hp.default.n_mfcc), 'x_mfccs'),
InputDesc(tf.float32, (None, n_timesteps, hp.default.n_fft // 2 + 1), 'y_spec'),
InputDesc(tf.float32, (None, n_timesteps, hp.default.n_mels), 'y_mel'), ]
def _build_graph(self, inputs):
self.x_mfcc, self.y_spec, self.y_mel = inputs
is_training = get_current_tower_context().is_training
# build net1
self.net1 = Net1()
with tf.variable_scope('net1'):
self.ppgs, _, _ = self.net1.network(self.x_mfcc, is_training)
self.ppgs = tf.identity(self.ppgs, name='ppgs')
# build net2
with tf.variable_scope('net2'):
self.pred_spec, self.pred_mel = self.network(self.ppgs, is_training)
self.pred_spec = tf.identity(self.pred_spec, name='pred_spec')
self.cost = self.loss()
# summaries
tf.summary.scalar('net2/train/loss', self.cost)
if not is_training:
tf.summary.scalar('net2/eval/summ_loss', self.cost)
def _get_optimizer(self):
gradprocs = [
tensorpack_extension.FilterGradientVariables('.*net2.*', verbose=False),
gradproc.MapGradient(
lambda grad: tf.clip_by_value(grad, hp.train2.clip_value_min, hp.train2.clip_value_max)),
gradproc.GlobalNormClip(hp.train2.clip_norm),
# gradproc.PrintGradient(),
# gradproc.CheckGradient(),
]
lr = tf.get_variable('learning_rate', initializer=hp.train2.lr, trainable=False)
opt = tf.train.AdamOptimizer(learning_rate=lr)
return optimizer.apply_grad_processors(opt, gradprocs)
@auto_reuse_variable_scope
def network(self, ppgs, is_training):
# Pre-net
prenet_out = prenet(ppgs,
num_units=[hp.train2.hidden_units, hp.train2.hidden_units // 2],
dropout_rate=hp.train2.dropout_rate,
is_training=is_training) # (N, T, E/2)
# CBHG1: mel-scale
pred_mel = cbhg(prenet_out, hp.train2.num_banks, hp.train2.hidden_units // 2,
hp.train2.num_highway_blocks, hp.train2.norm_type, is_training,
scope="cbhg_mel")
pred_mel = tf.layers.dense(pred_mel, self.y_mel.shape[-1], name='pred_mel') # (N, T, n_mels)
# CBHG2: linear-scale
pred_spec = tf.layers.dense(pred_mel, hp.train2.hidden_units // 2) # (N, T, n_mels)
pred_spec = cbhg(pred_spec, hp.train2.num_banks, hp.train2.hidden_units // 2,
hp.train2.num_highway_blocks, hp.train2.norm_type, is_training, scope="cbhg_linear")
pred_spec = tf.layers.dense(pred_spec, self.y_spec.shape[-1], name='pred_spec') # log magnitude: (N, T, 1+n_fft//2)
return pred_spec, pred_mel
def loss(self):
loss_spec = tf.reduce_mean(tf.squared_difference(self.pred_spec, self.y_spec))
loss_mel = tf.reduce_mean(tf.squared_difference(self.pred_mel, self.y_mel))
loss = loss_spec + loss_mel
return loss
# for speechbrain
!pip install -qq torch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 torchtext==0.12.0
!pip install -qq speechbrain==0.5.12
# pyannote.audio
!pip install -qq pyannote.audio
# for visualization purposes
!pip install -qq ipython==7.34.0
!wget -q http://groups.inf.ed.ac.uk/ami/AMICorpusMirror/amicorpus/ES2004a/audio/ES2004a.Mix-Headset.wav
DEMO_FILE = {'uri': 'ES2004a.Mix-Headset', 'audio': 'ES2004a.Mix-Headset.wav'}
# load groundtruth
from pyannote.database.util import load_rttm
_, groundtruth = load_rttm('ES2004a.rttm').popitem()
# visualize groundtruth
groundtruth
from pyannote.core import Segment, notebook
# make notebook visualization zoom on 600s < t < 660s time range
EXCERPT = Segment(600, 660)
notebook.crop = EXCERPT
# visualize excerpt groundtruth
groundtruth
from pyannote.audio import Audio
from IPython.display import Audio as IPythonAudio
waveform, sr = Audio().crop(DEMO_FILE, EXCERPT)
IPythonAudio(waveform.flatten(), rate=sr)
import google.colab
own_file, _ = google.colab.files.upload().popitem()
OWN_FILE = {'audio': own_file}
notebook.reset()
# load audio waveform and play it
waveform, sample_rate = Audio()(OWN_FILE)
IPythonAudio(data=waveform.squeeze(), rate=sample_rate, autoplay=True)
groundtruth_rttm, _ = google.colab.files.upload().popitem()
groundtruths = load_rttm(groundtruth_rttm)
if OWN_FILE['audio'] in groundtruths:
groundtruth = groundtruths[OWN_FILE['audio']]
else:
_, groundtruth = groundtruths.popitem()
groundtruth
from huggingface_hub import notebook_login
notebook_login()
from pyannote.audio import Pipeline
pipeline = Pipeline.from_pretrained('pyannote/speaker-diarization', use_auth_token=True)
diarization = pipeline(DEMO_FILE)
diarization
from pyannote.metrics.diarization import DiarizationErrorRate
metric = DiarizationErrorRate()
der = metric(groundtruth, diarization)
print(f'diarization error rate = {100 * der:.1f}%')
mapping = metric.optimal_mapping(groundtruth, diarization)
diarization.rename_labels(mapping=mapping)
groundtruth