Diffusers documentation

Kandinsky 2.2

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Kandinsky 2.2

Kandinsky 2.2 は、Arseniy Shakhmatov、Anton Razzhigaev、Aleksandr Nikolich、Vladimir Arkhipkin、Igor Pavlov、Andrey Kuznetsov、Denis Dimitrov によって作成されました。

そのGitHubページの説明は以下の通りです:

Kandinsky 2.2 は、より強力な新しい画像エンコーダーである CLIP-ViT-G と ControlNet のサポートを導入することで、先行バージョンである Kandinsky 2.1 に対して大幅な改善をもたらしました。画像エンコーダーを CLIP-ViT-G に切り替えたことで、モデルがより美的な画像を生成し、テキストをより正確に理解する能力が大幅に向上し、結果としてモデル全体の性能が強化されました。ControlNet メカニズムの追加により、モデルは画像生成のプロセスを効果的に制御できるようになりました。これにより、より正確で視覚的に魅力的な出力が得られ、テキスト主導の画像操作における新たな可能性が拓かれます。

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

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

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

KandinskyV22PriorPipeline

クラス diffusers.KandinskyV22PriorPipeline

< >

( prior: PriorTransformerimage_encoder: CLIPVisionModelWithProjectiontext_encoder: CLIPTextModelWithProjectiontokenizer: CLIPTokenizerscheduler: UnCLIPSchedulerimage_processor: CLIPImageProcessorPil )

パラメータ

  • prior (PriorTransformer) — テキスト埋め込みから画像埋め込みを近似するための、正規の unCLIP 事前分布モデル。
  • image_encoder (CLIPVisionModelWithProjection) — 凍結された画像エンコーダーです。
  • text_encoder (CLIPTextModelWithProjection) — 凍結されたテキストエンコーダー。
  • tokenizer (CLIPTokenizer) — CLIPTokenizer クラスのトークナイザー。
  • scheduler (UnCLIPScheduler) — prior と組み合わせて画像埋め込みを生成するために使用するスケジューラーです。
  • image_processor (CLIPImageProcessor) — clip の画像を前処理するために使用する image_processor です。

Kandinsky用の画像事前分布を生成するためのPipeline

このモデルは DiffusionPipeline を継承しています。すべてのパイプラインに対してライブラリが実装している汎用的なメソッド(ダウンロードや保存、特定のデバイスでの実行など)については、スーパークラスのドキュメントを参照してください。

__call__

< >

( prompt: str | list[str]negative_prompt: str | list[str] | None = Nonenum_images_per_prompt: int = 1num_inference_steps: int = 25generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = Noneguidance_scale: float = 4.0output_type: str | None = 'pt'return_dict: bool = Truecallback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = Nonecallback_on_step_end_tensor_inputs: list = ['latents'] ) → KandinskyPriorPipelineOutput or tuple

パラメータ

  • prompt (str または list[str]) — 画像生成をガイドするためのプロンプト、またはプロンプトのリスト。
  • negative_prompt (str または list[str]、省略可) — 画像生成をガイドしないプロンプトです。ガイダンスを使用していない場合は無視されます(guidance_scale が 1 未満の場合は無視されます)。
  • num_images_per_prompt (int、任意、デフォルトは 1) — プロンプトごとに生成する画像の数。
  • num_inference_steps (int、任意、デフォルト値は 100) — デノイジングのステップ数です。デノイジングのステップ数を増やすと、推論速度が遅くなる代わりに、通常はより高品質な画像が得られます。
  • generator (torch.Generator または list[torch.Generator]、省略可) — 生成を決定論的にするためのtorch ジェネレーター1つまたはリスト。
  • latents (torch.Tensor, 任意) — 画像生成の入力として使用する、ガウス分布からサンプリングされた事前生成されたノイズ付き潜在変数。異なるプロンプトで同じ生成を調整するために使用できます。指定されない場合、指定されたランダムgeneratorを使用してサンプリングを行い、潜在変数テンソルが生成されます。
  • guidance_scale (float、任意、デフォルト値は 4.0) — Classifier-Free Diffusion Guidance で定義されているガイダンススケールです。guidance_scale は、Imagen Paper の式 2 の w として定義されています。guidance_scale > 1 を設定することでガイダンススケールが有効になります。ガイダンススケールを高くすると、prompt のテキストと密接に関連した画像の生成が促されますが、通常は画像の品質が低下する傾向があります。
  • output_type (str, 任意、デフォルトは "pt") — 画像生成の出力形式です。"np" (np.array) または "pt" (torch.Tensor) から選択します。
  • return_dict (bool、省略可能、デフォルトは True) — True の場合、通常のタプルではなく ImagePipelineOutput を返します。
  • 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 属性にリストされている変数のみを、このリストに含めることができます。

戻り値

KandinskyPriorPipelineOutput または tuple

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

例:

>>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline
>>> import torch

>>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior")
>>> pipe_prior.to("cuda")
>>> prompt = "red cat, 4k photo"
>>> image_emb, negative_image_emb = pipe_prior(prompt).to_tuple()

>>> pipe = KandinskyV22Pipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder")
>>> pipe.to("cuda")
>>> image = pipe(
...     image_embeds=image_emb,
...     negative_image_embeds=negative_image_emb,
...     height=768,
...     width=768,
...     num_inference_steps=50,
... ).images
>>> image[0].save("cat.png")

interpolate

< >

( images_and_prompts: listweights: listnum_images_per_prompt: int = 1num_inference_steps: int = 25generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = Nonenegative_prior_prompt: str | None = Nonenegative_prompt: str = ''guidance_scale: float = 4.0device = None ) → KandinskyPriorPipelineOutput or tuple

パラメータ

  • images_and_prompts (list[str | PIL.Image.Image | torch.Tensor]) — 画像生成をガイドするためのプロンプトと画像のリスト。
  • weights — (list[float]): images_and_prompts 内の各条件に対する重みのリスト
  • num_images_per_prompt (int、任意、デフォルトは 1) — プロンプトごとに生成する画像の数。
  • num_inference_steps (int、任意、デフォルト値は 100) — デノイジングのステップ数です。デノイジングのステップ数を増やすと、推論速度が遅くなる代わりに、通常はより高品質な画像が得られます。
  • generator (torch.Generator または list[torch.Generator]、省略可) — 生成を決定論的にするためのtorch ジェネレーター1つまたはリスト。
  • latents (torch.Tensor, 任意) — 画像生成の入力として使用する、ガウス分布からサンプリングされた事前生成されたノイズ付き潜在変数。異なるプロンプトで同じ生成を調整するために使用できます。指定されない場合、指定されたランダムgeneratorを使用してサンプリングを行い、潜在変数テンソルが生成されます。
  • negative_prior_prompt (str, 省略可) — 事前拡散プロセスをガイドしないためのプロンプトです。ガイダンスを使用していない場合は無視されます(つまり、guidance_scaleが1未満の場合は無視されます)。
  • negative_prompt (str または list[str]、任意) — 画像生成を導かないためのプロンプトです。ガイダンスを使用していない場合は無視されます(つまり、guidance_scale が 1 未満の場合は無視されます)。
  • guidance_scale (float、任意、デフォルト値は 4.0) — Classifier-Free Diffusion Guidance で定義されているガイダンススケールです。guidance_scale は、Imagen Paper の式 2 の w として定義されています。guidance_scale > 1 を設定することでガイダンススケールが有効になります。ガイダンススケールを高くすると、prompt のテキストと密接に関連した画像の生成が促されますが、通常は画像の品質が低下する傾向があります。

戻り値

KandinskyPriorPipelineOutput または tuple

補間のために先行パイプラインを使用する際に呼び出される関数。

例:

>>> from diffusers import KandinskyV22PriorPipeline, KandinskyV22Pipeline
>>> from diffusers.utils import load_image
>>> import PIL
>>> import torch
>>> from torchvision import transforms

>>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-2-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]
>>> out = pipe_prior.interpolate(images_texts, weights)
>>> pipe = KandinskyV22Pipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16
... )
>>> pipe.to("cuda")
>>> image = pipe(
...     image_embeds=out.image_embeds,
...     negative_image_embeds=out.negative_image_embeds,
...     height=768,
...     width=768,
...     num_inference_steps=50,
... ).images[0]
>>> image.save("starry_cat.png")

KandinskyV22Pipeline

class diffusers.KandinskyV22Pipeline

< >

( unet: UNet2DConditionModelscheduler: DDPMSchedulermovq: VQModel )

パラメータ

  • scheduler (DDIMScheduler | DDPMScheduler) — unet と組み合わせて使用し、画像の潜在変数を生成するためのスケジューラです。
  • unet (UNet2DConditionModel) — 画像エンベディングのノイズ除去を行う条件付き U-Net アーキテクチャ。
  • movq (VQModel) — 潜在変数から画像を生成するための MoVQ デコーダー。

Kandinsky を使用したテキストから画像への生成パイプライン

このモデルは DiffusionPipeline を継承しています。すべてのパイプラインに対してライブラリが実装している汎用的なメソッド(ダウンロードや保存、特定のデバイスでの実行など)については、スーパークラスのドキュメントを参照してください。

__call__

< >

( image_embeds: typing.Union[torch.Tensor, list[torch.Tensor]]negative_image_embeds: typing.Union[torch.Tensor, list[torch.Tensor]]height: int = 512width: int = 512num_inference_steps: int = 100guidance_scale: float = 4.0num_images_per_prompt: int = 1generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: 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 = ['latents']**kwargs ) → ImagePipelineOutput or tuple

パラメータ

  • image_embeds (torch.Tensor または list[torch.Tensor]) — テキストプロンプトのクリップ画像埋め込みです。画像生成の条件付けに使用されます。
  • negative_image_embeds (torch.Tensor または list[torch.Tensor]) — ネガティブなテキストプロンプトの CLIP 画像埋め込みです。画像生成の条件付けに使用されます。
  • 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 ジェネレーター1つまたはリスト。
  • latents (torch.Tensor, 任意) — 画像生成の入力として使用する、ガウス分布からサンプリングされた事前生成されたノイズ付き潜在変数。異なるプロンプトで同じ生成を調整するために使用できます。指定されない場合、指定されたランダムgeneratorを使用してサンプリングを行い、潜在変数テンソルが生成されます。
  • output_type (str, 省略可能、デフォルトは "pil") — 画像生成の出力形式です。"pil" (PIL.Image.Image)、"np" (np.array)、または "pt" (torch.Tensor) から選択します。
  • return_dict (bool、省略可能、デフォルトは True) — True の場合、通常のタプルではなく ImagePipelineOutput を返します。
  • 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 KandinskyV22Pipeline, KandinskyV22PriorPipeline
>>> import torch

>>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior")
>>> pipe_prior.to("cuda")
>>> prompt = "red cat, 4k photo"
>>> out = pipe_prior(prompt)
>>> image_emb = out.image_embeds
>>> zero_image_emb = out.negative_image_embeds
>>> pipe = KandinskyV22Pipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder")
>>> pipe.to("cuda")
>>> image = pipe(
...     image_embeds=image_emb,
...     negative_image_embeds=zero_image_emb,
...     height=768,
...     width=768,
...     num_inference_steps=50,
... ).images
>>> image[0].save("cat.png")

KandinskyV22CombinedPipeline

class diffusers.KandinskyV22CombinedPipeline

< >

( unet: UNet2DConditionModelscheduler: DDPMSchedulermovq: VQModelprior_prior: PriorTransformerprior_image_encoder: CLIPVisionModelWithProjectionprior_text_encoder: CLIPTextModelWithProjectionprior_tokenizer: CLIPTokenizerprior_scheduler: UnCLIPSchedulerprior_image_processor: CLIPImageProcessorPil )

パラメータ

  • scheduler (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 と組み合わせて画像埋め込みを生成するために使用するスケジューラです。
  • prior_image_processor (CLIPImageProcessor) — clip の画像を前処理するために使用する image_processor。

Kandinsky を使用したテキストから画像への生成を行う統合 Pipeline

このモデルは DiffusionPipeline を継承しています。すべてのパイプラインに対してライブラリが実装している汎用的なメソッド(ダウンロードや保存、特定のデバイスでの実行など)については、スーパークラスのドキュメントを参照してください。

__call__

< >

( prompt: str | list[str]negative_prompt: str | list[str] | None = Nonenum_inference_steps: int = 100guidance_scale: float = 4.0num_images_per_prompt: int = 1height: int = 512width: int = 512prior_guidance_scale: float = 4.0prior_num_inference_steps: int = 25generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = Noneoutput_type: str | None = 'pil'callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = Nonecallback_steps: int = 1return_dict: bool = Trueprior_callback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = Noneprior_callback_on_step_end_tensor_inputs: list = ['latents']callback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = Nonecallback_on_step_end_tensor_inputs: list = ['latents'] ) → ImagePipelineOutput or 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 ジェネレーター1つまたはリスト。
  • 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 関数が呼び出される頻度です。指定しない場合、callback はすべてのステップで呼び出されます。
  • return_dict (bool、省略可能、デフォルトは True) — True の場合、通常のタプルではなく ImagePipelineOutput を返します。
  • prior_callback_on_step_end (Callable, 省略可) — prior パイプラインの推論中、各デノイジングステップの終了時に呼び出される関数です。この関数は、以下の引数とともに呼び出されます: prior_callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)。
  • prior_callback_on_step_end_tensor_inputs (list、省略可) — prior_callback_on_step_end 関数へのテンソル入力の一覧です。このリストで指定されたテンソルは、callback_kwargs 引数として渡されます。先験的パイプラインクラスの ._callback_tensor_inputs 属性に列挙されている変数のみを、このリストに含めることができます。
  • 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-2-2-decoder", 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

< >

( gpu_id: int | None = Nonedevice: typing.Union[torch.device, str] = None )

Offloads all models to CPU using accelerate, significantly reducing memory usage. When called, unet, text_encoder, vae and safety checker have their state dicts saved to CPU and then are moved to a torch.device('meta') and loaded to GPU only when their specific submodule has itsforwardmethod called. Note that offloading happens on a submodule basis. Memory savings are higher than withenable_model_cpu_offload`, but performance is lower.

KandinskyV22ControlnetPipeline

class diffusers.KandinskyV22ControlnetPipeline

< >

( unet: UNet2DConditionModelscheduler: DDPMSchedulermovq: VQModel )

パラメータ

  • scheduler (DDIMScheduler) — unet と組み合わせて使用し、画像の潜在変数を生成するためのスケジューラ。
  • unet (UNet2DConditionModel) — 画像エンベディングのノイズ除去を行う条件付き U-Net アーキテクチャ。
  • movq (VQModel) — 潜在変数から画像を生成するための MoVQ デコーダー。

Kandinsky を使用したテキストから画像への生成パイプライン

このモデルは DiffusionPipeline を継承しています。すべてのパイプラインに対してライブラリが実装している汎用的なメソッド(ダウンロードや保存、特定のデバイスでの実行など)については、スーパークラスのドキュメントを参照してください。

__call__

< >

( image_embeds: typing.Union[torch.Tensor, list[torch.Tensor]]negative_image_embeds: typing.Union[torch.Tensor, list[torch.Tensor]]hint: Tensorheight: int = 512width: int = 512num_inference_steps: int = 100guidance_scale: float = 4.0num_images_per_prompt: int = 1generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = Noneoutput_type: str | None = 'pil'callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = Nonecallback_steps: int = 1return_dict: bool = True ) → ImagePipelineOutput or tuple

パラメータ

  • hint (torch.Tensor) — ControlNet の条件です。
  • image_embeds (torch.Tensor または list[torch.Tensor]) — テキストプロンプトのクリップ画像埋め込みです。画像生成の条件付けに使用されます。
  • negative_image_embeds (torch.Tensor または list[torch.Tensor]) — ネガティブなテキストプロンプトの CLIP 画像埋め込みです。画像生成の条件付けに使用されます。
  • 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 ジェネレーター1つまたはリスト。
  • 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 関数が呼び出される頻度です。指定しない場合、callback はすべてのステップで呼び出されます。
  • return_dict (bool、省略可能、デフォルトは True) — True の場合、通常のタプルではなく ImagePipelineOutput を返します。

戻り値

ImagePipelineOutput または tuple

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

例:

KandinskyV22PriorEmb2EmbPipeline

クラス diffusers.KandinskyV22PriorEmb2EmbPipeline

< >

( prior: PriorTransformerimage_encoder: CLIPVisionModelWithProjectiontext_encoder: CLIPTextModelWithProjectiontokenizer: CLIPTokenizerscheduler: UnCLIPSchedulerimage_processor: CLIPImageProcessorPil )

パラメータ

  • prior (PriorTransformer) — テキスト埋め込みから画像埋め込みを近似するための、正規の unCLIP 事前分布モデル。
  • image_encoder (CLIPVisionModelWithProjection) — 凍結された画像エンコーダーです。
  • text_encoder (CLIPTextModelWithProjection) — 凍結されたテキストエンコーダー。
  • tokenizer (CLIPTokenizer) — CLIPTokenizer クラスのトークナイザー。
  • scheduler (UnCLIPScheduler) — prior と組み合わせて画像埋め込みを生成するために使用するスケジューラーです。

Kandinsky用の画像事前分布を生成するためのPipeline

このモデルは DiffusionPipeline を継承しています。すべてのパイプラインに対してライブラリが実装している汎用的なメソッド(ダウンロードや保存、特定のデバイスでの実行など)については、スーパークラスのドキュメントを参照してください。

__call__

< >

( prompt: str | list[str]image: typing.Union[torch.Tensor, list[torch.Tensor], PIL.Image.Image, list[PIL.Image.Image]]strength: float = 0.3negative_prompt: str | list[str] | None = Nonenum_images_per_prompt: int = 1num_inference_steps: int = 25generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Noneguidance_scale: float = 4.0output_type: str | None = 'pt'return_dict: bool = True ) → KandinskyPriorPipelineOutput or tuple

パラメータ

  • prompt (str または list[str]) — 画像生成をガイドするためのプロンプト、またはプロンプトのリスト。
  • image (torch.Tensor、PIL.Image.Image、list[torch.Tensor] または list[PIL.Image.Image]) — Image、または画像バッチを表すテンソル。画像埋め込みの起点として使用されます。直接潜在変数(latents)を渡す場合、image として画像の潜在変数を受け入れることもでき、その場合は再度エンコードされません。
  • strength (float、任意、デフォルト値は 0.8) — 概念的には、参照となるimageをどの程度変換するかを示します。0 から 1 の間の値である必要があります。image が起点として使用され、strengthが大きいほど、それに加えるノイズ量が増えます。デノイジングのステップ数は、最初に追加されるノイズ量に依存します。
  • negative_prompt (str または list[str]、省略可) — 画像生成をガイドしないプロンプトです。ガイダンスを使用していない場合は無視されます(guidance_scale が 1 未満の場合は無視されます)。
  • num_images_per_prompt (int、任意、デフォルトは 1) — プロンプトごとに生成する画像の数。
  • num_inference_steps (int、任意、デフォルト値は 100) — デノイジングのステップ数です。デノイジングのステップ数を増やすと、推論速度が遅くなる代わりに、通常はより高品質な画像が得られます。
  • generator (torch.Generator または list[torch.Generator]、省略可) — 生成を決定論的にするためのtorch ジェネレーター1つまたはリスト。
  • guidance_scale (float、任意、デフォルト値は 4.0) — Classifier-Free Diffusion Guidance で定義されているガイダンススケールです。guidance_scale は、Imagen Paper の式 2 の w として定義されています。guidance_scale > 1 を設定することでガイダンススケールが有効になります。ガイダンススケールを高くすると、prompt のテキストと密接に関連した画像の生成が促されますが、通常は画像の品質が低下する傾向があります。
  • output_type (str, 任意、デフォルトは "pt") — 画像生成の出力形式です。"np" (np.array) または "pt" (torch.Tensor) から選択します。
  • return_dict (bool、省略可能、デフォルトは True) — True の場合、通常のタプルではなく ImagePipelineOutput を返します。

戻り値

KandinskyPriorPipelineOutput または tuple

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

例:

>>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorEmb2EmbPipeline
>>> import torch

>>> pipe_prior = KandinskyPriorPipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
... )
>>> pipe_prior.to("cuda")

>>> prompt = "red cat, 4k photo"
>>> img = load_image(
...     "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinsky/cat.png"
... )
>>> image_emb, nagative_image_emb = pipe_prior(prompt, image=img, strength=0.2).to_tuple()

>>> pipe = KandinskyPipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-2-decoder, torch_dtype=torch.float16"
... )
>>> pipe.to("cuda")

>>> image = pipe(
...     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: listweights: listnum_images_per_prompt: int = 1num_inference_steps: int = 25generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = Nonenegative_prior_prompt: str | None = Nonenegative_prompt: str = ''guidance_scale: float = 4.0device = None ) → KandinskyPriorPipelineOutput or tuple

パラメータ

  • images_and_prompts (list[str | PIL.Image.Image | torch.Tensor]) — 画像生成をガイドするためのプロンプトと画像のリスト。
  • weights — (list[float]): images_and_prompts 内の各条件に対する重みのリスト
  • num_images_per_prompt (int、任意、デフォルトは 1) — プロンプトごとに生成する画像の数。
  • num_inference_steps (int、任意、デフォルト値は 100) — デノイジングのステップ数です。デノイジングのステップ数を増やすと、推論速度が遅くなる代わりに、通常はより高品質な画像が得られます。
  • generator (torch.Generator または list[torch.Generator]、省略可) — 生成を決定論的にするためのtorch ジェネレーター1つまたはリスト。
  • latents (torch.Tensor, 任意) — 画像生成の入力として使用する、ガウス分布からサンプリングされた事前生成されたノイズ付き潜在変数。異なるプロンプトで同じ生成を調整するために使用できます。指定されない場合、指定されたランダムgeneratorを使用してサンプリングを行い、潜在変数テンソルが生成されます。
  • negative_prior_prompt (str, 省略可) — 事前拡散プロセスをガイドしないためのプロンプトです。ガイダンスを使用していない場合は無視されます(つまり、guidance_scaleが1未満の場合は無視されます)。
  • negative_prompt (str または list[str]、任意) — 画像生成を導かないためのプロンプトです。ガイダンスを使用していない場合は無視されます(つまり、guidance_scale が 1 未満の場合は無視されます)。
  • guidance_scale (float、任意、デフォルト値は 4.0) — Classifier-Free Diffusion Guidance で定義されているガイダンススケールです。guidance_scale は、Imagen Paper の式 2 の w として定義されています。guidance_scale > 1 を設定することでガイダンススケールが有効になります。ガイダンススケールを高くすると、prompt のテキストと密接に関連した画像の生成が促されますが、通常は画像の品質が低下する傾向があります。

戻り値

KandinskyPriorPipelineOutput または tuple

補間のために先行パイプラインを使用する際に呼び出される関数。

例:

>>> from diffusers import KandinskyV22PriorEmb2EmbPipeline, KandinskyV22Pipeline
>>> from diffusers.utils import load_image
>>> import PIL

>>> import torch
>>> from torchvision import transforms

>>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-2-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 = KandinskyV22Pipeline.from_pretrained(
...     "kandinsky-community/kandinsky-2-2-decoder", 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")

KandinskyV22Img2ImgPipeline

クラス diffusers.KandinskyV22Img2ImgPipeline

< >

( unet: UNet2DConditionModelscheduler: DDPMSchedulermovq: VQModel )

パラメータ

  • scheduler (DDIMScheduler) — unet と組み合わせて使用し、画像の潜在変数を生成するためのスケジューラ。
  • unet (UNet2DConditionModel) — 画像エンベディングのノイズ除去を行う条件付き U-Net アーキテクチャ。
  • movq (VQModel) — 潜在変数から画像を生成するための MoVQ デコーダー。

Kandinsky を使用した画像から画像への生成のための Pipeline

このモデルは DiffusionPipeline を継承しています。すべてのパイプラインに対してライブラリが実装している汎用的なメソッド(ダウンロードや保存、特定のデバイスでの実行など)については、スーパークラスのドキュメントを参照してください。

__call__

< >

( image_embeds: typing.Union[torch.Tensor, list[torch.Tensor]]image: typing.Union[torch.Tensor, list[torch.Tensor], PIL.Image.Image, list[PIL.Image.Image]]negative_image_embeds: typing.Union[torch.Tensor, list[torch.Tensor]]height: int = 512width: int = 512num_inference_steps: int = 100guidance_scale: float = 4.0strength: float = 0.3num_images_per_prompt: int = 1generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = 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 = ['latents']**kwargs ) → ImagePipelineOutput or tuple

パラメータ

  • image_embeds (torch.Tensor または list[torch.Tensor]) — テキストプロンプトのクリップ画像埋め込みです。画像生成の条件付けに使用されます。
  • image (torch.Tensor、PIL.Image.Image、np.ndarray、list[torch.Tensor]、list[PIL.Image.Image]、または list[np.ndarray]) — プロセスの起点として使用されるImage、または画像バッチを表すテンソルです。直接ラテンツを渡す場合は、imageとして画像ラテンツを受け入れることもでき、その場合は再度エンコードされません。
  • strength (float、任意、デフォルト値は 0.8) — 概念的には、参照となるimageをどの程度変換するかを示します。0 から 1 の間の値である必要があります。image は起点として使用され、strengthが大きいほど、それに対してより多くのノイズが加えられます。デノイジングのステップ数は、最初に追加されるノイズの量に依存します。strengthが 1 の場合、加えられるノイズは最大となり、デノイジングプロセスはnum_inference_stepsで指定された全イテレーション数分実行されます。したがって、値が 1 の場合、imageは実質的に無視されます。
  • negative_image_embeds (torch.Tensor または list[torch.Tensor]) — ネガティブなテキストプロンプトの CLIP 画像埋め込みです。画像生成の条件付けに使用されます。
  • 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 ジェネレーター1つまたはリスト。
  • output_type (str, 省略可能、デフォルトは "pil") — 画像生成の出力形式です。"pil" (PIL.Image.Image)、"np" (np.array)、または "pt" (torch.Tensor) から選択します。
  • return_dict (bool、省略可能、デフォルトは True) — True の場合、通常のタプルではなく ImagePipelineOutput を返します。
  • 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

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

例:

KandinskyV22Img2ImgCombinedPipeline

クラス diffusers.KandinskyV22Img2ImgCombinedPipeline

< >

( unet: UNet2DConditionModelscheduler: DDPMSchedulermovq: VQModelprior_prior: PriorTransformerprior_image_encoder: CLIPVisionModelWithProjectionprior_text_encoder: CLIPTextModelWithProjectionprior_tokenizer: CLIPTokenizerprior_scheduler: UnCLIPSchedulerprior_image_processor: CLIPImageProcessorPil )

パラメータ

  • scheduler (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 と組み合わせて画像埋め込みを生成するために使用するスケジューラです。
  • prior_image_processor (CLIPImageProcessor) — clip の画像を前処理するために使用する image_processor。

Kandinsky を用いた画像から画像への生成のための統合 Pipeline

このモデルは DiffusionPipeline を継承しています。すべてのパイプラインに対してライブラリが実装している汎用的なメソッド(ダウンロードや保存、特定のデバイスでの実行など)については、スーパークラスのドキュメントを参照してください。

__call__

< >

( prompt: str | list[str]image: typing.Union[torch.Tensor, list[torch.Tensor], PIL.Image.Image, list[PIL.Image.Image]]negative_prompt: str | list[str] | None = Nonenum_inference_steps: int = 100guidance_scale: float = 4.0strength: float = 0.3num_images_per_prompt: int = 1height: int = 512width: int = 512prior_guidance_scale: float = 4.0prior_num_inference_steps: int = 25generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = Noneoutput_type: str | None = 'pil'callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = Nonecallback_steps: int = 1return_dict: bool = Trueprior_callback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = Noneprior_callback_on_step_end_tensor_inputs: list = ['latents']callback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = Nonecallback_on_step_end_tensor_inputs: list = ['latents'] ) → ImagePipelineOutput or tuple

パラメータ

  • prompt (str または list[str]) — 画像生成をガイドするためのプロンプト、またはプロンプトのリスト。
  • image (torch.Tensor、PIL.Image.Image、np.ndarray、list[torch.Tensor]、list[PIL.Image.Image]、または list[np.ndarray]) — プロセスの起点として使用されるImage、または画像バッチを表すテンソルです。直接ラテンツを渡す場合は、imageとして画像ラテンツを受け入れることもでき、その場合は再度エンコードされません。
  • negative_prompt (str または list[str]、省略可) — 画像生成をガイドしないプロンプトです。ガイダンスを使用していない場合は無視されます(guidance_scale が 1 未満の場合は無視されます)。
  • num_images_per_prompt (int、任意、デフォルトは 1) — プロンプトごとに生成する画像の数。
  • guidance_scale (float、任意、デフォルト値は 4.0) — Classifier-Free Diffusion Guidance で定義されているガイダンススケールです。guidance_scale は、Imagen Paper の式 2 の w として定義されています。guidance_scale > 1 を設定することでガイダンススケールが有効になります。ガイダンススケールを高くすると、prompt のテキストと密接に関連した画像の生成が促されますが、通常は画像の品質が低下する傾向があります。
  • strength (float, 任意、デフォルト値は 0.3) — 概念的には、参照となる image をどの程度変換するかを示します。0 から 1 の間の値である必要があります。image は開始点として使用され、strength が大きいほど、それに加えられるノイズ量が増えます。 デノイジングのステップ数は、最初に追加されるノイズ量に依存します。strength が 1 の場合、追加されるノイズは最大となり、デノイジングプロセスは num_inference_steps で指定された全イテレーション数分実行されます。したがって、値が 1 の場合、image は実質的に無視されます。
  • 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) — デノイジングのステップ数です。デノイジングのステップ数を増やすと、推論速度が遅くなる代わりに、通常はより高品質な画像が得られます。
  • generator (torch.Generator または list[torch.Generator]、省略可) — 生成を決定論的にするためのtorch ジェネレーター1つまたはリスト。
  • 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 関数が呼び出される頻度です。指定しない場合、callback はすべてのステップで呼び出されます。
  • return_dict (bool、省略可能、デフォルトは True) — True の場合、通常のタプルではなく ImagePipelineOutput を返します。
  • prior_callback_on_step_end (Callable, 省略可) — prior パイプラインの推論中、各デノイジングステップの終了時に呼び出される関数です。この関数は、以下の引数とともに呼び出されます: prior_callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)。
  • prior_callback_on_step_end_tensor_inputs (list、省略可) — prior_callback_on_step_end 関数へのテンソル入力の一覧です。このリストで指定されたテンソルは、callback_kwargs 引数として渡されます。先験的パイプラインクラスの ._callback_tensor_inputs 属性に列挙されている変数のみを、このリストに含めることができます。
  • 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
import torch
import requests
from io import BytesIO
from PIL import Image
import os

pipe = AutoPipelineForImage2Image.from_pretrained(
    "kandinsky-community/kandinsky-2-2-decoder", 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_model_cpu_offload

< >

( gpu_id: int | None = Nonedevice: typing.Union[torch.device, str] = None )

enable_sequential_cpu_offload とは異なり、accelerate を使用してすべてのモデルを CPU にオフロードし、パフォーマンスへの影響を最小限に抑えながらメモリ使用量を削減します。このメソッドでは、forward メソッドが呼び出されたときに、モデル全体を一度に GPU へ移動し、次のモデルが実行されるまで GPU に保持されます。enable_sequential_cpu_offload と比較するとメモリ削減効果は低くなりますが、unet の反復的な実行により、パフォーマンスは大幅に向上します。

enable_sequential_cpu_offload

< >

( gpu_id: int | None = Nonedevice: typing.Union[torch.device, str] = None )

Offloads all models to CPU using accelerate, significantly reducing memory usage. When called, unet, text_encoder, vae and safety checker have their state dicts saved to CPU and then are moved to a torch.device('meta') and loaded to GPU only when their specific submodule has itsforwardmethod called. Note that offloading happens on a submodule basis. Memory savings are higher than withenable_model_cpu_offload`, but performance is lower.

KandinskyV22ControlnetImg2ImgPipeline

class diffusers.KandinskyV22ControlnetImg2ImgPipeline

< >

( unet: UNet2DConditionModelscheduler: DDPMSchedulermovq: VQModel )

パラメータ

  • scheduler (DDIMScheduler) — unet と組み合わせて使用し、画像の潜在変数を生成するためのスケジューラ。
  • unet (UNet2DConditionModel) — 画像エンベディングのノイズ除去を行う条件付き U-Net アーキテクチャ。
  • movq (VQModel) — 潜在変数から画像を生成するための MoVQ デコーダー。

Kandinsky を使用した画像から画像への生成のための Pipeline

このモデルは DiffusionPipeline を継承しています。すべてのパイプラインに対してライブラリが実装している汎用的なメソッド(ダウンロードや保存、特定のデバイスでの実行など)については、スーパークラスのドキュメントを参照してください。

__call__

< >

( image_embeds: typing.Union[torch.Tensor, list[torch.Tensor]]image: typing.Union[torch.Tensor, list[torch.Tensor], PIL.Image.Image, list[PIL.Image.Image]]negative_image_embeds: typing.Union[torch.Tensor, list[torch.Tensor]]hint: Tensorheight: int = 512width: int = 512num_inference_steps: int = 100guidance_scale: float = 4.0strength: float = 0.3num_images_per_prompt: int = 1generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Noneoutput_type: str | None = 'pil'callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = Nonecallback_steps: int = 1return_dict: bool = True ) → ImagePipelineOutput or tuple

パラメータ

  • image_embeds (torch.Tensor または list[torch.Tensor]) — テキストプロンプトのクリップ画像埋め込みです。画像生成の条件付けに使用されます。
  • image (torch.Tensor、PIL.Image.Image、np.ndarray、list[torch.Tensor]、list[PIL.Image.Image]、または list[np.ndarray]) — プロセスの起点として使用されるImage、または画像バッチを表すテンソルです。直接ラテンツを渡す場合は、imageとして画像ラテンツを受け入れることもでき、その場合は再度エンコードされません。
  • strength (float、任意、デフォルト値は 0.8) — 概念的には、参照となるimageをどの程度変換するかを示します。0 から 1 の間の値である必要があります。image は起点として使用され、strengthが大きいほど、それに対してより多くのノイズが加えられます。デノイジングのステップ数は、最初に追加されるノイズの量に依存します。strengthが 1 の場合、加えられるノイズは最大となり、デノイジングプロセスはnum_inference_stepsで指定された全イテレーション数分実行されます。したがって、値が 1 の場合、imageは実質的に無視されます。
  • hint (torch.Tensor) — ControlNet の条件です。
  • negative_image_embeds (torch.Tensor または list[torch.Tensor]) — ネガティブなテキストプロンプトの CLIP 画像埋め込みです。画像生成の条件付けに使用されます。
  • 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 ジェネレーター1つまたはリスト。
  • 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 関数が呼び出される頻度です。指定しない場合、callback はすべてのステップで呼び出されます。
  • return_dict (bool、省略可能、デフォルトは True) — True の場合、通常のタプルではなく ImagePipelineOutput を返します。

戻り値

ImagePipelineOutput または tuple

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

例:

KandinskyV22InpaintPipeline

class diffusers.KandinskyV22InpaintPipeline

< >

( unet: UNet2DConditionModelscheduler: DDPMSchedulermovq: VQModel )

パラメータ

  • scheduler (DDIMScheduler) — unet と組み合わせて使用し、画像の潜在変数を生成するためのスケジューラ。
  • unet (UNet2DConditionModel) — 画像エンベディングのノイズ除去を行う条件付き U-Net アーキテクチャ。
  • movq (VQModel) — 潜在変数から画像を生成するための MoVQ デコーダー。

Kandinsky2.1 を用いたテキストガイド型画像インペインティングのパイプライン

このモデルは DiffusionPipeline を継承しています。すべてのパイプラインに対してライブラリが実装している汎用的なメソッド(ダウンロードや保存、特定のデバイスでの実行など)については、スーパークラスのドキュメントを参照してください。

__call__

< >

( image_embeds: typing.Union[torch.Tensor, list[torch.Tensor]]image: typing.Union[torch.Tensor, PIL.Image.Image]mask_image: typing.Union[PIL.Image.Image, torch.Tensor, numpy.ndarray]negative_image_embeds: typing.Union[torch.Tensor, list[torch.Tensor]]height: int = 512width: int = 512num_inference_steps: int = 100guidance_scale: float = 4.0num_images_per_prompt: int = 1generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: 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 = ['latents']**kwargs ) → ImagePipelineOutput or tuple

パラメータ

  • image_embeds (torch.Tensor または list[torch.Tensor]) — テキストプロンプトのクリップ画像埋め込みです。画像生成の条件付けに使用されます。
  • image (PIL.Image.Image) — インペイント対象のImage、または画像バッチを表すテンソル。つまり、画像の一部がmask_imageでマスクされ、promptに従って再描画されます。
  • mask_image (np.array) — image をマスクするための、画像バッチを表すテンソルです。マスク内の白いピクセルは再描画され、黒いピクセルは保持されます。mask_image が PIL 画像の場合、使用前に単一チャネル(輝度)に変換されます。テンソルの場合は、3 チャネルではなく 1 つの色チャネル(L)を含む必要があります。したがって、期待される形状は (B, H, W, 1) となります。
  • negative_image_embeds (torch.Tensor または list[torch.Tensor]) — ネガティブなテキストプロンプトの CLIP 画像埋め込みです。画像生成の条件付けに使用されます。
  • 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 ジェネレーター1つまたはリスト。
  • latents (torch.Tensor, 任意) — 画像生成の入力として使用する、ガウス分布からサンプリングされた事前生成されたノイズ付き潜在変数。異なるプロンプトで同じ生成を調整するために使用できます。指定されない場合、指定されたランダムgeneratorを使用してサンプリングを行い、潜在変数テンソルが生成されます。
  • output_type (str, 省略可能、デフォルトは "pil") — 画像生成の出力形式です。"pil" (PIL.Image.Image)、"np" (np.array)、または "pt" (torch.Tensor) から選択します。
  • return_dict (bool、省略可能、デフォルトは True) — True の場合、通常のタプルではなく ImagePipelineOutput を返します。
  • 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

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

例:

KandinskyV22InpaintCombinedPipeline

class diffusers.KandinskyV22InpaintCombinedPipeline

< >

( unet: UNet2DConditionModelscheduler: DDPMSchedulermovq: VQModelprior_prior: PriorTransformerprior_image_encoder: CLIPVisionModelWithProjectionprior_text_encoder: CLIPTextModelWithProjectionprior_tokenizer: CLIPTokenizerprior_scheduler: UnCLIPSchedulerprior_image_processor: CLIPImageProcessorPil )

パラメータ

  • scheduler (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 と組み合わせて画像埋め込みを生成するために使用するスケジューラです。
  • prior_image_processor (CLIPImageProcessor) — clip の画像を前処理するために使用する image_processor。

Kandinsky を用いたインペインティング生成のための統合パイプライン

このモデルは DiffusionPipeline を継承しています。すべてのパイプラインに対してライブラリが実装している汎用的なメソッド(ダウンロードや保存、特定のデバイスでの実行など)については、スーパークラスのドキュメントを参照してください。

__call__

< >

( prompt: str | list[str]image: typing.Union[torch.Tensor, list[torch.Tensor], PIL.Image.Image, list[PIL.Image.Image]]mask_image: typing.Union[torch.Tensor, list[torch.Tensor], PIL.Image.Image, list[PIL.Image.Image]]negative_prompt: str | list[str] | None = Nonenum_inference_steps: int = 100guidance_scale: float = 4.0num_images_per_prompt: int = 1height: int = 512width: int = 512prior_guidance_scale: float = 4.0prior_num_inference_steps: int = 25generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = Noneoutput_type: str | None = 'pil'return_dict: bool = Trueprior_callback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = Noneprior_callback_on_step_end_tensor_inputs: list = ['latents']callback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = Nonecallback_on_step_end_tensor_inputs: list = ['latents']**kwargs ) → ImagePipelineOutput or tuple

パラメータ

  • prompt (str または list[str]) — 画像生成をガイドするためのプロンプト、またはプロンプトのリスト。
  • image (torch.Tensor、PIL.Image.Image、np.ndarray、list[torch.Tensor]、list[PIL.Image.Image]、または list[np.ndarray]) — プロセスの起点として使用されるImage、または画像バッチを表すテンソルです。直接ラテンツを渡す場合は、imageとして画像ラテンツを受け入れることもでき、その場合は再度エンコードされません。
  • mask_image (np.array) — image をマスクするための、画像バッチを表すテンソルです。マスク内の白いピクセルは再描画され、黒いピクセルは保持されます。mask_image が PIL 画像の場合、使用前に単一チャネル(輝度)に変換されます。テンソルの場合は、3 チャネルではなく 1 つの色チャネル(L)を含む必要があります。したがって、期待される形状は (B, H, W, 1) となります。
  • negative_prompt (str または list[str]、省略可) — 画像生成をガイドしないプロンプトです。ガイダンスを使用していない場合は無視されます(guidance_scale が 1 未満の場合は無視されます)。
  • num_images_per_prompt (int、任意、デフォルトは 1) — プロンプトごとに生成する画像の数。
  • guidance_scale (float、任意、デフォルト値は 4.0) — Classifier-Free Diffusion Guidance で定義されているガイダンススケールです。guidance_scale は、Imagen Paper の式 2 の w として定義されています。guidance_scale > 1 を設定することでガイダンススケールが有効になります。ガイダンススケールを高くすると、prompt のテキストと密接に関連した画像の生成が促されますが、通常は画像の品質が低下する傾向があります。
  • 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) — デノイジングのステップ数です。デノイジングのステップ数を増やすと、推論速度が遅くなる代わりに、通常はより高品質な画像が得られます。
  • generator (torch.Generator または list[torch.Generator]、省略可) — 生成を決定論的にするためのtorch ジェネレーター1つまたはリスト。
  • latents (torch.Tensor, 任意) — 画像生成の入力として使用する、ガウス分布からサンプリングされた事前生成されたノイズ付き潜在変数。異なるプロンプトで同じ生成を調整するために使用できます。指定されない場合、指定されたランダムgeneratorを使用してサンプリングを行い、潜在変数テンソルが生成されます。
  • output_type (str, 省略可能、デフォルトは "pil") — 画像生成の出力形式です。"pil" (PIL.Image.Image)、"np" (np.array)、または "pt" (torch.Tensor) から選択します。
  • return_dict (bool、省略可能、デフォルトは True) — True の場合、通常のタプルではなく ImagePipelineOutput を返します。
  • prior_callback_on_step_end (Callable, 任意) — 推論中の各デノイジングステップの終了時に呼び出される関数です。この関数は、以下の引数とともに呼び出されます: prior_callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)。
  • prior_callback_on_step_end_tensor_inputs (list、任意) — prior_callback_on_step_end 関数へのテンソル入力の一覧です。このリストに指定されたテンソルは、callback_kwargs 引数として渡されます。パイプラインクラスの ._callback_tensor_inputs 属性に列挙されている変数のみを、このリストに含めることができます。
  • 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 AutoPipelineForInpainting
from diffusers.utils import load_image
import torch
import numpy as np

pipe = AutoPipelineForInpainting.from_pretrained(
    "kandinsky-community/kandinsky-2-2-decoder-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: int | None = Nonedevice: typing.Union[torch.device, str] = None )

Offloads all models to CPU using accelerate, significantly reducing memory usage. When called, unet, text_encoder, vae and safety checker have their state dicts saved to CPU and then are moved to a torch.device('meta') and loaded to GPU only when their specific submodule has itsforwardmethod called. Note that offloading happens on a submodule basis. Memory savings are higher than withenable_model_cpu_offload`, but performance is lower.

Update on GitHub