Diffusers 文档

康定斯基 2.1

Hugging Face's logo
加入 Hugging Face 社区

并获取增强的文档体验

开始

康定斯基 2.1

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

来自其 GitHub 页面的描述是

Kandinsky 2.1 继承了 Dall-E 2 和潜在扩散的最佳实践,同时引入了一些新想法。作为文本和图像编码器,它使用 CLIP 模型和扩散图像先验(映射),在 CLIP 模态的潜在空间之间进行映射。这种方法提高了模型的视觉性能,并在混合图像和文本引导的图像处理方面开辟了新的视野。

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

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

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

KandinskyPriorPipeline

class diffusers.KandinskyPriorPipeline

< >

( 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 生成图像先验的管线。

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

__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 ) KandinskyPriorPipelineOutputtuple

参数

  • prompt (strList[str]) — 用于引导图像生成的提示或提示列表。
  • negative_prompt (strList[str], 可选) — 不用于引导图像生成的提示或提示列表。当不使用引导时忽略(即,如果 guidance_scale 小于 1,则忽略)。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示要生成的图像数量。
  • num_inference_steps (int, 可选, 默认为 25) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但会牺牲更慢的推理速度。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个或一组 torch 生成器,用于使生成具有确定性。
  • latents (torch.Tensor, 可选) — 预生成的噪声潜在变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同的提示调整相同的生成结果。如果未提供,则将使用提供的随机 generator 采样生成潜在变量张量。
  • guidance_scale (float, 可选, 默认为 4.0) — Classifier-Free Diffusion Guidance 中定义的引导尺度。 guidance_scale 定义为 Imagen Paper 的公式 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 KandinskyPipeline, KandinskyPriorPipeline
>>> import torch

>>> pipe_prior = KandinskyPriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-1-prior")
>>> pipe_prior.to("cuda")

>>> prompt = "red cat, 4k photo"
>>> out = pipe_prior(prompt)
>>> image_emb = out.image_embeds
>>> negative_image_emb = out.negative_image_embeds

>>> pipe = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1")
>>> pipe.to("cuda")

>>> image = pipe(
...     prompt,
...     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, 可选, 默认为 1) — 每个提示要生成的图像数量。
  • num_inference_steps (int, 可选, 默认为 25) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但会牺牲更慢的推理速度。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个或一组 torch 生成器,用于使生成具有确定性。
  • 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 Guidance 中定义的引导尺度。 guidance_scale 定义为 Imagen Paper 的公式 2 中的 w。通过设置 guidance_scale > 1 启用引导尺度。较高的引导尺度鼓励生成与文本 prompt 紧密相关的图像,但通常以降低图像质量为代价。

返回

KandinskyPriorPipelineOutputtuple

使用先验管线进行插值时调用的函数。

示例

>>> from diffusers import KandinskyPriorPipeline, KandinskyPipeline
>>> from diffusers.utils import load_image
>>> import PIL

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

>>> pipe_prior = KandinskyPriorPipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-1-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 = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", 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")

KandinskyPipeline

class diffusers.KandinskyPipeline

< >

( text_encoder: MultilingualCLIP tokenizer: XLMRobertaTokenizer unet: UNet2DConditionModel scheduler: Union movq: VQModel )

参数

  • text_encoder (MultilingualCLIP) — 冻结的文本编码器。
  • tokenizer (XLMRobertaTokenizer) — 类 的分词器
  • scheduler (Union[DDIMScheduler,DDPMScheduler]) — 一个调度器,与 unet 结合使用以生成图像潜在变量。
  • unet (UNet2DConditionModel) — 用于对图像嵌入进行去噪的条件式 U-Net 架构。
  • movq (VQModel) — 用于从潜在空间生成图像的 MoVQ 解码器。

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

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

__call__

< >

( prompt: Union image_embeds: Union negative_image_embeds: Union negative_prompt: Union = None 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 ) ImagePipelineOutput or tuple

参数

  • prompt (strList[str]) — 用于引导图像生成的提示或多个提示。
  • 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 Paper 的公式 2 中的 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)。
  • callback (Callable, *可选的*) — 一个在推理过程中每 callback_steps 步调用的函数。该函数使用以下参数调用:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, *可选的*, 默认为 1) — 调用 callback 函数的频率。如果未指定,则在每个步骤都调用回调。
  • return_dict (bool, *可选的*, 默认为 True) — 是否返回 ImagePipelineOutput 而不是纯元组。

返回

ImagePipelineOutput or tuple

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

示例

>>> from diffusers import KandinskyPipeline, KandinskyPriorPipeline
>>> import torch

>>> pipe_prior = KandinskyPriorPipeline.from_pretrained("kandinsky-community/Kandinsky-2-1-prior")
>>> pipe_prior.to("cuda")

>>> prompt = "red cat, 4k photo"
>>> out = pipe_prior(prompt)
>>> image_emb = out.image_embeds
>>> negative_image_emb = out.negative_image_embeds

>>> pipe = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1")
>>> pipe.to("cuda")

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

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

KandinskyCombinedPipeline

class diffusers.KandinskyCombinedPipeline

< >

( text_encoder: MultilingualCLIP tokenizer: XLMRobertaTokenizer unet: UNet2DConditionModel scheduler: Union movq: VQModel prior_prior: PriorTransformer prior_image_encoder: CLIPVisionModelWithProjection prior_text_encoder: CLIPTextModelWithProjection prior_tokenizer: CLIPTokenizer prior_scheduler: UnCLIPScheduler prior_image_processor: CLIPImageProcessor )

参数

  • text_encoder (MultilingualCLIP) — 冻结的文本编码器。
  • tokenizer (XLMRobertaTokenizer) — 类 XLMRobertaTokenizer 的分词器。
  • 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 结合使用以生成图像嵌入。

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

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

__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 ) 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_scale 定义为 Imagen Paper 方程式 2 中的 w。通过设置 guidance_scale > 1 启用引导尺度。较高的引导尺度鼓励生成与文本 prompt 紧密相关的图像,但通常以较低的图像质量为代价。
  • prior_num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。更多去噪步骤通常会带来更高质量的图像,但会以较慢的推理速度为代价。
  • guidance_scale (float, 可选, 默认为 4.0) — Classifier-Free Diffusion Guidance 中定义的引导尺度。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)。
  • callback (Callable, 可选) — 在推理期间每 callback_steps 步调用的函数。该函数使用以下参数调用:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, 可选, 默认为 1) — 调用 callback 函数的频率。如果未指定,则在每一步都调用回调。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ImagePipelineOutput 而不是普通元组。

返回

ImagePipelineOutput or tuple

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

示例

from diffusers import AutoPipelineForText2Image
import torch

pipe = AutoPipelineForText2Image.from_pretrained(
    "kandinsky-community/kandinsky-2-1", 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 = 0 )

使用 🤗 Accelerate 将所有模型(unettext_encodervaesafety checker 状态字典)卸载到 CPU,从而显著减少内存使用量。模型被移动到 torch.device('meta'),并且仅在其特定子模块的 forward 方法被调用时才加载到 GPU 上。卸载是基于子模块进行的。内存节省高于使用 enable_model_cpu_offload,但性能较低。

KandinskyImg2ImgPipeline

class diffusers.KandinskyImg2ImgPipeline

< >

( text_encoder: MultilingualCLIP movq: VQModel tokenizer: XLMRobertaTokenizer unet: UNet2DConditionModel scheduler: DDIMScheduler )

参数

  • text_encoder (MultilingualCLIP) — 冻结的文本编码器。
  • tokenizer (XLMRobertaTokenizer) — class 的分词器
  • scheduler (DDIMScheduler) — 与 unet 结合使用的调度器,用于生成图像潜在表示。
  • unet (UNet2DConditionModel) — 用于去噪图像嵌入的条件 U-Net 架构。
  • movq (VQModel) — MoVQ 图像编码器和解码器

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

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

__call__

< >

( prompt: Union image: Union image_embeds: Tensor negative_image_embeds: Tensor negative_prompt: Union = None height: int = 512 width: int = 512 num_inference_steps: int = 100 strength: float = 0.3 guidance_scale: float = 7.0 num_images_per_prompt: int = 1 generator: Union = None output_type: Optional = 'pil' callback: Optional = None callback_steps: int = 1 return_dict: bool = True ) ImagePipelineOutputtuple

参数

  • prompt (strList[str]) — 用于引导图像生成的提示或提示列表。
  • image (torch.Tensor, PIL.Image.Image) — Image,或表示将用作流程起点的图像批次的张量。
  • 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) — 去噪步骤的数量。更多去噪步骤通常会以较慢的推理速度为代价带来更高的图像质量。
  • strength (float, 可选, 默认为 0.3) — 从概念上讲,表示要转换参考 image 的程度。必须介于 0 和 1 之间。image 将用作起点,strength 越大,添加到其中的噪声就越多。去噪步骤的数量取决于最初添加的噪声量。当 strength 为 1 时,添加的噪声将最大,并且去噪过程将运行完整数量的迭代,如 num_inference_steps 中指定。因此,值为 1 本质上会忽略 image
  • guidance_scale (float, 可选, 默认为 4.0) — Classifier-Free Diffusion Guidance 中定义的引导缩放。 guidance_scale 定义为 Imagen Paper 的公式 2 中的 w。通过设置 guidance_scale > 1 启用引导缩放。较高的引导缩放鼓励生成与文本 prompt 紧密相关的图像,通常以降低图像质量为代价。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示要生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 用于使生成确定性的一个或多个 torch 生成器 列表。
  • 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 而不是普通元组。

返回

ImagePipelineOutput or tuple

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

示例

>>> from diffusers import KandinskyImg2ImgPipeline, KandinskyPriorPipeline
>>> from diffusers.utils import load_image
>>> import torch

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

>>> prompt = "A red cartoon frog, 4k"
>>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)

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

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

>>> image = pipe(
...     prompt,
...     image=init_image,
...     image_embeds=image_emb,
...     negative_image_embeds=zero_image_emb,
...     height=768,
...     width=768,
...     num_inference_steps=100,
...     strength=0.2,
... ).images

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

KandinskyImg2ImgCombinedPipeline

class diffusers.KandinskyImg2ImgCombinedPipeline

< >

( text_encoder: MultilingualCLIP tokenizer: XLMRobertaTokenizer unet: UNet2DConditionModel scheduler: Union movq: VQModel prior_prior: PriorTransformer prior_image_encoder: CLIPVisionModelWithProjection prior_text_encoder: CLIPTextModelWithProjection prior_tokenizer: CLIPTokenizer prior_scheduler: UnCLIPScheduler prior_image_processor: CLIPImageProcessor )

参数

  • text_encoder (MultilingualCLIP) — 冻结的文本编码器。
  • tokenizer (XLMRobertaTokenizer) — class 的分词器
  • 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 结合使用的调度器,用于生成图像嵌入。

Kandinsky 图像到图像生成的组合 Pipeline

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

__call__

< >

( prompt: Union image: Union negative_prompt: Union = None num_inference_steps: int = 100 guidance_scale: float = 4.0 num_images_per_prompt: int = 1 strength: float = 0.3 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 ) ImagePipelineOutputtuple

参数

  • prompt (strList[str]) — 用于引导图像生成的提示或提示列表。
  • image (torch.Tensor, PIL.Image.Image, np.ndarray, List[torch.Tensor], List[PIL.Image.Image], 或 List[np.ndarray]) — Image,或表示图像批次的张量,将用作过程的起点。也可以接受图像潜在变量作为 image,如果直接传递潜在变量,则不会再次编码。
  • negative_prompt (strList[str], 可选) — 不用于引导图像生成的提示或提示列表。当不使用引导时忽略(即,如果 guidance_scale 小于 1 则忽略)。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示要生成的图像数量。
  • num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会以牺牲较慢的推理速度为代价,导致更高质量的图像。
  • height (int, 可选, 默认为 512) — 生成图像的高度像素。
  • width (int, 可选, 默认为 512) — 生成图像的宽度像素。
  • strength (float, 可选, 默认为 0.3) — 从概念上讲,表示要转换参考 image 的程度。必须介于 0 和 1 之间。image 将用作起点,strength 越大,添加到其中的噪声就越多。去噪步骤的数量取决于最初添加的噪声量。当 strength 为 1 时,添加的噪声将最大,去噪过程将运行 num_inference_steps 中指定的完整迭代次数。因此,值为 1 本质上会忽略 image
  • 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) — 去噪步骤的数量。更多的去噪步骤通常会以牺牲较慢的推理速度为代价,导致更高质量的图像。
  • guidance_scale (float, 可选, 默认为 4.0) — Classifier-Free Diffusion Guidance 中定义的引导尺度。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)。
  • callback (Callable, 可选) — 一个函数,在推理期间每 callback_steps 步调用一次。该函数使用以下参数调用:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, 可选, 默认为 1) — 调用 callback 函数的频率。如果未指定,则在每个步骤调用回调。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ImagePipelineOutput 而不是普通元组。

返回

ImagePipelineOutput or tuple

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

示例

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-1", 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_sequential_cpu_offload

< >

( gpu_id = 0 )

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

KandinskyInpaintPipeline

class diffusers.KandinskyInpaintPipeline

< >

( text_encoder: MultilingualCLIP movq: VQModel tokenizer: XLMRobertaTokenizer unet: UNet2DConditionModel scheduler: DDIMScheduler )

参数

  • text_encoder (MultilingualCLIP) — 冻结的文本编码器。
  • tokenizer (XLMRobertaTokenizer) — 类分词器
  • scheduler (DDIMScheduler) — 调度器,与 unet 结合使用以生成图像潜在表示。
  • unet (UNet2DConditionModel) — 条件 U-Net 架构,用于对图像嵌入进行去噪。
  • movq (VQModel) — MoVQ 图像编码器和解码器

用于文本引导的图像修复的 Pipeline,使用 Kandinsky2.1

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

__call__

< >

( prompt: Union image: Union mask_image: Union image_embeds: Tensor negative_image_embeds: Tensor negative_prompt: Union = None 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]) — 用于引导图像生成的提示或提示列表。
  • image (torch.Tensor, PIL.Image.Imagenp.ndarray) — Image,或表示图像批次的张量,将用作该过程的起始点。
  • mask_image (PIL.Image.Image,torch.Tensornp.ndarray) — Image,或表示图像批次的张量,用于遮罩 image。蒙版中的白色像素将被重新绘制,而黑色像素将被保留。仅当您传递的图像是 pytorch 张量时,才可以传递 pytorch 张量作为蒙版,并且它应包含一个颜色通道 (L) 而不是 3 个,因此预期的形状应为 (B, 1, H, W,)(B, H, W)(1, H, W)(H, W)。如果 image 是 PIL 图像或 numpy 数组,则蒙版也应为 PIL 图像或 numpy 数组。如果是 PIL 图像,则在使用前会将其转换为单通道(亮度)。如果是 numpy 数组,则预期形状为 (H, W)
  • 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 定义为 Imagen Paper 等式 2 中的 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)。
  • callback (Callable, 可选) — 在推理期间每 callback_steps 步调用的函数。该函数使用以下参数调用:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, 可选, 默认为 1) — 调用 callback 函数的频率。如果未指定,则在每个步骤都调用回调。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ImagePipelineOutput 而不是普通元组。

返回

ImagePipelineOutput or tuple

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

示例

>>> from diffusers import KandinskyInpaintPipeline, KandinskyPriorPipeline
>>> from diffusers.utils import load_image
>>> import torch
>>> import numpy as np

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

>>> prompt = "a hat"
>>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)

>>> pipe = KandinskyInpaintPipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-1-inpaint", torch_dtype=torch.float16
... )
>>> pipe.to("cuda")

>>> init_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)
>>> mask[:250, 250:-250] = 1

>>> out = pipe(
...     prompt,
...     image=init_image,
...     mask_image=mask,
...     image_embeds=image_emb,
...     negative_image_embeds=zero_image_emb,
...     height=768,
...     width=768,
...     num_inference_steps=50,
... )

>>> image = out.images[0]
>>> image.save("cat_with_hat.png")

KandinskyInpaintCombinedPipeline

class diffusers.KandinskyInpaintCombinedPipeline

< >

( text_encoder: MultilingualCLIP tokenizer: XLMRobertaTokenizer unet: UNet2DConditionModel scheduler: Union movq: VQModel prior_prior: PriorTransformer prior_image_encoder: CLIPVisionModelWithProjection prior_text_encoder: CLIPTextModelWithProjection prior_tokenizer: CLIPTokenizer prior_scheduler: UnCLIPScheduler prior_image_processor: CLIPImageProcessor )

参数

  • text_encoder (MultilingualCLIP) — 冻结的文本编码器。
  • tokenizer (XLMRobertaTokenizer) — 类
  • 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 结合使用的调度器,用于生成图像嵌入。

用于使用 Kandinsky 生成的组合管线

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

__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' callback: Optional = None callback_steps: int = 1 return_dict: bool = True ) ImagePipelineOutputtuple

参数

  • 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) — 表示图像批次的张量,用于 mask image。mask 中的白色像素将被重新绘制,而黑色像素将被保留。如果 mask_image 是 PIL 图像,它将在使用前转换为单通道(亮度)。如果它是一个张量,它应该包含一个颜色通道 (L) 而不是 3 个,所以期望的形状将是 (B, H, W, 1)
  • 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_scale 定义为 Imagen Paper 中公式 2 的 w。通过设置 guidance_scale > 1 启用引导尺度。更高的引导尺度鼓励生成与文本 prompt 紧密相关的图像,通常以较低的图像质量为代价。
  • prior_num_inference_steps (int, 可选, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但会以较慢的推理速度为代价。
  • guidance_scale (float, 可选, 默认为 4.0) — 引导尺度,如 Classifier-Free Diffusion Guidance 中定义。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)。
  • callback (Callable, 可选) — 一个在推理期间每 callback_steps 步调用的函数。该函数使用以下参数调用:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, 可选, 默认为 1) — 调用 callback 函数的频率。如果未指定,则在每个步骤都调用回调函数。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ImagePipelineOutput 而不是一个普通的元组。

返回

ImagePipelineOutput or tuple

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

示例

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

pipe = AutoPipelineForInpainting.from_pretrained(
    "kandinsky-community/kandinsky-2-1-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 = 0 )

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

< > 在 GitHub 上更新