Diffusers 文档

CogView3Plus

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

CogView3Plus

CogView3:通过中继扩散实现更精细更快速的文本到图像生成,来自清华大学 & ZhipuAI,作者:Wendi Zheng, Jiayan Teng, Zhuoyi Yang, Weihan Wang, Jidong Chen, Xiaotao Gu, Yuxiao Dong, Ming Ding, Jie Tang。

论文摘要如下:

文本到图像生成系统的最新进展很大程度上是由扩散模型驱动的。然而,单阶段文本到图像扩散模型在计算效率和图像细节的精细化方面仍然面临挑战。为了解决这个问题,我们提出了 CogView3,这是一个创新的级联框架,可以增强文本到图像扩散的性能。CogView3 是第一个在文本到图像生成领域实现中继扩散的模型,它通过首先创建低分辨率图像,然后应用基于中继的超分辨率来执行任务。这种方法不仅产生了具有竞争力的文本到图像输出,而且大大降低了训练和推理成本。我们的实验结果表明,CogView3 在人类评估中优于当前最先进的开源文本到图像扩散模型 SDXL 77.0%,同时仅需约 1/2 的推理时间。CogView3 的精馏变体实现了可比的性能,同时仅使用了 SDXL 推理时间的 1/10。

请务必查看调度器指南,了解如何探索调度器速度和质量之间的权衡,并查看跨 pipelines 重用组件部分,了解如何有效地将相同的组件加载到多个 pipelines 中。

此pipeline由zRzRzRzRzRzRzR贡献。原始代码库可以在这里找到。原始权重可以在hf.co/THUDM下找到。

CogView3PlusPipeline

class diffusers.CogView3PlusPipeline

< >

( tokenizer: T5Tokenizer text_encoder: T5EncoderModel vae: AutoencoderKL transformer: CogView3PlusTransformer2DModel scheduler: typing.Union[diffusers.schedulers.scheduling_ddim_cogvideox.CogVideoXDDIMScheduler, diffusers.schedulers.scheduling_dpm_cogvideox.CogVideoXDPMScheduler] )

参数

  • vae (AutoencoderKL) — 变分自编码器 (VAE) 模型,用于将图像编码和解码为潜在表示形式。
  • text_encoder (T5EncoderModel) — 冻结的文本编码器。CogView3Plus 使用 T5;特别是 t5-v1_1-xxl 变体。
  • tokenizer (T5Tokenizer) — T5Tokenizer 类的分词器。
  • transformer (CogView3PlusTransformer2DModel) — 一个文本条件 CogView3PlusTransformer2DModel,用于对编码后的图像潜在空间进行去噪。
  • scheduler (SchedulerMixin) — 一个调度器,与 transformer 结合使用,以对编码后的图像潜在空间进行去噪。

使用 CogView3Plus 进行文本到图像生成的 Pipeline。

此模型继承自 DiffusionPipeline。查看超类文档以获取库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等)。

__call__

< >

( prompt: typing.Union[str, typing.List[str], NoneType] = None negative_prompt: typing.Union[str, typing.List[str], NoneType] = None height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 50 timesteps: typing.Optional[typing.List[int]] = None guidance_scale: float = 5.0 num_images_per_prompt: int = 1 eta: float = 0.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.FloatTensor] = None prompt_embeds: typing.Optional[torch.FloatTensor] = None negative_prompt_embeds: typing.Optional[torch.FloatTensor] = None original_size: typing.Optional[typing.Tuple[int, int]] = None crops_coords_top_left: typing.Tuple[int, int] = (0, 0) output_type: str = 'pil' return_dict: bool = True callback_on_step_end: typing.Union[typing.Callable[[int, int, typing.Dict], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] max_sequence_length: int = 224 ) CogView3PipelineOutputtuple

参数

  • prompt (strList[str], 可选) — 用于引导图像生成的 prompt 或 prompts。如果未定义,则必须传递 prompt_embeds
  • negative_prompt (strList[str], 可选) — 不用于引导图像生成的 prompt 或 prompts。如果未定义,则必须传递 negative_prompt_embeds 代替。当不使用 guidance 时忽略(即,如果 guidance_scale 小于 1 则忽略)。
  • height (int, 可选, 默认为 self.transformer.config.sample_size * self.vae_scale_factor) — 生成图像的高度像素。如果未提供,则设置为 1024。
  • width (int, 可选, 默认为 self.transformer.config.sample_size * self.vae_scale_factor) — 生成图像的宽度像素。如果未提供,则设置为 1024。
  • num_inference_steps (int, 可选, 默认为 50) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但会牺牲推理速度。
  • timesteps (List[int], 可选) — 自定义 timesteps,用于在调度器中支持 timesteps 参数的 set_timesteps 方法的去噪过程。如果未定义,则将使用传递 num_inference_steps 时的默认行为。必须按降序排列。
  • guidance_scale (float, 可选, 默认为 5.0) — Guidance scale,如 Classifier-Free Diffusion Guidance 中定义。guidance_scale 定义为 Imagen Paper 的公式 2 中的 w。通过设置 guidance_scale > 1 启用 Guidance scale。较高的 guidance scale 鼓励生成与文本 prompt 紧密相关的图像,通常以降低图像质量为代价。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个 prompt 生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个或一组 torch generator(s),用于使生成具有确定性。
  • latents (torch.FloatTensor, 可选) — 预生成的噪声 latents,从高斯分布中采样,用作图像生成的输入。可用于使用不同的 prompts 调整相同的生成。如果未提供,将使用提供的随机 generator 采样生成 latents 张量。
  • prompt_embeds (torch.FloatTensor, 可选) — 预生成的文本 embeddings。可用于轻松调整文本输入,例如 prompt 权重。如果未提供,将从 prompt 输入参数生成文本 embeddings。
  • negative_prompt_embeds (torch.FloatTensor, 可选) — 预生成的负面文本 embeddings。可用于轻松调整文本输入,例如 prompt 权重。如果未提供,将从 negative_prompt 输入参数生成 negative_prompt_embeds。
  • original_size (Tuple[int], 可选, 默认为 (1024, 1024)) — 如果 original_sizetarget_size 不同,则图像将显示为下采样或上采样。如果未指定,original_size 默认为 (height, width)。作为 SDXL 微调的一部分,如 https://huggingface.co/papers/2307.01952 的 2.2 节中所述。
  • crops_coords_top_left (Tuple[int], 可选, 默认为 (0, 0)) — crops_coords_top_left 可用于生成一个图像,该图像看起来像是从 crops_coords_top_left 位置向下“裁剪”而来。通常,将 crops_coords_top_left 设置为 (0, 0) 可以获得效果良好且居中的图像。这是 SDXL 微调的一部分,如 https://huggingface.co/papers/2307.01952 的 2.2 节中所述。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。在 PIL: PIL.Image.Imagenp.array 之间选择。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput 而不是一个普通的元组。
  • attention_kwargs (dict, 可选) — 一个 kwargs 字典,如果指定,则会传递给 AttentionProcessor,定义在 diffusers.models.attention_processorself.processor 下。
  • callback_on_step_end (Callable, 可选) — 在推理期间,每个去噪步骤结束时调用的函数。该函数被调用时带有以下参数:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)callback_kwargs 将包含由 callback_on_step_end_tensor_inputs 指定的所有张量列表。
  • callback_on_step_end_tensor_inputs (List, 可选) — callback_on_step_end 函数的张量输入列表。列表中指定的张量将作为 callback_kwargs 参数传递。您将只能包含在管道类的 ._callback_tensor_inputs 属性中列出的变量。
  • max_sequence_length (int, 默认为 224) — 编码提示中的最大序列长度。可以设置为其他值,但可能会导致较差的结果。

返回

CogView3PipelineOutputtuple

如果 return_dict 为 True,则返回 CogView3PipelineOutput,否则返回 tuple。当返回元组时,第一个元素是包含生成图像的列表。

调用管道进行生成时调用的函数。

示例

>>> import torch
>>> from diffusers import CogView3PlusPipeline

>>> pipe = CogView3PlusPipeline.from_pretrained("THUDM/CogView3-Plus-3B", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")

>>> prompt = "A photo of an astronaut riding a horse on mars"
>>> image = pipe(prompt).images[0]
>>> image.save("output.png")

encode_prompt

< >

( prompt: typing.Union[str, typing.List[str]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None do_classifier_free_guidance: bool = True num_images_per_prompt: int = 1 prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None max_sequence_length: int = 224 device: typing.Optional[torch.device] = None dtype: typing.Optional[torch.dtype] = None )

参数

  • prompt (strList[str], 可选) — 要编码的提示
  • negative_prompt (strList[str], 可选) — 不用于引导图像生成的提示。如果未定义,则必须传递 negative_prompt_embeds。当不使用引导时(即,如果 guidance_scale 小于 1),则忽略此参数。
  • do_classifier_free_guidance (bool, 可选, 默认为 True) — 是否使用无分类器引导。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示应生成的图像数量。用于放置结果嵌入的 torch 设备
  • prompt_embeds (torch.Tensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从 prompt 输入参数生成文本嵌入。
  • negative_prompt_embeds (torch.Tensor, 可选) — 预生成的负面文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从 negative_prompt 输入参数生成 negative_prompt_embeds。
  • max_sequence_length (int, 默认为 224) — 编码提示中的最大序列长度。可以设置为其他值,但可能会导致较差的结果。
  • device — (torch.device, 可选): torch 设备
  • dtype — (torch.dtype, 可选): torch dtype

将提示编码为文本编码器隐藏状态。

CogView3PipelineOutput

class diffusers.pipelines.cogview3.pipeline_output.CogView3PipelineOutput

< >

( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )

参数

  • images (List[PIL.Image.Image]np.ndarray) — 长度为 batch_size 的去噪 PIL 图像列表,或形状为 (batch_size, height, width, num_channels) 的 numpy 数组。PIL 图像或 numpy 数组表示扩散管道的去噪图像。

CogView3 管道的输出类。

< > 在 GitHub 上更新