audio-diffusion

teticioGPL-3.0活跃维护

用 Hugging Face diffusers 把扩散模型从图像搬到音频,能直接训练和生成音乐片段,省去自己搭扩散流程的麻烦。

Apply diffusion models using the new Hugging Face diffusers package to synthesize music instead of images.

v1.5.7发布于 2024-09-25 15:51 UTC795star76fork同步于 2026-11-07 07:30 UTC

怎么上手

由 README 提炼,执行前请确认
pip install audiodiffusion
硬件要求按 README 说明

README 提到 64x64 梅尔频谱图训练可在单块商用级 GPU(如 RTX 2080 Ti)上运行;未给出具体显存数值,也未说明 CPU 运行或支持的 GPU 厂商范围。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

用 Hugging Face diffusers 把音频转成梅尔频谱图后做扩散生成,合成音乐片段

用 Hugging Face diffusers 把扩散模型从图像搬到音频,能直接训练和生成音乐片段,省去自己搭扩散流程的麻烦。

核心能力

  • 将音频切片转成 x_res×y_res 梅尔频谱图并可逆回音频
  • 支持 DDPM 训练与 DDIM 少步采样(约 50 步)
  • 支持在潜空间做音频插值、变体生成与风格迁移
  • 生成时可掩码输入音频局部,实现拼接式 out-painting
  • 支持以文本或音频编码为条件的条件音频生成

容易踩的坑

默认 sample_rate 为 22050,音频会被重采样,改动该值可能影响结果;使用预训练模型需从 Hugging Face 下载,推送到 hub 前需先 huggingface-cli login;训练 Stable Diffusion 相关模型需额外安装 omegaconf、pytorch_lightning>=2.0 等依赖及 CompVis 的 latent-diffusion、taming-transformers。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v1.5.7
  1. v1.5.7最新正式版2024-09-25 · 2024-09-25 15:51 UTC

    Update to latest version of accelerate

  2. v1.5.62023-05-28 · 2023-05-28 16:04 UTC

    这个版本没有写更新说明。

  3. v1.5.52023-05-23 · 2023-05-23 13:42 UTC

    这个版本没有写更新说明。

  4. v1.5.42023-05-17 · 2023-05-17 15:17 UTC

    这个版本没有写更新说明。

  5. v1.5.3v.1.5.32023-04-24 · 2023-04-24 07:30 UTC

    这个版本没有写更新说明。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    title: Audio Diffusion emoji: 🎵 colorFrom: pink colorTo: blue sdk: gradio sdk_version: 3.1.4 app_file: app.py pinned: false license: gpl-3.0

    audio-diffusion Open in Colab

    Apply diffusion models to synthesize music instead of images using the new Hugging Face diffusers package


    Sample automatically generated loop

    https://user-images.githubusercontent.com/44233095/204103172-27f25d63-5e77-40ca-91ab-d04a45d4726f.mp4

    Go to https://soundcloud.com/teticio2/sets/audio-diffusion-loops for more examples.


    Updates

    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.

    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.

    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).

    7/11/2022. Added pre-trained latent audio diffusion models teticio/latent-audio-diffusion-256 and 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.

    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.

    15/10/2022. Added latent audio diffusion (see below). Also added the possibility to train a DDIM (De-noising Diffusion Implicit Models). These have the benefit that samples can be generated with much fewer steps (~50) than used in training.

    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").

    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 notebook.


    mel spectrogram


    DDPM (De-noising Diffusion Probabilistic Models)

    Audio can be represented as images by transforming to a mel spectrogram, 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 notebook.

    A 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.

    You can play around with some pre-trained models on Google Colab or Hugging Face spaces. Check out some automatically generated loops here.

    ModelDatasetDescription
    teticio/audio-diffusion-256teticio/audio-diffusion-256My "liked" Spotify playlist
    teticio/audio-diffusion-breaks-256teticio/audio-diffusion-breaks-256Samples that have been used in music, sourced from WhoSampled and YouTube
    teticio/audio-diffusion-instrumental-hiphop-256teticio/audio-diffusion-instrumental-hiphop-256Instrumental Hip Hop music
    teticio/audio-diffusion-ddim-256teticio/audio-diffusion-256De-noising Diffusion Implicit Model
    teticio/latent-audio-diffusion-256teticio/audio-diffusion-256Latent Audio Diffusion model
    teticio/latent-audio-diffusion-ddim-256teticio/audio-diffusion-256Latent Audio Diffusion Implicit Model
    teticio/conditional-latent-audio-diffusion-512teticio/audio-diffusion-512Conditional Latent Audio Diffusion Model

    Generate Mel spectrogram dataset from directory of audio files

    Install from GitHub (includes training scripts)
    git clone https://github.com/teticio/audio-diffusion.git
    cd audio-diffusion
    pip install .
    
    Install from PyPI
    pip install audiodiffusion
    
    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 results
    python scripts/audio_to_images.py \
    --resolution 64,64 \
    --hop_length 1024 \
    --input_dir path-to-audio-files \
    --output_dir path-to-output-data
    
    Generate dataset of 256x256 Mel spectrograms and push to hub (you will need to be authenticated with huggingface-cli login)
    python scripts/audio_to_images.py \
    --resolution 256 \
    --input_dir path-to-audio-files \
    --output_dir data/audio-diffusion-256 \
    --push_to_hub teticio/audio-diffusion-256
    

    Note 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.

    Train model

    Run training on local machine
    accelerate launch --config_file config/accelerate_local.yaml \
    scripts/train_unet.py \
    --dataset_name data/audio-diffusion-64 \
    --hop_length 1024 \
    --output_dir models/ddpm-ema-audio-64 \
    --train_batch_size 16 \
    --num_epochs 100 \
    --gradient_accumulation_steps 1 \
    --learning_rate 1e-4 \
    --lr_warmup_steps 500 \
    --mixed_precision no
    
    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 hub
    accelerate launch --config_file config/accelerate_local.yaml \
    scripts/train_unet.py \
    --dataset_name teticio/audio-diffusion-256 \
    --output_dir models/audio-diffusion-256 \
    --num_epochs 100 \
    --train_batch_size 2 \
    --eval_batch_size 2 \
    --gradient_accumulation_steps 8 \
    --learning_rate 1e-4 \
    --lr_warmup_steps 500 \
    --mixed_precision no \
    --push_to_hub True \
    --hub_model_id audio-diffusion-256 \
    --hub_token $(cat $HOME/.huggingface/token)
    
    Run training on SageMaker
    accelerate launch --config_file config/accelerate_sagemaker.yaml \
    scripts/train_unet.py \
    --dataset_name teticio/audio-diffusion-256 \
    --output_dir models/ddpm-ema-audio-256 \
    --train_batch_size 16 \
    --num_epochs 100 \
    --gradient_accumulation_steps 1 \
    --learning_rate 1e-4 \
    --lr_warmup_steps 500 \
    --mixed_precision no
    

    DDIM (De-noising Diffusion Implicit Models)

    A DDIM can be trained by adding the parameter
    --scheduler ddim
    

    Inference 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).

    Latent Audio Diffusion

    Rather 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.

    At 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 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.

    Train latent diffusion model using pre-trained VAE
    accelerate launch ...
    ...
    --vae teticio/latent-audio-diffusion-256
    
    Install dependencies to train with Stable Diffusion
    pip install omegaconf pytorch_lightning>=2.0 torchvision einops
    pip install -e git+https://github.com/CompVis/stable-diffusion.git@main#egg=latent-diffusion
    pip install -e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers
    
    Train an autoencoder
    python scripts/train_vae.py \
    --dataset_name teticio/audio-diffusion-256 \
    --batch_size 2 \
    --gradient_accumulation_steps 12
    
    Train latent diffusion model
    accelerate launch ...
    ...
    --vae models/autoencoder-kl
    

    Conditional Audio Generation

    We 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. 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. To encode an audio into a 100 dimensional vector

    from audiodiffusion.audio_encoder import AudioEncoder
    
    audio_encoder = AudioEncoder.from_pretrained("teticio/audio-encoder")
    audio_encoder.encode(['/home/teticio/Music/liked/Agua Re - Holy Dance - Large Sound Mix.mp3'])
    

    Once you have prepared a dataset, you can encode the audio files with this script

    python scripts/encode_audio \
    --dataset_name teticio/audio-diffusion-256 \
    --out_file data/encodings.p
    

    Then you can train a model with

    accelerate launch ...
    ...
    --encodings data/encodings.p
    

    When generating audios, you will need to pass an encodings Tensor. See the conditional_generation.ipynb notebook for an example that uses encodings of Spotify track previews to influence the generation.