Created using Colaboratory

This commit is contained in:
Eloi Moliner Juanpere 2022-01-20 16:59:34 +02:00
parent 4fb2c52fbb
commit f88e6259fa

View File

@ -22,14 +22,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 1,
"id": "dd70762d", "id": "dd70762d",
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
}, },
"id": "dd70762d", "id": "dd70762d",
"outputId": "499681b1-46d6-4f21-b3b8-3a3348f6765b" "outputId": "24457550-e127-4d73-db47-297147b68f09"
}, },
"outputs": [ "outputs": [
{ {
@ -37,12 +37,32 @@
"name": "stdout", "name": "stdout",
"text": [ "text": [
"Cloning into 'denoising-historical-recordings'...\n", "Cloning into 'denoising-historical-recordings'...\n",
"remote: Enumerating objects: 177, done.\u001b[K\n", "remote: Enumerating objects: 181, done.\u001b[K\n",
"remote: Counting objects: 100% (177/177), done.\u001b[K\n", "remote: Counting objects: 100% (181/181), done.\u001b[K\n",
"remote: Compressing objects: 100% (153/153), done.\u001b[K\n", "remote: Compressing objects: 100% (157/157), done.\u001b[K\n",
"remote: Total 177 (delta 63), reused 99 (delta 16), pack-reused 0\u001b[K\n", "remote: Total 181 (delta 64), reused 98 (delta 16), pack-reused 0\u001b[K\n",
"Receiving objects: 100% (177/177), 97.29 KiB | 4.86 MiB/s, done.\n", "Receiving objects: 100% (181/181), 104.80 KiB | 17.47 MiB/s, done.\n",
"Resolving deltas: 100% (63/63), done.\n" "Resolving deltas: 100% (64/64), done.\n",
"--2022-01-20 14:40:36-- https://github.com/eloimoliner/denoising-historical-recordings/releases/download/v0.0/checkpoint.zip\n",
"Resolving github.com (github.com)... 140.82.112.3\n",
"Connecting to github.com (github.com)|140.82.112.3|:443... connected.\n",
"HTTP request sent, awaiting response... 302 Found\n",
"Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/401385223/354cec4e-d8be-4126-8b32-9e6509bca537?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220120%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220120T144036Z&X-Amz-Expires=300&X-Amz-Signature=1cf02e4ae397a518db9da71aad3dfc7dcb844c520bbfc49398ca6a54171ad637&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=401385223&response-content-disposition=attachment%3B%20filename%3Dcheckpoint.zip&response-content-type=application%2Foctet-stream [following]\n",
"--2022-01-20 14:40:36-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/401385223/354cec4e-d8be-4126-8b32-9e6509bca537?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220120%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220120T144036Z&X-Amz-Expires=300&X-Amz-Signature=1cf02e4ae397a518db9da71aad3dfc7dcb844c520bbfc49398ca6a54171ad637&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=401385223&response-content-disposition=attachment%3B%20filename%3Dcheckpoint.zip&response-content-type=application%2Foctet-stream\n",
"Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.110.133, 185.199.108.133, 185.199.111.133, ...\n",
"Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.110.133|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 264031940 (252M) [application/octet-stream]\n",
"Saving to: checkpoint.zip\n",
"\n",
"checkpoint.zip 100%[===================>] 251.80M 215MB/s in 1.2s \n",
"\n",
"2022-01-20 14:40:38 (215 MB/s) - checkpoint.zip saved [264031940/264031940]\n",
"\n",
"Archive: checkpoint.zip\n",
" inflating: denoising-historical-recordings/experiments/trained_model/checkpoint \n",
" inflating: denoising-historical-recordings/experiments/trained_model/checkpoint.data-00000-of-00001 \n",
" inflating: denoising-historical-recordings/experiments/trained_model/checkpoint.index \n"
] ]
} }
], ],
@ -64,10 +84,10 @@
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
}, },
"id": "HmHRpa5eloy2", "id": "HmHRpa5eloy2",
"outputId": "34af882d-8fb7-4b2a-dccd-50e328e0ab90" "outputId": "8666da99-94e0-4ed2-afca-f9e31b37c84c"
}, },
"id": "HmHRpa5eloy2", "id": "HmHRpa5eloy2",
"execution_count": 18, "execution_count": 2,
"outputs": [ "outputs": [
{ {
"output_type": "stream", "output_type": "stream",
@ -93,18 +113,17 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"#install dependencies\n", ""
"! pip install hydra-core==0.11.3"
], ],
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
}, },
"id": "nmic9hVzmSj6", "id": "nmic9hVzmSj6",
"outputId": "c549f0d1-9e5c-4445-ecd4-702697535011" "outputId": "4d1a91d8-e994-48d3-d27b-f3c0b933b60e"
}, },
"id": "nmic9hVzmSj6", "id": "nmic9hVzmSj6",
"execution_count": 15, "execution_count": 3,
"outputs": [ "outputs": [
{ {
"output_type": "stream", "output_type": "stream",
@ -112,15 +131,13 @@
"text": [ "text": [
"Collecting hydra-core==0.11.3\n", "Collecting hydra-core==0.11.3\n",
" Downloading hydra_core-0.11.3-py3-none-any.whl (72 kB)\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[?25l\r\u001b[K |████▌ | 10 kB 34.0 MB/s eta 0:00:01\r\u001b[K |█████████ | 20 kB 38.2 MB/s eta 0:00:01\r\u001b[K |█████████████▋ | 30 kB 43.5 MB/s eta 0:00:01\r\u001b[K |██████████████████▏ | 40 kB 47.4 MB/s eta 0:00:01\r\u001b[K |██████████████████████▊ | 51 kB 33.6 MB/s eta 0:00:01\r\u001b[K |███████████████████████████▎ | 61 kB 36.8 MB/s eta 0:00:01\r\u001b[K |███████████████████████████████▉| 71 kB 27.7 MB/s eta 0:00:01\r\u001b[K |████████████████████████████████| 72 kB 649 kB/s \n",
"\u001b[?25hCollecting omegaconf<1.5,>=1.4\n", "\u001b[?25hCollecting omegaconf<1.5,>=1.4\n",
" Downloading omegaconf-1.4.1-py3-none-any.whl (14 kB)\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", "Requirement already satisfied: PyYAML in /usr/local/lib/python3.7/dist-packages (from omegaconf<1.5,>=1.4->hydra-core==0.11.3) (3.13)\n",
"Collecting PyYAML\n", "Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from omegaconf<1.5,>=1.4->hydra-core==0.11.3) (1.15.0)\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", "Installing collected packages: omegaconf, hydra-core\n",
"\u001b[K |████████████████████████████████| 596 kB 39.4 MB/s \n", "Successfully installed hydra-core-0.11.3 omegaconf-1.4.1\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"
] ]
} }
] ]
@ -128,6 +145,10 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"#install dependencies\n",
"! pip install hydra-core==0.11.3\n",
"\n",
"#All the code goes here\n",
"import unet\n", "import unet\n",
"import tensorflow as tf\n", "import tensorflow as tf\n",
"import soundfile as sf\n", "import soundfile as sf\n",
@ -135,18 +156,7 @@
"from tqdm import tqdm\n", "from tqdm import tqdm\n",
"import scipy.signal\n", "import scipy.signal\n",
"import hydra\n", "import hydra\n",
"import os" "import os\n",
],
"metadata": {
"id": "TQBDTmO4mUBx"
},
"id": "TQBDTmO4mUBx",
"execution_count": 17,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#workaround to load hydra conf file\n", "#workaround to load hydra conf file\n",
"import yaml\n", "import yaml\n",
"from pathlib import Path\n", "from pathlib import Path\n",
@ -157,50 +167,15 @@
" __setattr__ = dict.__setitem__\n", " __setattr__ = dict.__setitem__\n",
" __delattr__ = dict.__delitem__\n", " __delattr__ = dict.__delitem__\n",
"args=dotdict(args)\n", "args=dotdict(args)\n",
"unet_args=dotdict(args.unet)" "unet_args=dotdict(args.unet)\n",
], "\n",
"metadata": {
"id": "yMI9sIUYo9Lp"
},
"id": "yMI9sIUYo9Lp",
"execution_count": 62,
"outputs": []
},
{
"cell_type": "code",
"source": [
"path_experiment=str(args.path_experiment)\n", "path_experiment=str(args.path_experiment)\n",
"\n", "\n",
"unet_model = unet.build_model_denoise(unet_args=unet_args)\n", "unet_model = unet.build_model_denoise(unet_args=unet_args)\n",
"\n", "\n",
"ckpt=os.path.join(\"/content/denoising-historical-recordings\",path_experiment, 'checkpoint')\n", "ckpt=os.path.join(\"/content/denoising-historical-recordings\",path_experiment, 'checkpoint')\n",
"unet_model.load_weights(ckpt)" "unet_model.load_weights(ckpt)\n",
], "\n",
"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": [
"<tensorflow.python.training.tracking.util.CheckpointLoadStatus at 0x7f1d0d679dd0>"
]
},
"metadata": {},
"execution_count": 67
}
]
},
{
"cell_type": "code",
"source": [
"def do_stft(noisy):\n", "def do_stft(noisy):\n",
" \n", " \n",
" window_fn = tf.signal.hamming_window\n", " window_fn = tf.signal.hamming_window\n",
@ -225,18 +200,8 @@
"\n", "\n",
" pred_cpx=data[...,0] + 1j * data[...,1]\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", " pred_time=tf.signal.inverse_stft(pred_cpx, win_size, hop_size, window_fn=inv_window_fn)\n",
" return pred_time\n" " return pred_time\n",
], "\n",
"metadata": {
"id": "iDJGN-1_taXR"
},
"id": "iDJGN-1_taXR",
"execution_count": 79,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def denoise_audio(audio):\n", "def denoise_audio(audio):\n",
"\n", "\n",
" data, samplerate = sf.read(audio)\n", " data, samplerate = sf.read(audio)\n",
@ -322,10 +287,10 @@
" return denoised_data" " return denoised_data"
], ],
"metadata": { "metadata": {
"id": "LcSmbzm_z8w9" "id": "TQBDTmO4mUBx"
}, },
"id": "LcSmbzm_z8w9", "id": "TQBDTmO4mUBx",
"execution_count": 72, "execution_count": 4,
"outputs": [] "outputs": []
}, },
{ {
@ -351,22 +316,22 @@
} }
}, },
"base_uri": "https://localhost:8080/", "base_uri": "https://localhost:8080/",
"height": 75 "height": 72
}, },
"id": "50Kmdy6AtbhW", "id": "50Kmdy6AtbhW",
"outputId": "31d357e7-5adb-45f4-a3fe-3bb11d05a5af" "outputId": "2d05860c-536d-45f8-92b4-d2ba6f5a54c5"
}, },
"id": "50Kmdy6AtbhW", "id": "50Kmdy6AtbhW",
"execution_count": 75, "execution_count": 5,
"outputs": [ "outputs": [
{ {
"output_type": "display_data", "output_type": "display_data",
"data": { "data": {
"text/html": [ "text/html": [
"\n", "\n",
" <input type=\"file\" id=\"files-d94b6f39-a36a-4c09-b590-17eb2e0fd976\" name=\"files[]\" multiple disabled\n", " <input type=\"file\" id=\"files-8c1b9bdf-8c48-4470-a703-92cbdabe7d22\" name=\"files[]\" multiple disabled\n",
" style=\"border:none\" />\n", " style=\"border:none\" />\n",
" <output id=\"result-d94b6f39-a36a-4c09-b590-17eb2e0fd976\">\n", " <output id=\"result-8c1b9bdf-8c48-4470-a703-92cbdabe7d22\">\n",
" Upload widget is only available when the cell has been executed in the\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", " current browser session. Please rerun this cell to enable.\n",
" </output>\n", " </output>\n",
@ -393,25 +358,21 @@
"for fn in uploaded.keys():\n", "for fn in uploaded.keys():\n",
" print('Denoising uploaded file \"{name}\"'.format(\n", " print('Denoising uploaded file \"{name}\"'.format(\n",
" name=fn))\n", " name=fn))\n",
" denoise_data=denoise_audio(fn)" " denoise_data=denoise_audio(fn)\n",
" basename=os.path.splitext(fn)[0]\n",
" wav_output_name=basename+\"_denoised\"+\".wav\"\n",
" sf.write(wav_output_name, denoise_data, 44100)"
], ],
"metadata": { "metadata": {
"colab": { "colab": {
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
}, },
"id": "0po6zpvrylc2", "id": "0po6zpvrylc2",
"outputId": "0d2b228e-dad8-40f8-f10a-118ea115180d" "outputId": "173f5355-2939-41fe-c702-591aa752fc7e"
}, },
"id": "0po6zpvrylc2", "id": "0po6zpvrylc2",
"execution_count": null, "execution_count": 6,
"outputs": [ "outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"\r 0%| | 0/41 [00:00<?, ?it/s]"
]
},
{ {
"output_type": "stream", "output_type": "stream",
"name": "stdout", "name": "stdout",
@ -424,20 +385,103 @@
"output_type": "stream", "output_type": "stream",
"name": "stderr", "name": "stderr",
"text": [ "text": [
" 71%|███████ | 29/41 [09:59<04:09, 20.77s/it]" "100%|██████████| 41/41 [00:30<00:00, 1.34it/s]\n"
] ]
} }
] ]
}, },
{
"cell_type": "code",
"source": [
"\n",
"files.download(wav_output_name)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 17
},
"id": "3tEshWBezYvf",
"outputId": "54588c26-0b3c-42bf-aca2-8316ab54603f"
},
"id": "3tEshWBezYvf",
"execution_count": 7,
"outputs": [
{
"output_type": "display_data",
"data": {
"application/javascript": [
"\n",
" async function download(id, filename, size) {\n",
" if (!google.colab.kernel.accessAllowed) {\n",
" return;\n",
" }\n",
" const div = document.createElement('div');\n",
" const label = document.createElement('label');\n",
" label.textContent = `Downloading \"${filename}\": `;\n",
" div.appendChild(label);\n",
" const progress = document.createElement('progress');\n",
" progress.max = size;\n",
" div.appendChild(progress);\n",
" document.body.appendChild(div);\n",
"\n",
" const buffers = [];\n",
" let downloaded = 0;\n",
"\n",
" const channel = await google.colab.kernel.comms.open(id);\n",
" // Send a message to notify the kernel that we're ready.\n",
" channel.send({})\n",
"\n",
" for await (const message of channel.messages) {\n",
" // Send a message to notify the kernel that we're ready.\n",
" channel.send({})\n",
" if (message.buffers) {\n",
" for (const buffer of message.buffers) {\n",
" buffers.push(buffer);\n",
" downloaded += buffer.byteLength;\n",
" progress.value = downloaded;\n",
" }\n",
" }\n",
" }\n",
" const blob = new Blob(buffers, {type: 'application/binary'});\n",
" const a = document.createElement('a');\n",
" a.href = window.URL.createObjectURL(blob);\n",
" a.download = filename;\n",
" div.appendChild(a);\n",
" a.click();\n",
" div.remove();\n",
" }\n",
" "
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"application/javascript": [
"download(\"download_3e3ca242-937f-408e-a16c-4aa5bb2b9e50\", \"Carmen-Habanera_(Love_is_Like_a_Woo_-_Marguerite_D'Alvarez_noisy_input_denoised.wav\", 17961334)"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {}
}
]
},
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"" ""
], ],
"metadata": { "metadata": {
"id": "3tEshWBezYvf" "id": "v_FuSJ4J-WO-"
}, },
"id": "3tEshWBezYvf", "id": "v_FuSJ4J-WO-",
"execution_count": null, "execution_count": null,
"outputs": [] "outputs": []
} }
@ -464,7 +508,8 @@
"name": "demo.ipynb", "name": "demo.ipynb",
"provenance": [], "provenance": [],
"include_colab_link": true "include_colab_link": true
} },
"accelerator": "GPU"
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 5 "nbformat_minor": 5