Diffusers 文档
BLIP-Diffusion
并获得增强的文档体验
开始使用
BLIP-Diffusion
BLIP-Diffusion 在 BLIP-Diffusion: Pre-trained Subject Representation for Controllable Text-to-Image Generation and Editing 中被提出。它支持零样本主题驱动生成和受控引导的零样本生成。
论文摘要如下:
主题驱动的文本到图像生成模型基于文本提示创建输入主题的新颖演绎。现有模型存在微调时间长和难以保持主题保真度的问题。为了克服这些限制,我们引入了 BLIP-Diffusion,这是一种新的主题驱动图像生成模型,支持多模态控制,可消耗主题图像和文本提示的输入。与其他主题驱动生成模型不同,BLIP-Diffusion 引入了一种新的多模态编码器,该编码器经过预训练以提供主题表示。我们首先按照 BLIP-2 预训练多模态编码器,以生成与文本对齐的视觉表示。然后,我们设计了一个主题表示学习任务,使扩散模型能够利用这种视觉表示并生成新的主题演绎。与以前的方法(如 DreamBooth)相比,我们的模型支持零样本主题驱动生成,以及针对自定义主题的高效微调,速度提升高达 20 倍。我们还证明,BLIP-Diffusion 可以灵活地与 ControlNet 和 prompt-to-prompt 等现有技术相结合,以实现新颖的主题驱动生成和编辑应用。项目页面位于 此 URL。
原始代码库可以在 salesforce/LAVIS 中找到。您可以在 hf.co/SalesForce 组织下找到官方 BLIP-Diffusion 检查点。
BlipDiffusionPipeline
和 BlipDiffusionControlNetPipeline
由 ayushtues
贡献。
请务必查看调度器指南,了解如何探索调度器速度和质量之间的权衡,并查看跨 pipelines 重用组件部分,了解如何有效地将相同的组件加载到多个 pipelines 中。
BlipDiffusionPipeline
class diffusers.BlipDiffusionPipeline
< source >( 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 的 Zero-Shot Subject Driven Generation 的 Pipeline。
此模型继承自 DiffusionPipeline。查看超类文档,了解库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等)。
__call__
< source >( 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 ) → ImagePipelineOutput 或 tuple
参数
- 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,如 Classifier-Free Diffusion Guidance 中所定义。guidance_scale
定义为 Imagen Paper 的公式 2 中的w
。 通过设置guidance_scale > 1
启用 Guidance scale。 较高的 guidance scale 鼓励生成与文本prompt
紧密相关的图像,但通常以较低的图像质量为代价。 - height (
int
, optional, defaults to 512) — 生成图像的高度。 - width (
int
, optional, defaults to 512) — 生成图像的宽度。 - num_inference_steps (
int
, optional, defaults to 50) — 去噪步骤的数量。 更多的去噪步骤通常会带来更高的图像质量,但会牺牲较慢的推理速度。 - generator (
torch.Generator
或List[torch.Generator]
, optional) — 一个或一组 torch 生成器,用于使生成具有确定性。 - neg_prompt (
str
, optional, defaults to "") — 不用于引导图像生成的提示或提示列表。 当不使用 guidance 时忽略(即,如果guidance_scale
小于1
则忽略)。 - prompt_strength (
float
, optional, defaults to 1.0) — 提示的强度。 指定提示与 prompt_reps 一起重复的次数,以放大提示。 - prompt_reps (
int
, optional, defaults to 20) — 提示与 prompt_strength 一起重复的次数,以放大提示。 - output_type (
str
, optional, defaults to"pil"
) — 生成图像的输出格式。 在以下选项之间选择:"pil"
(PIL.Image.Image
)、"np"
(np.array
) 或"pt"
(torch.Tensor
)。 - return_dict (
bool
, optional, defaults toTrue
) — 是否返回 ImagePipelineOutput 而不是普通元组。
返回值
ImagePipelineOutput 或 tuple
调用 pipeline 进行生成时调用的函数。
示例
>>> 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
< source >( 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,
optional
, defaults to 2) — 文本编码器中上下文标记的位置。
Pipeline for Canny Edge based Controlled subject-driven generation using Blip Diffusion. - 使用 Blip Diffusion 的基于 Canny 边缘控制的主题驱动生成管线。
此模型继承自 DiffusionPipeline。查看超类文档,了解库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等)。
__call__
< source >( 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 ) → ImagePipelineOutput or tuple
参数
- 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
, optional) — 预生成的噪声潜在空间,从高斯分布中采样,用作图像生成的输入。 可用于使用不同的提示调整相同的生成。如果未提供,将通过随机采样生成潜在张量。 - guidance_scale (
float
, optional, defaults to 7.5) — Guidance scale as defined in Classifier-Free Diffusion Guidance.guidance_scale
定义为 Imagen Paper 等式 2 中的w
。 通过设置guidance_scale > 1
启用引导尺度。 较高的引导尺度鼓励生成与文本prompt
紧密相关的图像,但通常以降低图像质量为代价。 - height (
int
, optional, defaults to 512) — 生成图像的高度。 - width (
int
, optional, defaults to 512) — 生成图像的宽度。 - seed (
int
, optional, defaults to 42) — 用于随机生成的种子。 - num_inference_steps (
int
, optional, defaults to 50) — 去噪步骤的数量。 更多的去噪步骤通常会带来更高质量的图像,但会牺牲较慢的推理速度。 - generator (
torch.Generator
orList[torch.Generator]
, optional) — 用于使生成具有确定性的一个或多个 torch 生成器。 - neg_prompt (
str
, optional, defaults to "") — 不用于引导图像生成的提示。 当不使用引导时忽略(即,如果 `guidance_scale` 小于 `1` 则忽略)。 - prompt_strength (
float
, optional, defaults to 1.0) — 提示的强度。 指定提示与 prompt_reps 一起重复的次数,以放大提示。 - prompt_reps (
int
, 可选, 默认为 20) — 提示重复的次数,与 prompt_strength 一起使用以增强提示效果。
返回值
ImagePipelineOutput 或 tuple
调用 pipeline 进行生成时调用的函数。
示例
>>> 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")