Getting started

Installation

To install Soundata simply do:

pip install soundata

Soundata is easily imported into your Python code by:

import soundata

Initializing a dataset

Print a list of all available dataset loaders by calling:

import soundata
print(soundata.list_datasets())

To use a loader, (for example, urbansound8k) you need to initialize it by calling:

import soundata
dataset = soundata.initialize('urbansound8k', data_home='choose_where_data_live')

You can indicate where the data would be stored and access by passing a path to data_home, as explained below. Now us8k is a Dataset object containing common methods, described in the following.

Downloading a dataset

All dataset loaders in soundata have a download() function that allows the user to download the canonical version of the dataset (when available). When initializing a dataset it is important to correctly set up the directory where the dataset is going to be stored and retrieved.

Downloading a dataset into the default folder:

In this first example, data_home is not specified. Thus, UrbanSound8K will be downloaded and retrieved from the default folder, sound_datasets, created in the user’s root folder:

import soundata
dataset = soundata.initialize('urbansound8k')
dataset.download()  # Dataset is downloaded into "sound_datasets" folder inside user's root folder
Downloading a dataset into a specified folder:

In the next example data_home is specified, so UrbanSound8K will be downloaded and retrieved from the specified location:

dataset = soundata.initialize('urbansound8k', data_home='Users/johnsmith/Desktop')
dataset.download()  # Dataset is downloaded to John Smith's desktop
Partially downloading a dataset

The download() function allows to partially download a dataset. In other words, if applicable, the user can select which elements of the dataset they want to download. Each dataset has a REMOTES dictionary were all the available downloadable elements are listed.

tau2019uas has different elements as seen in the REMOTES dictionary. You can specify a subset of these elements to download by passing the download() function a list of the REMOTES keys that we are interested in via the partial_download variable.

A partial download example for tau2019uas dataset could be:

dataset = soundata.initialize('tau2019uas')
dataset.download(partial_download=['development.audio.1', 'development.audio.2'])  # download only two remotes
Downloading a multipart dataset

In some cases, datasets consist of multiple remote files that have to be extracted together locally to correctly recover the data. In those cases, remotes that need to be extracted together should be grouped in a list, so all the necessary files are downloaded at once (even in a partial download). An example of this is the fsd50k loader:

Validating a dataset

Using the validate() method you can ensure that the files in our local copy of a dataset are identical to the canonical version of the dataset. The function computes the md5 checksum of every downloaded file to ensure it was downloaded correctly and isn’t corrupted.

For big datasets: In future soundata versions, a random validation will be included. This improvement will reduce validation time for very big datasets.

Accessing annotations

You can choose a random clip from a dataset with the choice_clip() method.

You can also access specific clips by id. The available clip ids can be acessed by doing dataset.clip_ids. In the next example we take the first clip id, and then we retrieve its tags annotation.

dataset = soundata.initialize('urbansound8k')
ids = dataset.clip_ids  # the list of urbansound8k's clip ids
clips = dataset.load_clips()  # Load all clips in the dataset
example_clip = clips[ids[0]]  # Get the first clip

# Accessing the clip's tags annotation
example_tags = example_clip.tags
print(example_tags)
>>>> Tags(confidence, labels, labels_unit)
print(example_tags.labels)
>>>> ['children_playing']

You can also load a single clip without loading all clips in the dataset:

ids = dataset.clip_ids  # the list of urbansound8k's clip ids
example_clip = dataset.clip(ids[0])  # load this particular clip
example_tags = example_clip.tags  # Get the tags for the first clip

Accessing data remotely

Annotations can also be accessed through load_*() methods which may be useful, for instance, when your data aren’t available locally. If you specify the annotation’s path, you can use the module’s loading functions directly. Let’s see an example.

Annotation classes

soundata defines annotation-specific data classes such as Tags or Events. These data classes are meant to standarize the format for all loaders, so you can use the same code with different datasets. The list and descriptions of available annotation classes can be found in Annotations.

Note

These classes are standarized to the point that the data allow for it. In some cases where the dataset has its own idiosyncrasies, the classes may be extended e.g. adding a customize, uncommon attribute.

Iterating over datasets and annotations

In general, most datasets are a collection of clips, and in most cases each clip has an audio file along with annotations.

With the load_clips() method, all clips are loaded as a dictionary with the clip id as keys and clip objects as values. The clip objects include their respective audio and annotations, which are lazy-loaded on access to keep things speedy and memory efficient.

dataset = soundata.initialize('urbansound8k')
for key, clip in dataset.load_clips().items():
    print(key, clip.audio_path)
>>>> soundscape_train_bimodal0 /Users/mf3734/sound_datasets/urbansed/audio/train/soundscape_train_bimodal0.wav
     .....

Alternatively, you can loop over the clip_ids list to directly access each clip in the dataset.

dataset = soundata.initialize('urbansound8k')
for clip_id in dataset.clip_ids:
    print(clip_id, dataset.clip(clip_id).audio_path)
>>>> soundscape_train_bimodal0 /Users/mf3734/sound_datasets/urbansed/audio/train/soundscape_train_bimodal0.wav
     .....

Including soundata in your pipeline

If you wanted to use urbansound8k to evaluate the performance of an urban sound classifier, (in our case, random_classifier), and then split the scores based on the metadata, you could do the following:

This is the result of the example above:

Using soundata with tensorflow

The following is a simple example of a generator that can be used to create a tensorflow Dataset.

In future soundata versions, generators for Tensorflow and PyTorch will be included out-of-the-box.