diff --git a/colab/demo.ipynb b/colab/demo.ipynb index b034ce9..cc55d35 100644 --- a/colab/demo.ipynb +++ b/colab/demo.ipynb @@ -1,33 +1,471 @@ { - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "id": "dd70762d", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "source": [ + "# New Section" + ], + "metadata": { + "id": "jbe_aWYkjWRH" + }, + "id": "jbe_aWYkjWRH" + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "dd70762d", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "dd70762d", + "outputId": "499681b1-46d6-4f21-b3b8-3a3348f6765b" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Cloning into 'denoising-historical-recordings'...\n", + "remote: Enumerating objects: 177, done.\u001b[K\n", + "remote: Counting objects: 100% (177/177), done.\u001b[K\n", + "remote: Compressing objects: 100% (153/153), done.\u001b[K\n", + "remote: Total 177 (delta 63), reused 99 (delta 16), pack-reused 0\u001b[K\n", + "Receiving objects: 100% (177/177), 97.29 KiB | 4.86 MiB/s, done.\n", + "Resolving deltas: 100% (63/63), done.\n" + ] + } + ], + "source": [ + "#download the files\n", + "! git clone https://github.com/eloimoliner/denoising-historical-recordings.git\n", + "! wget https://github.com/eloimoliner/denoising-historical-recordings/releases/download/v0.0/checkpoint.zip\n", + "! unzip checkpoint.zip -d denoising-historical-recordings/experiments/trained_model/" + ] + }, + { + "cell_type": "code", + "source": [ + "\n", + "%cd denoising-historical-recordings" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "HmHRpa5eloy2", + "outputId": "34af882d-8fb7-4b2a-dccd-50e328e0ab90" + }, + "id": "HmHRpa5eloy2", + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "/content/denoising-historical-recordings\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "" + ], + "metadata": { + "id": "gvd6KZkTlyhR" + }, + "id": "gvd6KZkTlyhR", + "execution_count": 12, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#install dependencies\n", + "! pip install hydra-core==0.11.3" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "nmic9hVzmSj6", + "outputId": "c549f0d1-9e5c-4445-ecd4-702697535011" + }, + "id": "nmic9hVzmSj6", + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Collecting hydra-core==0.11.3\n", + " Downloading hydra_core-0.11.3-py3-none-any.whl (72 kB)\n", + "\u001b[?25l\r\u001b[K |████▌ | 10 kB 19.6 MB/s eta 0:00:01\r\u001b[K |█████████ | 20 kB 12.0 MB/s eta 0:00:01\r\u001b[K |█████████████▋ | 30 kB 8.9 MB/s eta 0:00:01\r\u001b[K |██████████████████▏ | 40 kB 7.9 MB/s eta 0:00:01\r\u001b[K |██████████████████████▊ | 51 kB 5.3 MB/s eta 0:00:01\r\u001b[K |███████████████████████████▎ | 61 kB 5.4 MB/s eta 0:00:01\r\u001b[K |███████████████████████████████▉| 71 kB 5.7 MB/s eta 0:00:01\r\u001b[K |████████████████████████████████| 72 kB 372 kB/s \n", + "\u001b[?25hCollecting omegaconf<1.5,>=1.4\n", + " Downloading omegaconf-1.4.1-py3-none-any.whl (14 kB)\n", + "Requirement already satisfied: six in /usr/local/lib/python3.7/site-packages (from omegaconf<1.5,>=1.4->hydra-core==0.11.3) (1.15.0)\n", + "Collecting PyYAML\n", + " Downloading PyYAML-6.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (596 kB)\n", + "\u001b[K |████████████████████████████████| 596 kB 39.4 MB/s \n", + "\u001b[?25hInstalling collected packages: PyYAML, omegaconf, hydra-core\n", + "Successfully installed PyYAML-6.0 hydra-core-0.11.3 omegaconf-1.4.1\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "import unet\n", + "import tensorflow as tf\n", + "import soundfile as sf\n", + "import numpy as np\n", + "from tqdm import tqdm\n", + "import scipy.signal\n", + "import hydra\n", + "import os" + ], + "metadata": { + "id": "TQBDTmO4mUBx" + }, + "id": "TQBDTmO4mUBx", + "execution_count": 17, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#workaround to load hydra conf file\n", + "import yaml\n", + "from pathlib import Path\n", + "args = yaml.safe_load(Path('conf/conf.yaml').read_text())\n", + "class dotdict(dict):\n", + " \"\"\"dot.notation access to dictionary attributes\"\"\"\n", + " __getattr__ = dict.get\n", + " __setattr__ = dict.__setitem__\n", + " __delattr__ = dict.__delitem__\n", + "args=dotdict(args)\n", + "unet_args=dotdict(args.unet)" + ], + "metadata": { + "id": "yMI9sIUYo9Lp" + }, + "id": "yMI9sIUYo9Lp", + "execution_count": 62, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "path_experiment=str(args.path_experiment)\n", + "\n", + "unet_model = unet.build_model_denoise(unet_args=unet_args)\n", + "\n", + "ckpt=os.path.join(\"/content/denoising-historical-recordings\",path_experiment, 'checkpoint')\n", + "unet_model.load_weights(ckpt)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "cbdPyEpAp7s0", + "outputId": "63df20d2-1c00-41a5-c8fc-a6cdcfb57d08" + }, + "id": "cbdPyEpAp7s0", + "execution_count": 67, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 67 + } + ] + }, + { + "cell_type": "code", + "source": [ + "def do_stft(noisy):\n", + " \n", + " window_fn = tf.signal.hamming_window\n", + "\n", + " win_size=args.stft[\"win_size\"]\n", + " hop_size=args.stft[\"hop_size\"]\n", + "\n", + " \n", + " stft_signal_noisy=tf.signal.stft(noisy,frame_length=win_size, window_fn=window_fn, frame_step=hop_size, pad_end=True)\n", + " stft_noisy_stacked=tf.stack( values=[tf.math.real(stft_signal_noisy), tf.math.imag(stft_signal_noisy)], axis=-1)\n", + "\n", + " return stft_noisy_stacked\n", + "\n", + "def do_istft(data):\n", + " \n", + " window_fn = tf.signal.hamming_window\n", + "\n", + " win_size=args.stft[\"win_size\"]\n", + " hop_size=args.stft[\"hop_size\"]\n", + "\n", + " inv_window_fn=tf.signal.inverse_stft_window_fn(hop_size, forward_window_fn=window_fn)\n", + "\n", + " pred_cpx=data[...,0] + 1j * data[...,1]\n", + " pred_time=tf.signal.inverse_stft(pred_cpx, win_size, hop_size, window_fn=inv_window_fn)\n", + " return pred_time\n" + ], + "metadata": { + "id": "iDJGN-1_taXR" + }, + "id": "iDJGN-1_taXR", + "execution_count": 79, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "def denoise_audio(audio):\n", + "\n", + " data, samplerate = sf.read(audio)\n", + " print(data.dtype)\n", + " #Stereo to mono\n", + " if len(data.shape)>1:\n", + " data=np.mean(data,axis=1)\n", + " \n", + " if samplerate!=44100: \n", + " print(\"Resampling\")\n", + " \n", + " data=scipy.signal.resample(data, int((44100 / samplerate )*len(data))+1) \n", + " \n", + " \n", + " \n", + " segment_size=44100*5 #20s segments\n", + "\n", + " length_data=len(data)\n", + " overlapsize=2048 #samples (46 ms)\n", + " window=np.hanning(2*overlapsize)\n", + " window_right=window[overlapsize::]\n", + " window_left=window[0:overlapsize]\n", + " audio_finished=False\n", + " pointer=0\n", + " denoised_data=np.zeros(shape=(len(data),))\n", + " residual_noise=np.zeros(shape=(len(data),))\n", + " numchunks=int(np.ceil(length_data/segment_size))\n", + " \n", + " for i in tqdm(range(numchunks)):\n", + " if pointer+segment_size\n", + " \n", + " Upload widget is only available when the cell has been executed in the\n", + " current browser session. Please rerun this cell to enable.\n", + " \n", + " " + ], + "text/plain": [ + "" + ] + }, + "metadata": {} + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Saving Carmen-Habanera_(Love_is_Like_a_Woo_-_Marguerite_D'Alvarez_noisy_input.wav to Carmen-Habanera_(Love_is_Like_a_Woo_-_Marguerite_D'Alvarez_noisy_input.wav\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "for fn in uploaded.keys():\n", + " print('Denoising uploaded file \"{name}\"'.format(\n", + " name=fn))\n", + " denoise_data=denoise_audio(fn)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "0po6zpvrylc2", + "outputId": "0d2b228e-dad8-40f8-f10a-118ea115180d" + }, + "id": "0po6zpvrylc2", + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stderr", + "text": [ + "\r 0%| | 0/41 [00:00