Diffusers 文档
unCLIP
并获得增强的文档体验
开始使用
unCLIP
《使用 CLIP 潜在变量进行分层文本条件图像生成》由 Aditya Ramesh、Prafulla Dhariwal、Alex Nichol、Casey Chu 和 Mark Chen 撰写。🤗 Diffusers 中的 unCLIP 模型来源于 kakaobrain 的 karlo。
论文摘要如下:
像 CLIP 这样的对比模型已被证明能够学习图像的稳健表示,这些表示能够同时捕获语义和风格。为了利用这些表示进行图像生成,我们提出了一个两阶段模型:一个先验模型,根据文本标题生成 CLIP 图像嵌入;一个解码器,根据图像嵌入生成图像。我们发现,明确生成图像表示可以提高图像多样性,同时最大限度地减少真实感和标题相似性的损失。我们的基于图像表示的解码器还可以生成图像的变体,这些变体保留了其语义和风格,同时改变了图像表示中不存在的非必要细节。此外,CLIP 的联合嵌入空间能够以零样本方式进行语言引导的图像操作。我们使用扩散模型作为解码器,并对先验模型进行自回归和扩散模型的实验,发现后者在计算上更高效,并能生成更高质量的样本。
您可以在 lucidrains/DALLE2-pytorch 找到 lucidrains 的 DALL-E 2 复刻版。
UnCLIPPipeline
class diffusers.UnCLIPPipeline
< 源 >( prior: PriorTransformer decoder: UNet2DConditionModel text_encoder: CLIPTextModelWithProjection tokenizer: CLIPTokenizer text_proj: UnCLIPTextProjModel super_res_first: UNet2DModel super_res_last: UNet2DModel prior_scheduler: UnCLIPScheduler decoder_scheduler: UnCLIPScheduler super_res_scheduler: UnCLIPScheduler )
参数
- text_encoder (CLIPTextModelWithProjection) — 冻结的文本编码器。
- tokenizer (CLIPTokenizer) — 用于标记文本的
CLIPTokenizer
。 - prior (PriorTransformer) — 规范的 unCLIP 先验,用于从文本嵌入中近似图像嵌入。
- text_proj (
UnCLIPTextProjModel
) — 用于准备和组合嵌入的实用类,然后将其传递给解码器。 - decoder (UNet2DConditionModel) — 用于将图像嵌入反转为图像的解码器。
- super_res_first (UNet2DModel) — 超分辨率 UNet。用于超分辨率扩散过程的所有步骤,除了最后一步。
- super_res_last (UNet2DModel) — 超分辨率 UNet。用于超分辨率扩散过程的最后一步。
- prior_scheduler (
UnCLIPScheduler
) — 用于先验去噪过程的调度器(一个修改过的 DDPMScheduler)。 - decoder_scheduler (
UnCLIPScheduler
) — 用于解码器去噪过程的调度器(一个修改过的 DDPMScheduler)。 - super_res_scheduler (
UnCLIPScheduler
) — 用于超分辨率去噪过程的调度器(一个修改过的 DDPMScheduler)。
用于文本到图像生成的 unCLIP 管道。
此模型继承自 DiffusionPipeline。请查阅超类文档,了解所有管道实现的通用方法(下载、保存、在特定设备上运行等)。
__call__
< 源 >( prompt: typing.Union[str, typing.List[str], NoneType] = None num_images_per_prompt: int = 1 prior_num_inference_steps: int = 25 decoder_num_inference_steps: int = 25 super_res_num_inference_steps: int = 7 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None prior_latents: typing.Optional[torch.Tensor] = None decoder_latents: typing.Optional[torch.Tensor] = None super_res_latents: typing.Optional[torch.Tensor] = None text_model_output: typing.Union[transformers.models.clip.modeling_clip.CLIPTextModelOutput, typing.Tuple, NoneType] = None text_attention_mask: typing.Optional[torch.Tensor] = None prior_guidance_scale: float = 4.0 decoder_guidance_scale: float = 8.0 output_type: typing.Optional[str] = 'pil' return_dict: bool = True ) → ImagePipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
) — 用于引导图像生成的提示或提示列表。仅当传递text_model_output
和text_attention_mask
时,才可以不定义此参数。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示生成的图像数量。 - prior_num_inference_steps (
int
, 可选, 默认为 25) — 先验去噪的步数。更多去噪步数通常会带来更高质量的图像,但推理速度会变慢。 - decoder_num_inference_steps (
int
, 可选, 默认为 25) — 解码器去噪的步数。更多去噪步数通常会带来更高质量的图像,但推理速度会变慢。 - super_res_num_inference_steps (
int
, 可选, 默认为 7) — 超分辨率去噪的步数。更多去噪步数通常会带来更高质量的图像,但推理速度会变慢。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 用于使生成具有确定性的torch.Generator
。 - prior_latents (形状为 (批大小,嵌入维度) 的
torch.Tensor
, 可选) — 用于先验输入的预生成噪声潜在变量。 - decoder_latents (形状为 (批大小,通道,高度,宽度) 的
torch.Tensor
, 可选) — 用于解码器输入的预生成噪声潜在变量。 - super_res_latents (形状为 (批大小,通道,超分辨率高度,超分辨率宽度) 的
torch.Tensor
, 可选) — 用于解码器输入的预生成噪声潜在变量。 - prior_guidance_scale (
float
, 可选, 默认为 4.0) — 较高的引导比例值会促使模型生成与文本prompt
紧密相关的图像,但会降低图像质量。当guidance_scale > 1
时,启用引导比例。 - decoder_guidance_scale (
float
, 可选, 默认为 4.0) — 较高的引导比例值会促使模型生成与文本prompt
紧密相关的图像,但会降低图像质量。当guidance_scale > 1
时,启用引导比例。 - text_model_output (
CLIPTextModelOutput
, 可选) — 可从文本编码器派生的预定义CLIPTextModel
输出。预定义的文本输出可用于文本嵌入插值等任务。在这种情况下,请确保也传递text_attention_mask
。prompt
此时可以保留为None
。 - text_attention_mask (
torch.Tensor
, 可选) — 可从分词器派生的预定义 CLIP 文本注意力掩码。在传递text_model_output
时,文本注意力掩码是必需的。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。在PIL.Image
或np.array
之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 ImagePipelineOutput 而不是普通元组。
返回
ImagePipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 ImagePipelineOutput,否则返回一个 tuple
,其中第一个元素是生成的图像列表。
用于生成的管道的调用函数。
UnCLIPImageVariationPipeline
class diffusers.UnCLIPImageVariationPipeline
< 源 >( 解码器: UNet2DConditionModel 文本编码器: CLIPTextModelWithProjection 分词器: CLIPTokenizer 文本投影: UnCLIPTextProjModel 特征提取器: CLIPImageProcessor 图像编码器: CLIPVisionModelWithProjection 第一个超分辨率: UNet2DModel 最后一个超分辨率: UNet2DModel 解码器调度器: UnCLIPScheduler 超分辨率调度器: UnCLIPScheduler )
参数
- text_encoder (CLIPTextModelWithProjection) — 冻结的文本编码器。
- tokenizer (CLIPTokenizer) — 用于对文本进行分词的
CLIPTokenizer
。 - feature_extractor (CLIPImageProcessor) — 从生成的图像中提取特征的模型,用作
image_encoder
的输入。 - image_encoder (CLIPVisionModelWithProjection) — 冻结的 CLIP 图像编码器 (clip-vit-large-patch14)。
- text_proj (
UnCLIPTextProjModel
) — 用于准备和组合嵌入的实用程序类,然后将其传递给解码器。 - decoder (UNet2DConditionModel) — 用于将图像嵌入反转为图像的解码器。
- super_res_first (UNet2DModel) — 超分辨率 UNet。用于超分辨率扩散过程的所有步骤,除了最后一步。
- super_res_last (UNet2DModel) — 超分辨率 UNet。用于超分辨率扩散过程的最后一步。
- decoder_scheduler (
UnCLIPScheduler
) — 解码器去噪过程中使用的调度器(修改后的 DDPMScheduler)。 - super_res_scheduler (
UnCLIPScheduler
) — 超分辨率去噪过程中使用的调度器(修改后的 DDPMScheduler)。
使用 UnCLIP 从输入图像生成图像变体。
此模型继承自 DiffusionPipeline。请查阅超类文档,了解所有管道实现的通用方法(下载、保存、在特定设备上运行等)。
__call__
< 来源 >( 图像: typing.Union[PIL.Image.Image, typing.List[PIL.Image.Image], torch.Tensor, NoneType] = None 每个提示的图像数量: int = 1 解码器推理步数: int = 25 超分辨率推理步数: int = 7 生成器: typing.Optional[torch._C.Generator] = None 解码器潜在表示: typing.Optional[torch.Tensor] = None 超分辨率潜在表示: typing.Optional[torch.Tensor] = None 图像嵌入: typing.Optional[torch.Tensor] = None 解码器引导比例: float = 8.0 输出类型: typing.Optional[str] = 'pil' 返回字典: bool = True ) → ImagePipelineOutput 或 tuple
参数
- image (
PIL.Image.Image
或List[PIL.Image.Image]
或torch.Tensor
) — 用作起点的图像或表示图像批次的张量。如果提供张量,它需要与CLIPImageProcessor
配置兼容。只有在传递image_embeddings
时才能将其保留为None
。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示生成的图像数量。 - decoder_num_inference_steps (
int
, 可选, 默认为 25) — 解码器的去噪步数。更多去噪步数通常会带来更高质量的图像,但推理速度会变慢。 - super_res_num_inference_steps (
int
, 可选, 默认为 7) — 超分辨率的去噪步数。更多去噪步数通常会带来更高质量的图像,但推理速度会变慢。 - generator (
torch.Generator
, 可选) —torch.Generator
用于使生成具有确定性。 - decoder_latents (形状为 (批次大小,通道数,高度,宽度) 的
torch.Tensor
, 可选) — 预生成的噪声潜在表示,用作解码器的输入。 - super_res_latents (形状为 (批次大小,通道数,超分辨率高度,超分辨率宽度) 的
torch.Tensor
, 可选) — 预生成的噪声潜在表示,用作解码器的输入。 - decoder_guidance_scale (
float
, 可选, 默认为 4.0) — 较高的引导比例值会鼓励模型生成与文本prompt
紧密相关的图像,但图像质量会降低。当guidance_scale > 1
时,启用引导比例。 - image_embeddings (
torch.Tensor
, 可选) — 可从图像编码器派生的预定义图像嵌入。预定义图像嵌入可用于图像插值等任务。image
可以保留为None
。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。选择PIL.Image
或np.array
。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 ImagePipelineOutput 而不是普通的元组。
返回
ImagePipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 ImagePipelineOutput,否则返回一个 tuple
,其中第一个元素是生成的图像列表。
用于生成的管道的调用函数。
ImagePipelineOutput
class diffusers.ImagePipelineOutput
< 来源 >( 图像: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )
图像流水线的输出类。