-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Added partial visualisation for Audio Datasets under tfds #1683
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
base: master
Are you sure you want to change the base?
Changes from 36 commits
313b571
cb417fc
5489a5f
f70cf46
ebf78db
8075721
8fc8ddf
02bc9e2
5242458
f801b41
04af5b2
d84455d
581620d
8791219
17b9188
a5dc660
df53df0
15550dc
cfb259b
f01622e
0378ca4
e95ded9
42df323
2539974
ad62afe
ede24ba
10e96bb
bea3e4e
46c6260
314ee0c
344f8d3
8df4e9d
1e12f8d
bb07fa2
817dcfa
7af71b3
26fd653
3e99ff0
ac73594
b5d7ec6
cc487e9
801987f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,68 @@ | ||||
""" Audio Visualization | ||||
""" | ||||
from __future__ import absolute_import | ||||
from __future__ import division | ||||
from __future__ import print_function | ||||
|
||||
|
||||
import random | ||||
import IPython.display | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will crash for all user not running in a non-ipython environement. This should not be imported in the global scope |
||||
|
||||
from absl import logging | ||||
|
||||
from tensorflow_datasets.core import dataset_utils | ||||
from tensorflow_datasets.core import features as features_lib | ||||
from tensorflow_datasets.core import lazy_imports_lib | ||||
from tensorflow_datasets.core.visualization import visualizer | ||||
plt = lazy_imports_lib.lazy_imports.matplotlib.pyplot | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will raise ImportError for users not having installed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Edit - Thank you for your comments - A bug for the Groove 'Tensor' attribute will be helpful - #1741 : Just realized that you have generated the issue There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The bug has been fixed so it should works if you are rebasing from master. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does, Thanks! The auto inferring works now ( Link . I have tested it with Groove, crema_d, ljspeech. |
||||
|
||||
class AudioGridVisualizer(visualizer.Visualizer): | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you format your code with https://www.tensorflow.org/datasets/add_dataset#5_check_your_code_style (add docstring, new line before method declaration, correct docstring...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do! |
||||
def match(self, ds_info): | ||||
audio_keys = visualizer.extract_keys(ds_info.features, features_lib.Audio) | ||||
return len(audio_keys) > 0 | ||||
|
||||
def show( | ||||
self, | ||||
ds_info, | ||||
ds, | ||||
): | ||||
"""Display the dataset. | ||||
|
||||
Args: | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Arguments are not matching |
||||
ds_info: `tfds.core.DatasetInfo` object of the dataset to visualize. | ||||
ds: `tf.data.Dataset`. The tf.data.Dataset object to visualize. Examples | ||||
should not be batched. Examples will be consumed in order until | ||||
(rows * cols) are read or the dataset is consumed. | ||||
rows: `int`, number of rows of the display grid. | ||||
cols: `int`, number of columns of the display grid. | ||||
plot_scale: `float`, controls the plot size of the images. Keep this | ||||
value around 3 to get a good plot. High and low values may cause | ||||
the labels to get overlapped. | ||||
image_key: `string`, name of the feature that contains the image. If not | ||||
set, the system will try to auto-detect it. | ||||
""" | ||||
key = audio_keys[0] | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is it declared ? |
||||
audio_samples = [] | ||||
|
||||
samplerate = 16000 | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now you can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||||
for features in ds: | ||||
audio_samples.append(features[key].numpy()) | ||||
to_gen = [] | ||||
for _ in range(4): | ||||
value = random.randint(0, len(audio_samples)) | ||||
to_gen.append(audio_samples[value]) | ||||
|
||||
t1 = 0 | ||||
t2 = 100 * 1000 | ||||
for audio in to_gen: | ||||
newAudio = audio[t1:t2] | ||||
IPython.display.Audio(newAudio, rate=samplerate) | ||||
|
||||
fig, a = plt.subplots(2, 2) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make the code more generic, you could refactor the code to use And ideally, you could try to reuse the
Or make a similar util function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All Right! I made some edits accordingly |
||||
|
||||
a[0][0].plot(to_gen[0]) | ||||
a[0][1].plot(to_gen[1]) | ||||
a[1][0].plot(to_gen[2]) | ||||
a[1][1].plot(to_gen[3]) | ||||
plt.show() | ||||
return fig |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a test in
show_example_test.py
to make sure this works ? You can use https://docs.python.org/3/library/unittest.mock.html to make sure the AudioVisualizer is chosen.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a mock patch for the AudioVisualizer class and get the following type error :
TypeError: test_show_examples() takes 2 positional arguments but 3 were given
Ran 2 tests in 0.557s
FAILED (errors=1, skipped=1)