{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# DAC Audio Tokenizer" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\dslee\\AppData\\Roaming\\Python\\Python38\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "import os\n", "from pathlib import Path\n", "\n", "from model import DAC, DACConfig" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# settings\n", "fname = str(Path(os.getcwd()).joinpath('.sample_sound', 'jazz_swing.wav'))\n", "device = 'cpu'\n", "model_type_by_sampling_freq = '24khz'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\dslee\\AppData\\Roaming\\Python\\Python38\\site-packages\\audiotools\\ml\\layers\\base.py:172: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n", " model_dict = torch.load(location, \"cpu\")\n", "c:\\Users\\dslee\\anaconda3\\envs\\sound_effect_variation_generation\\lib\\site-packages\\torch\\nn\\utils\\weight_norm.py:134: FutureWarning: `torch.nn.utils.weight_norm` is deprecated in favor of `torch.nn.utils.parametrizations.weight_norm`.\n", " WeightNorm.apply(module, name, dim)\n" ] } ], "source": [ "# load the model\n", "config = DACConfig(model_type_by_sampling_freq=model_type_by_sampling_freq)\n", "model = DAC(config).to(device)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "zq.shape: torch.Size([1, 1024, 750])\n", "s.shape: torch.Size([1, 32, 750])\n" ] } ], "source": [ "# encoding\n", "zq, s = model.encode(fname)\n", "print('zq.shape:', zq.shape)\n", "print('s.shape:', s.shape)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "waveform.shape: torch.Size([1, 1, 239904])\n" ] } ], "source": [ "# decoding (from zq -- discrete latent vectors)\n", "waveform = model.decode(zq=zq)\n", "print('waveform.shape:', waveform.shape)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "waveform.shape: torch.Size([1, 1, 239904])\n" ] } ], "source": [ "# decoding (from s -- tokens)\n", "waveform = model.decode(s=s)\n", "print('waveform.shape:', waveform.shape)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# save waveform into an audio file\n", "model.waveform_to_audiofile(waveform, 'out.wav')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "d:\\projects\\descript-audio-codec-24khz\\model.py:209: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n", " return torch.load(fname)\n" ] } ], "source": [ "# save and load tokens\n", "model.save_tensor(s, 'tokens.pt')\n", "loaded_s = model.load_tensor('tokens.pt') # s == loaded_s" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "sound_effect_variation_generation", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.19" } }, "nbformat": 4, "nbformat_minor": 2 }