File size: 31,418 Bytes
814c8cf |
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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Interacting with Jukebox",
"provenance": [],
"collapsed_sections": [],
"machine_shape": "hm"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "sAdFGF-bqVMY",
"colab_type": "code",
"colab": {}
},
"source": [
"!pip install git+https://github.com/openai/jukebox.git"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "uq8uLwZCn0BV",
"colab_type": "text"
},
"source": [
"IMPORTANT NOTE ON SYSTEM REQUIREMENTS:\n",
"\n",
"If you are connecting to a hosted runtime, make sure it has a P100 GPU (optionally run !nvidia-smi to confirm). Go to Edit>Notebook Settings to set this.\n",
"\n",
"CoLab may first assign you a lower memory machine if you are using a hosted runtime. If so, the first time you try to load the 5B model, it will run out of memory, and then you'll be prompted to restart with more memory (then return to the top of this CoLab). If you continue to have memory issues after this (or run into issues on your own home setup), switch to the 1B model.\n",
"\n",
"If you are using a local GPU, we recommend V100 or P100 with 16GB GPU memory for best performance. For GPU’s with less memory, we recommend using the 1B model and a smaller batch size throughout. \n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "8qEqdj8u0gdN",
"colab_type": "code",
"colab": {}
},
"source": [
"!nvidia-smi"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "taDHgk1WCC_C",
"colab_type": "code",
"colab": {}
},
"source": [
"import jukebox\n",
"import torch as t\n",
"import librosa\n",
"import os\n",
"from IPython.display import Audio\n",
"from jukebox.make_models import make_vqvae, make_prior, MODELS, make_model\n",
"from jukebox.hparams import Hyperparams, setup_hparams\n",
"from jukebox.sample import sample_single_window, _sample, \\\n",
" sample_partial_window, upsample\n",
"from jukebox.utils.dist_utils import setup_dist_from_mpi\n",
"from jukebox.utils.torch_utils import empty_cache\n",
"rank, local_rank, device = setup_dist_from_mpi()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "89FftI5kc-Az",
"colab_type": "text"
},
"source": [
"# Sample from the 5B or 1B Lyrics Model\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "65aR2OZxmfzq",
"colab_type": "code",
"colab": {}
},
"source": [
"model = \"5b_lyrics\" # or \"1b_lyrics\" \n",
"hps = Hyperparams()\n",
"hps.sr = 44100\n",
"hps.n_samples = 3 if model=='5b_lyrics' else 8\n",
"hps.name = 'samples'\n",
"chunk_size = 16 if model==\"5b_lyrics\" else 32\n",
"max_batch_size = 3 if model==\"5b_lyrics\" else 16\n",
"hps.levels = 3\n",
"hps.hop_fraction = [.5,.5,.125]\n",
"\n",
"vqvae, *priors = MODELS[model]\n",
"vqvae = make_vqvae(setup_hparams(vqvae, dict(sample_length = 1048576)), device)\n",
"top_prior = make_prior(setup_hparams(priors[-1], dict()), vqvae, device)\n",
"\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "JYKiwkzy0Iyf",
"colab_type": "text"
},
"source": [
"Specify your choice of artist, genre, lyrics, and length of musical sample. "
]
},
{
"cell_type": "code",
"metadata": {
"id": "-sY9aGHcZP-u",
"colab_type": "code",
"colab": {}
},
"source": [
"sample_length_in_seconds = 60 # Full length of musical sample to generate - we find songs in the 1 to 4 minute\n",
" # range work well, with generation time proportional to sample length. \n",
" # This total length affects how quickly the model \n",
" # progresses through lyrics (model also generates differently\n",
" # depending on if it thinks it's in the beginning, middle, or end of sample)\n",
"\n",
"hps.sample_length = (int(sample_length_in_seconds*hps.sr)//top_prior.raw_to_tokens)*top_prior.raw_to_tokens\n",
"assert hps.sample_length >= top_prior.n_ctx*top_prior.raw_to_tokens, f'Please choose a larger sampling rate'"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"id": "qD0qxQeLaTR0",
"colab": {}
},
"source": [
"metas = [dict(artist = \"Zac Brown Band\",\n",
" genre = \"Country\",\n",
" total_length = hps.sample_length,\n",
" offset = 0,\n",
" lyrics = \"\"\"I met a traveller from an antique land,\n",
" Who said—“Two vast and trunkless legs of stone\n",
" Stand in the desert. . . . Near them, on the sand,\n",
" Half sunk a shattered visage lies, whose frown,\n",
" And wrinkled lip, and sneer of cold command,\n",
" Tell that its sculptor well those passions read\n",
" Which yet survive, stamped on these lifeless things,\n",
" The hand that mocked them, and the heart that fed;\n",
" And on the pedestal, these words appear:\n",
" My name is Ozymandias, King of Kings;\n",
" Look on my Works, ye Mighty, and despair!\n",
" Nothing beside remains. Round the decay\n",
" Of that colossal Wreck, boundless and bare\n",
" The lone and level sands stretch far away\n",
" \"\"\",\n",
" ),\n",
" ] * hps.n_samples\n",
"labels = [None, None, top_prior.labeller.get_batch_labels(metas, 'cuda')]"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "6PHC1XnEfV4Y",
"colab_type": "text"
},
"source": [
"Optionally adjust the sampling temperature (we've found .98 or .99 to be our favorite). \n"
]
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"id": "eNwKyqYraTR9",
"colab": {}
},
"source": [
"sampling_temperature = .98\n",
"\n",
"lower_batch_size = 16\n",
"max_batch_size = 3 if model == \"5b_lyrics\" else 16\n",
"lower_level_chunk_size = 32\n",
"chunk_size = 16 if model == \"5b_lyrics\" else 32\n",
"sampling_kwargs = [dict(temp=.99, fp16=True, max_batch_size=lower_batch_size,\n",
" chunk_size=lower_level_chunk_size),\n",
" dict(temp=0.99, fp16=True, max_batch_size=lower_batch_size,\n",
" chunk_size=lower_level_chunk_size),\n",
" dict(temp=sampling_temperature, fp16=True, \n",
" max_batch_size=max_batch_size, chunk_size=chunk_size)]"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "S3j0gT3HfrRD",
"colab_type": "text"
},
"source": [
"Now we're ready to sample from the model. We'll generate the top level (2) first, followed by the first upsampling (level 1), and the second upsampling (0). In this CoLab we load the top prior separately from the upsamplers, because of memory concerns on the hosted runtimes. If you are using a local machine, you can also load all models directly with make_models, and then use sample.py's ancestral_sampling to put this all in one step.\n",
"\n",
"After each level, we decode to raw audio and save the audio files. \n",
"\n",
"This next cell will take a while (approximately 10 minutes per 20 seconds of music sample)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "2nET_YBEopyp",
"colab_type": "code",
"colab": {}
},
"source": [
"zs = [t.zeros(hps.n_samples,0,dtype=t.long, device='cuda') for _ in range(len(priors))]\n",
"zs = _sample(zs, labels, sampling_kwargs, [None, None, top_prior], [2], hps)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "-gxY9aqHqfLJ",
"colab_type": "text"
},
"source": [
"Listen to the results from the top level (note this will sound very noisy until we do the upsampling stage). You may have more generated samples, depending on the batch size you requested."
]
},
{
"cell_type": "code",
"metadata": {
"id": "TPZENDGZqOOb",
"colab_type": "code",
"colab": {}
},
"source": [
"Audio(f'{hps.name}/level_2/item_0.wav')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "EJc3bQxmusc6",
"colab_type": "text"
},
"source": [
"We are now done with the large top_prior model, and instead load the upsamplers."
]
},
{
"cell_type": "code",
"metadata": {
"id": "W5VLX0zRapIm",
"colab_type": "code",
"colab": {}
},
"source": [
"# Set this False if you are on a local machine that has enough memory (this allows you to do the\n",
"# lyrics alignment visualization during the upsampling stage). For a hosted runtime, \n",
"# we'll need to go ahead and delete the top_prior if you are using the 5b_lyrics model.\n",
"if True:\n",
" del top_prior\n",
" empty_cache()\n",
" top_prior=None\n",
"upsamplers = [make_prior(setup_hparams(prior, dict()), vqvae, 'cpu') for prior in priors[:-1]]\n",
"labels[:2] = [prior.labeller.get_batch_labels(metas, 'cuda') for prior in upsamplers]"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "eH_jUhGDprAt",
"colab_type": "text"
},
"source": [
"Please note: this next upsampling step will take several hours. At the free tier, Google CoLab lets you run for 12 hours. As the upsampling is completed, samples will appear in the Files tab (you can access this at the left of the CoLab), under \"samples\" (or whatever hps.name is currently). Level 1 is the partially upsampled version, and then Level 0 is fully completed."
]
},
{
"cell_type": "code",
"metadata": {
"id": "9lkJgLolpZ6w",
"colab_type": "code",
"colab": {}
},
"source": [
"zs = upsample(zs, labels, sampling_kwargs, [*upsamplers, top_prior], hps)\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "3SJgBYJPri55",
"colab_type": "text"
},
"source": [
"Listen to your final sample!"
]
},
{
"cell_type": "code",
"metadata": {
"id": "2ip2PPE0rgAb",
"colab_type": "code",
"colab": {}
},
"source": [
"Audio(f'{hps.name}/level_0/item_0.wav')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "8JAgFxytwrLG",
"colab_type": "code",
"colab": {}
},
"source": [
"del upsamplers\n",
"empty_cache()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "LpvvFH85bbBC",
"colab_type": "text"
},
"source": [
"# Co-Composing with the 5B or 1B Lyrics Model"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "nFDROuS7gFQY",
"colab_type": "text"
},
"source": [
"For more control over the generations, try co-composing with either the 5B or 1B Lyrics Models. Again, specify your artist, genre, and lyrics. However, now instead of generating the entire sample, the model will return 3 short options for the opening of the piece (or up to 16 options if you use the 1B model instead). Choose your favorite, and then continue the loop, for as long as you like. Throughout these steps, you'll be listening to the audio at the top prior level, which means it will sound quite noisy. When you are satisfied with your co-creation, continue on through the upsampling section. This will render the piece in higher audio quality.\n",
"\n",
"NOTE: CoLab will first assign you a lower memory machine if you are using a hosted runtime. The next cell will run out of memory, and then you'll be prompted to restart with more memory (then return to the top of this CoLab). If you continue to have memory issues after this (or run into issues on your own home setup), switch to the 1B model. "
]
},
{
"cell_type": "code",
"metadata": {
"id": "3y-q8ifhGBlU",
"colab_type": "code",
"colab": {}
},
"source": [
"model = \"5b_lyrics\" # or \"1b_lyrics\"\n",
"hps = Hyperparams()\n",
"hps.sr = 44100\n",
"hps.n_samples = 3 if model=='5b_lyrics' else 16\n",
"hps.name = 'co_composer'\n",
"hps.sample_length = 1048576 if model==\"5b_lyrics\" else 786432 \n",
"chunk_size = 16 if model==\"5b_lyrics\" else 32\n",
"max_batch_size = 3 if model==\"5b_lyrics\" else 16\n",
"hps.hop_fraction = [.5, .5, .125] \n",
"hps.levels = 3\n",
"\n",
"vqvae, *priors = MODELS[model]\n",
"vqvae = make_vqvae(setup_hparams(vqvae, dict(sample_length = hps.sample_length)), device)\n",
"top_prior = make_prior(setup_hparams(priors[-1], dict()), vqvae, device)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "68hz4x7igq0c",
"colab_type": "text"
},
"source": [
"Choose your artist, genre, and lyrics here!"
]
},
{
"cell_type": "code",
"metadata": {
"id": "QDMvH_1zUHo6",
"colab_type": "code",
"colab": {}
},
"source": [
"total_sample_length_in_seconds = 120\n",
"metas = [dict(artist = \"Zac Brown Band\",\n",
" genre = \"Country\",\n",
" total_length = total_sample_length_in_seconds * hps.sr,\n",
" offset = 0,\n",
" lyrics = \"\"\"I met a traveller from an antique land,\n",
" Who said—“Two vast and trunkless legs of stone\n",
" Stand in the desert. . . . Near them, on the sand,\n",
" Half sunk a shattered visage lies, whose frown,\n",
" And wrinkled lip, and sneer of cold command,\n",
" Tell that its sculptor well those passions read\n",
" Which yet survive, stamped on these lifeless things,\n",
" The hand that mocked them, and the heart that fed;\n",
" And on the pedestal, these words appear:\n",
" My name is Ozymandias, King of Kings;\n",
" Look on my Works, ye Mighty, and despair!\n",
" Nothing beside remains. Round the decay\n",
" Of that colossal Wreck, boundless and bare\n",
" The lone and level sands stretch far away\n",
" \"\"\",\n",
" ),\n",
" ] * hps.n_samples\n",
"labels = top_prior.labeller.get_batch_labels(metas, 'cuda')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "B9onZMEXh34f",
"colab_type": "text"
},
"source": [
"## Generate 3 options for the start of the song\n",
"\n",
"Initial generation is set to be 4 seconds long, but feel free to change this"
]
},
{
"cell_type": "code",
"metadata": {
"id": "c6peEj8I_HHO",
"colab_type": "code",
"colab": {}
},
"source": [
"def seconds_to_tokens(sec, sr, prior, chunk_size):\n",
" tokens = sec * hps.sr // prior.raw_to_tokens\n",
" tokens = ((tokens // chunk_size) + 1) * chunk_size\n",
" assert tokens <= prior.n_ctx, 'Choose a shorter generation length to stay within the top prior context'\n",
" return tokens"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "2gn2GXt3zt3y",
"colab_type": "code",
"colab": {}
},
"source": [
"initial_generation_in_seconds = 4\n",
"tokens_to_sample = seconds_to_tokens(initial_generation_in_seconds, hps.sr, top_prior, chunk_size)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "U0zcWcMoiigl",
"colab_type": "text"
},
"source": [
"Change the sampling temperature if you like (higher is more random). Our favorite is in the range .98 to .995"
]
},
{
"cell_type": "code",
"metadata": {
"id": "NHbH68H7VMeO",
"colab_type": "code",
"colab": {}
},
"source": [
"sampling_temperature = .98\n",
"sampling_kwargs = dict(temp=sampling_temperature, fp16=True,\n",
" max_batch_size=max_batch_size, chunk_size=chunk_size)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "JGZEPe-WTt4g",
"colab_type": "code",
"colab": {}
},
"source": [
"zs=[t.zeros(hps.n_samples,0,dtype=t.long, device='cuda') for _ in range(3)]\n",
"zs=sample_partial_window(zs, labels, sampling_kwargs, 2, top_prior, tokens_to_sample, hps)\n",
"x = vqvae.decode(zs[2:], start_level=2).cpu().numpy()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "mveN4Be8jK2J",
"colab_type": "text"
},
"source": [
"Listen to your generated samples, and then pick a favorite. If you don't like any, go back and rerun the cell above. \n",
"\n",
"** NOTE this is at the noisy top level, upsample fully (in the next section) to hear the final audio version"
]
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"id": "LrJSGMhUOhZg",
"colab": {}
},
"source": [
"for i in range(hps.n_samples):\n",
" librosa.output.write_wav(f'noisy_top_level_generation_{i}.wav', x[i], sr=44100)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"id": "rQ4ersQ5OhZr",
"colab": {}
},
"source": [
"Audio('noisy_top_level_generation_0.wav')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"id": "-GdqzrGkOhZv",
"colab": {}
},
"source": [
"Audio('noisy_top_level_generation_1.wav')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"id": "gE5S8hyZOhZy",
"colab": {}
},
"source": [
"Audio('noisy_top_level_generation_2.wav')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "t2-mEJaqZfuS",
"colab_type": "text"
},
"source": [
"If you don't like any of the options, return a few cells back to \"Sample a few options...\" and rerun from there."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "o7CzSiv0MmFP",
"colab_type": "text"
},
"source": [
"## Choose your favorite sample and request longer generation\n",
"\n",
"---\n",
"\n",
"(Repeat from here)\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "j_XFtVi99CIY",
"colab_type": "code",
"colab": {}
},
"source": [
"my_choice=0"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Pgk3sHHBLYoq",
"colab_type": "code",
"colab": {}
},
"source": [
"zs[2]=zs[2][my_choice].repeat(hps.n_samples,1)\n",
"t.save(zs, 'zs-checkpoint2.t')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "W8Rd9xxm565S",
"colab_type": "code",
"colab": {}
},
"source": [
"# Set to True to load the previous checkpoint:\n",
"if False:\n",
" zs=t.load('zs-checkpoint2.t') "
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "k12xjMgHkRGP",
"colab_type": "text"
},
"source": [
"Choose the length of the continuation. The 1B model can generate up to 17 second samples and the 5B up to 23 seconds, but you'll want to pick a shorter continuation length so that it will be able to look back at what you've generated already. Here we've chosen 4 seconds."
]
},
{
"cell_type": "code",
"metadata": {
"id": "h3_-0a07kHHG",
"colab_type": "code",
"colab": {}
},
"source": [
"continue_generation_in_seconds=4\n",
"tokens_to_sample = seconds_to_tokens(continue_generation_in_seconds, hps.sr, top_prior, chunk_size)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "GpPG3Ifqk8ue",
"colab_type": "text"
},
"source": [
"The next step asks the top prior to generate more of the sample. It'll take up to a few minutes, depending on the sample length you request."
]
},
{
"cell_type": "code",
"metadata": {
"id": "YoHkeSTaEyLj",
"colab_type": "code",
"colab": {}
},
"source": [
"zs = sample_partial_window(zs, labels, sampling_kwargs, 2, top_prior, tokens_to_sample, hps)\n",
"x = vqvae.decode(zs[2:], start_level=2).cpu().numpy()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "ymhUqEdhleEi",
"colab_type": "text"
},
"source": [
"Now listen to the longer versions of the sample you selected, and again choose a favorite sample. If you don't like any, return back to the cell where you can load the checkpoint, and continue again from there.\n",
"\n",
"When the samples start getting long, you might not always want to listen from the start, so change the playback start time later on if you like."
]
},
{
"cell_type": "code",
"metadata": {
"id": "2H1LNLTa_R6a",
"colab_type": "code",
"colab": {}
},
"source": [
"playback_start_time_in_seconds = 0 "
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "r4SBGAmsnJtH",
"colab_type": "code",
"colab": {}
},
"source": [
"for i in range(hps.n_samples):\n",
" librosa.output.write_wav(f'top_level_continuation_{i}.wav', x[i][playback_start_time_in_seconds*44100:], sr=44100)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "2WeyE5Qtnmeo",
"colab_type": "code",
"colab": {}
},
"source": [
"Audio('top_level_continuation_0.wav')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "BKtfEtcaazXE",
"colab_type": "code",
"colab": {}
},
"source": [
"Audio('top_level_continuation_1.wav')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "7yrlS0XwK2S0",
"colab_type": "code",
"colab": {}
},
"source": [
"Audio('top_level_continuation_2.wav')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "-OJT704dvnGv",
"colab_type": "text"
},
"source": [
"To make a longer song, return back to \"Choose your favorite sample\" and loop through that again"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RzCrkCZJvUcQ",
"colab_type": "text"
},
"source": [
"# Upsample Co-Composition to Higher Audio Quality"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4MPgukwMmB0p",
"colab_type": "text"
},
"source": [
"Choose your favorite sample from your latest group of generations. (If you haven't already gone through the Co-Composition block, make sure to do that first so you have a generation to upsample)."
]
},
{
"cell_type": "code",
"metadata": {
"id": "yv-pNNPHBQYC",
"colab_type": "code",
"colab": {}
},
"source": [
"choice = 0\n",
"select_best_sample = True # Set false if you want to upsample all your samples \n",
" # upsampling sometimes yields subtly different results on multiple runs,\n",
" # so this way you can choose your favorite upsampling"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "v17cEAqyCgfo",
"colab_type": "code",
"colab": {}
},
"source": [
"if select_best_sample:\n",
" zs[2]=zs[2][choice].repeat(zs[2].shape[0],1)\n",
"\n",
"t.save(zs, 'zs-top-level-final.t')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "0YjK-Ac0tBfu",
"colab_type": "text"
},
"source": [
"Note: If you are using a CoLab hosted runtime on the free tier, you may want to download this zs-top-level-final.t file, and then restart an instance and load it in the next cell. The free tier will last a maximum of 12 hours, and the upsampling stage can take many hours, depending on how long a sample you have generated."
]
},
{
"cell_type": "code",
"metadata": {
"id": "qqlR9368s3jJ",
"colab_type": "code",
"colab": {}
},
"source": [
"if False:\n",
" zs = t.load('zs-top-level-final.t')\n",
"\n",
"assert zs[2].shape[1]>=2048, f'Please first generate at least 2048 tokens at the top level, currently you have {zs[2].shape[1]}'\n",
"hps.sample_length = zs[2].shape[1]*top_prior.raw_to_tokens"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "jzHwF_iqgIWM",
"colab_type": "code",
"colab": {}
},
"source": [
"# Set this False if you are on a local machine that has enough memory (this allows you to do the\n",
"# lyrics alignment visualization). For a hosted runtime, we'll need to go ahead and delete the top_prior\n",
"# if you are using the 5b_lyrics model.\n",
"if True:\n",
" del top_prior\n",
" empty_cache()\n",
" top_prior=None\n",
"\n",
"upsamplers = [make_prior(setup_hparams(prior, dict()), vqvae, 'cpu') for prior in priors[:-1]]"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "q22Ier6YSkKS",
"colab_type": "code",
"colab": {}
},
"source": [
"sampling_kwargs = [dict(temp=.99, fp16=True, max_batch_size=16, chunk_size=32),\n",
" dict(temp=0.99, fp16=True, max_batch_size=16, chunk_size=32),\n",
" None]\n",
"\n",
"if type(labels)==dict:\n",
" labels = [prior.labeller.get_batch_labels(metas, 'cuda') for prior in upsamplers] + [labels] "
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "T1MCa9_jnjpf",
"colab_type": "text"
},
"source": [
"This next step upsamples 2 levels. The level_1 samples will be available after around one hour (depending on the length of your sample) and are saved under {hps.name}/level_0/item_0.wav, while the fully upsampled level_0 will likely take 4-12 hours. You can access the wav files down below, or using the \"Files\" panel at the left of this CoLab.\n",
"\n",
"(Please note, if you are using this CoLab on Google's free tier, you may want to download intermediate steps as the connection will last for a maximum 12 hours.)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "NcNT5qIRMmHq",
"colab_type": "code",
"colab": {}
},
"source": [
"zs = upsample(zs, labels, sampling_kwargs, [*upsamplers, top_prior], hps)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "W2jTYLPBc29M",
"colab_type": "code",
"colab": {}
},
"source": [
"Audio(f'{hps.name}/level_0/item_0.wav')"
],
"execution_count": 0,
"outputs": []
}
]
} |