Diffusers 文档
Kandinsky 2.1
并获取增强的文档体验
开始使用
Kandinsky 2.1
Kandinsky 2.1 由 Arseniy Shakhmatov、Anton Razzhigaev、Aleksandr Nikolich、Vladimir Arkhipkin、Igor Pavlov、Andrey Kuznetsov 和 Denis Dimitrov 创建。
其 GitHub 页面上的描述是
Kandinsky 2.1 继承了 Dall-E 2 和潜在扩散的最佳实践,同时引入了一些新思路。作为文本和图像编码器,它使用了 CLIP 模型和扩散图像先验(映射),在 CLIP 模态的潜在空间之间进行映射。这种方法提高了模型的视觉性能,并在混合图像和文本引导的图像操作方面开辟了新的前景。
原始代码库可以在 ai-forever/Kandinsky-2 找到。
请查看 Hub 上的 Kandinsky Community 组织,获取用于文本到图像、图像到图像和图像修复等任务的官方模型检查点。
请务必查看 Schedulers 指南,了解如何探索 scheduler 速度和质量之间的权衡,并查看跨 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 生成图像先验的 Pipeline
此模型继承自 DiffusionPipeline。请查看超类文档,了解库为所有 pipelines 实现的通用方法(例如下载或保存,在特定设备上运行等)。
__call__
< source >( 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 ) → KandinskyPriorPipelineOutput
or tuple
参数
- prompt (
str
或List[str]
) — 用于引导图像生成的提示或提示列表。 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示或提示列表。当不使用引导时忽略(即,如果guidance_scale
小于1
则忽略)。 - num_images_per_prompt (
int
, 可选,默认为 1) — 每个提示要生成的图像数量。 - num_inference_steps (
int
, 可选,默认为 25) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但会以较慢的推理速度为代价。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 用于使生成结果确定性的单个或列表形式的 torch 生成器。 - latents (
torch.Tensor
, 可选) — 预生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于通过不同的提示调整相同的生成结果。如果未提供,则将通过使用提供的随机generator
进行采样来生成潜变量 tensor。 - 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
紧密相关的图像,但通常以降低图像质量为代价。 - output_type (
str
, 可选,默认为"pt"
) — 生成图像的输出格式。在以下选项之间选择:"np"
(np.array
) 或"pt"
(torch.Tensor
)。 - return_dict (
bool
, 可选,默认为True
) — 是否返回 ImagePipelineOutput 而不是纯元组。
返回值
KandinskyPriorPipelineOutput
or tuple
调用 pipeline 进行生成时调用的函数。
示例
>>> 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
< source >( 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 ) → KandinskyPriorPipelineOutput
or tuple
参数
- 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.Generator
或List[torch.Generator]
, 可选) — 用于使生成结果确定性的单个或列表形式的 torch 生成器。 - latents (
torch.Tensor
, 可选) — 预生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于通过不同的提示调整相同的生成结果。如果未提供,则将通过使用提供的随机generator
进行采样来生成潜变量 tensor。 - negative_prior_prompt (
str
, 可选) — 不用于引导先验扩散过程的提示。当不使用引导时忽略(即,如果guidance_scale
小于1
则忽略)。 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示。当不使用引导时忽略(即,如果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
紧密相关的图像,但通常以降低图像质量为代价。
返回值
KandinskyPriorPipelineOutput
or tuple
当使用 prior pipeline 进行插值时调用的函数。
示例
>>> 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
< source >( text_encoder: MultilingualCLIP tokenizer: XLMRobertaTokenizer unet: UNet2DConditionModel scheduler: typing.Union[diffusers.schedulers.scheduling_ddim.DDIMScheduler, diffusers.schedulers.scheduling_ddpm.DDPMScheduler] movq: VQModel )
参数
- text_encoder (
MultilingualCLIP
) — 冻结的文本编码器。 - tokenizer (
XLMRobertaTokenizer
) — class 的分词器 - scheduler (Union[
DDIMScheduler
,DDPMScheduler
]) — 一个调度器,与unet
结合使用以生成图像潜在表示。 - unet (UNet2DConditionModel) — 条件 U-Net 架构,用于对图像嵌入进行去噪。
- movq (VQModel) — MoVQ 解码器,用于从潜在表示生成图像。
用于使用 Kandinsky 进行文本到图像生成的 Pipeline
此模型继承自 DiffusionPipeline。请查看超类文档,了解库为所有 pipelines 实现的通用方法(例如下载或保存,在特定设备上运行等)。
__call__
< source >( prompt: typing.Union[str, typing.List[str]] image_embeds: typing.Union[torch.Tensor, typing.List[torch.Tensor]] negative_image_embeds: typing.Union[torch.Tensor, typing.List[torch.Tensor]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None 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 ) → ImagePipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
) — 用于引导图像生成的提示或提示列表。 - image_embeds (
torch.Tensor
或List[torch.Tensor]
) — 文本提示的 clip 图像嵌入,将用于调节图像生成。 - negative_image_embeds (
torch.Tensor
或List[torch.Tensor]
) — 负面文本提示的 clip 图像嵌入,将用于调节图像生成。 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示或提示列表。当不使用引导时忽略(即,如果guidance_scale
小于1
则忽略)。 - height (
int
, 可选, 默认为 512) — 生成图像的高度,单位为像素。 - width (
int
, 可选, 默认为 512) — 生成图像的宽度,单位为像素。 - num_inference_steps (
int
, 可选, 默认为 100) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但会以较慢的推理速度为代价。 - 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.Generator
或List[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 或 tuple
调用 pipeline 进行生成时调用的函数。
示例
>>> 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
< source >( text_encoder: MultilingualCLIP tokenizer: XLMRobertaTokenizer unet: UNet2DConditionModel scheduler: typing.Union[diffusers.schedulers.scheduling_ddim.DDIMScheduler, diffusers.schedulers.scheduling_ddpm.DDPMScheduler] 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
结合使用以生成图像潜在表示(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
结合使用以生成图像嵌入。
结合的 Pipeline,用于使用 Kandinsky 进行文本到图像的生成
此模型继承自 DiffusionPipeline。请查看超类文档,了解库为所有 pipelines 实现的通用方法(例如下载或保存,在特定设备上运行等)。
__call__
< source >( 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 ) → ImagePipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
) — 用于引导图像生成的提示或提示列表。 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示或提示列表。当不使用引导时忽略(即,如果guidance_scale
小于1
,则忽略)。 - num_images_per_prompt (
int
, 可选, 默认为 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) — 去噪步骤的数量。更多去噪步骤通常会带来更高质量的图像,但会牺牲推理速度。 - guidance_scale (
float
, 可选, 默认为 4.0) — 引导缩放比例,如 Classifier-Free Diffusion Guidance 中所定义。guidance_scale
定义为 Imagen Paper 方程式 2 中的w
。通过设置guidance_scale > 1
启用引导缩放比例。较高的引导缩放比例鼓励生成与文本prompt
紧密相关的图像,但通常以降低图像质量为代价。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个或一组 torch generator(s) 以使生成具有确定性。 - latents (
torch.Tensor
, 可选) — 预生成的噪声潜在表示,从高斯分布中采样,用作图像生成的输入。可用于通过不同的 prompt 调整相同的生成结果。如果未提供,将通过使用提供的随机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 或 tuple
调用 pipeline 进行生成时调用的函数。
示例
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
< source >( gpu_id: typing.Optional[int] = None device: typing.Union[torch.device, str] = 'cuda' )
使用 🤗 Accelerate 将所有模型(unet
、text_encoder
、vae
和 safety 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
) — 类 Tokenizer - scheduler (DDIMScheduler) — 与
unet
结合使用的调度器,用于生成图像潜在空间。 - unet (UNet2DConditionModel) — 条件 U-Net 架构,用于对图像嵌入进行去噪。
- movq (VQModel) — MoVQ 图像编码器和解码器
使用 Kandinsky 进行图像到图像生成的 Pipeline
此模型继承自 DiffusionPipeline。请查看超类文档,了解库为所有 pipelines 实现的通用方法(例如下载或保存,在特定设备上运行等)。
__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]] image_embeds: Tensor negative_image_embeds: Tensor negative_prompt: typing.Union[str, typing.List[str], NoneType] = 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: 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 ) → ImagePipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
) — 指导图像生成的提示或提示列表。 - image (
torch.Tensor
,PIL.Image.Image
) — 图像,或表示图像批次的张量,将用作过程的起点。 - image_embeds (
torch.Tensor
或List[torch.Tensor]
) — 文本提示的 clip 图像嵌入,将用于调节图像生成。 - negative_image_embeds (
torch.Tensor
或List[torch.Tensor]
) — 负面文本提示的 clip 图像嵌入,将用于调节图像生成。 - negative_prompt (
str
或List[str]
, 可选) — 不用于指导图像生成的提示或提示列表。当不使用 guidance 时忽略(即,如果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) — 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.Generator
或List[torch.Generator]
, 可选) — 用于使生成具有确定性的 torch generator 或 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
函数的频率。如果未指定,则在每个步骤都调用 callback。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 ImagePipelineOutput 而不是普通元组。
返回值
ImagePipelineOutput 或 tuple
调用 pipeline 进行生成时调用的函数。
示例
>>> 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: typing.Union[diffusers.schedulers.scheduling_ddim.DDIMScheduler, diffusers.schedulers.scheduling_ddpm.DDPMScheduler] 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
结合使用以生成图像潜在空间 (latent)。 - 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
结合使用以生成图像嵌入。
用于 Kandinsky 的图像到图像生成的组合 Pipeline
此模型继承自 DiffusionPipeline。请查看超类文档,了解库为所有 pipelines 实现的通用方法(例如下载或保存,在特定设备上运行等)。
__call__
< source >( 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 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: 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 ) → ImagePipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
) — 用于引导图像生成的 prompt 或 prompts。 - image (
torch.Tensor
,PIL.Image.Image
,np.ndarray
,List[torch.Tensor]
,List[PIL.Image.Image]
, 或List[np.ndarray]
) —Image
,或表示图像批次的 tensor,将用作该过程的起点。也可以接受图像潜在空间作为image
,如果直接传递潜在空间,则不会再次编码。 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的 prompt 或 prompts。当不使用 guidance 时忽略(即,如果guidance_scale
小于1
则忽略)。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个 prompt 生成的图像数量。 - 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。guidance_scale
定义为 Imagen Paper 等式 2 中的w
。 通过设置guidance_scale > 1
启用 Guidance scale。 较高的 guidance scale 鼓励生成与文本prompt
紧密相关的图像,但通常以较低的图像质量为代价。 - prior_num_inference_steps (
int
, 可选, 默认为 100) — 去噪步骤的数量。 更多的去噪步骤通常会带来更高质量的图像,但会牺牲推理速度。 - guidance_scale (
float
, 可选, 默认为 4.0) — Classifier-Free Diffusion Guidance 中定义的 Guidance scale。guidance_scale
定义为 Imagen Paper 等式 2 中的w
。 通过设置guidance_scale > 1
启用 Guidance scale。 较高的 guidance scale 鼓励生成与文本prompt
紧密相关的图像,但通常以较低的图像质量为代价。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个或一组 torch generator(s) 以使生成具有确定性。 - latents (
torch.Tensor
, 可选) — 预生成的噪声潜在空间,从高斯分布中采样,用作图像生成的输入。 可用于通过不同的 prompt 调整相同的生成。 如果未提供,则将通过使用提供的随机generator
进行采样来生成潜在空间 tensor。 - 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 而不是普通 tuple。
返回值
ImagePipelineOutput 或 tuple
调用 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-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
< source >( gpu_id: typing.Optional[int] = None device: typing.Union[torch.device, str] = 'cuda' )
使用 accelerate 将所有模型卸载到 CPU,从而显著减少内存使用量。 调用后,unet、text_encoder、vae 和 safety checker 的 state dicts 将保存到 CPU,然后移动到 torch.device('meta')
,并且仅当其特定子模块调用了 forward
方法时才加载到 GPU。 请注意,卸载是基于子模块进行的。 内存节省高于使用 enable_model_cpu_offload
,但性能较低。
KandinskyInpaintPipeline
类 diffusers.KandinskyInpaintPipeline
< source >( text_encoder: MultilingualCLIP movq: VQModel tokenizer: XLMRobertaTokenizer unet: UNet2DConditionModel scheduler: DDIMScheduler )
参数
- text_encoder (
MultilingualCLIP
) — 冻结的文本编码器。 - tokenizer (
XLMRobertaTokenizer
) — class 的分词器 - scheduler (DDIMScheduler) — 一个调度器,与
unet
结合使用以生成图像潜在表示 (latents)。 - unet (UNet2DConditionModel) — 条件 U-Net 架构,用于对图像嵌入进行去噪。
- movq (VQModel) — MoVQ 图像编码器和解码器
使用 Kandinsky2.1 进行文本引导的图像修补 (inpainting) 的 Pipeline。
此模型继承自 DiffusionPipeline。请查看超类文档,了解库为所有 pipelines 实现的通用方法(例如下载或保存,在特定设备上运行等)。
__call__
< source >( prompt: typing.Union[str, typing.List[str]] image: typing.Union[torch.Tensor, PIL.Image.Image] mask_image: typing.Union[torch.Tensor, PIL.Image.Image, numpy.ndarray] image_embeds: Tensor negative_image_embeds: Tensor negative_prompt: typing.Union[str, typing.List[str], NoneType] = None 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 ) → ImagePipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
) — 用于引导图像生成的 prompt 或 prompts。 - image (
torch.Tensor
,PIL.Image.Image
或np.ndarray
) —Image
,或表示图像批次的 tensor,将用作处理的起点。 - mask_image (
PIL.Image.Image
,torch.Tensor
或np.ndarray
) —Image
,或表示图像批次的 tensor,用于遮罩image
。蒙版中的白色像素将被重新绘制,而黑色像素将被保留。只有当您传递的图像是 pytorch tensor 时,您才可以传递 pytorch tensor 作为蒙版,并且它应该包含一个颜色通道 (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.Tensor
或List[torch.Tensor]
) — 用于文本 prompt 的 clip 图像嵌入,将用于调节图像生成。 - negative_image_embeds (
torch.Tensor
或List[torch.Tensor]
) — 用于负面文本 prompt 的 clip 图像嵌入,将用于调节图像生成。 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的 prompt 或 prompts。当不使用引导时忽略(即,如果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) — 每个 prompt 生成的图像数量。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 用于使生成确定化的单个或列表的 torch generator。 - latents (
torch.Tensor
, 可选) — 预生成的噪声潜在表示 (latents),从高斯分布中采样,用作图像生成的输入。 可用于使用不同的 prompt 微调相同的生成结果。 如果未提供,则将通过使用提供的随机generator
进行采样来生成 latents tensor。 - 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 而不是普通 tuple。
返回值
ImagePipelineOutput 或 tuple
调用 pipeline 进行生成时调用的函数。
示例
>>> 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
类 diffusers.KandinskyInpaintCombinedPipeline
< source >( text_encoder: MultilingualCLIP tokenizer: XLMRobertaTokenizer unet: UNet2DConditionModel scheduler: typing.Union[diffusers.schedulers.scheduling_ddim.DDIMScheduler, diffusers.schedulers.scheduling_ddpm.DDPMScheduler] 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 生成的组合管线
此模型继承自 DiffusionPipeline。请查看超类文档,了解库为所有 pipelines 实现的通用方法(例如下载或保存,在特定设备上运行等)。
__call__
< source >( 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' callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: int = 1 return_dict: bool = True ) → ImagePipelineOutput 或 tuple
参数
- prompt (
str
或List[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 (
str
或List[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.Generator
或List[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 或 tuple
调用 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-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
< source >( gpu_id: typing.Optional[int] = None device: typing.Union[torch.device, str] = 'cuda' )
使用 accelerate 将所有模型卸载到 CPU,从而显著减少内存使用量。 调用后,unet、text_encoder、vae 和 safety checker 的 state dicts 将保存到 CPU,然后移动到 torch.device('meta')
,并且仅当其特定子模块调用了 forward
方法时才加载到 GPU。 请注意,卸载是基于子模块进行的。 内存节省高于使用 enable_model_cpu_offload
,但性能较低。