File size: 5,318 Bytes
1e37f65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
{
 "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
}