Buckets:

mishig's picture
|
download
raw
41.5 kB

インペインティング

Inpaintingは、画像の特定の領域を置き換えたり編集したりする処理です。これにより、欠陥やアーティファクトの除去といった画像復元、あるいは画像の領域をまったく新しいものに置き換える用途にも有用なツールとなります。Inpaintingはマスクに依存して、画像のどの領域を埋めるかを決定します。埋める対象の領域は白いピクセルで、保持する領域は黒いピクセルで表されます。白いピクセルはプロンプトによって埋められます。

🤗 Diffusers を使用してインペインティングを行う方法をご紹介します。

  1. AutoPipelineForInpainting クラスを使用して、インペインティングのチェックポイントをロードします。チェックポイントに基づいて、読み込むべき適切なパイプラインクラスが自動的に検出されます。
import torch
from diffusers import AutoPipelineForInpainting
from diffusers.utils import load_image, make_image_grid

pipeline = AutoPipelineForInpainting.from_pretrained(
    "kandinsky-community/kandinsky-2-2-decoder-inpaint", dtype=torch.float16
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()

本ガイドでは、メモリを節約し推論速度を向上させるために、enable_model_cpu_offload() と enable_xformers_memory_efficient_attention() を使用しています。PyTorch 2.0 を使用している場合、パイプラインに対して enable_xformers_memory_efficient_attention() を呼び出す必要はありません。PyTorch 2.0 のネイティブな scaled-dot product attention がすでに使用されているためです。

  1. ベース画像とマスク画像を読み込みます:
init_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint.png")
mask_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint_mask.png")
  1. インペイントに使用するプロンプトを作成し、ベース画像とマスク画像とともにパイプラインに渡します:
prompt = "a black cat with glowing eyes, cute, adorable, disney, pixar, highly detailed, 8k"
negative_prompt = "bad anatomy, deformed, ugly, disfigured"
image = pipeline(prompt=prompt, negative_prompt=negative_prompt, image=init_image, mask_image=mask_image).images[0]
make_image_grid([init_image, mask_image, image], rows=1, cols=3)
ベースイメージ



マスク画像



生成された画像

マスク画像を作成する

本ガイドでは、利便性のため、すべてのコード例にマスク画像が用意されています。ご自身の画像でインペイントを行うことも可能ですが、そのためにはマスク画像を作成する必要があります。以下の Space を使用して、簡単にマスク画像を作成できます。

ベース画像をアップロードしてインペイントの元とし、スケッチツールでマスクを描画します。完了したら、Run をクリックしてマスク画像を生成し、ダウンロードしてください。

Xet Storage Details

Size:
41.5 kB
·
Xet hash:
c948318ddc381d4b5775986290edf4079a4abe7aac4c588a485880d88b843762

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.