File size: 6,098 Bytes
7e213c7
 
 
 
 
 
 
 
 
 
 
08f1526
7e213c7
08f1526
7e213c7
 
 
 
fb2212a
7e213c7
 
 
 
08f1526
7e213c7
fb2212a
 
 
 
08f1526
 
fb2212a
 
 
 
08f1526
fb2212a
7e213c7
 
 
 
 
 
 
 
 
 
 
 
 
 
08f1526
7629c95
7e213c7
 
 
 
08f1526
7e213c7
 
 
 
 
 
 
 
 
 
 
 
 
7629c95
7e213c7
 
 
 
 
 
fb2212a
7e213c7
 
 
 
 
 
fb2212a
 
 
 
 
 
 
 
 
7e213c7
 
 
 
 
7629c95
7e213c7
 
 
 
 
fb2212a
7e213c7
 
 
 
 
 
 
 
 
 
 
 
7629c95
7e213c7
 
 
 
 
fb2212a
7e213c7
 
 
 
7629c95
7e213c7
 
 
 
fb2212a
7e213c7
 
 
 
 
 
fb2212a
7e213c7
 
 
 
 
 
7629c95
 
7e213c7
fb2212a
 
 
 
 
 
 
7e213c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DAC Audio Tokenizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from pathlib import Path\n",
    "\n",
    "from model import DAC, DACConfig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "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 = '44khz'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "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": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zq.shape: torch.Size([1, 1024, 862])\n",
      "s.shape: torch.Size([1, 9, 862])\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": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "waveform.shape: torch.Size([1, 1, 441344])\n",
      "waveform.shape: torch.Size([1, 1, 441344])\n",
      "waveform.shape: torch.Size([1, 1, 441344])\n",
      "waveform.shape: torch.Size([1, 1, 441344])\n",
      "waveform.shape: torch.Size([1, 1, 441344])\n",
      "waveform.shape: torch.Size([1, 1, 441344])\n",
      "waveform.shape: torch.Size([1, 1, 441344])\n",
      "waveform.shape: torch.Size([1, 1, 441344])\n",
      "15.4 s ± 142 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\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": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "waveform.shape: torch.Size([1, 1, 441344])\n"
     ]
    }
   ],
   "source": [
    "# decoding (from s -- tokens)\n",
    "waveform = model.decode(s=s)\n",
    "print('waveform.shape:', waveform.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save waveform into an audio file\n",
    "model.waveform_to_audiofile(waveform, 'out.wav')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\projects\\descript-audio-codec\\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
}