Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a little helper function to vggish to get features from waveforms #31

Merged
merged 1 commit into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions openmic/vggish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,41 @@

__pproc__ = Postprocessor(PCA_PARAMS)
postprocess = __pproc__.postprocess


def waveform_to_features(data, sample_rate, compress=True):
'''Converts an audio waveform to VGGish features, with or without
PCA compression.

Parameters
----------
data : np.array of either one dimension (mono) or two dimensions (stereo)

sample_rate:
Sample rate of the audio data

compress : bool
If True, PCA and quantization are applied to the features.
If False, the features are taken directly from the model output

Returns
-------
time_points : np.ndarray, len=n
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per #29, we're making a conscious decision to converge to time_points then?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I literally just copied over from the existing functions. If we rename, we should do it all at once, and i think it's independent of this pr.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Time points in seconds of the features

features : np.ndarray, shape=(n, 128)
The output features, with or without PCA compression and quantization.
'''

import tensorflow as tf
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the rationale for within-function importing here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tf import is heavy (sometimes); we shouldn't do it until called for.

If it's already been loaded elsewhere, then this is free.


examples = waveform_to_examples(data, sample_rate)

with tf.Graph().as_default(), tf.Session() as sess:
time_points, features = transform(examples, sess)

if compress:
features_z = postprocess(features)
return time_points, features_z

return time_points, features
17 changes: 17 additions & 0 deletions tests/test_openmic_vggish_model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import pytest

import numpy as np
import soundfile as sf
import tensorflow as tf

import openmic.vggish.inputs
import openmic.vggish.model as model
from openmic.vggish import waveform_to_features


def test_model_transform_soundfile(ogg_file):
Expand All @@ -12,3 +15,17 @@ def test_model_transform_soundfile(ogg_file):
time_points, features = model.transform(examples, sess)

assert len(time_points) == len(features) > 1


def test_wf_to_features(ogg_file):
data, rate = sf.read(ogg_file)

time_points_z, features_z = waveform_to_features(data, rate, compress=True)
assert len(time_points_z) == len(features_z)

time_points, features = waveform_to_features(data, rate, compress=False)
assert len(time_points) == len(features)

assert np.allclose(time_points, time_points_z)

assert np.allclose(features_z, openmic.vggish.postprocess(features))