{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Fine Transformer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Libraries:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import torch\n", "from audiolm_pytorch import HubertWithKmeans\n", "from audiolm_pytorch import SemanticTransformer, SemanticTransformerTrainer\n", "from audiolm_pytorch import CoarseTransformer, CoarseTransformerTrainer\n", "from audiolm_pytorch import SoundStream, FineTransformer, FineTransformerTrainer\n", "from audiolm_pytorch import AudioLMSoundStream, AudioLM, MusicLMSoundStream\n", "from musiclm_pytorch import MuLaNEmbedQuantizer\n", "from musiclm_pytorch import MuLaN, AudioSpectrogramTransformer, TextTransformer\n", "import gc\n", "from nltk.tokenize import word_tokenize\n", "import nltk\n", "import librosa\n", "import numpy as np\n", "import pickle" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[nltk_data] Downloading package punkt to\n", "[nltk_data] C:\\Users\\hp\\AppData\\Roaming\\nltk_data...\n", "[nltk_data] Package punkt is already up-to-date!\n" ] } ], "source": [ "nltk.download('punkt')\n", "checkpoint_path = './models/hubert/hubert_base_ls960.pt'\n", "kmeans_path = './models/hubert/hubert_base_ls960_L9_km500.bin'\n", "\n", "audio_output_dir = './audio'\n", "batch_size = 1\n", "data_max_length = 320 * 32\n", "num_train_steps = 1000" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "training with dataset of 4806 samples and validating with randomly splitted 253 samples\n", "spectrogram yielded shape of (65, 841), but had to be cropped to (64, 832) to be patchified for transformer\n", "0: loss: 103.04938507080078\n", "0: valid loss 11.681041717529297\n", "0: saving model to results\n", "training complete\n", "save fine_transformer.pth\n" ] } ], "source": [ "audio_transformer = AudioSpectrogramTransformer(\n", " dim = 512,\n", " depth = 6,\n", " heads = 8,\n", " dim_head = 64,\n", " spec_n_fft = 128,\n", " spec_win_length = 24,\n", " spec_aug_stretch_factor = 0.8\n", ")\n", "\n", "text_transformer = TextTransformer(\n", " dim = 512,\n", " depth = 6,\n", " heads = 8,\n", " dim_head = 64\n", ")\n", "\n", "mulan = MuLaN(\n", " audio_transformer = audio_transformer,\n", " text_transformer = text_transformer\n", ")\n", "\n", "quantizer = MuLaNEmbedQuantizer(\n", " mulan = mulan, \n", " conditioning_dims = (1024, 1024, 1024), \n", " namespaces = ('semantic', 'coarse', 'fine')\n", ")\n", "\n", "\n", "def train_fine_transformer():\n", " soundstream = MusicLMSoundStream(\n", " codebook_size=1024, \n", " strides=(3, 4, 5, 8),\n", " target_sample_hz=24000,\n", " rq_num_quantizers=8\n", " )\n", "\n", " if torch.cuda.is_available():\n", " fine_transformer = FineTransformer(\n", " num_coarse_quantizers = 4,\n", " num_fine_quantizers = 4,\n", " codebook_size = 1024,\n", " dim = 1024,\n", " depth = 6,\n", " audio_text_condition = True\n", " ).cuda()\n", " else:\n", " fine_transformer = FineTransformer(\n", " num_coarse_quantizers = 4,\n", " num_fine_quantizers = 4,\n", " codebook_size = 1024,\n", " dim = 1024,\n", " depth = 6,\n", " audio_text_condition = True\n", " )\n", "\n", " trainer = FineTransformerTrainer(\n", " transformer=fine_transformer,\n", " codec=soundstream,\n", " folder=audio_output_dir,\n", " batch_size=batch_size,\n", " data_max_length=data_max_length,\n", " num_train_steps=num_train_steps,\n", " audio_conditioner = quantizer\n", " )\n", "\n", " trainer.train()\n", " torch.save(fine_transformer.state_dict(), 'fine_transformer.pth')\n", " print(\"save fine_transformer.pth\")\n", " del fine_transformer, trainer, soundstream\n", " gc.collect()\n", "\n", "\n", "train_fine_transformer()" ] } ], "metadata": { "kernelspec": { "display_name": "myenv", "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.11.2" } }, "nbformat": 4, "nbformat_minor": 2 }