Diffusers documentation
VAE 画像プロセッサ
VAE 画像プロセッサ
VaeImageProcessor は、StableDiffusionPipeline が VAE エンコーディング用の画像入力を準備し、デコード後の出力を後処理するための統一 API を提供します。これには、リサイズ、正規化、PIL Image、PyTorch、NumPy 配列間の変換などの変換が含まれます。
VaeImageProcessor を使用しているすべてのパイプラインは、PIL Image、PyTorch テンソル、または NumPy 配列を画像入力として受け入れ、ユーザーが指定した output_type 引数に基づいて出力を返します。エンコードされた画像の潜在変数をパイプラインに直接渡し、output_type 引数(例えば output_type="latent")を指定することで、潜在変数を特定の出力としてパイプラインから取得できます。これにより、1 つのパイプラインで生成された潜在変数を、潜在空間から離れることなく別のパイプラインの入力として渡すことができます。また、異なるパイプライン間で PyTorch テンソルを直接渡すことで、複数のパイプラインを連携して使用することがはるかに容易になります。
VaeImageProcessor
クラス diffusers.VaeImageProcessor
< source >( do_resize: bool = Truevae_scale_factor: int = 8vae_latent_channels: int = 4resample: str = 'lanczos'reducing_gap: int | None = Nonedo_normalize: bool = Truedo_binarize: bool = Falsedo_convert_rgb: bool = Falsedo_convert_grayscale: bool = False )
パラメータ
- do_resize (
bool、省略可能、デフォルトはTrue) — 画像の (高さ、幅) 次元をvae_scale_factorの倍数に縮小するかどうかを指定します。image_processor.VaeImageProcessor.preprocess() メソッドからheightおよびwidth引数を受け取ることができます。 - vae_scale_factor (
int、任意、デフォルトは8) — VAEのスケール係数。do_resizeがTrueの場合、画像はこの係数の倍数に自動的にリサイズされます。 - resample (
str、任意、デフォルトはlanczos) — 画像のリサイズ時に使用するリサンプリングフィルタ。 - do_normalize (
bool、省略可能、デフォルトはTrue) — 画像を[-1,1]に正規化するかどうかを指定します。 - do_binarize (
bool、省略可能、デフォルトはFalse) — 画像を0/1にバイナリ化するかどうかを指定します。 - do_convert_rgb (
bool、省略可能、デフォルトはFalse) — 画像をRGB形式に変換するかどうかを指定します。 - do_convert_grayscale (
bool、省略可能、デフォルトはFalse) — 画像をグレースケール形式に変換するかどうかを指定します。
VAE用の画像プロセッサ。
apply_overlay
< source >( mask: Imageinit_image: Imageimage: Imagecrop_coords: tuple[int, int, int, int] | None = None ) → PIL.Image.Image
…
binarize
< source >( image: Image ) → PIL.Image.Image
マスクを作成します。
ぼかし
< source >( image: Imageblur_factor: int = 4 ) → PIL.Image.Image
画像にガウスぼかしを適用します。
convert_to_grayscale
< source >( image: Image ) → PIL.Image.Image
指定されたPIL画像をグレースケールに変換します。
convert_to_rgb
< source >( image: Image ) → PIL.Image.Image
PIL画像をRGB形式に変換します。
denormalize
< source >( images: typing.Union[numpy.ndarray, torch.Tensor] ) → np.ndarray or torch.Tensor
画像配列を[0,1]の範囲にデノーマライズします。
get_crop_region
< source >( mask_image: Imagewidth: intheight: intpad = 0 ) → tuple
画像内のすべてのマスク領域を含む矩形領域を検出し、元の画像のアスペクト比に一致するように領域を拡張します。例えば、ユーザーが128x32の領域にマスクを描き、処理用の寸法が512x512の場合、領域は128x128に拡張されます。
get_default_height_width
< source >( image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor]height: int | None = Nonewidth: int | None = None ) → tuple[int, int]
パラメータ
- image (
PIL.Image.Image | np.ndarray | torch.Tensor) — 画像の入力です。PIL画像、NumPy配列、またはPyTorchテンソルを指定できます。NumPy配列の場合、形状は[batch, height, width]または[batch, height, width, channels]である必要があります。PyTorchテンソルの場合、形状は[batch, channels, height, width]である必要があります。 - height (
int | None、任意、デフォルトはNone) — 前処理された画像の高さ。Noneの場合、image入力の高さが使用されます。 - width (
int | None、任意、デフォルトはNone) — 前処理された画像の幅です。Noneの場合、image入力の幅が使用されます。
戻り値
tuple[int, int]
vae_scale_factorの最も近い整数倍にリサイズされた高さと幅のタプル。
vae_scale_factorの次の整数倍に縮小された画像の高さと幅を返します。
normalize
< source >( images: typing.Union[numpy.ndarray, torch.Tensor] ) → np.ndarray or torch.Tensor
画像配列を[-1,1]に正規化します。
numpy_to_pil
< source >( images: ndarray ) → list[PIL.Image.Image]
numpyの画像または画像のバッチをPIL画像に変換します。
numpy_to_pt
< source >( images: ndarray ) → torch.Tensor
NumPyの画像をPyTorchのテンソルに変換します。
pil_to_numpy
< source >( images: list[PIL.Image.Image] | PIL.Image.Image ) → np.ndarray
PIL画像またはPIL画像のリストをNumPy配列に変換します。
postprocess
< source >( image: Tensoroutput_type: str = 'pil'do_denormalize: list[bool] | None = None ) → PIL.Image.Image, np.ndarray or torch.Tensor
パラメータ
- image (
torch.Tensor) — 画像の入力です。形状がB x C x H x WのPyTorchテンソルである必要があります。 - output_type (
str, 任意、デフォルトはpil) — 画像の出力タイプです。pil、np、pt、latentのいずれかになります。 - do_denormalize (
list[bool]、省略可能、デフォルトはNone) — 画像を[0,1]の範囲にデノーマライズするかどうか。Noneの場合、VaeImageProcessorの設定にあるdo_normalizeの値が使用されます。
戻り値
PIL.Image.Image、np.ndarrayまたはtorch.Tensor
後処理された画像。
テンソル形式の画像出力をoutput_typeに後処理します。
preprocess
< source >( image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list[PIL.Image.Image], list[numpy.ndarray], list[torch.Tensor]]height: int | None = Nonewidth: int | None = Noneresize_mode: str = 'default'crops_coords: tuple[int, int, int, int] | None = None ) → torch.Tensor
パラメータ
- image (
PipelineImageInput) — 画像の入力です。受け付ける形式は、PIL画像、NumPy配列、PyTorchテンソルです。サポートされている形式のリストも受け付けます。 - height (
int, 任意) — 前処理済み画像の高さ。Noneの場合、get_default_height_width()を使用してデフォルトの高さを取得します。 - width (
int, 任意) — 前処理後の幅。Noneの場合、get_default_height_width() を使用してデフォルトの幅を取得します。 - resize_mode (
str, 任意、デフォルトはdefault) — リサイズモードです。defaultまたはfillのいずれかになります。defaultの場合、指定された幅と高さに収まるように画像をリサイズしますが、元の縦横比が維持されない場合があります。fillの場合、指定された幅と高さに収まるように、縦横比を維持したまま画像をリサイズし、その後、寸法内で画像を中央揃えにし、空いた部分を画像のデータで埋めます。cropの場合、指定された幅と高さに収まるように、縦横比を維持したまま画像をリサイズし、その後、寸法内で画像を中央揃えにし、余分な部分を切り取ります。なお、resize_mode のfillおよびcropは、PIL 画像のインプットのみサポートされています。 - crops_coords (
list[tuple[int, int, int, int]]、任意、デフォルトはNone) — バッチ内の各画像のクロップ座標です。Noneの場合、画像はクロップされません。
戻り値
torch.Tensor
前処理された画像。
画像の入力を前処理します。
pt_to_numpy
< source >( images: Tensor ) → np.ndarray
PyTorchテンソルをNumPy画像に変換します。
リサイズ
< source >( image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor]height: intwidth: intresize_mode: str = 'default' ) → PIL.Image.Image, np.ndarray or torch.Tensor
パラメータ
- image (
PIL.Image.Image、np.ndarrayまたはtorch.Tensor) — 画像の入力です。PIL画像、NumPy配列、またはPyTorchテンソルを指定できます。 - height (
int) — リサイズ後の高さ。 - width (
int) — リサイズ後の幅。 - resize_mode (
str, 任意、デフォルトはdefault) — 使用するリサイズモードです。defaultまたはfillのいずれかになります。defaultの場合、指定された幅と高さに収まるように画像をリサイズしますが、元の縦横比が維持されない場合があります。fillの場合、指定された幅と高さに収まるように、縦横比を維持したまま画像をリサイズし、その後、寸法内で画像を中央揃えにし、空いた部分を画像のデータで埋めます。cropの場合、指定された幅と高さに収まるように、縦横比を維持したまま画像をリサイズし、その後、寸法内で画像を中央揃えにし、余分な部分を切り取ります。なお、resize_mode のfillおよびcropは、PIL 画像の入力のみサポートされています。
戻り値
PIL.Image.Image、np.ndarrayまたはtorch.Tensor
リサイズされた画像。
画像をリサイズします。
InpaintProcessor
InpaintProcessorはmaskとimageの入力を受け取り、これらをまとめて処理します。オプションとして、padding_mask_cropを受け取り、マスクのオーバーレイを適用することもできます。
クラス diffusers.InpaintProcessor
< source >( do_resize: bool = Truevae_scale_factor: int = 8vae_latent_channels: int = 4resample: str = 'lanczos'reducing_gap: int | None = Nonedo_normalize: bool = Truedo_binarize: bool = Falsedo_convert_grayscale: bool = Falsemask_do_normalize: bool = Falsemask_do_binarize: bool = Truemask_do_convert_grayscale: bool = True )
インペインティング用の画像とマスクの画像プロセッサ。
postprocess
< source >( image: Tensoroutput_type: str = 'pil'original_image: PIL.Image.Image | None = Noneoriginal_mask: PIL.Image.Image | None = Nonecrops_coords: tuple[int, int, int, int] | None = None )
画像を後処理し、必要に応じてマスクのオーバーレイを適用します
preprocess
< source >( image: Imagemask: PIL.Image.Image | None = Noneheight: int | None = Nonewidth: int | None = Nonepadding_mask_crop: int | None = None )
画像とマスクを前処理します。
VaeImageProcessorLDM3D
VaeImageProcessorLDM3Dは、RGBおよび深度の入力を受け取り、RGBおよび深度の出力を返します。
クラス diffusers.VaeImageProcessorLDM3D
< source >( do_resize: bool = Truevae_scale_factor: int = 8resample: str = 'lanczos'do_normalize: bool = True )
パラメータ
- do_resize (
bool、省略可能、デフォルトはTrue) — 画像の (高さ、幅) 次元をvae_scale_factorの倍数に縮小するかどうかを指定します。 - vae_scale_factor (
int、任意、デフォルトは8) — VAEのスケール係数。do_resizeがTrueの場合、画像はこの係数の倍数に自動的にリサイズされます。 - resample (
str、任意、デフォルトはlanczos) — 画像のリサイズ時に使用するリサンプリングフィルタ。 - do_normalize (
bool、省略可能、デフォルトはTrue) — 画像を[-1,1]に正規化するかどうかを指定します。
VAE LDM3D用の画像プロセッサ。
depth_pil_to_numpy
< source >( images: list[PIL.Image.Image] | PIL.Image.Image ) → np.ndarray
PIL画像またはPIL画像のリストをNumPy配列に変換します。
numpy_to_depth
< source >( images: ndarray ) → list[PIL.Image.Image]
NumPyの深度画像または画像のバッチを、PIL画像のリストに変換します。
numpy_to_pil
< source >( images: ndarray ) → list[PIL.Image.Image]
NumPyの画像または画像のバッチを、PIL画像のリストに変換します。
preprocess
< source >( rgb: typing.Union[torch.Tensor, PIL.Image.Image, numpy.ndarray]depth: typing.Union[torch.Tensor, PIL.Image.Image, numpy.ndarray]height: int | None = Nonewidth: int | None = Nonetarget_res: int | None = None ) → tuple[torch.Tensor, torch.Tensor]
パラメータ
- rgb (
torch.Tensor | PIL.Image.Image | np.ndarray) — RGB 入力画像です。単一の画像またはバッチを指定できます。 - depth (
torch.Tensor | PIL.Image.Image | np.ndarray) — 深度入力画像です。単一の画像またはバッチを指定できます。 - height (
int | None、任意、デフォルトはNone) — 処理後の画像の希望する高さ。Noneの場合、入力画像の高さがデフォルトとして使用されます。 - width (
int | None、任意、デフォルトはNone) — 処理後の画像の希望する幅です。Noneの場合、入力画像の幅がデフォルトとして使用されます。 - target_res (
int | None、任意、デフォルトはNone) — 画像のリサイズ用のターゲット解像度。指定された場合、height と width を上書きします。
戻り値
tuple[torch.Tensor, torch.Tensor]
PyTorchテンソルとして処理済みのRGB画像と深度画像を含むタプル。
画像入力を前処理します。受け入れられる形式は、PIL画像、NumPy配列、またはPyTorchテンソルです。
RGB風の深度画像を深度マップに変換します。
PixArtImageProcessor
クラス diffusers.PixArtImageProcessor
< source >( do_resize: bool = Truevae_scale_factor: int = 8resample: str = 'lanczos'do_normalize: bool = Truedo_binarize: bool = Falsedo_convert_grayscale: bool = False )
パラメータ
- do_resize (
bool、省略可能、デフォルトはTrue) — 画像の (高さ、幅) 次元をvae_scale_factorの倍数に縮小するかどうかを指定します。image_processor.VaeImageProcessor.preprocess() メソッドからheightおよびwidth引数を受け取ることができます。 - vae_scale_factor (
int、任意、デフォルトは8) — VAEのスケール係数。do_resizeがTrueの場合、画像はこの係数の倍数に自動的にリサイズされます。 - resample (
str、任意、デフォルトはlanczos) — 画像のリサイズ時に使用するリサンプリングフィルタ。 - do_normalize (
bool、省略可能、デフォルトはTrue) — 画像を[-1,1]に正規化するかどうかを指定します。 - do_binarize (
bool、省略可能、デフォルトはFalse) — 画像を0/1にバイナリ化するかどうかを指定します。 - do_convert_rgb (
bool、省略可能、デフォルトはFalse) — 画像をRGB形式に変換するかどうかを指定します。 - do_convert_grayscale (
bool、省略可能、デフォルトはFalse) — 画像をグレースケール形式に変換するかどうかを指定します。
PixArtの画像リサイズおよびクロップ用の画像プロセッサ。
classify_height_width_bin
< source >( height: intwidth: intratios: dict ) → tuple[int, int]
アスペクト比に基づいて、バインされた高さおよび幅を返します。
resize_and_crop_tensor
< source >( samples: Tensornew_width: intnew_height: int ) → torch.Tensor
画像のテンソルを指定されたサイズにリサイズおよびクロップします。
IPAdapterMaskProcessor
クラス diffusers.IPAdapterMaskProcessor
< source >( do_resize: bool = Truevae_scale_factor: int = 8resample: str = 'lanczos'do_normalize: bool = Falsedo_binarize: bool = Truedo_convert_grayscale: bool = True )
パラメータ
- do_resize (
bool、省略可能、デフォルトはTrue) — 画像の (高さ、幅) 次元をvae_scale_factorの倍数に縮小するかどうかを指定します。 - vae_scale_factor (
int、任意、デフォルトは8) — VAEのスケール係数。do_resizeがTrueの場合、画像はこの係数の倍数に自動的にリサイズされます。 - resample (
str、任意、デフォルトはlanczos) — 画像のリサイズ時に使用するリサンプリングフィルタ。 - do_normalize (
bool、省略可能、デフォルトはFalse) — 画像を[-1,1]に正規化するかどうかを指定します。 - do_binarize (
bool、省略可能、デフォルトはTrue) — 画像を0/1にバイナリ化するかどうかを指定します。 - do_convert_grayscale (
bool、省略可能、デフォルトはTrue) — 画像をグレースケール形式に変換するかどうかを指定します。
IP Adapterの画像マスク用の画像プロセッサ。
ダウンサンプル
< source >( mask: Tensorbatch_size: intnum_queries: intvalue_embed_dim: int ) → torch.Tensor
指定されたマスクテンソルをダウンサンプリングし、スケーリングドットプロダクトアテンションの期待される次元に一致させます。マスクのアスペクト比が出力画像のアスペクト比と一致しない場合、警告が発行されます。