Diffusers documentation
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 模型系列构建的开源文本到图像扩散模型。与之前的版本相比,该模型在文本理解和视觉质量方面进行了增强,分别通过增大文本编码器和 Diffusion U-Net 模型的规模来实现。
其架构包含 3 个主要组件:
- FLAN-UL2,这是一个基于 T5 架构的编码器-解码器模型。
- 新的 U-Net 架构采用 BigGAN-deep 块,在保持参数量不变的情况下将深度加倍。
- Sber-MoVQGAN 是一种解码器,已被证明在图像修复方面具有更优的效果。
原始代码库可在 ai-forever/Kandinsky-3 中找到。
在 Hub 上查看 Kandinsky Community 组织,以获取文本生成图像、图像生成图像和图像修复等任务的官方模型检查点。
请务必查阅调度器指南,了解如何探索调度器速度与质量之间的权衡,并参阅跨流水线复用组件部分,了解如何高效地将相同组件加载到多个流水线中。
Kandinsky3Pipeline
类 diffusers.Kandinsky3Pipeline
< source >( tokenizer: T5Tokenizertext_encoder: T5EncoderModelunet: Kandinsky3UNetscheduler: DDPMSchedulermovq: VQModel )
__call__
< source >( prompt: str | list[str] = Nonenum_inference_steps: int = 25guidance_scale: float = 3.0negative_prompt: str | list[str] | None = Nonenum_images_per_prompt: int | None = 1height: int | None = 1024width: int | None = 1024generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Noneprompt_embeds: typing.Optional[torch.Tensor] = Nonenegative_prompt_embeds: typing.Optional[torch.Tensor] = Noneattention_mask: typing.Optional[torch.Tensor] = Nonenegative_attention_mask: typing.Optional[torch.Tensor] = Noneoutput_type: str | None = 'pil'return_dict: bool = Truelatents = Nonecallback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = Nonecallback_on_step_end_tensor_inputs: list[str] = ['latents']**kwargs ) → ImagePipelineOutput or tuple
参数
- prompt (
str或list[str],可选) — 用于引导图像生成的提示词或提示词列表。如果未定义,则必须改为传入prompt_embeds。 - num_inference_steps (
int,可选,默认为 25) — 去噪步数。更多的去噪步数通常能生成更高质量的图像,但代价是推理速度变慢。 - 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) — 生成图像的宽度(以像素为单位)。 - generator (
torch.Generator或list[torch.Generator],可选) — 一个或一组 torch 生成器,用于使生成过程具有确定性。 - prompt_embeds (
torch.Tensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示词加权。如果未提供,文本嵌入将从prompt输入参数生成。 - negative_prompt_embeds (
torch.Tensor, 可选) — 预生成的负向文本嵌入。可用于轻松调整文本输入,例如提示词加权。如果未提供,negative_prompt_embeds 将从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而不是普通元组。 - latents (
torch.Tensor, 可选) — 预先生成的噪声潜变量,从高斯分布中采样得到,用作图像生成的输入。可用于使用不同的提示词对同一生成过程进行微调。如果未提供,将通过使用提供的随机generator进行采样来生成一个潜变量张量。 - 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
调用生成 pipeline 时调用的函数。
示例:
>>> 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
< source >( promptdo_classifier_free_guidance = Truenum_images_per_prompt = 1device = Nonenegative_prompt = Noneprompt_embeds: typing.Optional[torch.Tensor] = Nonenegative_prompt_embeds: typing.Optional[torch.Tensor] = None_cut_context = Falseattention_mask: typing.Optional[torch.Tensor] = Nonenegative_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) — 是否使用 classifier free guidance - 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_embeds 将从negative_prompt输入参数生成。 - attention_mask (
torch.Tensor, 可选) — 预生成的注意力掩码。如果直接传入prompt_embeds,则必须提供此项。 - negative_attention_mask (
torch.Tensor,可选) — 预生成的负注意力掩码。如果直接传入negative_prompt_embeds,则必须提供此参数。
将提示词编码为文本编码器的隐藏状态。
Kandinsky3Img2ImgPipeline
class diffusers.Kandinsky3Img2ImgPipeline
< source >( tokenizer: T5Tokenizertext_encoder: T5EncoderModelunet: Kandinsky3UNetscheduler: DDPMSchedulermovq: VQModel )
__call__
< source >( prompt: str | list[str] = Noneimage: typing.Union[torch.Tensor, list[torch.Tensor], PIL.Image.Image, list[PIL.Image.Image]] = Nonestrength: float = 0.3num_inference_steps: int = 25guidance_scale: float = 3.0negative_prompt: str | list[str] | None = Nonenum_images_per_prompt: int | None = 1generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Noneprompt_embeds: typing.Optional[torch.Tensor] = Nonenegative_prompt_embeds: typing.Optional[torch.Tensor] = Noneattention_mask: typing.Optional[torch.Tensor] = Nonenegative_attention_mask: typing.Optional[torch.Tensor] = Noneoutput_type: str | None = 'pil'return_dict: bool = Truecallback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = Nonecallback_on_step_end_tensor_inputs: list[str] = ['latents']**kwargs ) → ImagePipelineOutput or 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]) —Image,或表示图像批次的张量,将作为该流程的起始点。 - 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_embeds 将从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
调用生成 pipeline 时调用的函数。
示例:
>>> 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 >( promptdo_classifier_free_guidance = Truenum_images_per_prompt = 1device = Nonenegative_prompt = Noneprompt_embeds: typing.Optional[torch.Tensor] = Nonenegative_prompt_embeds: typing.Optional[torch.Tensor] = None_cut_context = Trueattention_mask: typing.Optional[torch.Tensor] = Nonenegative_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 输入参数生成 negative_prompt_embeds。
attention_mask (torch.Tensor, 可选):
预生成的注意力掩码。如果直接传入 prompt_embeds,则必须提供此项。
negative_attention_mask (torch.Tensor, 可选):
预生成的负向注意力掩码。如果直接传入 negative_prompt_embeds,则必须提供此项。