Diffusers 文档
BLIP-Diffusion
并获得增强的文档体验
开始使用
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。
BlipDiffusionPipeline
和 BlipDiffusionControlNetPipeline
由 ayushtues
贡献。
请务必查看 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 ) → 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
定义为 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
orList[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 而不是一个普通元组。
返回
ImagePipelineOutput 或 tuple
调用管道进行生成时调用的函数。
示例
>>> 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 ) → ImagePipelineOutput 或 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
, 可选) — 预生成的噪声潜在表示,从高斯分布中采样,用作图像生成的输入。可用于使用不同提示微调相同的生成。如果未提供,将通过随机采样生成一个潜在张量。 - guidance_scale (
float
, 可选, 默认为 7.5) — 指导尺度,定义于无分类器扩散指导。guidance_scale
在 Imagen 论文 的公式 2 中定义为w
。通过设置guidance_scale > 1
启用指导尺度。较高的指导尺度鼓励生成与文本prompt
紧密相关的图像,但通常会牺牲图像质量。 - height (
int
, 可选, 默认为 512) — 生成图像的高度。 - width (
int
, 可选, 默认为 512) — 生成图像的宽度。 - seed (
int
, 可选, 默认为 42) — 用于随机生成的种子。 - num_inference_steps (
int
, 可选, 默认为 50) — 去噪步数。更多的去噪步数通常会带来更高质量的图像,但会牺牲推理速度。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个或一个列表的 torch generator(s) 以使生成具有确定性。 - neg_prompt (
str
, 可选, 默认为 "") — 用于不指导图像生成的提示。当不使用指导时(即,如果guidance_scale
小于1
)将被忽略。 - prompt_strength (
float
, 可选, 默认为 1.0) — 提示的强度。与 prompt_reps 一起指定提示重复的次数以放大提示。 - prompt_reps (
int
, 可选, 默认为 20) — 与 prompt_strength 一起指定提示重复的次数以放大提示。
返回
ImagePipelineOutput 或 tuple
调用管道进行生成时调用的函数。
示例
>>> 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")