There is such a variety of species! Who should we choose, how and what to do with the data?

1. What is next?

Now that we have got a basic grasp of our bird data and have listened extensively for their calls, let's choose a random subset and think about feature preprocessing for our neural networks. We have to make sure:

For proof of concept our data-set is not too big (remember - we have access to 200k bird calls in xeno-canto database) for fast neural network training;

The dataset is balanced;

We apply right preprocessing methods;

It would be better if we could actually fit the whole dataset to the memory of our GPU at once, so that we would be able to pre-process our data using matrix operations in the GPU;

2. Considerations for choosing random subset

Obviously we should favour the entries where we have taxonomic data and vernacular names (it will be easier to interpet data without consulting wikipedia each time that Gallus means chicken for example).

As expected, this produces the below counts. We have ca. 365k birds songs, 286k of which have taxonomic data and only 134k of which have vernacular genus names. Actually this really not bad given that average bird song mat contain 5 - 10 actual songs (see later).

(286147, 29)(134855, 29)(367577, 29)

Let's see how many bird songs we have on average for birds with vernacular genus names.

We see that average is between 10^2 (100) and 10^2.5 (300), which is significantly better than having just 30 songs per species on average. One might argue that species may have significantly different songs, but for a proof of concept being able to tell a crow from a titmouse will be ok.

I will omit some boring eye inspection steps where I basically produced pivot tables and looked through the majority of the data manually. In the end I decided to use this simple algorithm to produce random dataset:

Choose only bird genus with > 200 songs;

Choose random 200 songs for any such given genus;

This gives us 132 unique bird genus and 26400 bird songs, which I assume is enough for proof of concept, provided that each song weights ca. 100kb (it was naive of me to suppose that).

merged_df = pd.read_csv('merged_data.csv')# Given that we have an average of ca. 300 calls per class# let's choose only the genus with > 300 calls and let's choose 300 calls per genus, so that the sample is not biasedsample_df = pd.DataFrame()for vgenus in merged_df[pd.notnull(merged_df.vgenus)].vgenus.unique():

3. Downloading and preprocessing the data

Obviously, we cannot feed raw sound into CNNs (because if we simplify - they learn shapes). Raw sound data when decoded looks something like this in the most simplistic case (check out this awesome notebook for more):

Your browser does not support the audio element.

A general rule of thumb is that if a person cannot tell the difference in 90-95% cases with naked eye, then the machine probably would also have a hard time telling the difference. In a more realistic environment sound may look like this (check out this amazing notebook):

Your browser does not support the audio element.

Your browser does not support the audio element.

Compare these three examples - you will see that they are not really so different when visualized. But compare all of this with the spectrogram like this:

It immediately becomes apparent that we should feed our networks not raw sound but preprocessed sound in the form of spectrograms or any deeper form of sound analysis available with librosa (i believe that logs of mel-spectrograms and MFCC are the obvious candidates). Also given that we have to download all the bird songs separately (one of my professional web-scraping friends recommends this tool to me - http://docs.grablib.org/en/latest/spider/intro.html - but I guess getting multi-curl scraping tools is a task itself).

So we need to download data, preprocess it and save it as numpy arrays.

Below you can find a simplistic python script I used for this purpose. It manages to download 1500-1700 audio files and then dies for some reason without errors or explanations. Pay attention to the timeout alarm bit and try-catch statements - sometimes files take just too much time to be processed. Also this script uses only 50% of my 4 CPUs and ca. 10% of my internet bandwidth, which is not efficient, but we are still in MVP phase here...

(Remove last break in case you want to download more than one file. sample.csv is a random subset produced above).

The whole process is a bit slow and kinda looks like this. The boring part is that I cannot just leave it overnight because it's failing. I guess for full-scale models I will need to learn the scraping libraries properly...

4. So, what do we have inside? Let's play with the visuals and the data!

Well, obviously I had some results, when I was writing this article =) Let's see what we managed to download and how it will affect the later stages of our project. This is based on ca. 1700 observations from our ca. 22k random subset (I guess we can start testing basic CNNs on ca. 3-5k birds songs).

This is a bit of a problem - on average mel spectrogram files range from 100kb to 1mb in size. I hoped that we would have 10x smaller files. This mostly impacts feasibility of running an MVP of the model by loading the whole dataset into memory at one...

All of this is nice, but probably we should have some fun after all? Let's do it. Let's visualize our spectrograms and play 10 random bird songs. This is really really beautiful and inspiring. This is really like some form of modern art. Just look and enjoy (you can repeat all of this if you want to...) =)

# Make a new figure plt.figure(figsize=(12,4)) # Display the spectrogram on a mel scale # sample rate and hop length parameters are used to render the time axis librosa.display.specshow(log_S, sr=sr, x_axis='time', y_axis='mel') # Put a descriptive title on the plot plt.title('mel power spectrogram') # draw a color bar plt.colorbar(format='%+02.0f dB') # Make the figure layout compact plt.tight_layout() plt.show()