Diffusers 文档
坎定斯基 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 和 Latent Diffusion 的最佳实践,同时引入了一些新思想。它使用 CLIP 模型作为文本和图像编码器,并在 CLIP 模态的潜在空间之间使用扩散图像先验(映射)。这种方法提高了模型的视觉性能,并在图像混合和文本引导图像操作方面开辟了新视野。
原始代码库可在 ai-forever/Kandinsky-2 找到。
请查看 Hub 上的 Kandinsky 社区 组织,获取用于文本到图像、图像到图像和图像修复等任务的官方模型检查点。
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: 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
或 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
采样生成一个潜在张量。 - guidance_scale (
float
, 可选, 默认为 4.0) — 无分类器扩散引导中定义的引导比例。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 而不是普通的元组。
返回
KandinskyPriorPipelineOutput
或 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")
interpolate
< 来源 >( images_and_prompts: typing.List[typing.Union[str, PIL.Image.Image, torch.Tensor]] weights: typing.List[float] num_images_per_prompt: int = 1 num_inference_steps: int = 25 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None negative_prior_prompt: typing.Optional[str] = None negative_prompt: str = '' guidance_scale: float = 4.0 device = None ) → KandinskyPriorPipelineOutput
或 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
采样生成一个潜在张量。 - negative_prior_prompt (
str
, 可选) — 不用于指导先验扩散过程的提示。在使用引导时(即guidance_scale
小于1
时)将被忽略。 - negative_prompt (
str
或List[str]
, 可选) — 不用于指导图像生成的提示或提示列表。在使用引导时(即guidance_scale
小于1
时)将被忽略。 - guidance_scale (
float
, 可选, 默认为 4.0) — 无分类器扩散引导中定义的引导比例。guidance_scale
定义为 Imagen Paper 中公式 2 的w
。通过设置guidance_scale > 1
启用引导比例。较高的引导比例鼓励生成与文本prompt
紧密相关的图像,通常以牺牲较低图像质量为代价。
返回
KandinskyPriorPipelineOutput
或 tuple
当使用先验管道进行插值时调用的函数。
示例
>>> 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: typing.Union[diffusers.schedulers.scheduling_ddim.DDIMScheduler, diffusers.schedulers.scheduling_ddpm.DDPMScheduler] movq: VQModel )
参数
- text_encoder (
MultilingualCLIP
) — 冻结的文本编码器。 - tokenizer (
XLMRobertaTokenizer
) — 类的分词器 - scheduler (Union[
DDIMScheduler
,DDPMScheduler
]) — 与unet
结合使用的调度器,用于生成图像潜在变量。 - unet (UNet2DConditionModel) — 用于对图像嵌入进行去噪的条件 U-Net 架构。
- movq (VQModel) — 用于从潜在变量生成图像的 MoVQ 解码器。
使用 Kandinsky 进行文本到图像生成的 Pipeline
此模型继承自 DiffusionPipeline。有关库为所有管道实现的通用方法(例如下载或保存、在特定设备上运行等),请查看超类文档。
__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]
) — 用于文本提示的剪辑图像嵌入,将用于条件图像生成。 - negative_image_embeds (
torch.Tensor
或List[torch.Tensor]
) — 用于否定文本提示的剪辑图像嵌入,将用于条件图像生成。 - 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
调用管道进行生成时调用的函数。
示例
>>> 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
结合使用的调度器,用于生成图像潜在变量。 - 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__
< 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) — 每个提示生成的图像数量。 - 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
调用管道进行生成时调用的函数。
示例
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] = None )
使用 🤗 Accelerate 将所有模型(unet
、text_encoder
、vae
和 safety checker
状态字典)卸载到 CPU,显著降低内存使用。模型被移动到 torch.device('meta')
,仅当调用其特定子模块的 forward
方法时才加载到 GPU。卸载是基于子模块进行的。与使用 enable_model_cpu_offload
相比,内存节省更高,但性能更低。
KandinskyImg2ImgPipeline
class diffusers.KandinskyImg2ImgPipeline
< source >( 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 图像编码器和解码器。
使用 Kandinsky 进行图像到图像生成的管道。
此模型继承自 DiffusionPipeline。有关库为所有管道实现的通用方法(例如下载或保存、在特定设备上运行等),请查看超类文档。
__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
,或表示图像批次的张量,将用作此过程的起点。 - image_embeds (
torch.Tensor
或List[torch.Tensor]
) — 用于文本提示的剪辑图像嵌入,将用于条件图像生成。 - negative_image_embeds (
torch.Tensor
或List[torch.Tensor]
) — 用于负文本提示的剪辑图像嵌入,将用于条件图像生成。 - negative_prompt (
str
或List[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) — 无分类器扩散引导中定义的引导比例。guidance_scale
定义为 Imagen Paper 中公式 2 的w
。通过设置guidance_scale > 1
启用引导比例。更高的引导比例鼓励生成与文本prompt
紧密相关的图像,通常以牺牲图像质量为代价。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示生成的图像数量。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个或多个 torch generator(s) 以使生成具有确定性。 - 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
调用管道进行生成时调用的函数。
示例
>>> 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
) — Tokenizer 类。 - 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: 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]
) — 用于引导图像生成的提示或提示列表。 - image (
torch.Tensor
,PIL.Image.Image
,np.ndarray
,List[torch.Tensor]
,List[PIL.Image.Image]
, 或List[np.ndarray]
) —Image
,或表示图像批次的张量,将用作此过程的起点。如果直接传递潜空间,则不会再次编码。 - 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) — 生成图像的宽度(像素)。 - strength (
float
, 可选, 默认为 0.3) — 概念上,表示参考image
的转换程度。必须在 0 到 1 之间。image
将用作起点,strength
越大,添加的噪声越多。去噪步数取决于最初添加的噪声量。当strength
为 1 时,添加的噪声将达到最大值,去噪过程将运行num_inference_steps
中指定的完整迭代次数。因此,值为 1 实际上会忽略image
。 - 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.Generator
或List[torch.Generator]
, 可选) — 一个或多个 torch generator(s) 以使生成具有确定性。 - latents (
torch.Tensor
, 可选) — 预生成的带噪声的潜在空间,从高斯分布中采样,用作图像生成的输入。可用于使用不同提示微调同一生成。如果未提供,将使用提供的随机generator
采样生成一个潜在空间张量。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。在以下选项中选择:"pil"
(PIL.Image.Image
)、"np"
(np.array
) 或"pt"
(torch.Tensor
)。 - callback (
Callable
, 可选) — 在推理过程中每callback_steps
步调用的函数。该函数调用时带有以下参数:callback(step: int, timestep: int, latents: torch.Tensor)
。 - callback_steps (
int
, 可选, 默认为 1) — 调用callback
函数的频率。如果未指定,则在每一步都调用回调。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 ImagePipelineOutput 而不是普通元组。
返回
ImagePipelineOutput 或 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: typing.Optional[int] = None device: typing.Union[torch.device, str] = None )
使用 Accelerate 将所有模型卸载到 CPU,显著减少内存使用。调用时,unet、text_encoder、vae 和 safety checker 的状态字典将保存到 CPU,然后移动到 torch.device('meta')
,仅在其特定子模块调用 forward
方法时才加载到 GPU。请注意,卸载是基于子模块进行的。内存节省高于 enable_model_cpu_offload
,但性能较低。
KandinskyInpaintPipeline
class diffusers.KandinskyInpaintPipeline
< 源 >( 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 图像编码器和解码器
使用 Kandinsky2.1 进行文本引导图像修复的管线
此模型继承自 DiffusionPipeline。有关库为所有管道实现的通用方法(例如下载或保存、在特定设备上运行等),请查看超类文档。
__call__
< 来源 >( 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]
) — 用于引导图像生成的提示词。 - image (
torch.Tensor
,PIL.Image.Image
或np.ndarray
) — 将用作过程起点的图像或表示图像批次的张量。 - mask_image (
PIL.Image.Image
,torch.Tensor
或np.ndarray
) — 用于遮盖image
的图像或表示图像批次的张量。遮罩中的白色像素将被重新绘制,而黑色像素将被保留。只有当您传入的图像是 pytorch 张量时,才能传入 pytorch 张量作为遮罩,并且它应该包含一个颜色通道 (L) 而不是 3 个,因此预期的形状将是(B, 1, H, W,)
、(B, H, W)
、(1, H, W)
或(H, W)
。如果图像是 PIL 图像或 numpy 数组,遮罩也应该是 PIL 图像或 numpy 数组。如果它是 PIL 图像,在使用前它将被转换为单通道(亮度)。如果它是 numpy 数组,预期形状是(H, W)
。 - image_embeds (
torch.Tensor
或List[torch.Tensor]
) — 用于文本提示的剪辑图像嵌入,将用于条件图像生成。 - negative_image_embeds (
torch.Tensor
或List[torch.Tensor]
) — 用于负文本提示的剪辑图像嵌入,将用于条件图像生成。 - 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 generator(s) 列表,用于使生成具有确定性。 - latents (
torch.Tensor
, 可选) — 预生成的带噪声的潜在变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同的提示词调整相同的生成。如果未提供,将使用提供的随机generator
采样生成一个潜在变量张量。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。选择:"pil"
(PIL.Image.Image
)、"np"
(np.array
) 或"pt"
(torch.Tensor
)。 - callback (
Callable
, 可选) — 在推理过程中每callback_steps
步调用一次的函数。函数将使用以下参数调用:callback(step: int, timestep: int, latents: torch.Tensor)
。 - callback_steps (
int
, 可选, 默认为 1) — 调用callback
函数的频率。如果未指定,则在每一步都调用回调函数。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 ImagePipelineOutput 而不是普通元组。
返回
ImagePipelineOutput 或 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: 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
结合使用以生成图像潜在变量的调度器。 - 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: 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]
) — 将用作过程起点的图像或表示图像批次的张量。如果直接传入潜在变量,则不会再次编码。 - 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 generator(s) 列表,用于使生成具有确定性。 - latents (
torch.Tensor
, 可选) — 预生成的带噪声的潜在变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同的提示词调整相同的生成。如果未提供,将使用提供的随机generator
采样生成一个潜在变量张量。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。选择:"pil"
(PIL.Image.Image
)、"np"
(np.array
) 或"pt"
(torch.Tensor
)。 - callback (
Callable
, 可选) — 在推理过程中每callback_steps
步调用一次的函数。函数将使用以下参数调用:callback(step: int, timestep: int, latents: torch.Tensor)
。 - callback_steps (
int
, 可选, 默认为 1) — 调用callback
函数的频率。如果未指定,则在每一步都调用回调函数。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 ImagePipelineOutput 对象,而不是普通的元组。
返回
ImagePipelineOutput 或 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: typing.Optional[int] = None device: typing.Union[torch.device, str] = None )
使用 Accelerate 将所有模型卸载到 CPU,显著减少内存使用。调用时,unet、text_encoder、vae 和 safety checker 的状态字典将保存到 CPU,然后移动到 torch.device('meta')
,仅在其特定子模块调用 forward
方法时才加载到 GPU。请注意,卸载是基于子模块进行的。内存节省高于 enable_model_cpu_offload
,但性能较低。