-
Notifications
You must be signed in to change notification settings - Fork 6
/
models.py
236 lines (186 loc) · 7.18 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
import tensorflow as tf
import tensorflow.contrib.slim as slim
from audio_nets import tenet, tc_resnet
_available_nets = [
"TENet6Model",
"TENet12Model",
"TENet6NarrowModel",
"TENet12NarrowModel",
"TCResNet8Model",
"TCResNet14Model",
]
def _log_mel_spectrogram(audio, **kwargs):
# only accept single channels
audio = tf.squeeze(audio, -1)
stfts = tf.contrib.signal.stft(audio,
frame_length=kwargs["window_size_samples"],
frame_step=kwargs["window_stride_samples"])
spectrograms = tf.math.real(stfts * tf.math.conj(stfts))
num_spectrogram_bins = spectrograms.shape[-1].value
linear_to_mel_weight_matrix = tf.contrib.signal.linear_to_mel_weight_matrix(
kwargs["num_mel_bins"],
num_spectrogram_bins,
kwargs["sample_rate"],
kwargs["lower_edge_hertz"],
kwargs["upper_edge_hertz"],
)
mel_spectrograms = tf.tensordot(
spectrograms, linear_to_mel_weight_matrix, 1)
mel_spectrograms.set_shape(
spectrograms.shape[:-
1].concatenate(linear_to_mel_weight_matrix.shape[-1:])
)
log_offset = 1e-6
log_mel_spectrograms = tf.math.log(mel_spectrograms + log_offset)
return log_mel_spectrograms
def preprocess_mfcc(inputs, **kwargs):
log_mel_spectrograms = _log_mel_spectrogram(inputs,
**kwargs)
mfccs = tf.contrib.signal.mfccs_from_log_mel_spectrograms(
log_mel_spectrograms)
mfccs = mfccs[..., :kwargs["num_mfccs"]]
mfccs = tf.expand_dims(mfccs, axis=-1)
return mfccs
class AudioNetModel:
def __init__(self, args):
self.args = args
self.weight_decay = self.args.weight_decay
self.dropout_keep_prob = 1 - self.args.dropout
self.width_multiplier = self.args.width_multiplier
self.scope_name = self.args.scope_name
def build(self, wavs, labels, is_training):
self.audio = preprocess_mfcc(wavs, **vars(self.args))
self.labels = labels
self.is_training = is_training
self.logits = self.build_inference(self.audio, is_training=is_training)
self.model_variables, self.model_l2_variables = self.get_variables(
self.scope_name
)
self.total_loss, self.model_loss = self.build_loss(
self.logits, self.labels
)
self.acc = self.build_acc(self.logits, self.labels)
def get_variables(self, scope=''):
def exclude_batch_norm(name):
return ("batch_normalization" not in name) and ("BatchNorm" not in name)
def include_model_scope(name, scope):
if scope == '':
return True
return name.split(':')[0].split('/')[0].strip() == scope
model_variables = [v for v in slim.get_variables_to_restore(
) if include_model_scope(v.name, scope)]
model_l2_variables = [v for v in tf.compat.v1.trainable_variables(
) if exclude_batch_norm(v.name) and include_model_scope(v.name, scope)]
return model_variables, model_l2_variables
def build_loss(self, logits, labels):
model_loss = tf.compat.v1.losses.sparse_softmax_cross_entropy(
labels=labels, logits=logits,
)
l2_loss = self.args.weight_decay * tf.add_n(
[tf.nn.l2_loss(tf.cast(v, tf.float32))
for v in self.model_l2_variables]
) if self.model_l2_variables != [] else 0
total_loss = model_loss + l2_loss
return total_loss, model_loss
def build_acc(self, logits, labels):
self.preds = tf.math.argmax(logits, -1)
acc = tf.reduce_mean(tf.cast(tf.equal(labels, self.preds), tf.float32))
return acc
def build_inference(self, inputs, is_training=True):
raise NotImplementedError
class TCResNet8Model(AudioNetModel):
def __init__(self, args):
super().__init__(args)
def build_inference(self, inputs, is_training):
with slim.arg_scope(tc_resnet.TCResNet_arg_scope(
is_training=is_training,
weight_decay=self.weight_decay,
keep_prob=self.dropout_keep_prob)
):
logits = tc_resnet.TCResNet8(
inputs,
self.args.num_classes,
width_multiplier=self.width_multiplier,
scope=self.scope_name
)
return logits
class TCResNet14Model(AudioNetModel):
def __init__(self, args):
super().__init__(args)
def build_inference(self, inputs, is_training):
with slim.arg_scope(tc_resnet.TCResNet_arg_scope(
is_training=is_training,
weight_decay=self.weight_decay,
keep_prob=self.dropout_keep_prob)
):
logits = tc_resnet.TCResNet14(
inputs,
self.args.num_classes,
width_multiplier=self.width_multiplier,
scope=self.scope_name
)
return logits
class TENet6Model(AudioNetModel):
def __init__(self, args):
super().__init__(args)
def build_inference(self, inputs, is_training):
with slim.arg_scope(tenet.TENet_arg_scope(
is_training=is_training,
weight_decay=self.weight_decay,
keep_prob=self.dropout_keep_prob)
):
logits = tenet.TENet6(
inputs,
self.args.num_classes,
kernel_list=self.args.kernel_list,
scope=self.scope_name,
)
return logits
class TENet12Model(AudioNetModel):
def __init__(self, args):
super().__init__(args)
def build_inference(self, inputs, is_training):
with slim.arg_scope(tenet.TENet_arg_scope(
is_training=is_training,
weight_decay=self.weight_decay,
keep_prob=self.dropout_keep_prob)
):
logits = tenet.TENet12(
inputs,
self.args.num_classes,
kernel_list=self.args.kernel_list,
scope=self.scope_name,
)
return logits
class TENet6NarrowModel(AudioNetModel):
def __init__(self, args):
super().__init__(args)
def build_inference(self, inputs, is_training):
with slim.arg_scope(tenet.TENet_arg_scope(
is_training=is_training,
weight_decay=self.weight_decay,
keep_prob=self.dropout_keep_prob)
):
logits = tenet.TENet6Narrow(
inputs,
self.args.num_classes,
kernel_list=self.args.kernel_list,
scope=self.scope_name,
)
return logits
class TENet12NarrowModel(AudioNetModel):
def __init__(self, args):
super().__init__(args)
def build_inference(self, inputs, is_training):
with slim.arg_scope(tenet.TENet_arg_scope(
is_training=is_training,
weight_decay=self.weight_decay,
keep_prob=self.dropout_keep_prob)
):
logits = tenet.TENet12Narrow(
inputs,
self.args.num_classes,
kernel_list=self.args.kernel_list,
scope=self.scope_name,
)
return logits