Diffusers documentation

Kandinsky 3

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Kandinsky 3

LoRA

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つの主要なコンポーネントが含まれています。

  1. T5アーキテクチャをベースとしたエンコーダーデコーダーモデルであるFLAN-UL2。
  2. BigGAN-deepブロックを特徴とする新しいU-Netアーキテクチャは、パラメータ数を維持したまま深さを2倍にします。
  3. Sber-MoVQGANは、画像復元において優れた結果をもたらすことが実証されているデコーダーです。

元のコードベースは ai-forever/Kandinsky-3 で確認できます。

Hub上のKandinsky Community組織では、テキストから画像への変換、画像から画像への変換、インペインティングなどのタスク向けの公式モデルチェックポイントを確認できます。

スケジューラーの ガイド を確認し、スケジューラーの速度と品質のトレードオフをどのように探索するかを学び、パイプライン間でコンポーネントを再利用する セクションを確認して、同じコンポーネントを複数のパイプラインに効率的にロードする方法を学びましょう。

Kandinsky3Pipeline

class diffusers.Kandinsky3Pipeline

< >

( tokenizer: T5Tokenizertext_encoder: T5EncoderModelunet: Kandinsky3UNetscheduler: DDPMSchedulermovq: VQModel )

__call__

< >

( 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 ジェネレーター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を直接渡す場合は、必ず指定する必要があります。
  • 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

生成用のパイプラインを呼び出したときに実行される関数。

例:

>>> 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

< >

( 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 入力引数から negative_prompt_embeds が生成されます。
  • attention_mask (torch.Tensor, 任意) — 事前に生成されたアテンションマスク。prompt_embedsを直接渡す場合は、必ず指定する必要があります。
  • negative_attention_mask (torch.Tensor, 任意) — 事前に生成されたネガティブアテンションマスク。negative_prompt_embedsを直接渡す場合は、必ず指定する必要があります。

プロンプトをテキストエンコーダーの隠れ状態にエンコードします。

Kandinsky3Img2ImgPipeline

class diffusers.Kandinsky3Img2ImgPipeline

< >

( tokenizer: T5Tokenizertext_encoder: T5EncoderModelunet: Kandinsky3UNetscheduler: DDPMSchedulermovq: VQModel )

__call__

< >

( 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 ジェネレーター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を直接渡す場合は、必ず指定する必要があります。
  • 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

< >

( 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 )

パラメータ

  • prompt (str または list[str]、省略可能) — エンコードするプロンプト

プロンプトをテキストエンコーダーの隠れ状態にエンコードします。

device: (torch.device, 省略可): 生成される埋め込みを配置する torch デバイス num_images_per_prompt (int, 省略可, デフォルトは 1): 1 つのプロンプトあたり生成すべき画像の数 do_classifier_free_guidance (bool, 省略可, デフォルトは True): classifier free guidance を使用するかどうか negative_prompt (str または list[str], 省略可): 画像生成をガイドしないプロンプト。未定義の場合は、代わりに negative_prompt_embeds を渡す必要があります。未定義の場合は、代わりに negative_prompt_embeds を渡す必要があります。 guidance を使用していない場合は無視されます(つまり、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 を直接渡す場合は、提供する必要があります。

Update on GitHub