Increasing experimentation agility

Before we dive into classifier training, let's think about experimentation agility. Although we have the word "fast" in FFT, it is much slower than the creation of the features in our text-based chapters. And because we are still in an experimentation phase, we might want to think about how we could speed up the whole feature-creation process.

Of course, the creation of the FFT per file will be the same each time we run the classifier. We could, therefore, cache it and read the cached FFT representation instead of the complete WAV file. We do this with the create_fft() function, which, in turn, uses scipy.fft() to create the FFT. For the sake of simplicity (and speed!), let's fix the number of FFT components to the first 1,000 in this example. With our current knowledge, we do not know whether these are the most important ones for music-genre classification, only that they show the highest intensities in the preceding FFT example. If we later want to use more or fewer FFT components, we would have to recreate the FFT representations for each sound file:

import numpy as np
import scipy

def create_fft(fn):
sample_rate, X = scipy.io.wavfile.read(fn)

fft_features = abs(scipy.fft(X)[:1000])
np.save(Path(fn).with_suffix('.fft'), fft_features)

for wav_fn in Path(GENRE_DIR).glob('**/*.wav'):
create_fft(wav_fn)

We save the data using NumPy's save() function, which always appends .npy to the filename. We only have to do this once for every WAV file needed for training or predicting.

The corresponding FFT reading function is read_fft():

def read_fft(genre_list, base_dir=GENRE_DIR):
X = []
y = []
for label, genre in enumerate(genre_list):
genre_dir = Path(base_dir) / genre
for fn in genre_dir.glob("*.fft.npy"):
fft_features = np.load(fn)

X.append(fft_features[:1000])
y.append(label)

return np.array(X), np.array(y)

In our scrambled music directory, we expect the following music genres:

GENRES = ["classical", "jazz", "country", "pop", "rock", "metal"]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset