Diffusers 文档
AudioLDM
并获得增强的文档体验
开始使用
AudioLDM
AudioLDM 在 Haohe Liu 等人的论文《AudioLDM:使用潜在扩散模型进行文本到音频生成》中被提出。受 Stable Diffusion 的启发,AudioLDM 是一种文本到音频的潜在扩散模型 (LDM),它从 CLAP 潜在空间学习连续音频表示。AudioLDM 接受文本提示作为输入,并预测相应的音频。它可以生成文本条件的声音效果、人声和音乐。
论文摘要如下
文本到音频(TTA)系统最近因其基于文本描述合成通用音频的能力而受到关注。然而,先前在 TTA 中的研究限制了生成质量,且计算成本很高。在本研究中,我们提出了 AudioLDM,一个 TTA 系统,它建立在潜在空间上,以学习来自对比语言-音频预训练(CLAP)潜在空间的连续音频表示。预训练的 CLAP 模型使我们能够使用音频嵌入训练 LDM,同时在采样期间提供文本嵌入作为条件。通过学习音频信号及其组成的潜在表示,而无需对跨模态关系进行建模,AudioLDM 在生成质量和计算效率方面都具有优势。在 AudioCaps 上使用单个 GPU 训练,AudioLDM 在客观和主观指标(例如,弗雷歇距离)衡量下,实现了最先进的 TTA 性能。此外,AudioLDM 是第一个支持以零样本方式进行各种文本引导音频操作(例如,风格迁移)的 TTA 系统。我们的实现和演示可在 这个 https URL 上找到。
原始代码库可以在 haoheliu/AudioLDM 中找到。
提示
在构建提示时,请记住
- 描述性提示输入效果最佳;您可以使用形容词来描述声音(例如,“高质量”或“清晰”),并使提示上下文具体化(例如,“森林中的水流”而不是“水流”)。
- 最好使用“猫”或“狗”等通用术语,而不是模型可能不熟悉的特定名称或抽象对象。
在推理过程中
- 预测音频样本的质量可以通过
num_inference_steps
参数控制;更高的步数会带来更高的音频质量,但会牺牲更慢的推理速度。 - 预测音频样本的长度可以通过更改
audio_length_in_s
参数来控制。
请务必查看 Schedulers 指南,了解如何探索 scheduler 速度和质量之间的权衡,并查看 在 pipelines 之间重用组件 部分,了解如何有效地将相同的组件加载到多个 pipelines 中。
AudioLDMPipeline
类 diffusers.AudioLDMPipeline
< 源代码 >( vae: AutoencoderKL text_encoder: ClapTextModelWithProjection tokenizer: typing.Union[transformers.models.roberta.tokenization_roberta.RobertaTokenizer, transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast] unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers vocoder: SpeechT5HifiGan )
参数
- vae (AutoencoderKL) — 变分自编码器 (VAE) 模型,用于将图像编码和解码为潜在表示形式。
- text_encoder (ClapTextModelWithProjection) — 冻结的文本编码器 (
ClapTextModelWithProjection
),特指 laion/clap-htsat-unfused 变体。 - tokenizer (
PreTrainedTokenizer
) — 用于标记化文本的 RobertaTokenizer。 - unet (UNet2DConditionModel) — 一个
UNet2DConditionModel
,用于对编码的音频潜在表示进行去噪。 - scheduler (SchedulerMixin) — 调度器,与
unet
结合使用,对编码的音频潜在表示进行去噪。可以是 DDIMScheduler、LMSDiscreteScheduler 或 PNDMScheduler 之一。 - vocoder (SpeechT5HifiGan) —
SpeechT5HifiGan
类的声码器。
使用 AudioLDM 进行文本到音频生成的 Pipeline。
此模型继承自 DiffusionPipeline。有关所有 pipeline 通用的方法(下载、保存、在特定设备上运行等),请查看超类文档。
__call__
< source >( prompt: typing.Union[str, typing.List[str]] = None audio_length_in_s: typing.Optional[float] = None num_inference_steps: int = 10 guidance_scale: float = 2.5 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' ) → AudioPipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
, 可选) — 用于引导音频生成的提示或提示列表。如果未定义,则需要传递prompt_embeds
。 - audio_length_in_s (
int
, 可选, 默认为 5.12) — 生成音频样本的长度,以秒为单位。 - num_inference_steps (
int
, 可选, 默认为 10) — 去噪步骤的数量。更多去噪步骤通常会带来更高质量的音频,但会牺牲推理速度。 - guidance_scale (
float
, 可选, 默认为 2.5) — 更高的 guidance scale 值会鼓励模型生成与文本prompt
紧密相关的音频,但会降低音质。当guidance_scale > 1
时,guidance scale 生效。 - negative_prompt (
str
或List[str]
, 可选) — 用于引导音频生成中不应包含的内容的提示或提示列表。如果未定义,则需要传递negative_prompt_embeds
。不使用 guidance 时忽略 (guidance_scale < 1
)。 - num_waveforms_per_prompt (
int
, 可选, 默认为 1) — 每个提示要生成的波形数量。 - eta (
float
, 可选, 默认为 0.0) — 对应于 DDIM 论文中的参数 eta (η)。仅适用于 DDIMScheduler,在其他调度器中将被忽略。 - generator (
torch.Generator
或List[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"
(返回 NumPynp.ndarray
)或"pt"
(返回 PyTorchtorch.Tensor
对象)之间选择。
返回值
AudioPipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 AudioPipelineOutput,否则返回 tuple
,其中第一个元素是包含生成的音频的列表。
pipeline 的调用函数,用于生成。
示例
>>> from diffusers import AudioLDMPipeline
>>> import torch
>>> import scipy
>>> repo_id = "cvssp/audioldm-s-full-v2"
>>> pipe = AudioLDMPipeline.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)
AudioPipelineOutput
class diffusers.AudioPipelineOutput
< 源码 >( audios: ndarray )
音频管道的输出类。