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 通过引入新的、更强大的图像编码器 - CLIP-ViT-G 和 ControlNet 支持,在其前身 Kandinsky 2.1 的基础上进行了重大改进。 切换到 CLIP-ViT-G 作为图像编码器,显著提高了模型生成更美观图片和更好理解文本的能力,从而提升了模型的整体性能。 ControlNet 机制的加入,使得模型可以有效地控制图像生成过程。 这带来了更准确、更具视觉吸引力的输出,并为文本引导的图像操作开辟了新的可能性。

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

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

请务必查看 schedulers 指南,了解如何探索 scheduler 速度和质量之间的权衡,并查看 跨 pipelines 重用组件 部分,了解如何有效地将相同组件加载到多个 pipelines 中。

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 的图像的图像处理器。

用于为 Kandinsky 生成图像先验的 Pipeline

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

__call__

< >

( prompt: Union negative_prompt: Union = None num_images_per_prompt: int = 1 num_inference_steps: int = 25 generator: Union = None latents: Optional = None guidance_scale: float = 4.0 output_type: Optional = 'pt' return_dict: bool = True callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['latents'] ) KandinskyPriorPipelineOutput or tuple

参数

  • prompt (strList[str]) — 引导图像生成的提示或提示语。
  • negative_prompt (strList[str], optional) — 不引导图像生成的提示或提示语。当不使用引导时忽略(即,如果 guidance_scale 小于 1 则忽略)。
  • num_images_per_prompt (int, optional, 默认为 1) — 每个提示要生成的图像数量。
  • num_inference_steps (int, optional, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会以较慢的推理速度为代价,带来更高质量的图像。
  • generator (torch.GeneratorList[torch.Generator], optional) — 一个或一组 torch generator(s) 以使生成具有确定性。
  • latents (torch.Tensor, optional) — 预生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于通过不同的提示调整相同的生成结果。如果未提供,则将使用提供的随机 generator 采样生成潜变量张量。
  • guidance_scale (float, optional, 默认为 4.0) — Classifier-Free Diffusion Guidance 中定义的引导缩放。 guidance_scale 定义为 Imagen Paper 公式 2 中的 w。通过设置 guidance_scale > 1 启用引导缩放。较高的引导缩放鼓励生成与文本 prompt 紧密相关的图像,但通常以降低图像质量为代价。
  • output_type (str, optional, 默认为 "pt") — 生成图像的输出格式。在以下选项之间选择:"np" (np.array) 或 "pt" (torch.Tensor)。
  • return_dict (bool, optional, 默认为 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 参数传递。您将只能包含 pipeline 类的 ._callback_tensor_inputs 属性中列出的变量。

返回值

KandinskyPriorPipelineOutputtuple

调用 pipeline 进行生成时调用的函数。

示例

>>> 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: List weights: List num_images_per_prompt: int = 1 num_inference_steps: int = 25 generator: Union = None latents: Optional = None negative_prior_prompt: Optional = 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, optional, 默认为 1) — 每个提示要生成的图像数量。
  • num_inference_steps (int, optional, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会以较慢的推理速度为代价,带来更高质量的图像。
  • generator (torch.GeneratorList[torch.Generator], optional) — 一个或一组 torch generator(s) 以使生成具有确定性。
  • latents (torch.Tensor, optional) — 预生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于通过不同的提示调整相同的生成结果。如果未提供,则将使用提供的随机 generator 采样生成潜变量张量。
  • negative_prior_prompt (str, optional) — 不引导先验扩散过程的提示。当不使用引导时忽略(即,如果 guidance_scale 小于 1 则忽略)。
  • negative_prompt (strList[str], 可选) — 用于指导图像生成时 *不* желаемый 的提示。当不使用 guidance 时会被忽略 (即,如果 guidance_scale 小于 1,则忽略此参数)。
  • guidance_scale (float, 可选, 默认为 4.0) — Guidance scale,定义在 Classifier-Free Diffusion Guidance 中。 guidance_scale 定义为 Imagen Paper 的公式 2 中的 w。通过设置 guidance_scale > 1 启用 Guidance scale。较高的 guidance scale 鼓励生成与文本 prompt 紧密相关的图像,但通常会以降低图像质量为代价。

返回值

KandinskyPriorPipelineOutputtuple

使用先验 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 结合使用以生成图像潜在表示 (latent)。
  • unet (UNet2DConditionModel) — 条件 U-Net 架构,用于对图像嵌入 (embedding) 进行去噪。
  • movq (VQModel) — MoVQ 解码器,用于从潜在表示 (latent) 生成图像。

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

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

__call__

< >

( image_embeds: Union negative_image_embeds: Union height: int = 512 width: int = 512 num_inference_steps: int = 100 guidance_scale: float = 4.0 num_images_per_prompt: int = 1 generator: Union = None latents: Optional = None output_type: Optional = 'pil' return_dict: bool = True callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['latents'] **kwargs ) ImagePipelineOutputtuple

参数

  • image_embeds (torch.TensorList[torch.Tensor]) — 用于文本 prompt 的 clip 图像嵌入 (embeddings),将用于条件图像生成。
  • negative_image_embeds (torch.TensorList[torch.Tensor]) — 用于负面文本 prompt 的 clip 图像嵌入 (embeddings),将用于条件图像生成。
  • height (int, 可选, 默认为 512) — 生成图像的高度像素值。
  • width (int, 可选, 默认为 512) — 生成图像的宽度像素值。
  • num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但会以较慢的推理速度为代价。
  • guidance_scale (float, 可选, 默认为 4.0) — Guidance scale,定义在 Classifier-Free Diffusion Guidance 中。 guidance_scale 定义为 Imagen Paper 的公式 2 中的 w。通过设置 guidance_scale > 1 启用 Guidance scale。较高的 guidance scale 鼓励生成与文本 prompt 紧密相关的图像,但通常会以降低图像质量为代价。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个 prompt 生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个或一组 torch generator(s),用于使生成结果确定化。
  • latents (torch.Tensor, 可选) — 预生成的噪声潜在表示 (latents),从高斯分布中采样,用作图像生成的输入。可用于通过不同的 prompt 调整相同的生成结果。如果未提供,则将通过使用提供的随机 generator 进行采样来生成潜在表示 (latents) 张量。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。从以下选项中选择:"pil" (PIL.Image.Image), "np" (np.array) 或 "pt" (torch.Tensor)。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ImagePipelineOutput 而不是普通的 tuple。
  • 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

调用 pipeline 进行生成时调用的函数。

示例

>>> 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 结合使用以生成图像潜在表示 (latent)。
  • 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) — 一个 image_processor,用于预处理来自 clip 的图像。

用于使用 Kandinsky 进行文本到图像生成的组合管线

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

__call__

< >

( prompt: Union negative_prompt: Union = 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: Union = None latents: Optional = None output_type: Optional = 'pil' callback: Optional = None callback_steps: int = 1 return_dict: bool = True prior_callback_on_step_end: Optional = None prior_callback_on_step_end_tensor_inputs: List = ['latents'] callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['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) — 无分类器扩散引导中定义的引导缩放。 guidance_scale 定义为 Imagen Paper 的公式 2 中的 w。通过设置 guidance_scale > 1 启用引导缩放。较高的引导缩放鼓励生成与文本 prompt 紧密相关的图像,但通常以较低的图像质量为代价。
  • prior_num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会以较慢的推理速度为代价,带来更高质量的图像。
  • guidance_scale (float, 可选, 默认为 4.0) — 无分类器扩散引导中定义的引导缩放。 guidance_scale 定义为 Imagen Paper 的公式 2 中的 w。通过设置 guidance_scale > 1 启用引导缩放。较高的引导缩放鼓励生成与文本 prompt 紧密相关的图像,但通常以较低的图像质量为代价。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个或一组 torch 生成器,用于使生成结果确定。
  • 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, 可选) — 一个函数,在 prior 管线的推理过程中,在每个去噪步骤结束时调用。该函数使用以下参数调用: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 参数传递。您将只能包含在 prior 管线类的 ._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

调用 pipeline 进行生成时调用的函数。

示例

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: Optional = None device: Union = 'cuda' )

使用 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 结合使用以生成图像潜在空间。
  • unet (UNet2DConditionModel) — 条件 U-Net 架构,用于去噪图像嵌入。
  • movq (VQModel) — MoVQ 解码器,用于从潜在空间生成图像。

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

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

__call__

< >

( image_embeds: Union negative_image_embeds: Union 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: Union = None latents: Optional = None output_type: Optional = 'pil' callback: Optional = 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) — Guidance scale,如 Classifier-Free Diffusion Guidance 中定义。 guidance_scale 定义为 Imagen Paper 等式 2 中的 w。通过设置 guidance_scale > 1 启用 Guidance scale。较高的 guidance scale 鼓励生成与文本 prompt 紧密相关的图像,但通常以降低图像质量为代价。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示要生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 用于使生成确定性的一个或多个 torch generator
  • 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

调用 pipeline 进行生成时调用的函数。

示例

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 生成图像先验的 Pipeline

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

__call__

< >

( prompt: Union image: Union strength: float = 0.3 negative_prompt: Union = None num_images_per_prompt: int = 1 num_inference_steps: int = 25 generator: Union = None guidance_scale: float = 4.0 output_type: Optional = 'pt' return_dict: bool = True ) KandinskyPriorPipelineOutputtuple

参数

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

返回值

KandinskyPriorPipelineOutputtuple

调用 pipeline 进行生成时调用的函数。

示例

>>> 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: List weights: List num_images_per_prompt: int = 1 num_inference_steps: int = 25 generator: Union = None latents: Optional = None negative_prior_prompt: Optional = 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, optional, 默认为 1) — 每个提示词生成的图像数量。
  • generator (torch.Generator or List[torch.Generator], optional) — One or a list of torch generator(s) to make generation deterministic.
  • latents (torch.Tensor, optional) — Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor will ge generated by sampling using the supplied random generator.
  • negative_prior_prompt (str, optional) — The prompt not to guide the prior diffusion process. Ignored when not using guidance (i.e., ignored if guidance_scale is less than 1).
  • negative_prompt (str or List[str], optional) — The prompt not to guide the image generation. Ignored when not using guidance (i.e., ignored if guidance_scale is less than 1).
  • guidance_scale (float, optional, defaults to 4.0) — Guidance scale as defined in Classifier-Free Diffusion Guidance. guidance_scale is defined as w of equation 2. of Imagen Paper. Guidance scale is enabled by setting guidance_scale > 1. Higher guidance scale encourages to generate images that are closely linked to the text prompt, usually at the expense of lower image quality.

返回值

KandinskyPriorPipelineOutputtuple

使用先验 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) — A scheduler to be used in combination with unet to generate image latents.
  • unet (UNet2DConditionModel) — 用于去噪图像嵌入的条件 U-Net 架构。
  • movq (VQModel) — MoVQ 解码器,用于从潜在空间生成图像。

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

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

__call__

< >

( image_embeds: Union image: Union negative_image_embeds: Union 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: Union = None output_type: Optional = 'pil' return_dict: bool = True callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['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 Guidanceguidance_scale 定义为 Imagen Paper 的等式 2 中的 w。通过设置 guidance_scale > 1 启用 Guidance scale。较高的 guidance scale 鼓励生成与文本 prompt 紧密相关的图像,通常以降低图像质量为代价。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个 prompt 生成的图像数量。
  • 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 而不是普通的 tuple。
  • 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 参数传递。您将只能包含管道类的 ._callback_tensor_inputs 属性中列出的变量。

返回值

ImagePipelineOutputtuple

调用 pipeline 进行生成时调用的函数。

示例

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,用于从文本嵌入近似图像嵌入。
  • 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: Union image: Union negative_prompt: Union = 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: Union = None latents: Optional = None output_type: Optional = 'pil' callback: Optional = None callback_steps: int = 1 return_dict: bool = True prior_callback_on_step_end: Optional = None prior_callback_on_step_end_tensor_inputs: List = ['latents'] callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['latents'] ) ImagePipelineOutput or tuple

参数

  • prompt (strList[str]) — 用于引导图像生成的提示或提示列表。
  • image (torch.Tensor, PIL.Image.Image, np.ndarray, List[torch.Tensor], List[PIL.Image.Image], 或 List[np.ndarray]) — 图像,或表示图像批次的张量,将用作流程的起点。也可以接受图像潜在表示作为 image,如果直接传递潜在表示,则不会再次编码。
  • negative_prompt (strList[str], 可选) — 不用于引导图像生成的提示或提示列表。当不使用引导时忽略(即,如果 guidance_scale 小于 1 则忽略)。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示生成的图像数量。
  • guidance_scale (float, 可选, 默认为 4.0) — 引导缩放比例,如 Classifier-Free Diffusion Guidance 中定义。 guidance_scale 定义为 Imagen Paper 等式 2 中的 w。通过设置 guidance_scale > 1 启用引导缩放比例。较高的引导缩放比例鼓励生成与文本 prompt 紧密相关的图像,但通常以较低的图像质量为代价。
  • strength (float, 可选, 默认为 0.3) — 从概念上讲,表示要转换参考 image 的程度。必须介于 0 和 1 之间。image 将用作起点,强度越大,添加的噪声越多。去噪步骤的数量取决于最初添加的噪声量。当 strength 为 1 时,添加的噪声将是最大的,并且去噪过程将运行 num_inference_steps 中指定的完整迭代次数。因此,值为 1 实际上忽略了 image
  • num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会以较慢的推理速度为代价,带来更高质量的图像。
  • height (int, 可选, 默认为 512) — 生成图像的高度像素值。
  • width (int, 可选, 默认为 512) — 生成图像的宽度像素值。
  • prior_guidance_scale (float, 可选, 默认为 4.0) — 引导缩放比例,如 Classifier-Free Diffusion Guidance 中定义。 guidance_scale 定义为 Imagen Paper 等式 2 中的 w。通过设置 guidance_scale > 1 启用引导缩放比例。较高的引导缩放比例鼓励生成与文本 prompt 紧密相关的图像,但通常以较低的图像质量为代价。
  • prior_num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会以较慢的推理速度为代价,带来更高质量的图像。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 用于使生成结果确定性的一个或一组 torch 生成器。
  • 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

调用 pipeline 进行生成时调用的函数。

示例

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: Optional = None device: Union = 'cuda' )

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

enable_sequential_cpu_offload

< >

( gpu_id: Optional = None device: Union = 'cuda' )

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

KandinskyV22ControlnetImg2ImgPipeline

diffusers.KandinskyV22ControlnetImg2ImgPipeline

< >

( unet: UNet2DConditionModel scheduler: DDPMScheduler movq: VQModel )

参数

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

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

__call__

< >

( image_embeds: Union image: Union negative_image_embeds: Union 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: Union = None output_type: Optional = 'pil' callback: Optional = None callback_steps: int = 1 return_dict: bool = True ) ImagePipelineOutput or tuple

参数

  • image_embeds (torch.TensorList[torch.Tensor]) — 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 (torch.Tensor, PIL.Image.Image, np.ndarray, List[torch.Tensor], List[PIL.Image.Image], 或 List[np.ndarray]) — 图像或表示图像批次的张量,将用作过程的起点。也可以接受图像潜在表示作为 image,如果直接传递潜在表示,则不会再次编码。
  • strength (float, 可选,默认为 0.8) — strength (float, 可选,默认为 0.8) — 从概念上讲,表示转换参考 image 的程度。必须介于 0 和 1 之间。image 将用作起点,strength 越大,添加的噪声就越多。去噪步骤的数量取决于最初添加的噪声量。当 strength 为 1 时,添加的噪声将最大,并且去噪过程将运行 num_inference_steps 中指定的完整迭代次数。因此,值为 1 本质上会忽略 image
  • hint (torch.Tensor) — hint (torch.Tensor) — ControlNet 条件。
  • negative_image_embeds (torch.TensorList[torch.Tensor]) — negative_image_embeds (torch.TensorList[torch.Tensor]) — 负面文本提示的 clip 图像嵌入,将用于调节图像生成。
  • height (int, 可选,默认为 512) — height (int, 可选,默认为 512) — 生成图像的高度,以像素为单位。
  • width (int, 可选,默认为 512) — width (int, 可选,默认为 512) — 生成图像的宽度,以像素为单位。
  • num_inference_steps (int, 可选,默认为 100) — num_inference_steps (int, 可选,默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会以较慢的推理速度为代价, menghasilkan 更高质量的图像。
  • guidance_scale (float, 可选,默认为 4.0) — guidance_scale (float, 可选,默认为 4.0) — Guidance scale,如 Classifier-Free Diffusion Guidance 中所定义。guidance_scale 定义为 Imagen Paper 等式 2 中的 w。通过设置 guidance_scale > 1 启用 Guidance scale。较高的 guidance scale 鼓励生成与文本 prompt 紧密相关的图像,通常以降低图像质量为代价。
  • num_images_per_prompt (int, 可选,默认为 1) — num_images_per_prompt (int, 可选,默认为 1) — 每个 prompt 生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — generator (torch.GeneratorList[torch.Generator], 可选) — 一个或多个 torch generator,用于使生成具有确定性。
  • output_type (str, 可选,默认为 "pil") — output_type (str, 可选,默认为 "pil") — 生成图像的输出格式。在以下选项之间选择:"pil" (PIL.Image.Image)、"np" (np.array) 或 "pt" (torch.Tensor)。
  • callback (Callable, 可选) — callback (Callable, 可选) — 一个函数,在推理期间每 callback_steps 步调用一次。该函数使用以下参数调用:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, 可选,默认为 1) — callback_steps (int, 可选,默认为 1) — 调用 callback 函数的频率。如果未指定,则在每个步骤都调用回调。
  • return_dict (bool, 可选,默认为 True) — return_dict (bool, 可选,默认为 True) — 是否返回 ImagePipelineOutput 而不是普通元组。

返回值

ImagePipelineOutputtuple

调用 pipeline 进行生成时调用的函数。

示例

KandinskyV22InpaintPipeline

diffusers.KandinskyV22InpaintPipeline

< >

( unet: UNet2DConditionModel scheduler: DDPMScheduler movq: VQModel )

参数

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

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

__call__

< >

( image_embeds: Union image: Union mask_image: Union negative_image_embeds: Union height: int = 512 width: int = 512 num_inference_steps: int = 100 guidance_scale: float = 4.0 num_images_per_prompt: int = 1 generator: Union = None latents: Optional = None output_type: Optional = 'pil' return_dict: bool = True callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['latents'] **kwargs ) ImagePipelineOutput or tuple

参数

  • image_embeds (torch.TensorList[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.TensorList[torch.Tensor]) — 用于负面文本提示的 clip 图像嵌入,将用于调节图像生成。
  • height (int, 可选, 默认为 512) — 生成图像的高度像素。
  • width (int, 可选, 默认为 512) — 生成图像的宽度像素。
  • num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会以较慢的推理速度为代价,但通常会生成更高质量的图像。
  • guidance_scale (float, 可选, 默认为 4.0) — 无分类器扩散引导 中定义的引导缩放。Imagen Paper 的公式 2 中将 guidance_scale 定义为 w。通过设置 guidance_scale > 1 启用引导缩放。较高的引导缩放鼓励生成与文本 prompt 紧密相关的图像,但通常以降低图像质量为代价。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示要生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 用于使生成具有确定性的一个或一组 torch 生成器
  • 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 参数传递。您只能包含管道类的 ._callback_tensor_inputs 属性中列出的变量。

返回值

ImagePipelineOutputtuple

调用 pipeline 进行生成时调用的函数。

示例

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 图像的 image_processor。

使用 Kandinsky 的组合 Pipeline 用于修复生成

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

__call__

< >

( prompt: Union image: Union mask_image: Union negative_prompt: Union = 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: Union = None latents: Optional = None output_type: Optional = 'pil' return_dict: bool = True prior_callback_on_step_end: Optional = None prior_callback_on_step_end_tensor_inputs: List = ['latents'] callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['latents'] **kwargs ) ImagePipelineOutput or tuple

参数

  • prompt (strList[str]) — 用于引导图像生成的提示语。
  • image (torch.Tensor, PIL.Image.Image, np.ndarray, List[torch.Tensor], List[PIL.Image.Image], 或 List[np.ndarray]) — Image,或表示图像批次的张量,将用作过程的起点。也可以接受图像潜在表示作为 image,如果直接传递潜在表示,则不会再次编码。
  • mask_image (np.array) — 表示图像批次的张量,用于遮罩 image。蒙版中的白色像素将被重新绘制,而黑色像素将被保留。如果 mask_image 是 PIL 图像,它将在使用前转换为单通道(亮度)。如果它是张量,它应该包含一个颜色通道 (L) 而不是 3 个,因此预期的形状将是 (B, H, W, 1)
  • negative_prompt (strList[str], 可选) — 不用于引导图像生成的提示语。当不使用引导时将被忽略(即,如果 guidance_scale 小于 1 则忽略)。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示语要生成的图像数量。
  • guidance_scale (float, 可选, 默认为 4.0) — Classifier-Free Diffusion Guidance 中定义的引导尺度。 guidance_scale 定义为 Imagen Paper 等式 2 中的 w。通过设置 guidance_scale > 1 启用引导尺度。较高的引导尺度鼓励生成与文本 prompt 紧密相关的图像,通常以降低图像质量为代价。
  • num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。 更多的去噪步骤通常会带来更高质量的图像,但代价是推理速度较慢。
  • height (int, 可选, 默认为 512) — 生成图像的像素高度。
  • width (int, 可选, 默认为 512) — 生成图像的像素宽度。
  • prior_guidance_scale (float, 可选, 默认为 4.0) — Classifier-Free Diffusion Guidance 中定义的引导尺度。 guidance_scale 定义为 Imagen Paper 等式 2 中的 w。通过设置 guidance_scale > 1 启用引导尺度。较高的引导尺度鼓励生成与文本 prompt 紧密相关的图像,通常以降低图像质量为代价。
  • prior_num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。 更多的去噪步骤通常会带来更高质量的图像,但代价是推理速度较慢。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 用于使生成结果具有确定性的一个或一组 torch 生成器
  • 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, 可选) — 在推理期间,在每个去噪步骤结束时调用的函数。 该函数使用以下参数调用: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 参数传递。 您将只能包含管道类的 ._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 参数传递。 您将只能包含管道类的 ._callback_tensor_inputs 属性中列出的变量。

返回值

ImagePipelineOutputtuple

调用 pipeline 进行生成时调用的函数。

示例

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: Optional = None device: Union = 'cuda' )

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

< > Update on GitHub