Learn practical skills, build real-world projects, and advance your career

Retriving and visualizing the data

In this notebook we (1) demonstrate how to access the raw 'wav' files with python and (2) show a simple visualization of some audio files.

Retriving the Data

To access the data, we use the utils.py present in the dataset.

ESC50 is a Class usefull to access the data as 16000 or 44100KHz raw waves.
You call ESC50 with the desired data augmentation desired:
ESC50(only_ESC10, folds, randomize, audio_rate, strongAugment, pad, inputLength, random_crop, mix, normalize)
It has 2 generator included :

  • _data_gen to retrieve audio samples, one at a time.
  • batch_gen(batch_size) to retrieve batches of audio samples (calls the former).
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import sys
import os
sys.path.insert(0, os.path.abspath('../input/'))
from utils import ESC50

train_splits = [1,2,3,4]
test_split = 5

shared_params = {'csv_path': '../input/esc50.csv',
                 'wav_dir': '../input/audio/audio',
                 'dest_dir': '../input/audio/audio/16000',
                 'audio_rate': 16000,
                 'only_ESC10': True,
                 'pad': 0,
                 'normalize': True}

train_gen = ESC50(folds=train_splits,
                  randomize=True,
                  strongAugment=True,
                  random_crop=True,
                  inputLength=2,
                  mix=True,
                  **shared_params).batch_gen(16)

test_gen = ESC50(folds=[test_split],
                 randomize=False,
                 strongAugment=False,
                 random_crop=False,
                 inputLength=4,
                 mix=False,
                 **shared_params).batch_gen(16)

X, Y = next(train_gen)
X.shape, Y.shape
((16, 32000, 1), (16, 10))

Get the classes

We now compute the spectrogram of each audio sample we have collected before (in data) and plot it next to its raw wave.

df = pd.DataFrame.from_csv('../input/esc50.csv')
classes = df[['target', 'category']].as_matrix().tolist()
classes = set(['{} {}'.format(c[0], c[1]) for c in classes])
classes = np.array([c.split(' ') for c in classes])
classes = {k: v for k, v in classes}
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:1: FutureWarning: from_csv is deprecated. Please use read_csv(...) instead. Note that some of the default arguments are different, so please refer to the documentation for from_csv when changing your function calls """Entry point for launching an IPython kernel. /opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:2: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.

Visualizing the Data

We now compute the spectrogram of each audio sample we have collected before (in data) and plot it next to its raw wave.