Diffusers 文件
示例繪畫
並獲得增強的文件體驗
開始使用
示例繪畫
示例繪畫:基於示例的擴散模型影像編輯由 Binxin Yang、Shuyang Gu、Bo Zhang、Ting Zhang、Xuejin Chen、Xiaoyan Sun、Dong Chen、Fang Wen 撰寫。
論文摘要如下:
語言引導影像編輯最近取得了巨大成功。本文首次探討了基於示例的影像編輯,以實現更精確的控制。我們透過利用自監督訓練來解耦和重組源影像和示例影像,從而實現了這一目標。然而,樸素的方法會導致明顯的融合偽影。我們仔細分析了它,並提出了資訊瓶頸和強增強,以避免直接複製和貼上示例影像的簡單解決方案。同時,為了確保編輯過程的可控性,我們為示例影像設計了任意形狀的遮罩,並利用無分類器引導來增加與示例影像的相似性。整個框架只涉及擴散模型的一次前向傳播,無需任何迭代最佳化。我們證明了我們的方法取得了令人印象深刻的效能,並能夠在野外影像上實現高保真度的可控編輯。
原始程式碼庫可在Fantasy-Studio/Paint-by-Example找到,您可以在演示中試用。
提示
示例繪畫由官方的Fantasy-Studio/Paint-by-Example檢查點支援。該檢查點從CompVis/stable-diffusion-v1-4預熱,以根據示例影像和參考影像修復部分遮罩的影像。
PaintByExamplePipeline
類 diffusers.PaintByExamplePipeline
< 來源 >( vae: AutoencoderKL image_encoder: PaintByExampleImageEncoder unet: UNet2DConditionModel scheduler: typing.Union[diffusers.schedulers.scheduling_ddim.DDIMScheduler, diffusers.schedulers.scheduling_pndm.PNDMScheduler, diffusers.schedulers.scheduling_lms_discrete.LMSDiscreteScheduler] safety_checker: StableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor requires_safety_checker: bool = False )
__call__
< 來源 >( example_image: typing.Union[torch.Tensor, PIL.Image.Image] image: typing.Union[torch.Tensor, PIL.Image.Image] mask_image: typing.Union[torch.Tensor, PIL.Image.Image] height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 50 guidance_scale: float = 5.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_images_per_prompt: typing.Optional[int] = 1 eta: float = 0.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: int = 1 ) → StableDiffusionPipelineOutput 或 tuple
引數
- example_image (
torch.Tensor
或PIL.Image.Image
或List[PIL.Image.Image]
) — 用於引導影像生成的示例影像。 - image (
torch.Tensor
或PIL.Image.Image
或List[PIL.Image.Image]
) — 要修復的影像批次的Image
或張量(影像的某些部分被mask_image
遮罩,並根據prompt
重新繪製)。 - mask_image (
torch.Tensor
或PIL.Image.Image
或List[PIL.Image.Image]
) — 用於遮罩image
的影像批次的Image
或張量。遮罩中的白色畫素將被重新繪製,而黑色畫素將被保留。如果mask_image
是 PIL 影像,則在使用前會轉換為單通道(亮度)。如果它是張量,則應包含一個顏色通道(L)而不是 3 個,因此預期形狀為(B, H, W, 1)
。 - height (
int
, 可選, 預設為self.unet.config.sample_size * self.vae_scale_factor
) — 生成影像的高度(畫素)。 - width (
int
, 可選, 預設為self.unet.config.sample_size * self.vae_scale_factor
) — 生成影像的寬度(畫素)。 - num_inference_steps (
int
, 可選, 預設為 50) — 去噪步數。更多去噪步數通常會帶來更高質量的影像,但推理速度會變慢。 - guidance_scale (
float
, 可選, 預設為 7.5) — 較高的引導比例值會鼓勵模型生成與文字prompt
緊密相關的影像,但會降低影像質量。當guidance_scale > 1
時,啟用引導比例。 - negative_prompt (
str
或List[str]
, 可選) — 用於引導影像生成中不包含內容的提示或提示列表。如果未定義,則需要傳遞negative_prompt_embeds
。當不使用引導時(guidance_scale < 1
),此引數將被忽略。 - num_images_per_prompt (
int
, 可選, 預設為 1) — 每個提示生成的影像數量。 - eta (
float
, 可選, 預設為 0.0) — 對應於 DDIM 論文中的引數 eta (η)。僅適用於 DDIMScheduler,在其他排程器中被忽略。 - generator (
torch.Generator
或List[torch.Generator]
, 可選) — 一個torch.Generator
,用於使生成具有確定性。 - latents (
torch.Tensor
, 可選) — 從高斯分佈中取樣的預生成噪聲潛在變數,用作影像生成的輸入。可用於使用不同的提示調整相同的生成。如果未提供,則使用提供的隨機generator
進行取樣生成一個潛在變數張量。 - output_type (
str
, 可選, 預設為"pil"
) — 生成影像的輸出格式。選擇PIL.Image
或np.array
。 - return_dict (
bool
, 可選, 預設為True
) — 是否返回 StableDiffusionPipelineOutput 而不是普通元組。 - callback (
Callable
, 可選) — 在推理期間每callback_steps
步呼叫的函式。該函式呼叫時帶有以下引數:callback(step: int, timestep: int, latents: torch.Tensor)
。 - callback_steps (
int
, 可選, 預設為 1) — 呼叫callback
函式的頻率。如果未指定,則在每個步驟都呼叫回撥。
返回
StableDiffusionPipelineOutput 或 tuple
如果 return_dict
為 True
,則返回 StableDiffusionPipelineOutput,否則返回一個 tuple
,其中第一個元素是生成的影像列表,第二個元素是指示相應生成的影像是否包含“不適合工作”(nsfw)內容的 bool
列表。
用於生成的管道的呼叫函式。
示例
>>> import PIL
>>> import requests
>>> import torch
>>> from io import BytesIO
>>> from diffusers import PaintByExamplePipeline
>>> def download_image(url):
... response = requests.get(url)
... return PIL.Image.open(BytesIO(response.content)).convert("RGB")
>>> img_url = (
... "https://raw.githubusercontent.com/Fantasy-Studio/Paint-by-Example/main/examples/image/example_1.png"
... )
>>> mask_url = (
... "https://raw.githubusercontent.com/Fantasy-Studio/Paint-by-Example/main/examples/mask/example_1.png"
... )
>>> example_url = "https://raw.githubusercontent.com/Fantasy-Studio/Paint-by-Example/main/examples/reference/example_1.jpg"
>>> init_image = download_image(img_url).resize((512, 512))
>>> mask_image = download_image(mask_url).resize((512, 512))
>>> example_image = download_image(example_url).resize((512, 512))
>>> pipe = PaintByExamplePipeline.from_pretrained(
... "Fantasy-Studio/Paint-by-Example",
... torch_dtype=torch.float16,
... )
>>> pipe = pipe.to("cuda")
>>> image = pipe(image=init_image, mask_image=mask_image, example_image=example_image).images[0]
>>> image
StableDiffusionPipelineOutput
類 diffusers.pipelines.stable_diffusion.StableDiffusionPipelineOutput
< 來源 >( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] nsfw_content_detected: typing.Optional[typing.List[bool]] )
Stable Diffusion 管道的輸出類。