From 83d8bae37c0dfe73e51ef38128a055f9aa8fa9c8 Mon Sep 17 00:00:00 2001 From: dscripka Date: Tue, 5 Sep 2023 07:58:37 -0400 Subject: [PATCH] Convert clips to 16-bit PCM before saving [skip ci] --- notebooks/automatic_model_training.ipynb | 25 ++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/notebooks/automatic_model_training.ipynb b/notebooks/automatic_model_training.ipynb index eb576ca..bf26a24 100644 --- a/notebooks/automatic_model_training.ipynb +++ b/notebooks/automatic_model_training.ipynb @@ -68,12 +68,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 91, "id": "8f30acfd", "metadata": { "ExecuteTime": { - "end_time": "2023-09-04T13:42:01.183840Z", - "start_time": "2023-09-04T13:41:59.752153Z" + "end_time": "2023-09-05T11:52:51.331099Z", + "start_time": "2023-09-05T11:52:50.994098Z" } }, "outputs": [], @@ -127,8 +127,8 @@ "id": "58e4811e", "metadata": { "ExecuteTime": { - "end_time": "2023-09-04T01:07:17.746749Z", - "start_time": "2023-09-04T01:07:17.740846Z" + "end_time": "2023-09-05T11:53:57.310392Z", + "start_time": "2023-09-05T11:53:54.490518Z" } }, "outputs": [], @@ -141,9 +141,10 @@ " os.mkdir(output_dir)\n", "rir_dataset = datasets.load_dataset(\"davidscripka/MIT_environmental_impulse_responses\", split=\"train\", streaming=True)\n", "\n", + "# Save clips to 16-bit PCM wav files\n", "for row in tqdm(rir_dataset):\n", " name = row['audio']['path'].split('/')[-1]\n", - " scipy.io.wavfile.write(os.path.join(output_dir, name), 16000, row['audio']['array'])\n" + " scipy.io.wavfile.write(os.path.join(output_dir, name), 16000, (row['audio']['array']*32767).astype(np.int16))\n" ] }, { @@ -157,8 +158,10 @@ "\n", "# Audioset Dataset (https://research.google.com/audioset/dataset/index.html)\n", "# Download one part of the audioset .tar files, extract, and convert to 16khz\n", - "# For full-scale training, it's reccomended to download the entire dataset, and\n", - "# even combine with other background noise datasets (e.g., FSD50k, Freesound, etc.)\n", + "# For full-scale training, it's recommended to download the entire dataset from \n", + "# https://huggingface.co/datasets/agkphysics/AudioSet, and\n", + "# even potentially combine it with other background noise datasets (e.g., FSD50k, Freesound, etc.)\n", + "\n", "fname = \"bal_train09.tar\"\n", "out_dir = f\"audioset/{fname}\"\n", "link = \"https://huggingface.co/datasets/agkphysics/AudioSet/resolve/main/\" + fname\n", @@ -172,11 +175,12 @@ "if not os.path.exists(output_dir):\n", " os.mkdir(output_dir)\n", "\n", + "# Save clips to 16-bit PCM wav files\n", "audioset_dataset = datasets.Dataset.from_dict({\"audio\": [str(i) for i in Path(\"audioset/audio\").glob(\"**/*.flac\")]})\n", "audioset_dataset = audioset_dataset.cast_column(\"audio\", datasets.Audio(sampling_rate=16000))\n", "for row in tqdm(audioset_dataset):\n", " name = row['audio']['path'].split('/')[-1].replace(\".flac\", \".wav\")\n", - " scipy.io.wavfile.write(os.path.join(output_dir, name), 16000, row['audio']['array'])\n", + " scipy.io.wavfile.write(os.path.join(output_dir, name), 16000, (row['audio']['array']*32767).astype(np.int16))\n", "\n", "# Free Music Archive dataset\n", "# https://github.com/mdeff/fma\n", @@ -187,11 +191,12 @@ "fma_dataset = datasets.load_dataset(\"rudraml/fma\", name=\"small\", split=\"train\", streaming=True)\n", "fma_dataset = iter(fma_dataset.cast_column(\"audio\", datasets.Audio(sampling_rate=16000)))\n", "\n", + "# Save clips to 16-bit PCM wav files\n", "n_hours = 1 # use only 1 hour of clips for this example notebook, recommend increasing for full-scale training\n", "for i in tqdm(range(n_hours*3600//30)): # this works because the FMA dataset is all 30 second clips\n", " row = next(fma_dataset)\n", " name = row['audio']['path'].split('/')[-1].replace(\".mp3\", \".wav\")\n", - " scipy.io.wavfile.write(os.path.join(output_dir, name), 16000, row['audio']['array'])\n", + " scipy.io.wavfile.write(os.path.join(output_dir, name), 16000, (row['audio']['array']*32767).astype(np.int16))\n", " i += 1\n", " if i == n_hours*3600//30:\n", " break\n"