Diffusers 文件

BLIP-Diffusion

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

BLIP-Diffusion

BLIP-Diffusion 在論文 BLIP-Diffusion: Pre-trained Subject Representation for Controllable Text-to-Image Generation and Editing 中被提出。它實現了零樣本(zero-shot)主體驅動生成和控制引導的零樣本生成。

論文摘要如下:

主體驅動的文字到影像生成模型根據文字提示建立輸入主體的全新再現。現有模型存在微調時間長和難以保持主體保真度的問題。為了克服這些限制,我們引入了 BLIP-Diffusion,這是一種新的主體驅動影像生成模型,支援多模態控制,可以同時接收主體影像和文字提示作為輸入。與其他主體驅動的生成模型不同,BLIP-Diffusion 引入了一種新的多模態編碼器,該編碼器經過預訓練以提供主體表徵。我們首先遵循 BLIP-2 的方法預訓練多模態編碼器,以生成與文字對齊的視覺表徵。然後,我們設計了一個主體表徵學習任務,使擴散模型能夠利用這種視覺表徵生成新的主體再現。與 DreamBooth 等先前的方法相比,我們的模型實現了零樣本主體驅動生成,並且對定製主體的微調效率提高了多達 20 倍。我們還展示了 BLIP-Diffusion 可以靈活地與 ControlNet 和 prompt-to-prompt 等現有技術結合,實現新穎的主體驅動生成和編輯應用。專案頁面位於此 URL

原始程式碼庫可以在 salesforce/LAVIS 找到。您可以在 hf.co/SalesForce 組織下找到官方的 BLIP-Diffusion checkpoints。

BlipDiffusionPipelineBlipDiffusionControlNetPipelineayushtues 貢獻。

請務必檢視 Schedulers 指南,瞭解如何在排程器速度和質量之間進行權衡,並參閱在多個 pipeline 之間複用元件部分,學習如何高效地將相同元件載入到多個 pipeline 中。

BlipDiffusionPipeline

class diffusers.BlipDiffusionPipeline

< >

( tokenizer: CLIPTokenizer text_encoder: ContextCLIPTextModel vae: AutoencoderKL unet: UNet2DConditionModel scheduler: PNDMScheduler qformer: Blip2QFormerModel image_processor: BlipImageProcessor ctx_begin_pos: int = 2 mean: typing.List[float] = None std: typing.List[float] = None )

引數

  • tokenizer (CLIPTokenizer) — 用於文字編碼器的分詞器
  • text_encoder (ContextCLIPTextModel) — 用於編碼文字提示的文字編碼器
  • vae (AutoencoderKL) — VAE 模型,用於將潛在向量對映到影像
  • unet (UNet2DConditionModel) — 用於對影像嵌入進行去噪的條件 U-Net 架構。
  • scheduler (PNDMScheduler) — 與 unet 結合使用的排程器,用於生成影像潛在向量。
  • qformer (Blip2QFormerModel) — QFormer 模型,用於從文字和影像中獲取多模態嵌入。
  • image_processor (BlipImageProcessor) — 用於預處理和後處理影像的影像處理器。
  • ctx_begin_pos (int, optional, defaults to 2) — 文字編碼器中上下文 token 的位置。

用於零樣本主體驅動生成的 Blip Diffusion Pipeline。

該模型繼承自 DiffusionPipeline。請檢視超類文件,瞭解該庫為所有 pipeline 實現的通用方法(例如下載或儲存、在特定裝置上執行等)。

__call__

< >

( prompt: typing.List[str] reference_image: Image source_subject_category: typing.List[str] target_subject_category: typing.List[str] latents: typing.Optional[torch.Tensor] = None guidance_scale: float = 7.5 height: int = 512 width: int = 512 num_inference_steps: int = 50 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None neg_prompt: typing.Optional[str] = '' prompt_strength: float = 1.0 prompt_reps: int = 20 output_type: typing.Optional[str] = 'pil' return_dict: bool = True ) ImagePipelineOutputtuple

引數

  • prompt (List[str]) — 用於指導影像生成的提示或提示列表。
  • reference_image (PIL.Image.Image) — 用於條件生成的參考影像。
  • source_subject_category (List[str]) — 源主體類別。
  • target_subject_category (List[str]) — 目標主體類別。
  • latents (torch.Tensor, optional) — 預生成的噪聲潛在向量,從高斯分佈中取樣,用作影像生成的輸入。可用於使用不同提示微調相同的生成過程。如果未提供,將透過隨機取樣生成一個潛在向量張量。
  • guidance_scale (float, optional, defaults to 7.5) — 在 無分類器擴散引導 中定義的引導比例。guidance_scale 定義為 Imagen 論文 中公式 2 的 w。透過設定 guidance_scale > 1 啟用引導比例。更高的引導比例鼓勵生成與文字 prompt 緊密相關的影像,但通常會犧牲影像質量。
  • height (int, optional, defaults to 512) — 生成影像的高度。
  • width (int, optional, defaults to 512) — 生成影像的寬度。
  • num_inference_steps (int, optional, defaults to 50) — 去噪步數。更多的去噪步數通常會帶來更高質量的影像,但會犧牲推理速度。
  • generator (torch.Generator or List[torch.Generator], optional) — 一個或多個 torch generator(s),用於使生成過程具有確定性。
  • neg_prompt (str, 可選, 預設為 "") — 用於不指導影像生成的提示。當不使用指導時(即,如果 guidance_scale 小於 1)將被忽略。
  • prompt_strength (float, 可選, 預設為 1.0) — 提示的強度。與 prompt_reps 一起指定提示重複的次數以放大提示。
  • prompt_reps (int, 可選, 預設為 20) — 與 prompt_strength 一起指定提示重複的次數以放大提示。
  • output_type (str, 可選, 預設為 "pil") — 生成影像的輸出格式。可選擇:"pil" (PIL.Image.Image),"np" (np.array) 或 "pt" (torch.Tensor)。
  • return_dict (bool, 可選, 預設為 True) — 是否返回 ImagePipelineOutput 而不是一個普通元組。

返回

ImagePipelineOutputtuple

呼叫管道進行生成時呼叫的函式。

示例

>>> from diffusers.pipelines import BlipDiffusionPipeline
>>> from diffusers.utils import load_image
>>> import torch

>>> blip_diffusion_pipe = BlipDiffusionPipeline.from_pretrained(
...     "Salesforce/blipdiffusion", torch_dtype=torch.float16
... ).to("cuda")


>>> cond_subject = "dog"
>>> tgt_subject = "dog"
>>> text_prompt_input = "swimming underwater"

>>> cond_image = load_image(
...     "https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/dog.jpg"
... )
>>> guidance_scale = 7.5
>>> num_inference_steps = 25
>>> negative_prompt = "over-exposure, under-exposure, saturated, duplicate, out of frame, lowres, cropped, worst quality, low quality, jpeg artifacts, morbid, mutilated, out of frame, ugly, bad anatomy, bad proportions, deformed, blurry, duplicate"


>>> output = blip_diffusion_pipe(
...     text_prompt_input,
...     cond_image,
...     cond_subject,
...     tgt_subject,
...     guidance_scale=guidance_scale,
...     num_inference_steps=num_inference_steps,
...     neg_prompt=negative_prompt,
...     height=512,
...     width=512,
... ).images
>>> output[0].save("image.png")

BlipDiffusionControlNetPipeline

class diffusers.BlipDiffusionControlNetPipeline

< >

( tokenizer: CLIPTokenizer text_encoder: ContextCLIPTextModel vae: AutoencoderKL unet: UNet2DConditionModel scheduler: PNDMScheduler qformer: Blip2QFormerModel controlnet: ControlNetModel image_processor: BlipImageProcessor ctx_begin_pos: int = 2 mean: typing.List[float] = None std: typing.List[float] = None )

引數

  • tokenizer (CLIPTokenizer) — 文字編碼器的分詞器
  • text_encoder (ContextCLIPTextModel) — 用於編碼文字提示的文字編碼器
  • vae (AutoencoderKL) — 用於將潛在表示對映到影像的 VAE 模型
  • unet (UNet2DConditionModel) — 用於對影像嵌入進行去噪的條件 U-Net 架構。
  • scheduler (PNDMScheduler) — 與 unet 結合使用以生成影像潛在表示的排程器。
  • qformer (Blip2QFormerModel) — 從文字和影像中獲取多模態嵌入的 QFormer 模型。
  • controlnet (ControlNetModel) — 用於獲取條件影像嵌入的 ControlNet 模型。
  • image_processor (BlipImageProcessor) — 用於對影像進行預處理和後處理的影像處理器。
  • ctx_begin_pos (int, 可選, 預設為 2) — 文字編碼器中上下文標記的位置。

用於基於 Canny 邊緣的受控主題驅動生成的 Blip Diffusion 流水線。

該模型繼承自 DiffusionPipeline。請檢視超類文件,瞭解該庫為所有 pipeline 實現的通用方法(例如下載或儲存、在特定裝置上執行等)。

__call__

< >

( prompt: typing.List[str] reference_image: Image condtioning_image: Image source_subject_category: typing.List[str] target_subject_category: typing.List[str] latents: typing.Optional[torch.Tensor] = None guidance_scale: float = 7.5 height: int = 512 width: int = 512 num_inference_steps: int = 50 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None neg_prompt: typing.Optional[str] = '' prompt_strength: float = 1.0 prompt_reps: int = 20 output_type: typing.Optional[str] = 'pil' return_dict: bool = True ) ImagePipelineOutputtuple

引數

  • prompt (List[str]) — 指導影像生成的提示。
  • reference_image (PIL.Image.Image) — 用於條件生成的參考影像。
  • condtioning_image (PIL.Image.Image) — 用於條件生成的 canny 邊緣影像。
  • source_subject_category (List[str]) — 源主題類別。
  • target_subject_category (List[str]) — 目標主題類別。
  • latents (torch.Tensor, 可選) — 預生成的噪聲潛在表示,從高斯分佈中取樣,用作影像生成的輸入。可用於使用不同提示微調相同的生成。如果未提供,將透過隨機取樣生成一個潛在張量。
  • guidance_scale (float, 可選, 預設為 7.5) — 指導尺度,定義於無分類器擴散指導guidance_scaleImagen 論文 的公式 2 中定義為 w。透過設定 guidance_scale > 1 啟用指導尺度。較高的指導尺度鼓勵生成與文字 prompt 緊密相關的影像,但通常會犧牲影像質量。
  • height (int, 可選, 預設為 512) — 生成影像的高度。
  • width (int, 可選, 預設為 512) — 生成影像的寬度。
  • seed (int, 可選, 預設為 42) — 用於隨機生成的種子。
  • num_inference_steps (int, 可選, 預設為 50) — 去噪步數。更多的去噪步數通常會帶來更高質量的影像,但會犧牲推理速度。
  • generator (torch.GeneratorList[torch.Generator], 可選) — 一個或一個列表的 torch generator(s) 以使生成具有確定性。
  • neg_prompt (str, 可選, 預設為 "") — 用於不指導影像生成的提示。當不使用指導時(即,如果 guidance_scale 小於 1)將被忽略。
  • prompt_strength (float, 可選, 預設為 1.0) — 提示的強度。與 prompt_reps 一起指定提示重複的次數以放大提示。
  • prompt_reps (int, 可選, 預設為 20) — 與 prompt_strength 一起指定提示重複的次數以放大提示。

返回

ImagePipelineOutputtuple

呼叫管道進行生成時呼叫的函式。

示例

>>> from diffusers.pipelines import BlipDiffusionControlNetPipeline
>>> from diffusers.utils import load_image
>>> from controlnet_aux import CannyDetector
>>> import torch

>>> blip_diffusion_pipe = BlipDiffusionControlNetPipeline.from_pretrained(
...     "Salesforce/blipdiffusion-controlnet", torch_dtype=torch.float16
... ).to("cuda")

>>> style_subject = "flower"
>>> tgt_subject = "teapot"
>>> text_prompt = "on a marble table"

>>> cldm_cond_image = load_image(
...     "https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/kettle.jpg"
... ).resize((512, 512))
>>> canny = CannyDetector()
>>> cldm_cond_image = canny(cldm_cond_image, 30, 70, output_type="pil")
>>> style_image = load_image(
...     "https://huggingface.co/datasets/ayushtues/blipdiffusion_images/resolve/main/flower.jpg"
... )
>>> guidance_scale = 7.5
>>> num_inference_steps = 50
>>> negative_prompt = "over-exposure, under-exposure, saturated, duplicate, out of frame, lowres, cropped, worst quality, low quality, jpeg artifacts, morbid, mutilated, out of frame, ugly, bad anatomy, bad proportions, deformed, blurry, duplicate"


>>> output = blip_diffusion_pipe(
...     text_prompt,
...     style_image,
...     cldm_cond_image,
...     style_subject,
...     tgt_subject,
...     guidance_scale=guidance_scale,
...     num_inference_steps=num_inference_steps,
...     neg_prompt=negative_prompt,
...     height=512,
...     width=512,
... ).images
>>> output[0].save("image.png")
< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.