Diffusers 文件

MusicLDM

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

MusicLDM

MusicLDM 在 Ke Chen、Yusong Wu、Haohe Liu、Marianna Nezhurina、Taylor Berg-Kirkpatrick 和 Shlomo Dubnov 提出的MusicLDM: Enhancing Novelty in Text-to-Music Generation Using Beat-Synchronous Mixup Strategies 中被提出。MusicLDM 接收文字提示作為輸入,並預測相應的音樂樣本。

Stable DiffusionAudioLDM 的啟發,MusicLDM 是一個文字到音樂的 潛在擴散模型 (LDM),它從 CLAP 潛在空間學習連續的音訊表示。

MusicLDM 在 466 小時的音樂資料語料庫上進行訓練。節拍同步資料增強策略被應用於音樂樣本,包括時域和潛在空間。使用節拍同步資料增強策略鼓勵模型在訓練樣本之間進行插值,但保持在訓練資料的領域內。結果是生成的音樂更加多樣化,同時保持了相應的風格。

論文摘要如下:

擴散模型在跨模態生成任務中顯示出有希望的結果,包括文字到影像和文字到音訊生成。然而,作為一種特殊型別的音訊,生成音樂面臨獨特的挑戰,因為音樂資料可用性有限以及與版權和剽竊相關的敏感問題。在本文中,為了解決這些挑戰,我們首先構建了一個最先進的文字到音樂模型 MusicLDM,它將 Stable Diffusion 和 AudioLDM 架構應用於音樂領域。我們透過在音樂資料樣本集合上重新訓練對比語言-音訊預訓練模型 (CLAP) 和 Hifi-GAN 聲音編碼器(作為 MusicLDM 的元件)來實現這一目標。然後,為了解決訓練資料的限制並避免剽竊,我們利用節拍跟蹤模型並提出了兩種不同的資料增強混合策略:節拍同步音訊混合和節拍同步潛在混合,它們分別直接或透過潛在嵌入空間重新組合訓練音訊。這種混合策略鼓勵模型在音樂訓練樣本之間進行插值,並在訓練資料的凸包內生成新的音樂,使生成的音樂更加多樣化,同時仍然忠實於相應的風格。除了流行的評估指標外,我們還設計了一些基於 CLAP 分數的新評估指標,以證明我們提出的 MusicLDM 和節拍同步混合策略提高了生成音樂的質量和新穎性,以及輸入文字與生成音樂之間的對應關係。

此管道由 sanchit-gandhi 貢獻。

提示

在構建提示時,請記住:

  • 描述性的提示輸入效果最佳;使用形容詞描述聲音(例如,“高品質”或“清晰”),並儘可能使提示上下文特定(例如,“帶有快速節拍和合成器旋律的電子舞曲”比“電子舞曲”效果更好)。
  • 使用*負面提示*可以顯著提高生成音訊的質量。嘗試使用“低質量,一般質量”作為負面提示。

在推理過程中

  • 生成音訊樣本的*質量*可以透過 `num_inference_steps` 引數控制;步數越多,音訊質量越高,但推理速度越慢。
  • 可以一次生成多個波形:將 `num_waveforms_per_prompt` 設定為大於 1 的值即可啟用。生成波形和提示文字之間將執行自動評分,並根據其與文字輸入的餘弦相似度(在聯合文字-音訊嵌入空間中)對音訊進行從最佳到最差的排名。
  • 生成音訊樣本的*長度*可以透過更改 `audio_length_in_s` 引數來控制。

請務必檢視排程器指南,瞭解如何權衡排程器速度和質量,並檢視跨管道重用元件部分,瞭解如何高效地將相同元件載入到多個管道中。

MusicLDMPipeline

class diffusers.MusicLDMPipeline

< >

( vae: AutoencoderKL text_encoder: typing.Union[transformers.models.clap.modeling_clap.ClapTextModelWithProjection, transformers.models.clap.modeling_clap.ClapModel] tokenizer: typing.Union[transformers.models.roberta.tokenization_roberta.RobertaTokenizer, transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast] feature_extractor: typing.Optional[transformers.models.clap.feature_extraction_clap.ClapFeatureExtractor] unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers vocoder: SpeechT5HifiGan )

__call__

< >

( prompt: typing.Union[str, typing.List[str]] = None audio_length_in_s: typing.Optional[float] = None num_inference_steps: int = 200 guidance_scale: float = 2.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_waveforms_per_prompt: typing.Optional[int] = 1 eta: float = 0.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None return_dict: bool = True callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: typing.Optional[int] = 1 cross_attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None output_type: typing.Optional[str] = 'np' ) AudioPipelineOutputtuple

引數

  • prompt (strList[str], 可選) — 用於引導音訊生成的提示。如果未定義,您需要傳遞 prompt_embeds
  • audio_length_in_s (int, 可選, 預設為 10.24) — 生成音訊樣本的長度(秒)。
  • num_inference_steps (int, 可選, 預設為 200) — 去噪步數。更多去噪步數通常會帶來更高質量的音訊,但會犧牲推理速度。
  • guidance_scale (float, 可選, 預設為 2.0) — 較高的引導比例值鼓勵模型生成與文字 `prompt` 密切相關的音訊,但會犧牲較低的音質。當 `guidance_scale > 1` 時啟用引導比例。
  • negative_prompt (strList[str], 可選) — 用於引導音訊生成中不包含的內容的提示。如果未定義,您需要改為傳遞 negative_prompt_embeds。不使用引導時(guidance_scale < 1)忽略。
  • num_waveforms_per_prompt (int, 可選, 預設為 1) — 為每個提示生成的波形數量。如果 num_waveforms_per_prompt > 1,且文字編碼模型是聯合文字-音訊模型 (ClapModel),且分詞器是 [~transformers.ClapProcessor],則將在生成的輸出和輸入文字之間執行自動評分。該評分根據生成的波形與聯合文字-音訊嵌入空間中文字輸入的餘弦相似度進行排名。
  • eta (float, 可選, 預設為 0.0) — 對應於 DDIM 論文中的引數 eta (η)。僅適用於 DDIMScheduler,在其他排程器中被忽略。
  • generator (torch.GeneratorList[torch.Generator], 可選) — torch.Generator,用於使生成確定性。
  • latents (torch.Tensor, 可選) — 預生成的從高斯分佈取樣的噪聲潛在變數,用作影像生成的輸入。可用於使用不同提示微調相同的生成。如果未提供,則使用提供的隨機 generator 取樣生成潛在張量。
  • prompt_embeds (torch.Tensor, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入(提示詞權重)。如果未提供,則從 prompt 輸入引數生成文字嵌入。
  • negative_prompt_embeds (torch.Tensor, 可選) — 預生成的負面文字嵌入。可用於輕鬆調整文字輸入(提示詞權重)。如果未提供,則從 negative_prompt 輸入引數生成 negative_prompt_embeds
  • return_dict (bool, 可選, 預設為 True) — 是否返回 AudioPipelineOutput 而不是普通元組。
  • callback (Callable, 可選) — 在推理過程中每隔 callback_steps 步呼叫的函式。該函式以以下引數呼叫:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, 可選, 預設為 1) — 呼叫 callback 函式的頻率。如果未指定,則在每一步都呼叫回撥。
  • cross_attention_kwargs (dict, 可選) — 一個 kwargs 字典,如果指定,將作為引數傳遞給 self.processor 中定義的 AttentionProcessor
  • output_type (str, 可選, 預設為 "np") — 生成音訊的輸出格式。選擇 "np" 返回 NumPy np.ndarray"pt" 返回 PyTorch torch.Tensor 物件。設定為 "latent" 返回潛在擴散模型 (LDM) 輸出。

返回

AudioPipelineOutputtuple

如果 return_dictTrue,則返回 AudioPipelineOutput,否則返回一個 tuple,其中第一個元素是包含生成音訊的列表。

用於生成的管道的呼叫函式。

示例

>>> from diffusers import MusicLDMPipeline
>>> import torch
>>> import scipy

>>> repo_id = "ucsd-reach/musicldm"
>>> pipe = MusicLDMPipeline.from_pretrained(repo_id, torch_dtype=torch.float16)
>>> pipe = pipe.to("cuda")

>>> prompt = "Techno music with a strong, upbeat tempo and high melodic riffs"
>>> audio = pipe(prompt, num_inference_steps=10, audio_length_in_s=5.0).audios[0]

>>> # save the audio sample as a .wav file
>>> scipy.io.wavfile.write("techno.wav", rate=16000, data=audio)

enable_model_cpu_offload

< >

( gpu_id = 0 )

使用 accelerate 將所有模型解除安裝到 CPU,在對效能影響較低的情況下減少記憶體使用。與 enable_sequential_cpu_offload 相比,此方法在呼叫 forward 方法時一次將一個完整的模型移動到加速器,並且模型保留在加速器中直到下一個模型執行。記憶體節省低於 enable_sequential_cpu_offload,但由於 unet 的迭代執行,效能要好得多。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.