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
則必須提供。