Diffusers 文档
Kandinsky 3
并获得增强的文档体验
开始使用
Kandinsky 3
Kandinsky 3 由 Vladimir Arkhipkin、Anastasia Maltseva、Igor Pavlov、Andrei Filatov、Arseniy Shakhmatov、Andrey Kuznetsov、Denis Dimitrov 和 Zein Shaheen 共同创建。
其 GitHub 页面上的描述如下:
Kandinsky 3.0 是一个开源的文本到图像扩散模型,建立在 Kandinsky2-x 模型家族的基础上。与前代模型相比,通过分别增加文本编码器和扩散 U-Net 模型的尺寸,该模型在文本理解和视觉质量方面得到了提升。
其架构包含 3 个主要组件:
- FLAN-UL2,一个基于 T5 架构的编码器-解码器模型。
- 新的 U-Net 架构,采用 BigGAN-deep 块,在保持相同参数数量的同时,深度加倍。
- Sber-MoVQGAN,一个经证明在图像修复方面具有卓越效果的解码器。
原始代码库可在 ai-forever/Kandinsky-3 找到。
请查看 Hub 上的 Kandinsky 社区 组织,获取用于文本到图像、图像到图像和图像修复等任务的官方模型检查点。
Kandinsky3Pipeline
class diffusers.Kandinsky3Pipeline
< 来源 >( tokenizer: T5Tokenizer text_encoder: T5EncoderModel unet: Kandinsky3UNet scheduler: DDPMScheduler movq: VQModel )
__call__
< 来源 >( prompt: typing.Union[str, typing.List[str]] = None num_inference_steps: int = 25 guidance_scale: float = 3.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_images_per_prompt: typing.Optional[int] = 1 height: typing.Optional[int] = 1024 width: typing.Optional[int] = 1024 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None attention_mask: typing.Optional[torch.Tensor] = None negative_attention_mask: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True latents = None callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] **kwargs ) → ImagePipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
, 可选) — 用于引导图像生成的提示或提示列表。如果未定义,则必须传入prompt_embeds
。 - num_inference_steps (
int
, 可选, 默认为 25) — 去噪步数。更多去噪步数通常会带来更高质量的图像,但会以较慢的推理速度为代价。 - timesteps (
List[int]
, 可选) — 用于去噪过程的自定义时间步。如果未定义,则使用等间距的num_inference_steps
时间步。必须按降序排列。 - guidance_scale (
float
, 可选, 默认为 3.0) — 如 Classifier-Free Diffusion Guidance 中定义的引导比例。guidance_scale
定义为 Imagen Paper 中公式 2 的w
。通过设置guidance_scale > 1
启用引导比例。更高的引导比例鼓励生成与文本prompt
紧密相关的图像,通常以牺牲图像质量为代价。 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示或提示列表。如果未定义,则必须传入negative_prompt_embeds
。当不使用引导时(即,如果guidance_scale
小于1
),则忽略此参数。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示生成的图像数量。 - height (
int
, 可选, 默认为 self.unet.config.sample_size) — 生成图像的高度(像素)。 - width (
int
, 可选, 默认为 self.unet.config.sample_size) — 生成图像的宽度(像素)。 - eta (
float
, 可选, 默认为 0.0) — 对应于 DDIM 论文:https://huggingface.co/papers/2010.02502 中的参数 eta (η)。仅适用于 schedulers.DDIMScheduler,对其他调度器将被忽略。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个或多个 torch 生成器,用于使生成具有确定性。 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示加权。如果未提供,将从prompt
输入参数生成文本嵌入。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负文本嵌入。可用于轻松调整文本输入,例如 提示加权。如果未提供,负提示嵌入将从negative_prompt
输入参数生成。 - attention_mask (
torch.Tensor
, 可选) — 预生成的注意力掩码。如果直接传入prompt_embeds
,则必须提供。 - negative_attention_mask (
torch.Tensor
, 可选) — 预生成的负注意力掩码。如果直接传入negative_prompt_embeds
,则必须提供。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。选择 PIL:PIL.Image.Image
或np.array
。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回~pipelines.stable_diffusion.IFPipelineOutput
而非纯元组。 - callback (
Callable
, 可选) — 在推理过程中,每callback_steps
步都会调用的函数。该函数将使用以下参数调用:callback(step: int, timestep: int, latents: torch.Tensor)
。 - callback_steps (
int
, 可选, 默认为 1) — 调用callback
函数的频率。如果未指定,回调将在每一步都被调用。 - clean_caption (
bool
, 可选, 默认为True
) — 是否在创建嵌入之前清理标题。需要安装beautifulsoup4
和ftfy
。如果未安装依赖项,嵌入将从原始提示创建。 - cross_attention_kwargs (
dict
, 可选) — 一个 kwargs 字典,如果指定,将作为参数传递给 diffusers.models.attention_processor 中self.processor
定义的AttentionProcessor
。
返回
ImagePipelineOutput 或 tuple
调用管道进行生成时调用的函数。
示例
>>> from diffusers import AutoPipelineForText2Image
>>> import torch
>>> pipe = AutoPipelineForText2Image.from_pretrained(
... "kandinsky-community/kandinsky-3", variant="fp16", torch_dtype=torch.float16
... )
>>> pipe.enable_model_cpu_offload()
>>> prompt = "A photograph of the inside of a subway train. There are raccoons sitting on the seats. One of them is reading a newspaper. The window shows the city in the background."
>>> generator = torch.Generator(device="cpu").manual_seed(0)
>>> image = pipe(prompt, num_inference_steps=25, generator=generator).images[0]
encode_prompt
< 来源 >( prompt do_classifier_free_guidance = True num_images_per_prompt = 1 device = None negative_prompt = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None _cut_context = False attention_mask: typing.Optional[torch.Tensor] = None negative_attention_mask: typing.Optional[torch.Tensor] = None )
参数
- prompt (
str
或List[str]
, 可选) — 待编码的提示 - device — (
torch.device
, 可选): 放置结果嵌入的 torch 设备 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示应生成的图像数量 - do_classifier_free_guidance (
bool
, 可选, 默认为True
) — 是否使用分类器自由指导 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示词。如果未定义,则必须传递negative_prompt_embeds
。如果未定义,则必须传递negative_prompt_embeds
。当不使用指导时(即,如果guidance_scale
小于1
时)将被忽略。 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示词权重。如果未提供,文本嵌入将根据prompt
输入参数生成。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负向文本嵌入。可用于轻松调整文本输入,例如提示词权重。如果未提供,负向提示词嵌入将根据negative_prompt
输入参数生成。 - attention_mask (
torch.Tensor
, 可选) — 预生成的注意力掩码。如果直接传递prompt_embeds
则必须提供。 - negative_attention_mask (
torch.Tensor
, 可选) — 预生成的负向注意力掩码。如果直接传递negative_prompt_embeds
则必须提供。
将提示编码为文本编码器隐藏状态。
Kandinsky3Img2ImgPipeline
class diffusers.Kandinsky3Img2ImgPipeline
< source >( tokenizer: T5Tokenizer text_encoder: T5EncoderModel unet: Kandinsky3UNet scheduler: DDPMScheduler movq: VQModel )
__call__
< source >( prompt: typing.Union[str, typing.List[str]] = None image: typing.Union[torch.Tensor, PIL.Image.Image, typing.List[torch.Tensor], typing.List[PIL.Image.Image]] = None strength: float = 0.3 num_inference_steps: int = 25 guidance_scale: float = 3.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_images_per_prompt: typing.Optional[int] = 1 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None attention_mask: typing.Optional[torch.Tensor] = None negative_attention_mask: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] **kwargs ) → ImagePipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
, 可选) — 用于引导图像生成的提示词。如果未定义,则必须传递prompt_embeds
。 - image (
torch.Tensor
,PIL.Image.Image
,np.ndarray
,List[torch.Tensor]
,List[PIL.Image.Image]
或List[np.ndarray]
) — 用作过程起始点的图像,或代表图像批次的张量。 - strength (
float
, 可选, 默认为 0.8) — 指示转换参考image
的程度。必须介于 0 和 1 之间。image
用作起点,strength
越高,添加的噪声越多。去噪步骤的数量取决于最初添加的噪声量。当strength
为 1 时,添加的噪声最大,去噪过程将运行num_inference_steps
中指定的全部迭代次数。值为 1 基本上会忽略image
。 - num_inference_steps (
int
, 可选, 默认为 50) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但会以较慢的推理速度为代价。 - guidance_scale (
float
, 可选, 默认为 3.0) — 如 Classifier-Free Diffusion Guidance 中定义的指导比例。guidance_scale
定义为 Imagen Paper 方程 2 中的w
。通过设置guidance_scale > 1
启用指导比例。较高的指导比例会促使生成与文本prompt
紧密相关的图像,但通常会以较低的图像质量为代价。 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示词。如果未定义,则必须传递negative_prompt_embeds
。当不使用指导时(即,如果guidance_scale
小于1
时)将被忽略。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示词生成的图像数量。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个或多个 torch 生成器,用于使生成过程具有确定性。 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示词权重。如果未提供,文本嵌入将根据prompt
输入参数生成。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负向文本嵌入。可用于轻松调整文本输入,例如提示词权重。如果未提供,负向提示词嵌入将根据negative_prompt
输入参数生成。 - attention_mask (
torch.Tensor
, 可选) — 预生成的注意力掩码。如果直接传递prompt_embeds
则必须提供。 - negative_attention_mask (
torch.Tensor
, 可选) — 预生成的负向注意力掩码。如果直接传递negative_prompt_embeds
则必须提供。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。在 PIL:PIL.Image.Image
或np.array
之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回~pipelines.stable_diffusion.IFPipelineOutput
而不是普通元组。 - callback_on_step_end (
Callable
, 可选) — 在推理过程中,每个去噪步骤结束时调用的函数。该函数将使用以下参数调用:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
。callback_kwargs
将包含callback_on_step_end_tensor_inputs
中指定的所有张量列表。 - callback_on_step_end_tensor_inputs (
List
, 可选) —callback_on_step_end
函数的张量输入列表。列表中指定的张量将作为callback_kwargs
参数传递。您只能包含管道类的._callback_tensor_inputs
属性中列出的变量。
返回
ImagePipelineOutput 或 tuple
调用管道进行生成时调用的函数。
示例
>>> from diffusers import AutoPipelineForImage2Image
>>> from diffusers.utils import load_image
>>> import torch
>>> pipe = AutoPipelineForImage2Image.from_pretrained(
... "kandinsky-community/kandinsky-3", variant="fp16", torch_dtype=torch.float16
... )
>>> pipe.enable_model_cpu_offload()
>>> prompt = "A painting of the inside of a subway train with tiny raccoons."
>>> image = load_image(
... "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinsky3/t2i.png"
... )
>>> generator = torch.Generator(device="cpu").manual_seed(0)
>>> image = pipe(prompt, image=image, strength=0.75, num_inference_steps=25, generator=generator).images[0]
encode_prompt
< source >( prompt do_classifier_free_guidance = True num_images_per_prompt = 1 device = None negative_prompt = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None _cut_context = False attention_mask: typing.Optional[torch.Tensor] = None negative_attention_mask: typing.Optional[torch.Tensor] = None )
将提示编码为文本编码器隐藏状态。
device: (torch.device
, 可选): 用于放置结果嵌入的 torch 设备 num_images_per_prompt (int
, 可选, 默认为 1): 每个提示词应生成的图像数量 do_classifier_free_guidance (bool
, 可选, 默认为 True
): 是否使用分类器自由指导 negative_prompt (str
或 List[str]
, 可选): 不用于引导图像生成的提示词。如果未定义,则必须传递 negative_prompt_embeds
。如果未定义,则必须传递 negative_prompt_embeds
。当不使用指导时(即,如果 guidance_scale
小于 1
时)将被忽略。 prompt_embeds (torch.Tensor
, 可选): 预生成的文本嵌入。可用于轻松调整文本输入,例如提示词权重。如果未提供,文本嵌入将根据 prompt
输入参数生成。 negative_prompt_embeds (torch.Tensor
, 可选): 预生成的负向文本嵌入。可用于轻松调整文本输入,例如提示词权重。如果未提供,负向提示词嵌入将根据 negative_prompt
输入参数生成。 attention_mask (torch.Tensor
, 可选): 预生成的注意力掩码。如果直接传递 prompt_embeds
则必须提供。 negative_attention_mask (torch.Tensor
, 可选): 预生成的负向注意力掩码。如果直接传递 negative_prompt_embeds
则必须提供。