Spaces:
Runtime error
Runtime error
File size: 6,318 Bytes
7aaaf62 001a426 f30235e 399a445 83721e5 7aaaf62 001a426 f30235e 7aaaf62 f30235e 7aaaf62 001a426 7aaaf62 f30235e 7aaaf62 f30235e 7aaaf62 f30235e 7aaaf62 f30235e 7aaaf62 96e542f 7aaaf62 6caf083 7aaaf62 f30235e 7aaaf62 f30235e 7aaaf62 001a426 7aaaf62 f30235e 6caf083 7aaaf62 6caf083 7aaaf62 6caf083 83721e5 7aaaf62 f30235e 6caf083 7aaaf62 f30235e 7aaaf62 83721e5 7aaaf62 6caf083 001a426 7aaaf62 6caf083 7aaaf62 f30235e 6caf083 7aaaf62 f30235e 399a445 83721e5 6caf083 83721e5 6caf083 001a426 399a445 6caf083 399a445 f30235e 83721e5 399a445 f30235e 6caf083 f30235e 6caf083 f30235e 6caf083 f30235e 6caf083 7aaaf62 |
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "b451ab22",
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import random\n",
"import numpy as np\n",
"from PIL import Image\n",
"from datasets import load_dataset\n",
"from IPython.display import Audio\n",
"from diffusers import AutoencoderKL, AudioDiffusionPipeline, Mel"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "324cef44",
"metadata": {},
"outputs": [],
"source": [
"mel = Mel()\n",
"vae = AutoencoderKL.from_pretrained('../models/autoencoder-kl')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "da55ce79",
"metadata": {},
"outputs": [],
"source": [
"vae.config"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5fea99ff",
"metadata": {},
"outputs": [],
"source": [
"ds = load_dataset('teticio/audio-diffusion-256')"
]
},
{
"cell_type": "markdown",
"id": "3a65ec4d",
"metadata": {},
"source": [
"### Reconstruct audio"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "426c6edd",
"metadata": {},
"outputs": [],
"source": [
"image = random.choice(ds['train'])['image']\n",
"display(image)\n",
"Audio(data=mel.image_to_audio(image), rate=mel.get_sample_rate())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29c9011d",
"metadata": {},
"outputs": [],
"source": [
"# encode\n",
"input_image = np.frombuffer(image.tobytes(), dtype=\"uint8\").reshape(\n",
" (image.height, image.width, 1))\n",
"input_image = ((input_image / 255) * 2 - 1).transpose(2, 0, 1)\n",
"posterior = vae.encode(torch.tensor([input_image],\n",
" dtype=torch.float32)).latent_dist\n",
"latents = posterior.sample()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "323ba46d",
"metadata": {},
"outputs": [],
"source": [
"# reconstruct\n",
"output_image = vae.decode(latents)['sample']\n",
"output_image = torch.clamp(output_image, -1., 1.)\n",
"output_image = (output_image + 1.0) / 2.0 # -1,1 -> 0,1; c,h,w\n",
"output_image = (output_image.detach().cpu().numpy() *\n",
" 255).round().astype(\"uint8\").transpose(0, 2, 3, 1)[0, :, :, 0]\n",
"output_image = Image.fromarray(output_image)\n",
"display(output_image)\n",
"Audio(data=mel.image_to_audio(output_image), rate=mel.get_sample_rate())"
]
},
{
"cell_type": "markdown",
"id": "00ff2ffa",
"metadata": {},
"source": [
"### Random sample from latent space\n",
"(Don't expect interesting results!)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "156a06a2",
"metadata": {},
"outputs": [],
"source": [
"# sample\n",
"output_image = vae.decode(torch.randn_like(latents))['sample']\n",
"output_image = torch.clamp(output_image, -1., 1.)\n",
"output_image = (output_image + 1.0) / 2.0 # -1,1 -> 0,1; c,h,w\n",
"output_image = (output_image.detach().cpu().numpy() *\n",
" 255).round().astype(\"uint8\").transpose(0, 2, 3, 1)[0, :, :, 0]\n",
"output_image = Image.fromarray(output_image)\n",
"display(output_image)\n",
"Audio(data=mel.image_to_audio(output_image), rate=mel.get_sample_rate())"
]
},
{
"cell_type": "markdown",
"id": "ee3997cf",
"metadata": {},
"source": [
"### Interpolate between two audios in latent space"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "46019770",
"metadata": {},
"outputs": [],
"source": [
"image2 = random.choice(ds['train'])['image']\n",
"display(image2)\n",
"Audio(data=mel.image_to_audio(image2), rate=mel.get_sample_rate())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6552b19",
"metadata": {},
"outputs": [],
"source": [
"# encode\n",
"input_image2 = np.frombuffer(image2.tobytes(), dtype=\"uint8\").reshape(\n",
" (image2.height, image2.width, 1))\n",
"input_image2 = ((input_image2 / 255) * 2 - 1).transpose(2, 0, 1)\n",
"posterior2 = vae.encode(torch.tensor([input_image2],\n",
" dtype=torch.float32)).latent_dist\n",
"latents2 = posterior2.sample()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "060a0b25",
"metadata": {},
"outputs": [],
"source": [
"# interpolate\n",
"alpha = 0.5 #@param {type:\"slider\", min:0, max:1, step:0.1}\n",
"output_image = vae.decode(\n",
" AudioDiffusionPipeline.slerp(latents, latents2, alpha))['sample']\n",
"output_image = torch.clamp(output_image, -1., 1.)\n",
"output_image = (output_image + 1.0) / 2.0 # -1,1 -> 0,1; c,h,w\n",
"output_image = (output_image.detach().cpu().numpy() *\n",
" 255).round().astype(\"uint8\").transpose(0, 2, 3, 1)[0, :, :, 0]\n",
"output_image = Image.fromarray(output_image)\n",
"display(output_image)\n",
"display(Audio(data=mel.image_to_audio(image), rate=mel.get_sample_rate()))\n",
"display(Audio(data=mel.image_to_audio(image2), rate=mel.get_sample_rate()))\n",
"display(\n",
" Audio(data=mel.image_to_audio(output_image), rate=mel.get_sample_rate()))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6c74105",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "huggingface",
"language": "python",
"name": "huggingface"
},
"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.10.6"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|