Diffusers 文档
aMUSEd
并获得增强的文档体验
开始使用
aMUSEd
aMUSEd 由 Suraj Patil、William Berman、Robin Rombach 和 Patrick von Platen 在 aMUSEd: An Open MUSE Reproduction 中提出。
Amused 是一种轻量级文本到图像模型,基于 MUSE 架构。Amused 在需要轻量级快速模型的应用中特别有用,例如一次性快速生成多张图像。
Amused 是一种基于 vqvae 令牌的 transformer,与许多扩散模型相比,它能以更少的前向传播生成图像。与 muse 不同,它使用较小的文本编码器 CLIP-L/14 而不是 t5-xxl。由于其参数数量少且前向传播生成过程少,amused 可以快速生成许多图像。这种优势在大批量处理时尤为明显。
论文摘要如下:
我们推出了 aMUSEd,这是一款基于 MUSE 的开源轻量级蒙版图像模型 (MIM),用于文本到图像生成。aMUSEd 拥有 MUSE 10% 的参数,专注于快速图像生成。我们认为与潜在扩散(主流的文本到图像生成方法)相比,MIM 尚未得到充分探索。与潜在扩散相比,MIM 需要更少的推理步骤,并且更具可解释性。此外,MIM 仅需一张图像即可进行微调以学习其他风格。我们希望通过展示 MIM 在大规模文本到图像生成中的有效性并发布可复现的训练代码来鼓励对 MIM 进行进一步探索。我们还发布了两种模型的检查点,它们直接生成 256x256 和 512x512 分辨率的图像。
模型 | 参数 |
---|---|
amused-256 | 603M |
amused-512 | 608M |
AmusedPipeline
class diffusers.AmusedPipeline
< 源代码 >( vqvae: VQModel tokenizer: CLIPTokenizer text_encoder: CLIPTextModelWithProjection transformer: UVit2DModel scheduler: AmusedScheduler )
__call__
< 源代码 >( prompt: typing.Union[str, typing.List[str], NoneType] = None height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 12 guidance_scale: float = 10.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_images_per_prompt: typing.Optional[int] = 1 generator: typing.Optional[torch._C.Generator] = None latents: typing.Optional[torch.IntTensor] = None prompt_embeds: typing.Optional[torch.Tensor] = None encoder_hidden_states: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None negative_encoder_hidden_states: typing.Optional[torch.Tensor] = None output_type = 'pil' return_dict: bool = True callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: int = 1 cross_attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None micro_conditioning_aesthetic_score: int = 6 micro_conditioning_crop_coord: typing.Tuple[int, int] = (0, 0) temperature: typing.Union[int, typing.Tuple[int, int], typing.List[int]] = (2, 0) ) → ImagePipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
,可选) — 用于引导图像生成的提示词。如果未定义,则需要传递prompt_embeds
。 - height (
int
,可选,默认为self.transformer.config.sample_size * self.vae_scale_factor
) — 生成图像的高度(以像素为单位)。 - width (
int
,可选,默认为self.unet.config.sample_size * self.vae_scale_factor
) — 生成图像的宽度(以像素为单位)。 - num_inference_steps (
int
,可选,默认为 16) — 去噪步数。去噪步数越多通常会生成更高质量的图像,但推理速度会变慢。 - guidance_scale (
float
,可选,默认为 10.0) — 较高的指导比例值会促使模型生成与文本prompt
紧密相关的图像,但会以较低的图像质量为代价。当guidance_scale > 1
时启用指导比例。 - negative_prompt (
str
或List[str]
,可选) — 引导图像生成中不包含的内容的提示词。如果未定义,则需要传递negative_prompt_embeds
。当不使用指导时 (guidance_scale < 1
) 忽略。 - num_images_per_prompt (
int
,可选,默认为 1) — 每个提示生成的图像数量。 - generator (
torch.Generator
,可选) — 用于使生成具有确定性的torch.Generator
。 - latents (
torch.IntTensor
,可选) — 预生成的令牌,表示self.vqvae
中的潜在向量,用作图像生成的输入。如果未提供,起始潜在向量将完全被遮罩。 - prompt_embeds (
torch.Tensor
,可选) — 预生成的文本嵌入。可用于轻松调整文本输入(提示权重)。如果未提供,文本嵌入将从prompt
输入参数生成。来自池化和投影的最终隐藏状态的单个向量。 - encoder_hidden_states (
torch.Tensor
,可选) — 预生成的来自文本编码器的倒数第二个隐藏状态,提供额外的文本条件。 - negative_prompt_embeds (
torch.Tensor
,可选) — 预生成的负面文本嵌入。可用于轻松调整文本输入(提示权重)。如果未提供,negative_prompt_embeds
将从negative_prompt
输入参数生成。 - negative_encoder_hidden_states (
torch.Tensor
,可选) — 与积极提示词的encoder_hidden_states
类似。 - output_type (
str
,可选,默认为"pil"
) — 生成图像的输出格式。在PIL.Image
或np.array
之间选择。 - return_dict (
bool
,可选,默认为True
) — 是否返回 StableDiffusionPipelineOutput 而不是普通的元组。 - 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
。 - micro_conditioning_aesthetic_score (
int
, 可选, 默认为 6) — 根据 laion 美学分类器设定的目标美学分数。请参阅 https://laion.ai/blog/laion-aesthetics/ 以及 https://huggingface.ac.cn/papers/2307.01952 中的微条件化部分。 - micro_conditioning_crop_coord (
Tuple[int]
, 可选, 默认为 (0, 0)) — 目标高度、宽度裁剪坐标。请参阅 https://huggingface.ac.cn/papers/2307.01952 中的微条件化部分。 - temperature (
Union[int, Tuple[int, int], List[int]]
, 可选, 默认为 (2, 0)) — 配置self.scheduler
上的温度调度器,请参阅AmusedScheduler#set_timesteps
。
返回
ImagePipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 ImagePipelineOutput,否则返回一个 tuple
,其中第一个元素是生成的图像列表。
用于生成的管道的调用函数。
enable_xformers_memory_efficient_attention
< 来源 >( attention_op: typing.Optional[typing.Callable] = None )
参数
- attention_op (
Callable
, 可选) — 覆盖默认的None
操作符,用作 xFormers 的memory_efficient_attention()
函数的op
参数。
启用 xFormers 的内存高效注意力。启用此选项后,您应该会观察到 GPU 内存使用量降低,并且推理速度可能会加快。训练期间的速度提升无法保证。
⚠️ 当内存高效注意力和切片注意力同时启用时,内存高效注意力优先。
示例
>>> import torch
>>> from diffusers import DiffusionPipeline
>>> from xformers.ops import MemoryEfficientAttentionFlashAttentionOp
>>> pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16)
>>> pipe = pipe.to("cuda")
>>> pipe.enable_xformers_memory_efficient_attention(attention_op=MemoryEfficientAttentionFlashAttentionOp)
>>> # Workaround for not accepting attention shape using VAE for Flash Attention
>>> pipe.vae.enable_xformers_memory_efficient_attention(attention_op=None)
class diffusers.AmusedImg2ImgPipeline
< 来源 >( vqvae: VQModel tokenizer: CLIPTokenizer text_encoder: CLIPTextModelWithProjection transformer: UVit2DModel scheduler: AmusedScheduler )
__call__
< 来源 >( prompt: typing.Union[str, typing.List[str], NoneType] = None image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]] = None strength: float = 0.5 num_inference_steps: int = 12 guidance_scale: float = 10.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_images_per_prompt: typing.Optional[int] = 1 generator: typing.Optional[torch._C.Generator] = None prompt_embeds: typing.Optional[torch.Tensor] = None encoder_hidden_states: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None negative_encoder_hidden_states: typing.Optional[torch.Tensor] = None output_type = 'pil' return_dict: bool = True callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: int = 1 cross_attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None micro_conditioning_aesthetic_score: int = 6 micro_conditioning_crop_coord: typing.Tuple[int, int] = (0, 0) temperature: typing.Union[int, typing.Tuple[int, int], typing.List[int]] = (2, 0) ) → ImagePipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
, 可选) — 用于引导图像生成的提示词。如果未定义,您需要传入prompt_embeds
。 - image (
torch.Tensor
,PIL.Image.Image
,np.ndarray
,List[torch.Tensor]
,List[PIL.Image.Image]
, 或List[np.ndarray]
) — 用作起点的图像、Numpy 数组或表示图像批次的张量。对于 Numpy 数组和 PyTorch 张量,预期值范围应在[0, 1]
之间。如果它是张量或张量列表,则预期形状应为(B, C, H, W)
或(C, H, W)
。如果它是 Numpy 数组或数组列表,则预期形状应为(B, H, W, C)
或(H, W, C)
。它也可以接受图像潜在表示作为image
,但如果直接传入潜在表示则不会再次编码。 - strength (
float
, 可选, 默认为 0.5) — 指示参考image
的转换程度。必须在 0 到 1 之间。image
用作起点,strength
越高,添加的噪声越多。去噪步骤的数量取决于最初添加的噪声量。当strength
为 1 时,添加的噪声最大,去噪过程将运行num_inference_steps
中指定的完整迭代次数。值为 1 基本忽略image
。 - num_inference_steps (
int
, 可选, 默认为 12) — 去噪步骤的数量。更多的去噪步骤通常会生成更高质量的图像,但推理速度会变慢。 - guidance_scale (
float
, 可选, 默认为 10.0) — 更高的引导尺度值鼓励模型生成与文本prompt
紧密相关的图像,但代价是图像质量会降低。当guidance_scale > 1
时启用引导尺度。 - negative_prompt (
str
或List[str]
, 可选) — 用于引导图像生成中不包含内容的提示词。如果未定义,您需要传入negative_prompt_embeds
。当不使用引导时(guidance_scale < 1
)将被忽略。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示要生成的图像数量。 - generator (
torch.Generator
, 可选) — 用于使生成确定性的torch.Generator
。 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入(提示权重)。如果未提供,则从prompt
输入参数生成文本嵌入。这是来自池化和投影后的最终隐藏状态的单个向量。 - encoder_hidden_states (
torch.Tensor
, 可选) — 文本编码器预生成的倒数第二个隐藏状态,提供额外的文本条件。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负面文本嵌入。可用于轻松调整文本输入(提示权重)。如果未提供,则从negative_prompt
输入参数生成negative_prompt_embeds
。 - negative_encoder_hidden_states (
torch.Tensor
, 可选) — 与正面提示的encoder_hidden_states
类似。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。可选择PIL.Image
或np.array
。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 StableDiffusionPipelineOutput 而不是普通元组。 - 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
的op
参数传入。 - micro_conditioning_aesthetic_score (
int
, 可选, 默认为 6) — 根据 laion 美学分类器设定的目标美学分数。请参阅 https://laion.ai/blog/laion-aesthetics/ 以及 https://huggingface.ac.cn/papers/2307.01952 中的微条件化部分。 - micro_conditioning_crop_coord (
Tuple[int]
, 可选, 默认为 (0, 0)) — 目标高度、宽度裁剪坐标。请参阅 https://huggingface.ac.cn/papers/2307.01952 中的微条件化部分。 - temperature (
Union[int, Tuple[int, int], List[int]]
, 可选, 默认为 (2, 0)) — 配置self.scheduler
上的温度调度器,请参阅AmusedScheduler#set_timesteps
。
返回
ImagePipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 ImagePipelineOutput,否则返回一个 tuple
,其中第一个元素是生成的图像列表。
用于生成的管道的调用函数。
示例
>>> import torch
>>> from diffusers import AmusedImg2ImgPipeline
>>> from diffusers.utils import load_image
>>> pipe = AmusedImg2ImgPipeline.from_pretrained(
... "amused/amused-512", variant="fp16", torch_dtype=torch.float16
... )
>>> pipe = pipe.to("cuda")
>>> prompt = "winter mountains"
>>> input_image = (
... load_image(
... "https://huggingface.co/datasets/diffusers/docs-images/resolve/main/open_muse/mountains.jpg"
... )
... .resize((512, 512))
... .convert("RGB")
... )
>>> image = pipe(prompt, input_image).images[0]
enable_xformers_memory_efficient_attention
< 来源 >( attention_op: typing.Optional[typing.Callable] = None )
参数
- attention_op (
Callable
, 可选) — 覆盖默认的None
操作符,用作 xFormers 的memory_efficient_attention()
函数的op
参数。
启用 xFormers 的内存高效注意力。启用此选项后,您应该会观察到 GPU 内存使用量降低,并且推理速度可能会加快。训练期间的速度提升无法保证。
⚠️ 当内存高效注意力和切片注意力同时启用时,内存高效注意力优先。
示例
>>> import torch
>>> from diffusers import DiffusionPipeline
>>> from xformers.ops import MemoryEfficientAttentionFlashAttentionOp
>>> pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16)
>>> pipe = pipe.to("cuda")
>>> pipe.enable_xformers_memory_efficient_attention(attention_op=MemoryEfficientAttentionFlashAttentionOp)
>>> # Workaround for not accepting attention shape using VAE for Flash Attention
>>> pipe.vae.enable_xformers_memory_efficient_attention(attention_op=None)
class diffusers.AmusedInpaintPipeline
< 来源 >( vqvae: VQModel tokenizer: CLIPTokenizer text_encoder: CLIPTextModelWithProjection transformer: UVit2DModel scheduler: AmusedScheduler )
__call__
< 来源 >( prompt: typing.Union[str, typing.List[str], NoneType] = None image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]] = None mask_image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]] = None strength: float = 1.0 num_inference_steps: int = 12 guidance_scale: float = 10.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_images_per_prompt: typing.Optional[int] = 1 generator: typing.Optional[torch._C.Generator] = None prompt_embeds: typing.Optional[torch.Tensor] = None encoder_hidden_states: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None negative_encoder_hidden_states: typing.Optional[torch.Tensor] = None output_type = 'pil' return_dict: bool = True callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: int = 1 cross_attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None micro_conditioning_aesthetic_score: int = 6 micro_conditioning_crop_coord: typing.Tuple[int, int] = (0, 0) temperature: typing.Union[int, typing.Tuple[int, int], typing.List[int]] = (2, 0) ) → ImagePipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
, 可选) — 用于引导图像生成的提示词。如果未定义,您需要传入prompt_embeds
。 - image (
torch.Tensor
,PIL.Image.Image
,np.ndarray
,List[torch.Tensor]
,List[PIL.Image.Image]
, 或List[np.ndarray]
) — 用作起点的图像、Numpy 数组或表示图像批次的张量。对于 Numpy 数组和 PyTorch 张量,预期值范围应在[0, 1]
之间。如果它是张量或张量列表,则预期形状应为(B, C, H, W)
或(C, H, W)
。如果它是 Numpy 数组或数组列表,则预期形状应为(B, H, W, C)
或(H, W, C)
。它也可以接受图像潜在表示作为image
,但如果直接传入潜在表示则不会再次编码。 - mask_image (
torch.Tensor
,PIL.Image.Image
,np.ndarray
,List[torch.Tensor]
,List[PIL.Image.Image]
, 或List[np.ndarray]
) — 图像、Numpy 数组或表示图像批次的张量,用于遮盖image
。遮罩中的白色像素将被重新绘制,而黑色像素将保留。如果mask_image
是 PIL 图像,它在使用前将转换为单通道(亮度)。如果它是 Numpy 数组或 PyTorch 张量,它应该包含一个颜色通道(L)而不是 3 个,因此 PyTorch 张量的预期形状为(B, 1, H, W)
、(B, H, W)
、(1, H, W)
或(H, W)
。对于 Numpy 数组,预期形状为(B, H, W, 1)
、(B, H, W)
、(H, W, 1)
或(H, W)
。 - strength (
float
, 可选, 默认为 1.0) — 指示参考image
的转换程度。必须在 0 到 1 之间。image
用作起点,strength
越高,添加的噪声越多。去噪步骤的数量取决于最初添加的噪声量。当strength
为 1 时,添加的噪声最大,去噪过程将运行num_inference_steps
中指定的完整迭代次数。值为 1 基本忽略image
。 - num_inference_steps (
int
, 可选, 默认为 16) — 去噪步骤的数量。更多的去噪步骤通常会生成更高质量的图像,但推理速度会变慢。 - guidance_scale (
float
, 可选, 默认为 10.0) — 更高的引导尺度值鼓励模型生成与文本prompt
紧密相关的图像,但代价是图像质量会降低。当guidance_scale > 1
时启用引导尺度。 - negative_prompt (
str
或List[str]
, 可选) — 用于引导图像生成中不包含内容的提示词。如果未定义,您需要传入negative_prompt_embeds
。当不使用引导时(guidance_scale < 1
)将被忽略。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示要生成的图像数量。 - generator (
torch.Generator
, 可选) — 用于使生成确定性的torch.Generator
。 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入(提示权重)。如果未提供,则从prompt
输入参数生成文本嵌入。这是来自池化和投影后的最终隐藏状态的单个向量。 - encoder_hidden_states (
torch.Tensor
, 可选) — 文本编码器预生成的倒数第二个隐藏状态,提供额外的文本条件。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负面文本嵌入。可用于轻松调整文本输入(提示权重)。如果未提供,则从negative_prompt
输入参数生成negative_prompt_embeds
。 - negative_encoder_hidden_states (
torch.Tensor
, 可选) — 类似于用于正面提示的encoder_hidden_states
。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。可在PIL.Image
或np.array
之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 StableDiffusionPipelineOutput 而非普通元组。 - callback (
Callable
, 可选) — 在推理过程中每callback_steps
步调用的函数。该函数调用时带有以下参数:callback(step: int, timestep: int, latents: torch.Tensor)
。 - callback_steps (
int
, 可选, 默认为 1) — 调用callback
函数的频率。如果未指定,则在每一步都调用回调。 - cross_attention_kwargs (
dict
, 可选) — 如果指定,此 kwargs 字典将作为op
参数传递给self.processor
中定义的AttentionProcessor
。 - micro_conditioning_aesthetic_score (
int
, 可选, 默认为 6) — 根据 laion 美学分类器设定的目标美学分数。请参阅 https://laion.ai/blog/laion-aesthetics/ 和 https://huggingface.ac.cn/papers/2307.01952 中的微条件部分。 - micro_conditioning_crop_coord (
Tuple[int]
, 可选, 默认为 (0, 0)) — 目标高度、宽度裁剪坐标。请参阅 https://huggingface.ac.cn/papers/2307.01952 中的微条件部分。 - temperature (
Union[int, Tuple[int, int], List[int]]
, 可选, 默认为 (2, 0)) — 配置self.scheduler
上的温度调度器,请参阅AmusedScheduler#set_timesteps
。
返回
ImagePipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 ImagePipelineOutput,否则返回一个 tuple
,其中第一个元素是生成的图像列表。
用于生成的管道的调用函数。
示例
>>> import torch
>>> from diffusers import AmusedInpaintPipeline
>>> from diffusers.utils import load_image
>>> pipe = AmusedInpaintPipeline.from_pretrained(
... "amused/amused-512", variant="fp16", torch_dtype=torch.float16
... )
>>> pipe = pipe.to("cuda")
>>> prompt = "fall mountains"
>>> input_image = (
... load_image(
... "https://huggingface.co/datasets/diffusers/docs-images/resolve/main/open_muse/mountains_1.jpg"
... )
... .resize((512, 512))
... .convert("RGB")
... )
>>> mask = (
... load_image(
... "https://huggingface.co/datasets/diffusers/docs-images/resolve/main/open_muse/mountains_1_mask.png"
... )
... .resize((512, 512))
... .convert("L")
... )
>>> pipe(prompt, input_image, mask).images[0].save("out.png")
enable_xformers_memory_efficient_attention
< 来源 >( attention_op: typing.Optional[typing.Callable] = None )
参数
- attention_op (
Callable
, 可选) — 覆盖默认的None
运算符,用作 xFormers 的memory_efficient_attention()
函数的op
参数。
启用 xFormers 的内存高效注意力。启用此选项后,您应该会观察到 GPU 内存使用量降低,并且推理速度可能会加快。训练期间的速度提升无法保证。
⚠️ 当内存高效注意力和切片注意力同时启用时,内存高效注意力优先。
示例
>>> import torch
>>> from diffusers import DiffusionPipeline
>>> from xformers.ops import MemoryEfficientAttentionFlashAttentionOp
>>> pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16)
>>> pipe = pipe.to("cuda")
>>> pipe.enable_xformers_memory_efficient_attention(attention_op=MemoryEfficientAttentionFlashAttentionOp)
>>> # Workaround for not accepting attention shape using VAE for Flash Attention
>>> pipe.vae.enable_xformers_memory_efficient_attention(attention_op=None)