joao_marques:

What tf.squeeze actually does to the audio_binary I provided?


The original dataset’s waveforms are in mono (https://arxiv.org/pdf/1804.03209.pdf 1) (single channel):

Each utterance is stored as a one-second (or less) WAVE format file, with the sample data encoded as linear 16-bit single-channel PCM values, at a 16 KHz rate.

If you decode the WAV file using tf.audio.decode_wav to a normalized tensor with dtype float32:
test_audio = tf.io.read_file('data/mini_speech_commands/down/0a9f9af7_nohash_0.wav')
test_decoded_audio, _ = tf.audio.decode_wav(contents=test_audio)

the resulting tensor shape will be:
print(test_decoded_audio)

tf.Tensor(... shape(13654, 1), dtype=float32)

where 1 is the mono channel. Before you perform a short-term Fourier transform with the get_spectrogram() function to obtain a spectrogram (a 2D image), you remove the last dimension with tf.squeeze:
test_squeezed_decoded_audio = tf.squeeze(test_decoded_audio)

The input for the tf.keras.layers.Conv2D has a shape of (BATCH_SIZE, {IMAGE_SIZE}, CHANNELS). Therefore, you add the mono channel (1) back in the get_spectrogram_and_label_id() function with spectrogram = tf.expand_dims(spectrogram, axis=-1) (thanks @markdaoust).
You can also learn more about short-time Fourier transforms in the following external notebooks (they were created to accompany a book):

Basic idea (STFT): C2_STFT-Basic

Padding for STFT: C2_STFT-Padding


Here’s another potentially useful resource:


Preprocessing Audio Datasets for Machine Learning - YouTube (April 2021) - by the Sound of AI channel, with accompanying code on GitHub.