Diffusers 文档

Kandinsky 2.2

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

Kandinsky 2.2

Kandinsky 2.2 由 Arseniy ShakhmatovAnton RazzhigaevAleksandr NikolichVladimir ArkhipkinIgor PavlovAndrey KuznetsovDenis Dimitrov 创建。

其 GitHub 页面的描述如下:

Kandinsky 2.2 在其前身 Kandinsky 2.1 的基础上带来了重大改进,引入了一个新的、更强大的图像编码器——CLIP-ViT-G,并支持 ControlNet。将图像编码器切换为 CLIP-ViT-G 显著增强了模型生成更具美感图片和更好理解文本的能力,从而提升了模型的整体性能。ControlNet 机制的加入使得模型能够有效地控制图像生成过程。这带来了更准确、视觉上更吸引人的输出,并为文本引导的图像处理开辟了新的可能性。

原始代码库可以在 ai-forever/Kandinsky-2 找到。

请查看 Hub 上的 Kandinsky 社区 组织,获取用于文本到图像、图像到图像和图像修复等任务的官方模型检查点。

请务必查看调度器指南,了解如何在调度器速度和质量之间进行权衡,并参阅在不同 pipeline 中复用组件部分,学习如何高效地将相同组件加载到多个 pipeline 中。

KandinskyV22PriorPipeline

class diffusers.KandinskyV22PriorPipeline

< >

( prior: PriorTransformer image_encoder: CLIPVisionModelWithProjection text_encoder: CLIPTextModelWithProjection tokenizer: CLIPTokenizer scheduler: UnCLIPScheduler image_processor: CLIPImageProcessor )

参数

  • prior (PriorTransformer) — 典型的 unCLIP prior 模型,用于从文本嵌入中近似图像嵌入。
  • image_encoder (CLIPVisionModelWithProjection) — 冻结的图像编码器。
  • text_encoder (CLIPTextModelWithProjection) — 冻结的文本编码器。
  • tokenizer (CLIPTokenizer) — CLIPTokenizer 类的分词器。
  • scheduler (UnCLIPScheduler) — 与 prior 结合使用的调度器,用于生成图像嵌入。
  • image_processor (CLIPImageProcessor) — 用于预处理 clip 图像的 image_processor。

用于为 Kandinsky 生成图像 prior 的 pipeline。

该模型继承自 DiffusionPipeline。请查看超类文档以了解该库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等)。

__call__

< >

( prompt: typing.Union[str, typing.List[str]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_images_per_prompt: int = 1 num_inference_steps: int = 25 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None guidance_scale: float = 4.0 output_type: typing.Optional[str] = 'pt' return_dict: bool = True callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] ) KandinskyPriorPipelineOutputtuple

参数

  • prompt (strList[str]) — 用于指导图像生成的提示或提示列表。
  • negative_prompt (strList[str]可选) — 不用于指导图像生成的提示或提示列表。当不使用引导时会被忽略(即,如果 guidance_scale 小于 1)。
  • num_images_per_prompt (int可选,默认为 1) — 每个提示生成的图像数量。
  • num_inference_steps (int可选,默认为 100) — 去噪步数。更多的去噪步数通常会带来更高质量的图像,但会牺牲推理速度。
  • generator (torch.GeneratorList[torch.Generator]可选) — 一个或多个 torch generator(s),用于使生成过程具有确定性。
  • latents (torch.Tensor可选) — 预生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同提示微调相同的生成过程。如果未提供,将使用提供的随机 generator 进行采样生成一个潜变量张量。
  • guidance_scale (float可选,默认为 4.0) — 无分类器扩散引导中定义的引导比例。guidance_scale 定义为 Imagen 论文中公式 2 的 w。通过设置 guidance_scale > 1 来启用引导比例。更高的引导比例会鼓励生成与文本 prompt 紧密相关的图像,但通常会牺牲较低的图像质量。
  • output_type (str可选,默认为 "pt") — 生成图像的输出格式。在 "np" (np.array) 或 "pt" (torch.Tensor) 之间选择。
  • return_dict (bool可选,默认为 True) — 是否返回 ImagePipelineOutput 而不是普通的元组。
  • callback_on_step_end (Callable可选) — 在推理过程中每个去噪步骤结束时调用的函数。该函数使用以下参数调用:`callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`。`callback_kwargs` 将包含由 `callback_on_step_end_tensor_inputs` 指定的所有张量的列表。
  • callback_on_step_end_tensor_inputs (List可选) — `callback_on_step_end` 函数的张量输入列表。列表中指定的张量将作为 `callback_kwargs` 参数传递。您只能包含 pipeline 类的 `._callback_tensor_inputs` 属性中列出的变量。

返回

KandinskyPriorPipelineOutputtuple

调用管道进行生成时调用的函数。

示例

>>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline
>>> import torch

>>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior")
>>> pipe_prior.to("cuda")
>>> prompt = "red cat, 4k photo"
>>> image_emb, negative_image_emb = pipe_prior(prompt).to_tuple()

>>> pipe = KandinskyV22Pipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder")
>>> pipe.to("cuda")
>>> image = pipe(
...     image_embeds=image_emb,
...     negative_image_embeds=negative_image_emb,
...     height=768,
...     width=768,
...     num_inference_steps=50,
... ).images
>>> image[0].save("cat.png")

interpolate

< >

( images_and_prompts: typing.List[typing.Union[str, PIL.Image.Image, torch.Tensor]] weights: typing.List[float] num_images_per_prompt: int = 1 num_inference_steps: int = 25 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None negative_prior_prompt: typing.Optional[str] = None negative_prompt: str = '' guidance_scale: float = 4.0 device = None ) KandinskyPriorPipelineOutputtuple

参数

  • images_and_prompts (List[Union[str, PIL.Image.Image, torch.Tensor]]) — 用于指导图像生成的提示和图像列表。
  • weights — (List[float]): `images_and_prompts` 中每个条件的权重列表。
  • num_images_per_prompt (int可选,默认为 1) — 每个提示生成的图像数量。
  • num_inference_steps (int可选,默认为 100) — 去噪步数。更多的去噪步数通常会带来更高质量的图像,但会牺牲推理速度。
  • generator (torch.GeneratorList[torch.Generator]可选) — 一个或多个 torch generator(s),用于使生成过程具有确定性。
  • latents (torch.Tensor可选) — 预生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同提示微调相同的生成过程。如果未提供,将使用提供的随机 generator 进行采样生成一个潜变量张量。
  • negative_prior_prompt (str可选) — 不用于指导 prior 扩散过程的提示。当不使用引导时会被忽略(即,如果 guidance_scale 小于 1)。
  • negative_prompt (strList[str]可选) — 不用于指导图像生成的提示。当不使用引导时会被忽略(即,如果 guidance_scale 小于 1)。
  • guidance_scale (float可选,默认为 4.0) — 无分类器扩散引导中定义的引导比例。guidance_scale 定义为 Imagen 论文中公式 2 的 w。通过设置 guidance_scale > 1 来启用引导比例。更高的引导比例会鼓励生成与文本 prompt 紧密相关的图像,但通常会牺牲较低的图像质量。

返回

KandinskyPriorPipelineOutputtuple

在使用 prior pipeline 进行插值时调用的函数。

示例

>>> from diffusers import KandinskyV22PriorPipeline, KandinskyV22Pipeline
>>> from diffusers.utils import load_image
>>> import PIL
>>> import torch
>>> from torchvision import transforms

>>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
... )
>>> pipe_prior.to("cuda")
>>> img1 = load_image(
...     "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
...     "/kandinsky/cat.png"
... )
>>> img2 = load_image(
...     "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
...     "/kandinsky/starry_night.jpeg"
... )
>>> images_texts = ["a cat", img1, img2]
>>> weights = [0.3, 0.3, 0.4]
>>> out = pipe_prior.interpolate(images_texts, weights)
>>> pipe = KandinskyV22Pipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16
... )
>>> pipe.to("cuda")
>>> image = pipe(
...     image_embeds=out.image_embeds,
...     negative_image_embeds=out.negative_image_embeds,
...     height=768,
...     width=768,
...     num_inference_steps=50,
... ).images[0]
>>> image.save("starry_cat.png")

KandinskyV22Pipeline

class diffusers.KandinskyV22Pipeline

< >

( unet: UNet2DConditionModel scheduler: DDPMScheduler movq: VQModel )

参数

  • scheduler (Union[DDIMScheduler,DDPMScheduler]) — 与 unet 结合使用的调度器,用于生成图像潜在表示 (image latents)。
  • unet (UNet2DConditionModel) — 用于对图像嵌入进行去噪的条件 U-Net 架构。
  • movq (VQModel) — MoVQ 解码器,用于从潜在表示生成图像。

使用 Kandinsky 进行文本到图像生成的 Pipeline

该模型继承自 DiffusionPipeline。请查看超类文档以了解该库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等)。

__call__

< >

( image_embeds: typing.Union[torch.Tensor, typing.List[torch.Tensor]] negative_image_embeds: typing.Union[torch.Tensor, typing.List[torch.Tensor]] height: int = 512 width: int = 512 num_inference_steps: int = 100 guidance_scale: float = 4.0 num_images_per_prompt: int = 1 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] **kwargs ) ImagePipelineOutputtuple

参数

  • image_embeds (torch.TensorList[torch.Tensor]) — 用于文本提示的 CLIP 图像嵌入,将用于条件化图像生成。
  • negative_image_embeds (torch.TensorList[torch.Tensor]) — 用于负面文本提示的 CLIP 图像嵌入,将用于条件化图像生成。
  • height (int, 可选, 默认为 512) — 生成图像的高度(以像素为单位)。
  • width (int, 可选, 默认为 512) — 生成图像的宽度(以像素为单位)。
  • num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但会牺牲推理速度。
  • guidance_scale (float, 可选, 默认为 4.0) — 指导尺度,定义于 Classifier-Free Diffusion Guidance 中。guidance_scaleImagen 论文 的公式 2 中定义为 w。通过设置 guidance_scale > 1 启用指导尺度。较高的指导尺度会鼓励生成与文本 prompt 紧密相关的图像,但通常会牺牲图像质量。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个或多个 torch generator(s),用于使生成过程具有确定性。
  • latents (torch.Tensor, 可选) — 预生成的噪声潜在表示,从高斯分布中采样,用作图像生成的输入。可用于调整使用不同提示的相同生成过程。如果未提供,将使用提供的随机 generator 进行采样生成一个潜在表示张量。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。可选值包括:"pil" (PIL.Image.Image)、"np" (np.array) 或 "pt" (torch.Tensor)。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ImagePipelineOutput 而不是普通的元组。
  • callback_on_step_end (Callable, 可选) — 在推理过程中每个去噪步骤结束时调用的函数。该函数调用时带有以下参数:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)callback_kwargs 将包含 callback_on_step_end_tensor_inputs 指定的所有张量列表。
  • callback_on_step_end_tensor_inputs (List, 可选) — callback_on_step_end 函数的张量输入列表。列表中指定的张量将作为 callback_kwargs 参数传递。您只能包含在您的 pipeline 类的 ._callback_tensor_inputs 属性中列出的变量。

返回

ImagePipelineOutputtuple

调用管道进行生成时调用的函数。

示例

>>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline
>>> import torch

>>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior")
>>> pipe_prior.to("cuda")
>>> prompt = "red cat, 4k photo"
>>> out = pipe_prior(prompt)
>>> image_emb = out.image_embeds
>>> zero_image_emb = out.negative_image_embeds
>>> pipe = KandinskyV22Pipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder")
>>> pipe.to("cuda")
>>> image = pipe(
...     image_embeds=image_emb,
...     negative_image_embeds=zero_image_emb,
...     height=768,
...     width=768,
...     num_inference_steps=50,
... ).images
>>> image[0].save("cat.png")

KandinskyV22CombinedPipeline

class diffusers.KandinskyV22CombinedPipeline

< >

( unet: UNet2DConditionModel scheduler: DDPMScheduler movq: VQModel prior_prior: PriorTransformer prior_image_encoder: CLIPVisionModelWithProjection prior_text_encoder: CLIPTextModelWithProjection prior_tokenizer: CLIPTokenizer prior_scheduler: UnCLIPScheduler prior_image_processor: CLIPImageProcessor )

参数

  • scheduler (Union[DDIMScheduler,DDPMScheduler]) — 与 unet 结合使用的调度器,用于生成图像潜在表示 (image latents)。
  • unet (UNet2DConditionModel) — 用于对图像嵌入进行去噪的条件 U-Net 架构。
  • movq (VQModel) — MoVQ 解码器,用于从潜在表示生成图像。
  • prior_prior (PriorTransformer) — 用于从文本嵌入中近似图像嵌入的规范 unCLIP 先验。
  • prior_image_encoder (CLIPVisionModelWithProjection) — 冻结的图像编码器。
  • prior_text_encoder (CLIPTextModelWithProjection) — 冻结的文本编码器。
  • prior_tokenizer (CLIPTokenizer) — CLIPTokenizer 类的分词器。
  • prior_scheduler (UnCLIPScheduler) — 与 prior 结合使用的调度器,用于生成图像嵌入。
  • prior_image_processor (CLIPImageProcessor) — 用于预处理来自 CLIP 的图像的图像处理器。

使用 Kandinsky 进行文本到图像生成的组合 Pipeline

该模型继承自 DiffusionPipeline。请查看超类文档以了解该库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等)。

__call__

< >

( prompt: typing.Union[str, typing.List[str]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_inference_steps: int = 100 guidance_scale: float = 4.0 num_images_per_prompt: int = 1 height: int = 512 width: int = 512 prior_guidance_scale: float = 4.0 prior_num_inference_steps: int = 25 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: int = 1 return_dict: bool = True prior_callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None prior_callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] ) ImagePipelineOutputtuple

参数

  • prompt (strList[str]) — 用于指导图像生成的提示或提示列表。
  • negative_prompt (strList[str], 可选) — 不用于指导图像生成的提示或提示列表。当不使用指导时(即,如果 guidance_scale 小于 1),此项将被忽略。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示生成的图像数量。
  • num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但会牺牲推理速度。
  • height (int, 可选, 默认为 512) — 生成图像的高度(以像素为单位)。
  • width (int, 可选, 默认为 512) — 生成图像的宽度(以像素为单位)。
  • prior_guidance_scale (float, 可选, 默认为 4.0) — 指导尺度,定义于 Classifier-Free Diffusion Guidance 中。guidance_scaleImagen 论文 的公式 2 中定义为 w。通过设置 guidance_scale > 1 启用指导尺度。较高的指导尺度会鼓励生成与文本 prompt 紧密相关的图像,但通常会牺牲图像质量。
  • prior_num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但会牺牲推理速度。
  • guidance_scale (float, 可选, 默认为 4.0) — 指导尺度,定义于 Classifier-Free Diffusion Guidance 中。guidance_scaleImagen 论文 的公式 2 中定义为 w。通过设置 guidance_scale > 1 启用指导尺度。较高的指导尺度会鼓励生成与文本 prompt 紧密相关的图像,但通常会牺牲图像质量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个或多个 torch generator(s),用于使生成过程具有确定性。
  • latents (torch.Tensor, 可选) — 预生成的噪声潜在表示,从高斯分布中采样,用作图像生成的输入。可用于调整使用不同提示的相同生成过程。如果未提供,将使用提供的随机 generator 进行采样生成一个潜在表示张量。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。可选值包括:"pil" (PIL.Image.Image)、"np" (np.array) 或 "pt" (torch.Tensor)。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ImagePipelineOutput 而不是普通的元组。
  • prior_callback_on_step_end (Callable, 可选) — 在先验 pipeline 推理过程中每个去噪步骤结束时调用的函数。该函数调用时带有以下参数:prior_callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
  • prior_callback_on_step_end_tensor_inputs (List, 可选) — prior_callback_on_step_end 函数的张量输入列表。列表中指定的张量将作为 callback_kwargs 参数传递。您只能包含在您的先验 pipeline 类的 ._callback_tensor_inputs 属性中列出的变量。
  • callback_on_step_end (Callable, 可选) — 在解码器 pipeline 推理过程中每个去噪步骤结束时调用的函数。该函数调用时带有以下参数:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)callback_kwargs 将包含 callback_on_step_end_tensor_inputs 指定的所有张量列表。
  • callback_on_step_end_tensor_inputs (List, 可选) — callback_on_step_end 函数的张量输入列表。列表中指定的张量将作为 callback_kwargs 参数传递。您只能包含在您的 pipeline 类的 ._callback_tensor_inputs 属性中列出的变量。

返回

ImagePipelineOutputtuple

调用管道进行生成时调用的函数。

示例

from diffusers import AutoPipelineForText2Image
import torch

pipe = AutoPipelineForText2Image.from_pretrained(
    "kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16
)
pipe.enable_model_cpu_offload()

prompt = "A lion in galaxies, spirals, nebulae, stars, smoke, iridescent, intricate detail, octane render, 8k"

image = pipe(prompt=prompt, num_inference_steps=25).images[0]

enable_sequential_cpu_offload

< >

( gpu_id: typing.Optional[int] = None device: typing.Union[torch.device, str] = None )

使用 accelerate 将所有模型卸载到 CPU,显著减少内存使用。调用时,unet、text_encoder、vae 和 safety checker 的状态字典将保存到 CPU,然后移动到 torch.device('meta'),仅当其特定子模块的 forward 方法被调用时才加载到 GPU。请注意,卸载是基于子模块的。内存节省比 enable_model_cpu_offload 更高,但性能较低。

KandinskyV22ControlnetPipeline

class diffusers.KandinskyV22ControlnetPipeline

< >

( unet: UNet2DConditionModel scheduler: DDPMScheduler movq: VQModel )

参数

  • scheduler (DDIMScheduler) — 与 unet 结合使用的调度器,用于生成图像潜在表示 (image latents)。
  • unet (UNet2DConditionModel) — 用于对图像嵌入进行去噪的条件 U-Net 架构。
  • movq (VQModel) — MoVQ 解码器,用于从潜在表示生成图像。

使用 Kandinsky 进行文本到图像生成的 Pipeline

该模型继承自 DiffusionPipeline。请查看超类文档以了解该库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等)。

__call__

< >

( image_embeds: typing.Union[torch.Tensor, typing.List[torch.Tensor]] negative_image_embeds: typing.Union[torch.Tensor, typing.List[torch.Tensor]] hint: Tensor height: int = 512 width: int = 512 num_inference_steps: int = 100 guidance_scale: float = 4.0 num_images_per_prompt: int = 1 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: int = 1 return_dict: bool = True ) ImagePipelineOutputtuple

参数

  • prompt (strList[str]) — 用于指导图像生成的提示或提示列表。
  • hint (torch.Tensor) — controlnet 条件。
  • image_embeds (torch.TensorList[torch.Tensor]) — 用于文本提示的 clip 图像嵌入,将用于对图像生成进行条件化。
  • negative_image_embeds (torch.TensorList[torch.Tensor]) — 用于负面文本提示的 clip 图像嵌入,将用于对图像生成进行条件化。
  • negative_prompt (strList[str], 可选) — 不用于指导图像生成的提示或提示列表。当不使用指导时(即,如果 guidance_scale 小于 1),此参数将被忽略。
  • height (int, 可选, 默认为 512) — 生成图像的高度(以像素为单位)。
  • width (int, 可选, 默认为 512) — 生成图像的宽度(以像素为单位)。
  • num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但代价是推理速度较慢。
  • guidance_scale (float, 可选, 默认为 4.0) — 指导尺度,定义于 Classifier-Free Diffusion Guidanceguidance_scale 定义为 Imagen 论文 中公式 2 的 w。通过设置 guidance_scale > 1 启用指导尺度。较高的指导尺度会鼓励生成与文本 prompt 紧密相关的图像,但通常会牺牲图像质量。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个或多个 torch generator(s),用于使生成过程具有确定性。
  • latents (torch.Tensor, 可选) — 预生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同提示微调相同的生成。如果未提供,将使用提供的随机 generator 采样生成潜变量张量。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。可选择:"pil" (PIL.Image.Image)、"np" (np.array) 或 "pt" (torch.Tensor)。
  • callback (Callable, 可选) — 一个在推理期间每 callback_steps 步调用的函数。该函数使用以下参数调用:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, 可选, 默认为 1) — 调用 callback 函数的频率。如果未指定,则在每一步都调用回调。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ImagePipelineOutput 而不是普通的元组。

返回

ImagePipelineOutputtuple

调用管道进行生成时调用的函数。

示例

KandinskyV22PriorEmb2EmbPipeline

class diffusers.KandinskyV22PriorEmb2EmbPipeline

< >

( prior: PriorTransformer image_encoder: CLIPVisionModelWithProjection text_encoder: CLIPTextModelWithProjection tokenizer: CLIPTokenizer scheduler: UnCLIPScheduler image_processor: CLIPImageProcessor )

参数

  • prior (PriorTransformer) — 典型的 unCLIP 先验模型,用于从文本嵌入中近似图像嵌入。
  • image_encoder (CLIPVisionModelWithProjection) — 冻结的图像编码器。
  • text_encoder (CLIPTextModelWithProjection) — 冻结的文本编码器。
  • tokenizer (CLIPTokenizer) — CLIPTokenizer 类的分词器。
  • scheduler (UnCLIPScheduler) — 与 prior 结合使用的调度器,用于生成图像嵌入。

用于为 Kandinsky 生成图像 prior 的 pipeline。

该模型继承自 DiffusionPipeline。请查看超类文档以了解该库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等)。

__call__

< >

( prompt: typing.Union[str, typing.List[str]] image: typing.Union[torch.Tensor, typing.List[torch.Tensor], PIL.Image.Image, typing.List[PIL.Image.Image]] strength: float = 0.3 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_images_per_prompt: int = 1 num_inference_steps: int = 25 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None guidance_scale: float = 4.0 output_type: typing.Optional[str] = 'pt' return_dict: bool = True ) KandinskyPriorPipelineOutputtuple

参数

  • prompt (strList[str]) — 用于指导图像生成的提示或提示列表。
  • strength (float, 可选, 默认为 0.8) — 概念上,指示对参考 emb 的转换程度。必须介于 0 和 1 之间。image 将被用作起点,strength 越大,添加的噪声越多。去噪步骤的数量取决于最初添加的噪声量。
  • emb (torch.Tensor) — 图像嵌入。
  • negative_prompt (strList[str], 可选) — 不用于指导图像生成的提示或提示列表。当不使用指导时(即,如果 guidance_scale 小于 1),此参数将被忽略。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示生成的图像数量。
  • num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但代价是推理速度较慢。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个或多个 torch generator(s),用于使生成过程具有确定性。
  • guidance_scale (float, 可选, 默认为 4.0) — 指导尺度,定义于 Classifier-Free Diffusion Guidanceguidance_scale 定义为 Imagen 论文 中公式 2 的 w。通过设置 guidance_scale > 1 启用指导尺度。较高的指导尺度会鼓励生成与文本 prompt 紧密相关的图像,但通常会牺牲图像质量。
  • output_type (str, 可选, 默认为 "pt") — 生成图像的输出格式。可选择:"np" (np.array) 或 "pt" (torch.Tensor)。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ImagePipelineOutput 而不是普通的元组。

返回

KandinskyPriorPipelineOutputtuple

调用管道进行生成时调用的函数。

示例

>>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorEmb2EmbPipeline
>>> import torch

>>> pipe_prior = KandinskyPriorPipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
... )
>>> pipe_prior.to("cuda")

>>> prompt = "red cat, 4k photo"
>>> img = load_image(
...     "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
...     "/kandinsky/cat.png"
... )
>>> image_emb, nagative_image_emb = pipe_prior(prompt, image=img, strength=0.2).to_tuple()

>>> pipe = KandinskyPipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-2-decoder, torch_dtype=torch.float16"
... )
>>> pipe.to("cuda")

>>> image = pipe(
...     image_embeds=image_emb,
...     negative_image_embeds=negative_image_emb,
...     height=768,
...     width=768,
...     num_inference_steps=100,
... ).images

>>> image[0].save("cat.png")

interpolate

< >

( images_and_prompts: typing.List[typing.Union[str, PIL.Image.Image, torch.Tensor]] weights: typing.List[float] num_images_per_prompt: int = 1 num_inference_steps: int = 25 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None negative_prior_prompt: typing.Optional[str] = None negative_prompt: str = '' guidance_scale: float = 4.0 device = None ) KandinskyPriorPipelineOutputtuple

参数

  • images_and_prompts (List[Union[str, PIL.Image.Image, torch.Tensor]]) — 用于指导图像生成的提示和图像列表。
  • weights — (List[float]): images_and_prompts 中每个条件的权重列表
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示生成的图像数量。
  • num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但代价是推理速度较慢。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个或多个 torch generator(s),用于使生成过程具有确定性。
  • latents (torch.Tensor, 可选) — 预生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同提示微调相同的生成。如果未提供,将使用提供的随机 generator 采样生成潜变量张量。
  • negative_prior_prompt (str, 可选) — 不用于指导先验扩散过程的提示。当不使用指导时(即,如果 guidance_scale 小于 1),此参数将被忽略。
  • negative_prompt (strList[str], 可选) — 不用于指导图像生成的提示。当不使用指导时(即,如果 guidance_scale 小于 1),此参数将被忽略。
  • guidance_scale (float, 可选, 默认为 4.0) — 指导尺度,定义于 Classifier-Free Diffusion Guidanceguidance_scale 定义为 Imagen 论文 中公式 2 的 w。通过设置 guidance_scale > 1 启用指导尺度。较高的指导尺度会鼓励生成与文本 prompt 紧密相关的图像,但通常会牺牲图像质量。

返回

KandinskyPriorPipelineOutputtuple

在使用 prior pipeline 进行插值时调用的函数。

示例

>>> from diffusers import KandinskyV22PriorEmb2EmbPipeline, KandinskyV22Pipeline
>>> from diffusers.utils import load_image
>>> import PIL

>>> import torch
>>> from torchvision import transforms

>>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
... )
>>> pipe_prior.to("cuda")

>>> img1 = load_image(
...     "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
...     "/kandinsky/cat.png"
... )

>>> img2 = load_image(
...     "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
...     "/kandinsky/starry_night.jpeg"
... )

>>> images_texts = ["a cat", img1, img2]
>>> weights = [0.3, 0.3, 0.4]
>>> image_emb, zero_image_emb = pipe_prior.interpolate(images_texts, weights)

>>> pipe = KandinskyV22Pipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16
... )
>>> pipe.to("cuda")

>>> image = pipe(
...     image_embeds=image_emb,
...     negative_image_embeds=zero_image_emb,
...     height=768,
...     width=768,
...     num_inference_steps=150,
... ).images[0]

>>> image.save("starry_cat.png")

KandinskyV22Img2ImgPipeline

class diffusers.KandinskyV22Img2ImgPipeline

< >

( unet: UNet2DConditionModel scheduler: DDPMScheduler movq: VQModel )

参数

  • scheduler (DDIMScheduler) — 与 unet 结合使用的调度器,用于生成图像潜变量。
  • unet (UNet2DConditionModel) — 用于对图像嵌入进行去噪的条件 U-Net 架构。
  • movq (VQModel) — MoVQ 解码器,用于从潜变量生成图像。

使用 Kandinsky 进行图生图的流水线

该模型继承自 DiffusionPipeline。请查看超类文档以了解该库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等)。

__call__

< >

( image_embeds: typing.Union[torch.Tensor, typing.List[torch.Tensor]] image: typing.Union[torch.Tensor, PIL.Image.Image, typing.List[torch.Tensor], typing.List[PIL.Image.Image]] negative_image_embeds: typing.Union[torch.Tensor, typing.List[torch.Tensor]] height: int = 512 width: int = 512 num_inference_steps: int = 100 guidance_scale: float = 4.0 strength: float = 0.3 num_images_per_prompt: int = 1 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] **kwargs ) ImagePipelineOutputtuple

参数

  • image_embeds (torch.TensorList[torch.Tensor]) — 用于文本提示的 clip 图像嵌入,将用于对图像生成进行条件化。
  • image (torch.Tensor, PIL.Image.Image, np.ndarray, List[torch.Tensor], List[PIL.Image.Image], 或 List[np.ndarray]) — Image 或表示图像批次的张量,将用作该过程的起点。也可以接受图像潜变量作为 image,如果直接传递潜变量,则不会再次编码。
  • strength (float, 可选, 默认为 0.8) — 概念上,指示对参考 image 的转换程度。必须介于 0 和 1 之间。image 将被用作起点,strength 越大,添加的噪声越多。去噪步骤的数量取决于最初添加的噪声量。当 strength 为 1 时,添加的噪声将达到最大,去噪过程将运行在 num_inference_steps 中指定的完整迭代次数。因此,值为 1 实际上会忽略 image
  • negative_image_embeds (torch.TensorList[torch.Tensor]) — 用于负面文本提示的 clip 图像嵌入,将用于对图像生成进行条件化。
  • height (int, 可选, 默认为 512) — 生成图像的高度(以像素为单位)。
  • width (int, 可选, 默认为 512) — 生成图像的宽度(以像素为单位)。
  • num_inference_steps (int, 可选, 默认为 100) — 去噪步数。更多的去噪步数通常会带来更高质量的图像,但代价是推理速度变慢。
  • guidance_scale (float, 可选, 默认为 4.0) — 指导比例(guidance scale),定义于无分类器扩散指导 (Classifier-Free Diffusion Guidance)论文中。guidance_scaleImagen 论文的公式2中定义为 w。通过设置 guidance_scale > 1 启用指导比例。更高的指导比例会鼓励生成与文本 prompt 紧密相关的图像,但通常会牺牲图像质量。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个或一组 torch generator(s),用于使生成过程具有确定性。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。可选值为:"pil" (PIL.Image.Image)、"np" (np.array) 或 "pt" (torch.Tensor)。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ImagePipelineOutput 而不是普通的元组。
  • callback_on_step_end (Callable, 可选) — 在推理过程中每个去噪步骤结束时调用的函数。该函数调用时会传入以下参数:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)callback_kwargs 将包含由 callback_on_step_end_tensor_inputs 指定的所有张量的列表。
  • callback_on_step_end_tensor_inputs (List, 可选) — callback_on_step_end 函数的张量输入列表。列表中指定的张量将作为 callback_kwargs 参数传递。你只能包含 pipeline 类中 ._callback_tensor_inputs 属性所列出的变量。

返回

ImagePipelineOutputtuple

调用管道进行生成时调用的函数。

示例

KandinskyV22Img2ImgCombinedPipeline

class diffusers.KandinskyV22Img2ImgCombinedPipeline

< >

( unet: UNet2DConditionModel scheduler: DDPMScheduler movq: VQModel prior_prior: PriorTransformer prior_image_encoder: CLIPVisionModelWithProjection prior_text_encoder: CLIPTextModelWithProjection prior_tokenizer: CLIPTokenizer prior_scheduler: UnCLIPScheduler prior_image_processor: CLIPImageProcessor )

参数

  • scheduler (Union[DDIMScheduler,DDPMScheduler]) — 一个与 unet 结合使用的调度器,用于生成图像潜变量。
  • unet (UNet2DConditionModel) — 用于对图像嵌入进行去噪的条件 U-Net 架构。
  • movq (VQModel) — MoVQ 解码器,用于从潜变量生成图像。
  • prior_prior (PriorTransformer) — 经典的 unCLIP 先验,用于从文本嵌入近似图像嵌入。
  • prior_image_encoder (CLIPVisionModelWithProjection) — 冻结的图像编码器。
  • prior_text_encoder (CLIPTextModelWithProjection) — 冻结的文本编码器。
  • prior_tokenizer (CLIPTokenizer) — 类别为 CLIPTokenizer 的分词器。
  • prior_scheduler (UnCLIPScheduler) — 一个与 prior 结合使用的调度器,用于生成图像嵌入。
  • prior_image_processor (CLIPImageProcessor) — 用于预处理来自 clip 的图像的 image_processor。

使用 Kandinsky 进行图生图的组合 Pipeline

该模型继承自 DiffusionPipeline。请查看超类文档以了解该库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等)。

__call__

< >

( prompt: typing.Union[str, typing.List[str]] image: typing.Union[torch.Tensor, PIL.Image.Image, typing.List[torch.Tensor], typing.List[PIL.Image.Image]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_inference_steps: int = 100 guidance_scale: float = 4.0 strength: float = 0.3 num_images_per_prompt: int = 1 height: int = 512 width: int = 512 prior_guidance_scale: float = 4.0 prior_num_inference_steps: int = 25 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: int = 1 return_dict: bool = True prior_callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None prior_callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] ) ImagePipelineOutputtuple

参数

  • prompt (strList[str]) — 用于指导图像生成的提示或提示列表。
  • image (torch.TensorPIL.Image.Imagenp.ndarrayList[torch.Tensor]List[PIL.Image.Image]List[np.ndarray]) — Image 或表示图像批次的张量,将作为该过程的起点。如果直接传递潜变量作为 image,则不会再次编码。
  • negative_prompt (strList[str], 可选) — 不用于指导图像生成的提示或提示列表。当不使用指导时(即如果 guidance_scale 小于 1),此参数将被忽略。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示生成的图像数量。
  • guidance_scale (float, 可选, 默认为 4.0) — 指导比例(guidance scale),定义于无分类器扩散指导 (Classifier-Free Diffusion Guidance)论文中。guidance_scaleImagen 论文的公式2中定义为 w。通过设置 guidance_scale > 1 启用指导比例。更高的指导比例会鼓励生成与文本 prompt 紧密相关的图像,但通常会牺牲图像质量。
  • strength (float, 可选, 默认为 0.3) — 概念上,表示对参考 image 的转换程度。必须在 0 到 1 之间。image 将作为起点,strength 越大,添加的噪声就越多。去噪步骤的数量取决于最初添加的噪声量。当 strength 为 1 时,添加的噪声将是最大的,去噪过程将运行在 num_inference_steps 中指定的完整迭代次数。因此,值为 1 实际上会忽略 image
  • num_inference_steps (int, 可选, 默认为 100) — 去噪步数。更多的去噪步数通常会带来更高质量的图像,但代价是推理速度变慢。
  • height (int, 可选, 默认为 512) — 生成图像的高度(以像素为单位)。
  • width (int, 可选, 默认为 512) — 生成图像的宽度(以像素为单位)。
  • prior_guidance_scale (float, 可选, 默认为 4.0) — 指导比例(guidance scale),定义于无分类器扩散指导 (Classifier-Free Diffusion Guidance)论文中。guidance_scaleImagen 论文的公式2中定义为 w。通过设置 guidance_scale > 1 启用指导比例。更高的指导比例会鼓励生成与文本 prompt 紧密相关的图像,但通常会牺牲图像质量。
  • prior_num_inference_steps (int, 可选, 默认为 100) — 去噪步数。更多的去噪步数通常会带来更高质量的图像,但代价是推理速度变慢。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个或一组 torch generator(s),用于使生成过程具有确定性。
  • latents (torch.Tensor, 可选) — 预生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于调整使用不同提示的相同生成。如果未提供,将使用提供的随机 generator 进行采样生成一个潜变量张量。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。可选值为:"pil" (PIL.Image.Image)、"np" (np.array) 或 "pt" (torch.Tensor)。
  • callback (Callable, 可选) — 在推理过程中每 callback_steps 步调用一次的函数。该函数调用时会传入以下参数:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, 可选, 默认为 1) — 调用 callback 函数的频率。如果未指定,则在每一步都调用回调函数。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ImagePipelineOutput 而不是普通的元组。

返回

ImagePipelineOutputtuple

调用管道进行生成时调用的函数。

示例

from diffusers import AutoPipelineForImage2Image
import torch
import requests
from io import BytesIO
from PIL import Image
import os

pipe = AutoPipelineForImage2Image.from_pretrained(
    "kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16
)
pipe.enable_model_cpu_offload()

prompt = "A fantasy landscape, Cinematic lighting"
negative_prompt = "low quality, bad quality"

url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"

response = requests.get(url)
image = Image.open(BytesIO(response.content)).convert("RGB")
image.thumbnail((768, 768))

image = pipe(prompt=prompt, image=original_image, num_inference_steps=25).images[0]

enable_model_cpu_offload

< >

( gpu_id: typing.Optional[int] = None device: typing.Union[torch.device, str] = None )

使用 accelerate 将所有模型卸载到 CPU,以较低的性能影响减少内存使用。与 enable_sequential_cpu_offload 相比,此方法在调用模型的 forward 方法时一次性将整个模型移至 GPU,并且模型会一直保留在 GPU 中直到下一个模型运行。内存节省量低于 enable_sequential_cpu_offload,但由于 unet 的迭代执行,性能要好得多。

enable_sequential_cpu_offload

< >

( gpu_id: typing.Optional[int] = None device: typing.Union[torch.device, str] = None )

使用 accelerate 将所有模型卸载到 CPU,显著减少内存使用。调用时,unet、text_encoder、vae 和 safety checker 的状态字典将保存到 CPU,然后移动到 torch.device('meta'),仅当其特定子模块的 forward 方法被调用时才加载到 GPU。请注意,卸载是基于子模块的。内存节省比 enable_model_cpu_offload 更高,但性能较低。

KandinskyV22ControlnetImg2ImgPipeline

class diffusers.KandinskyV22ControlnetImg2ImgPipeline

< >

( unet: UNet2DConditionModel scheduler: DDPMScheduler movq: VQModel )

参数

  • scheduler (DDIMScheduler) — 一个与 unet 结合使用的调度器,用于生成图像潜变量。
  • unet (UNet2DConditionModel) — 用于对图像嵌入进行去噪的条件 U-Net 架构。
  • movq (VQModel) — MoVQ 解码器,用于从潜变量生成图像。

使用 Kandinsky 进行图生图的流水线

该模型继承自 DiffusionPipeline。请查看超类文档以了解该库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等)。

__call__

< >

( image_embeds: typing.Union[torch.Tensor, typing.List[torch.Tensor]] image: typing.Union[torch.Tensor, PIL.Image.Image, typing.List[torch.Tensor], typing.List[PIL.Image.Image]] negative_image_embeds: typing.Union[torch.Tensor, typing.List[torch.Tensor]] hint: Tensor height: int = 512 width: int = 512 num_inference_steps: int = 100 guidance_scale: float = 4.0 strength: float = 0.3 num_images_per_prompt: int = 1 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None output_type: typing.Optional[str] = 'pil' callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: int = 1 return_dict: bool = True ) ImagePipelineOutputtuple

参数

  • image_embeds (torch.TensorList[torch.Tensor]) — 用于文本提示的 clip 图像嵌入,将用于条件化图像生成。
  • image (torch.TensorPIL.Image.Imagenp.ndarrayList[torch.Tensor]List[PIL.Image.Image]List[np.ndarray]) — Image 或表示图像批次的张量,将作为该过程的起点。如果直接传递潜变量作为 image,则不会再次编码。
  • strength (float, 可选, 默认为 0.8) — 概念上,表示对参考 image 的转换程度。必须在 0 到 1 之间。image 将作为起点,strength 越大,添加的噪声就越多。去噪步骤的数量取决于最初添加的噪声量。当 strength 为 1 时,添加的噪声将是最大的,去噪过程将运行在 num_inference_steps 中指定的完整迭代次数。因此,值为 1 实际上会忽略 image
  • hint (torch.Tensor) — controlnet 条件。
  • negative_image_embeds (torch.TensorList[torch.Tensor]) — 用于负面文本提示的 clip 图像嵌入,将用于条件化图像生成。
  • height (int, 可选, 默认为 512) — 生成图像的高度(以像素为单位)。
  • width (int, 可选, 默认为 512) — 生成图像的宽度(以像素为单位)。
  • num_inference_steps (int, 可选, 默认为 100) — 去噪步数。更多的去噪步数通常会带来更高质量的图像,但代价是推理速度变慢。
  • guidance_scale (float, optional, defaults to 4.0) — 指导比例(guidance scale),定义于Classifier-Free Diffusion Guidanceguidance_scale被定义为Imagen论文中公式2的w。通过设置guidance_scale > 1来启用指导比例。更高的指导比例会鼓励生成与文本prompt紧密相关的图像,但通常会牺牲图像质量。
  • num_images_per_prompt (int, optional, defaults to 1) — 每个提示(prompt)生成的图像数量。
  • generator (torch.Generator or List[torch.Generator], optional) — 一个或一组torch生成器,用于使生成过程具有确定性。
  • output_type (str, optional, defaults to "pil") — 生成图像的输出格式。可选择:"pil" (PIL.Image.Image)、"np" (np.array) 或 "pt" (torch.Tensor)。
  • callback (Callable, optional) — 一个在推理过程中每隔callback_steps步被调用的函数。该函数调用时带有以下参数:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, optional, defaults to 1) — 调用callback函数的频率。如果未指定,则在每一步都调用回调。
  • return_dict (bool, optional, defaults to True) — 是否返回ImagePipelineOutput而不是普通的元组。

返回

ImagePipelineOutputtuple

调用管道进行生成时调用的函数。

示例

KandinskyV22InpaintPipeline

class diffusers.KandinskyV22InpaintPipeline

< >

( unet: UNet2DConditionModel scheduler: DDPMScheduler movq: VQModel )

参数

  • scheduler (DDIMScheduler) — 与unet结合使用的调度器,用于生成图像潜变量。
  • unet (UNet2DConditionModel) — 用于对图像嵌入进行去噪的条件U-Net架构。
  • movq (VQModel) — 用于从潜变量生成图像的MoVQ解码器。

使用Kandinsky2.1进行文本引导图像修复的管线

该模型继承自 DiffusionPipeline。请查看超类文档以了解该库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等)。

__call__

< >

( image_embeds: typing.Union[torch.Tensor, typing.List[torch.Tensor]] image: typing.Union[torch.Tensor, PIL.Image.Image] mask_image: typing.Union[torch.Tensor, PIL.Image.Image, numpy.ndarray] negative_image_embeds: typing.Union[torch.Tensor, typing.List[torch.Tensor]] height: int = 512 width: int = 512 num_inference_steps: int = 100 guidance_scale: float = 4.0 num_images_per_prompt: int = 1 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] **kwargs ) ImagePipelineOutputtuple

参数

  • image_embeds (torch.Tensor or List[torch.Tensor]) — 用于文本提示的CLIP图像嵌入,将用于调节图像生成。
  • image (PIL.Image.Image) — Image,或表示要修复的图像批次的张量,图像的部分区域将被mask_image遮盖并根据prompt重新绘制。
  • mask_image (np.array) — 表示用于遮盖image的图像批次的张量。遮罩中的白色像素将被重新绘制,而黑色像素将被保留。如果mask_image是PIL图像,它在使用前将被转换为单通道(亮度)。如果是张量,它应该包含一个颜色通道(L)而不是3个,因此预期的形状是(B, H, W, 1)
  • negative_image_embeds (torch.Tensor or List[torch.Tensor]) — 用于负面文本提示的CLIP图像嵌入,将用于调节图像生成。
  • height (int, optional, defaults to 512) — 生成图像的高度(以像素为单位)。
  • width (int, optional, defaults to 512) — 生成图像的宽度(以像素为单位)。
  • num_inference_steps (int, optional, defaults to 100) — 去噪步数。更多的去噪步数通常会带来更高质量的图像,但会牺牲推理速度。
  • guidance_scale (float, optional, defaults to 4.0) — 指导比例(guidance scale),定义于Classifier-Free Diffusion Guidanceguidance_scale被定义为Imagen论文中公式2的w。通过设置guidance_scale > 1来启用指导比例。更高的指导比例会鼓励生成与文本prompt紧密相关的图像,但通常会牺牲图像质量。
  • num_images_per_prompt (int, optional, defaults to 1) — 每个提示(prompt)生成的图像数量。
  • generator (torch.Generator or List[torch.Generator], optional) — 一个或一组torch生成器,用于使生成过程具有确定性。
  • latents (torch.Tensor, optional) — 预生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于调整使用不同提示的相同生成过程。如果未提供,将使用提供的随机generator进行采样生成一个潜变量张量。
  • output_type (str, optional, defaults to "pil") — 生成图像的输出格式。可选择:"pil" (PIL.Image.Image)、"np" (np.array) 或 "pt" (torch.Tensor)。
  • return_dict (bool, optional, defaults to True) — 是否返回ImagePipelineOutput而不是普通的元组。
  • callback_on_step_end (Callable, optional) — 一个在推理过程中每个去噪步骤结束时调用的函数。该函数调用时带有以下参数:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)callback_kwargs将包含由callback_on_step_end_tensor_inputs指定的所有张量列表。
  • callback_on_step_end_tensor_inputs (List, optional) — 用于callback_on_step_end函数的张量输入列表。列表中指定的张量将作为callback_kwargs参数传递。你只能包含管线类._callback_tensor_inputs属性中列出的变量。

返回

ImagePipelineOutputtuple

调用管道进行生成时调用的函数。

示例

KandinskyV22InpaintCombinedPipeline

class diffusers.KandinskyV22InpaintCombinedPipeline

< >

( unet: UNet2DConditionModel scheduler: DDPMScheduler movq: VQModel prior_prior: PriorTransformer prior_image_encoder: CLIPVisionModelWithProjection prior_text_encoder: CLIPTextModelWithProjection prior_tokenizer: CLIPTokenizer prior_scheduler: UnCLIPScheduler prior_image_processor: CLIPImageProcessor )

参数

  • scheduler (Union[DDIMScheduler,DDPMScheduler]) — 与unet结合使用的调度器,用于生成图像潜变量。
  • unet (UNet2DConditionModel) — 用于对图像嵌入进行去噪的条件U-Net架构。
  • movq (VQModel) — 用于从潜变量生成图像的MoVQ解码器。
  • prior_prior (PriorTransformer) — 典型的unCLIP先验,用于从文本嵌入中近似图像嵌入。
  • prior_image_encoder (CLIPVisionModelWithProjection) — 冻结的图像编码器。
  • prior_text_encoder (CLIPTextModelWithProjection) — 冻结的文本编码器。
  • prior_tokenizer (CLIPTokenizer) — CLIPTokenizer类的分词器。
  • prior_scheduler (UnCLIPScheduler) — 与prior结合使用的调度器,用于生成图像嵌入。
  • prior_image_processor (CLIPImageProcessor) — 用于预处理来自CLIP的图像的图像处理器。

使用Kandinsky进行修复生成的组合管线

该模型继承自 DiffusionPipeline。请查看超类文档以了解该库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等)。

__call__

< >

( prompt: typing.Union[str, typing.List[str]] image: typing.Union[torch.Tensor, PIL.Image.Image, typing.List[torch.Tensor], typing.List[PIL.Image.Image]] mask_image: typing.Union[torch.Tensor, PIL.Image.Image, typing.List[torch.Tensor], typing.List[PIL.Image.Image]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_inference_steps: int = 100 guidance_scale: float = 4.0 num_images_per_prompt: int = 1 height: int = 512 width: int = 512 prior_guidance_scale: float = 4.0 prior_num_inference_steps: int = 25 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True prior_callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None prior_callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] **kwargs ) ImagePipelineOutputtuple

参数

  • prompt (str or List[str]) — 用于指导图像生成的提示或提示列表。
  • image (torch.Tensor, PIL.Image.Image, np.ndarray, List[torch.Tensor], List[PIL.Image.Image], or List[np.ndarray]) — Image,或表示图像批次的张量,将用作该过程的起点。也可以接受图像潜变量作为image,如果直接传递潜变量,则不会再次编码。
  • mask_image (np.array) — 表示用于遮盖image的图像批次的张量。遮罩中的白色像素将被重新绘制,而黑色像素将被保留。如果mask_image是PIL图像,它在使用前将被转换为单通道(亮度)。如果是张量,它应该包含一个颜色通道(L)而不是3个,因此预期的形状是(B, H, W, 1)
  • negative_prompt (str or List[str], optional) — 不用于指导图像生成的提示或提示列表。在不使用指导时(即,如果guidance_scale小于1)将被忽略。
  • num_images_per_prompt (int, optional, defaults to 1) — 每个提示(prompt)生成的图像数量。
  • guidance_scale (float, optional, defaults to 4.0) — 指导比例(guidance scale),定义于Classifier-Free Diffusion Guidanceguidance_scale被定义为Imagen论文中公式2的w。通过设置guidance_scale > 1来启用指导比例。更高的指导比例会鼓励生成与文本prompt紧密相关的图像,但通常会牺牲图像质量。
  • num_inference_steps (int, optional, defaults to 100) — 去噪步数。更多的去噪步数通常会带来更高质量的图像,但会牺牲推理速度。
  • height (int, optional, defaults to 512) — 生成图像的高度(以像素为单位)。
  • width (int, optional, defaults to 512) — 生成图像的宽度(以像素为单位)。
  • prior_guidance_scale (float, optional, defaults to 4.0) — 指导比例(guidance scale),定义于Classifier-Free Diffusion Guidanceguidance_scale被定义为Imagen论文中公式2的w。通过设置guidance_scale > 1来启用指导比例。更高的指导比例会鼓励生成与文本prompt紧密相关的图像,但通常会牺牲图像质量。
  • prior_num_inference_steps (int, optional, defaults to 100) — 去噪步数。更多的去噪步数通常会带来更高质量的图像,但会牺牲推理速度。
  • generator (torch.Generator or List[torch.Generator], optional) — 一个或一组torch生成器,用于使生成过程具有确定性。
  • latents (torch.Tensor, optional) — 预生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于调整使用不同提示的相同生成过程。如果未提供,将使用提供的随机generator进行采样生成一个潜变量张量。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。可选择:"pil" (PIL.Image.Image)、"np" (np.array) 或 "pt" (torch.Tensor)。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ImagePipelineOutput 而不是一个普通的元组。
  • prior_callback_on_step_end (Callable, 可选) — 一个在推理过程中每个去噪步骤结束时调用的函数。该函数调用时会传入以下参数:prior_callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
  • prior_callback_on_step_end_tensor_inputs (List, 可选) — prior_callback_on_step_end 函数的张量输入列表。列表中指定的张量将作为 callback_kwargs 参数传递。你只能包含 pipeline 类 ._callback_tensor_inputs 属性中列出的变量。
  • callback_on_step_end (Callable, 可选) — 一个在推理过程中每个去噪步骤结束时调用的函数。该函数调用时会传入以下参数:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)callback_kwargs 将包含 callback_on_step_end_tensor_inputs 指定的所有张量的列表。
  • callback_on_step_end_tensor_inputs (List, 可选) — callback_on_step_end 函数的张量输入列表。列表中指定的张量将作为 callback_kwargs 参数传递。你只能包含 pipeline 类 ._callback_tensor_inputs 属性中列出的变量。

返回

ImagePipelineOutputtuple

调用管道进行生成时调用的函数。

示例

from diffusers import AutoPipelineForInpainting
from diffusers.utils import load_image
import torch
import numpy as np

pipe = AutoPipelineForInpainting.from_pretrained(
    "kandinsky-community/kandinsky-2-2-decoder-inpaint", torch_dtype=torch.float16
)
pipe.enable_model_cpu_offload()

prompt = "A fantasy landscape, Cinematic lighting"
negative_prompt = "low quality, bad quality"

original_image = load_image(
    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png"
)

mask = np.zeros((768, 768), dtype=np.float32)
# Let's mask out an area above the cat's head
mask[:250, 250:-250] = 1

image = pipe(prompt=prompt, image=original_image, mask_image=mask, num_inference_steps=25).images[0]

enable_sequential_cpu_offload

< >

( gpu_id: typing.Optional[int] = None device: typing.Union[torch.device, str] = None )

使用 accelerate 将所有模型卸载到 CPU,显著减少内存使用。调用时,unet、text_encoder、vae 和 safety checker 的状态字典将保存到 CPU,然后移动到 torch.device('meta'),仅当其特定子模块的 forward 方法被调用时才加载到 GPU。请注意,卸载是基于子模块的。内存节省比 enable_model_cpu_offload 更高,但性能较低。

< > 在 GitHub 上更新