Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jean Ibarz
ml_binaural_audio
Commits
5655cba0
Commit
5655cba0
authored
Feb 11, 2021
by
Jean Ibarz
Browse files
Added code to support training of a 'left_center_right' model.
parent
b834cfe6
Changes
2
Hide whitespace changes
Inline
Side-by-side
core/utils.py
View file @
5655cba0
...
...
@@ -220,3 +220,14 @@ def one_hot_encoding_left_or_right(a, dtype='int'):
b
=
np
.
zeros
((
a
.
size
,
a
.
max
()
+
1
))
b
[
np
.
arange
(
a
.
size
),
a
]
=
1
return
b
def
azimuth_to_left_center_right_onehot
(
labels
,
in_place
=
False
):
if
not
in_place
:
labels
=
labels
.
copy
()
labels
=
labels
.
astype
(
dtype
=
np
.
int
,
copy
=
False
)
labels
[(
labels
>
180
)
&
(
labels
<
360
)]
=
0
# left side
labels
[(
labels
==
180
)
|
(
labels
==
360
)]
=
1
# center
labels
[(
labels
>
0
)
&
(
labels
<
180
)]
=
2
# right side
labels
=
tf
.
one_hot
(
labels
,
depth
=
3
)
return
labels
scripts/train_model.py
View file @
5655cba0
import
argparse
from
datetime
import
datetime
from
core.utils
import
load_ircam_hrirs_data
,
split_dataset
,
generate_signals
,
generate_augmented_labelled_dataset
from
core.utils
import
load_ircam_hrirs_data
,
split_dataset
,
generate_signals
,
generate_augmented_labelled_dataset
,
\
azimuth_to_left_center_right_onehot
import
numpy
as
np
import
tensorflow
as
tf
from
core.logger
import
experiment_logger
...
...
@@ -57,7 +58,7 @@ if __name__ == '__main__':
if
not
'convolve_mode'
in
exp_config
.
keys
()
or
exp_config
[
'convolve_mode'
]
==
'valid'
:
# the valid part from the convolved sound is sound[len(hrir)-1:-len(hrir)+1]
# hence we have to do len(hrir)-1 left and right zero padding so that the dirac belong to the result
n_zeros
=
512
-
1
n_zeros
=
512
-
1
zeros
=
np
.
zeros
(
shape
=
(
2
,
n_zeros
))
sounds
=
np
.
concatenate
([
zeros
,
np
.
ones
(
shape
=
(
2
,
1
)),
zeros
],
axis
=
1
)
training_sounds
,
test_sounds
=
np
.
array_split
(
sounds
,
2
,
axis
=
0
)
...
...
@@ -136,9 +137,23 @@ if __name__ == '__main__':
opt = tf.keras.optimizers.RMSprop(learning_rate=exp_config['learning_rate'])
# opt = tf.keras.optimizers.Adam(learning_rate=lr)
model.compile(optimizer=opt,
loss=MeanAbsoluteAzimuthError(), # tf.keras.losses.MeanSquaredError(),
metrics=[])
if exp_config['model_name'] == 'default':
model.compile(optimizer=opt,
loss=MeanAbsoluteAzimuthError(), # tf.keras.losses.MeanSquaredError(),
metrics=[])
elif exp_config['model_name'] == 'left_center_right':
model.compile(optimizer=opt,
loss=tf.keras.losses.CategoricalCrossentropy(), # tf.keras.losses.MeanSquaredError(),
metrics=[ # tf.keras.metrics.TruePositives(name='tp'),
# tf.keras.metrics.FalsePositives(name='fp'),
# tf.keras.metrics.TrueNegatives(name='tn'),
# tf.keras.metrics.FalseNegatives(name='fn'),
tf.keras.metrics.Precision(name='precision'),
tf.keras.metrics.Recall(name='recall'),
tf.keras.metrics.CategoricalAccuracy(name='acc'),
# tf.keras.metrics.AUC(name='auc'),
])
# model.summary()
# model = default_model_creator(lr=exp_config['learning_rate'])
...
...
@@ -165,6 +180,10 @@ if __name__ == '__main__':
n_samples=n_samples,
n_augment=1)
if exp_config['model_name'] == 'left_center_right':
training_labels = azimuth_to_left_center_right_onehot(training_labels, in_place=True)
test_labels = azimuth_to_left_center_right_onehot(test_labels, in_place=True)
# Expand dimensions of arrays to comply with the stanrdized shape BWHF (Batch, Width, Height, Features).
# Here, width=time axis, and Height=channel (left or right)
training_signals = np.expand_dims(training_signals, axis=-1)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment