CoolFace
Apppublic

teticio/audio-diffusion

sourceHugging Facegpl-3.0updated 4y agoView on Hugging Face
54likes
README.md235 linesDownload Raw Back to root
1---2title: Audio Diffusion3emoji: 🎵4colorFrom: pink5colorTo: blue6sdk: gradio7sdk_version: 3.1.48app_file: app.py9pinned: false10license: gpl-3.011---12# audio-diffusion [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/teticio/audio-diffusion/blob/master/notebooks/gradio_app.ipynb)13 14## Apply diffusion models to synthesize music instead of images using the new Hugging Face [diffusers](https://github.com/huggingface/diffusers) package15 16---17#### Sample automatically generated loop18 19https://user-images.githubusercontent.com/44233095/204103172-27f25d63-5e77-40ca-91ab-d04a45d4726f.mp420 21Go to https://soundcloud.com/teticio2/sets/audio-diffusion-loops for more examples.22 23---24#### Updates25 26**25/12/2022**. Now it is possible to train models conditional on an encoding (of text or audio, for example). See the section on Conditional Audio Generation below.27 28**5/12/2022**. 🤗 Exciting news! `AudioDiffusionPipeline` has been migrated to the Hugging Face `diffusers` package so that it is even easier for others to use and contribute.29 30**2/12/2022**. Added Mel to pipeline and updated the pretrained models to save Mel config (they are now no longer compatible with previous versions of this repo). It is relatively straightforward to migrate previously trained models to the new format (see https://huggingface.co/teticio/audio-diffusion-256).31 32**7/11/2022**. Added pre-trained latent audio diffusion models [teticio/latent-audio-diffusion-256](https://huggingface.co/teticio/latent-audio-diffusion-256) and [teticio/latent-audio-diffusion-ddim-256](https://huggingface.co/teticio/latent-audio-diffusion-ddim-256). You can use the pre-trained VAE to train your own latent diffusion models on a different set of audio files.33 34**22/10/2022**. Added DDIM encoder and ability to interpolate between audios in latent "noise" space. Mel spectrograms no longer have to be square (thanks to Tristan for this one), so you can set the vertical (frequency) and horizontal (time) resolutions independently.35 36**15/10/2022**. Added latent audio diffusion (see below). Also added the possibility to train a DDIM ([De-noising Diffusion Implicit Models](https://arxiv.org/pdf/2010.02502.pdf)). These have the benefit that samples can be generated with much fewer steps (~50) than used in training.37 38**4/10/2022**. It is now possible to mask parts of the input audio during generation which means you can stitch several samples together (think "out-painting").39 40**27/9/2022**. You can now generate an audio based on a previous one. You can use this to generate variations of the same audio or even to "remix" a track (via a sort of "style transfer"). You can find examples of how to do this in the [`test_model.ipynb`](https://colab.research.google.com/github/teticio/audio-diffusion/blob/master/notebooks/test_model.ipynb) notebook.41 42---43 44![mel spectrogram](https://user-images.githubusercontent.com/44233095/205305826-8b39c917-26c5-49b4-887c-776f5d69e970.png)45 46---47 48## DDPM ([De-noising Diffusion Probabilistic Models](https://arxiv.org/abs/2006.11239))49 50Audio can be represented as images by transforming to a [mel spectrogram](https://en.wikipedia.org/wiki/Mel-frequency_cepstrum), such as the one shown above. The class `Mel` in `mel.py` can convert a slice of audio into a mel spectrogram of `x_res` x `y_res` and vice versa. The higher the resolution, the less audio information will be lost. You can see how this works in the [`test_mel.ipynb`](https://github.com/teticio/audio-diffusion/blob/main/notebooks/test_mel.ipynb) notebook.51 52A DDPM is trained on a set of mel spectrograms that have been generated from a directory of audio files. It is then used to synthesize similar mel spectrograms, which are then converted back into audio.53 54You can play around with some pre-trained models on [Google Colab](https://colab.research.google.com/github/teticio/audio-diffusion/blob/master/notebooks/test_model.ipynb) or [Hugging Face spaces](https://huggingface.co/spaces/teticio/audio-diffusion). Check out some automatically generated loops [here](https://soundcloud.com/teticio2/sets/audio-diffusion-loops).55 56| Model | Dataset | Description |57|-------|---------|-------------|58| [teticio/audio-diffusion-256](https://huggingface.co/teticio/audio-diffusion-256) | [teticio/audio-diffusion-256](https://huggingface.co/datasets/teticio/audio-diffusion-256) | My "liked" Spotify playlist |59| [teticio/audio-diffusion-breaks-256](https://huggingface.co/teticio/audio-diffusion-breaks-256) | [teticio/audio-diffusion-breaks-256](https://huggingface.co/datasets/teticio/audio-diffusion-breaks-256) | Samples that have been used in music, sourced from [WhoSampled](https://whosampled.com) and [YouTube](https://youtube.com) |60| [teticio/audio-diffusion-instrumental-hiphop-256](https://huggingface.co/teticio/audio-diffusion-instrumental-hiphop-256) | [teticio/audio-diffusion-instrumental-hiphop-256](https://huggingface.co/datasets/teticio/audio-diffusion-instrumental-hiphop-256) | Instrumental Hip Hop music |61| [teticio/audio-diffusion-ddim-256](https://huggingface.co/teticio/audio-diffusion-ddim-256) | [teticio/audio-diffusion-256](https://huggingface.co/datasets/teticio/audio-diffusion-256) | De-noising Diffusion Implicit Model |62| [teticio/latent-audio-diffusion-256](https://huggingface.co/teticio/latent-audio-diffusion-256) | [teticio/audio-diffusion-256](https://huggingface.co/datasets/teticio/audio-diffusion-256) | Latent Audio Diffusion model |63| [teticio/latent-audio-diffusion-ddim-256](https://huggingface.co/teticio/latent-audio-diffusion-ddim-256) | [teticio/audio-diffusion-256](https://huggingface.co/datasets/teticio/audio-diffusion-256) | Latent Audio Diffusion Implicit Model |64| [teticio/conditional-latent-audio-diffusion-512](https://huggingface.co/teticio/latent-audio-diffusion-512) | [teticio/audio-diffusion-512](https://huggingface.co/datasets/teticio/audio-diffusion-512) | Conditional Latent Audio Diffusion Model |65 66---67 68## Generate Mel spectrogram dataset from directory of audio files69 70#### Install from GitHub (includes training scripts)71 72```bash73git clone https://github.com/teticio/audio-diffusion.git74cd audio-diffusion75pip install .76```77 78#### Install from PyPI79 80```bash81pip install audiodiffusion82```83 84#### Training can be run with Mel spectrograms of resolution 64x64 on a single commercial grade GPU (e.g. RTX 2080 Ti). The `hop_length` should be set to 1024 for better results85 86```bash87python scripts/audio_to_images.py \88--resolution 64,64 \89--hop_length 1024 \90--input_dir path-to-audio-files \91--output_dir path-to-output-data92```93 94#### Generate dataset of 256x256 Mel spectrograms and push to hub (you will need to be authenticated with `huggingface-cli login`)95 96```bash97python scripts/audio_to_images.py \98--resolution 256 \99--input_dir path-to-audio-files \100--output_dir data/audio-diffusion-256 \101--push_to_hub teticio/audio-diffusion-256102```103 104Note that the default `sample_rate` is 22050 and audios will be resampled if they are at a different rate. If you change this value, you may find that the results in the `test_mel.ipynb` notebook are not good (for example, if `sample_rate` is 48000) and that it is necessary to adjust `n_fft` (for example, to 2000 instead of the default value of 2048; alternatively, you can resample to a `sample_rate` of 44100). Make sure you use the same parameters for training and inference. You should also bear in mind that not all resolutions work with the neural network architecture as currently configured - you should be safe if you stick to powers of 2.105 106## Train model107 108#### Run training on local machine109 110```bash111accelerate launch --config_file config/accelerate_local.yaml \112scripts/train_unet.py \113--dataset_name data/audio-diffusion-64 \114--hop_length 1024 \115--output_dir models/ddpm-ema-audio-64 \116--train_batch_size 16 \117--num_epochs 100 \118--gradient_accumulation_steps 1 \119--learning_rate 1e-4 \120--lr_warmup_steps 500 \121--mixed_precision no122```123 124#### Run training on local machine with `batch_size` of 2 and `gradient_accumulation_steps` 8 to compensate, so that 256x256 resolution model fits on commercial grade GPU and push to hub125 126```bash127accelerate launch --config_file config/accelerate_local.yaml \128scripts/train_unet.py \129--dataset_name teticio/audio-diffusion-256 \130--output_dir models/audio-diffusion-256 \131--num_epochs 100 \132--train_batch_size 2 \133--eval_batch_size 2 \134--gradient_accumulation_steps 8 \135--learning_rate 1e-4 \136--lr_warmup_steps 500 \137--mixed_precision no \138--push_to_hub True \139--hub_model_id audio-diffusion-256 \140--hub_token $(cat $HOME/.huggingface/token)141```142 143#### Run training on SageMaker144 145```bash146accelerate launch --config_file config/accelerate_sagemaker.yaml \147scripts/train_unet.py \148--dataset_name teticio/audio-diffusion-256 \149--output_dir models/ddpm-ema-audio-256 \150--train_batch_size 16 \151--num_epochs 100 \152--gradient_accumulation_steps 1 \153--learning_rate 1e-4 \154--lr_warmup_steps 500 \155--mixed_precision no156```157 158## DDIM ([De-noising Diffusion Implicit Models](https://arxiv.org/pdf/2010.02502.pdf))159 160#### A DDIM can be trained by adding the parameter161 162```bash163--scheduler ddim164```165 166Inference can the be run with far fewer steps than the number used for training (e.g., ~50), allowing for much faster generation. Without retraining, the parameter `eta` can be used to replicate a DDPM if it is set to 1 or a DDIM if it is set to 0, with all values in between being valid. When `eta` is 0 (the default value), the de-noising procedure is deterministic, which means that it can be run in reverse as a kind of encoder that recovers the original noise used in generation. A function `encode` has been added to `AudioDiffusionPipeline` for this purpose. It is then possible to interpolate between audios in the latent "noise" space using the function `slerp` (Spherical Linear intERPolation).167 168## Latent Audio Diffusion169 170Rather than de-noising images directly, it is interesting to work in the "latent space" after first encoding images using an autoencoder. This has a number of advantages. Firstly, the information in the images is compressed into a latent space of a much lower dimension, so it is much faster to train de-noising diffusion models and run inference with them. Secondly, similar images tend to be clustered together and interpolating between two images in latent space can produce meaningful combinations.171 172At the time of writing, the Hugging Face `diffusers` library is geared towards inference and lacking in training functionality (rather like its cousin `transformers` in the early days of development). In order to train a VAE (Variational AutoEncoder), I use the [stable-diffusion](https://github.com/CompVis/stable-diffusion) repo from CompVis and convert the checkpoints to `diffusers` format. Note that it uses a perceptual loss function for images; it would be nice to try a perceptual *audio* loss function.173 174#### Train latent diffusion model using pre-trained VAE175 176```bash177accelerate launch ...178...179--vae teticio/latent-audio-diffusion-256180```181 182#### Install dependencies to train with Stable Diffusion183 184```bash185pip install omegaconf pytorch_lightning==1.7.7 torchvision einops186pip install -e git+https://github.com/CompVis/stable-diffusion.git@main#egg=latent-diffusion187pip install -e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers188```189 190#### Train an autoencoder191 192```bash193python scripts/train_vae.py \194--dataset_name teticio/audio-diffusion-256 \195--batch_size 2 \196--gradient_accumulation_steps 12197```198 199#### Train latent diffusion model200 201```bash202accelerate launch ...203...204--vae models/autoencoder-kl205```206 207## Conditional Audio Generation208 209We can generate audio conditional on a text prompt - or indeed anything which can be encoded into a bunch of numbers - much like DALL-E2, Midjourney and Stable Diffusion. It is generally harder to find good quality datasets of audios together with descriptions, although the people behind the dataset used to train Stable Diffusion are making some very interesting progress [here](https://github.com/LAION-AI/audio-dataset). I have chosen to encode the audio directly instead based on "how it sounds", using a [model which I trained on hundreds of thousands of Spotify playlists](https://github.com/teticio/Deej-AI). To encode an audio into a 100 dimensional vector210 211```python212from audiodiffusion.audio_encoder import AudioEncoder213 214audio_encoder = AudioEncoder.from_pretrained("teticio/audio-encoder")215audio_encoder.encode(['/home/teticio/Music/liked/Agua Re - Holy Dance - Large Sound Mix.mp3'])216```217 218Once you have prepared a dataset, you can encode the audio files with this script219 220```bash221python scripts/encode_audio \222--dataset_name teticio/audio-diffusion-256 \223--out_file data/encodings.p224```225 226Then you can train a model with227 228```bash229accelerate launch ...230...231--encodings data/encodings.p232```233 234When generating audios, you will need to pass an `encodings` Tensor. See the [`conditional_generation.ipynb`](https://colab.research.google.com/github/teticio/audio-diffusion/blob/master/notebooks/conditional_generation.ipynb) notebook for an example that uses encodings of Spotify track previews to influence the generation.235