<jupyter_start><jupyter_text>Small DatasetTo start with the problem, we are going to use a small subset for testing.<jupyter_code>import pandas as pd
import numpy as np

import torchaudio
import torchaudio.transforms as T

from tqdm.notebook import tqdm

import matplotlib.pyplot as plt

import librosa
df = pd.read_csv("../datasets/AnimalSoundFull.csv")
df.head()
df_aves = df[df["class"] == "Aves"].reset_index(drop=True)
df_aves.head()
df_mammalia = df[df["class"] == "Mammalia"].reset_index(drop=True)
df_mammalia.head()
df_aves.shape, df_mammalia.shape
num_samples = 1000
np.random.seed(42)

df_small_aves = df_aves.sample(n=num_samples)
df_small_mammalia = df_mammalia.sample(n=num_samples)

df_small = pd.concat([df_small_aves, df_small_mammalia]).reset_index(drop=True).drop(columns=["identifier",
                                                                                              "species",
                                                                                              "genus",
                                                                                              "family",
                                                                                              "phylum"
                                                                                             ])
df_small
def getSpectrogram(row):
  wf, sample_rate = torchaudio.load("../data/" + row.file_name)
  
  n_fft = 1024
  win_length = None
  hop_length = 512
  n_mels = 128

  mel_spectrogram = T.MelSpectrogram(
      sample_rate=sample_rate,
      n_fft=n_fft,
      win_length=win_length,
      hop_length=hop_length,
      center=True,
      pad_mode="reflect",
      power=2.0,
      norm='slaney',
      onesided=True,
      n_mels=n_mels,
      mel_scale="htk",
  )
  
  melspec = mel_spectrogram(wf)[0]
  
  height = 128*2
  width = height*4
  dpi = 100
  
  fig = plt.figure(frameon=False, figsize=(width/dpi, height/dpi), dpi=dpi)

  
  ax = plt.Axes(fig, [0., 0., 1., 1.])
  ax.set_axis_off()
  fig.add_axes(ax)
  
  im = ax.imshow(librosa.power_to_db(melspec), origin='lower', aspect="auto")
  
  file_name = "../spectrograms/small_dataset/" + str(row["gbifID"]) + ".jpg"
  
  plt.savefig(file_name)
  plt.close()
      
  return
tqdm.pandas(desc="Creating Spectrograms")
_ = df_small.progress_apply(getSpectrogram, axis=1)
df_small.to_csv("../datasets/Aves-Mammalia.csv", index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11311475409836065, 'fraction_numerical': 0.013114754098360656, 'mean_word_length': 1.9163679808841099, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '267893', 'license': 'MIT', 'n_tokens_mistral': 827, 'n_tokens_neox': 783, 'n_words': 132, 'path': 'data_processing/createSmallDataset.ipynb', 'repo_name': 'Pheithar/Animal-Sounds', 'size': '2440'}