Diffusers 文档

OmniGen

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

OmniGen

OmniGen:统一图像生成,作者:BAAI 的 Shitao Xiao, Yueze Wang, Junjie Zhou, Huaying Yuan, Xingrun Xing, Ruiran Yan, Chaofan Li, Shuting Wang, Tiejun Huang, Zheng Liu。

论文摘要如下:

大型语言模型(LLM)的出现统一了语言生成任务,并彻底改变了人机交互。然而,在图像生成领域,能够在一个框架内处理各种任务的统一模型仍有待探索。在这项工作中,我们引入了 OmniGen,这是一种用于统一图像生成的新型扩散模型。OmniGen 具有以下特点:1) 统一性:OmniGen 不仅展示了文本到图像的生成能力,还固有地支持各种下游任务,例如图像编辑、主体驱动生成和视觉条件生成。2) 简单性:OmniGen 的架构高度简化,无需额外插件。此外,与现有扩散模型相比,它更易于使用,可以通过指令端到端地完成复杂任务,无需额外的中间步骤,极大地简化了图像生成工作流程。3) 知识迁移:得益于统一格式的学习,OmniGen 有效地在不同任务之间迁移知识,管理未见过的任务和领域,并展示了新颖的能力。我们还探讨了模型的推理能力和思维链机制的潜在应用。这项工作代表了通用图像生成模型的首次尝试,我们将在 https://github.com/VectorSpaceLab/OmniGen 发布我们的资源,以促进未来的发展。

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

此管道由 staoxiao 贡献。原始代码库可在 此处 找到。原始权重可在 hf.co/shitao 下找到。

推理

首先,加载管道

import torch
from diffusers import OmniGenPipeline

pipe = OmniGenPipeline.from_pretrained("Shitao/OmniGen-v1-diffusers", torch_dtype=torch.bfloat16)
pipe.to("cuda")

对于文本到图像,传递一个文本提示。默认情况下,OmniGen 生成 1024x1024 的图像。您可以尝试设置 heightwidth 参数来生成不同大小的图像。

prompt = "Realistic photo. A young woman sits on a sofa, holding a book and facing the camera. She wears delicate silver hoop earrings adorned with tiny, sparkling diamonds that catch the light, with her long chestnut hair cascading over her shoulders. Her eyes are focused and gentle, framed by long, dark lashes. She is dressed in a cozy cream sweater, which complements her warm, inviting smile. Behind her, there is a table with a cup of water in a sleek, minimalist blue mug. The background is a serene indoor setting with soft natural light filtering through a window, adorned with tasteful art and flowers, creating a cozy and peaceful ambiance. 4K, HD."
image = pipe(
    prompt=prompt,
    height=1024,
    width=1024,
    guidance_scale=3,
    generator=torch.Generator(device="cpu").manual_seed(111),
).images[0]
image.save("output.png")

OmniGen 支持多模态输入。当输入包含图像时,您需要在文本提示中添加占位符 <img><|image_1|></img> 来表示图像。建议启用 use_input_image_size_as_output 以使编辑后的图像与原始图像大小相同。

prompt="<img><|image_1|></img> Remove the woman's earrings. Replace the mug with a clear glass filled with sparkling iced cola."
input_images=[load_image("https://raw.githubusercontent.com/VectorSpaceLab/OmniGen/main/imgs/docs_img/t2i_woman_with_book.png")]
image = pipe(
    prompt=prompt, 
    input_images=input_images, 
    guidance_scale=2, 
    img_guidance_scale=1.6,
    use_input_image_size_as_output=True,
    generator=torch.Generator(device="cpu").manual_seed(222)).images[0]
image.save("output.png")

OmniGenPipeline

class diffusers.OmniGenPipeline

< >

( transformer: OmniGenTransformer2DModel scheduler: FlowMatchEulerDiscreteScheduler vae: AutoencoderKL tokenizer: LlamaTokenizer )

参数

  • transformer (OmniGenTransformer2DModel) — OmniGen 的自回归 Transformer 架构。
  • scheduler (FlowMatchEulerDiscreteScheduler) — 一个与 transformer 结合使用的调度器,用于对编码图像的潜在表示进行去噪。
  • vae (AutoencoderKL) — 用于将图像编码和解码为潜在表示的变分自编码器 (VAE) 模型。
  • tokenizer (LlamaTokenizer) — LlamaTokenizer 类的文本分词器。

用于多模态到图像生成的 OmniGen 管道。

参考:https://huggingface.co/papers/2409.11340

__call__

< >

( prompt: typing.Union[str, typing.List[str]] input_images: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor], typing.List[typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]]]] = None height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 50 max_input_image_size: int = 1024 timesteps: typing.List[int] = None guidance_scale: float = 2.5 img_guidance_scale: float = 1.6 use_input_image_size_as_output: bool = False num_images_per_prompt: typing.Optional[int] = 1 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] )

参数

  • prompt (strList[str], 可选) — 用于引导图像生成的提示词。如果输入包含图像,需要在提示词中添加占位符 <img><|image_i|></img> 来指示第 i 个图像的位置。
  • input_images (PipelineImageInputList[PipelineImageInput], 可选) — 输入图像列表。我们将用列表中第 i 个图像替换提示中的“<|image_i|>”。
  • height (int, 可选, 默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的高度(像素)。默认设置为 1024 以获得最佳效果。
  • width (int, 可选, 默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的宽度(像素)。默认设置为 1024 以获得最佳效果。
  • num_inference_steps (int, 可选, 默认为 50) — 去噪步数。更多的去噪步数通常会带来更高质量的图像,但会以较慢的推理速度为代价。
  • max_input_image_size (int, 可选, 默认为 1024) — 输入图像的最大尺寸,将用于将输入图像裁剪到最大尺寸。
  • timesteps (List[int], 可选) — 用于去噪过程的自定义时间步,与支持在其 set_timesteps 方法中带有 timesteps 参数的调度器一起使用。如果未定义,将使用传递 num_inference_steps 时的默认行为。必须按降序排列。
  • guidance_scale (float, 可选, 默认为 2.5) — 如 Classifier-Free Diffusion Guidance 中定义的引导比例。guidance_scale 定义为 Imagen Paper 中公式 2 的 w。通过设置 guidance_scale > 1 启用引导比例。更高的引导比例会鼓励生成与文本 prompt 紧密相关的图像,通常以牺牲图像质量为代价。
  • img_guidance_scale (float, 可选, 默认为 1.6) — 如 Instrucpix2pix 中公式 3 定义。
  • use_input_image_size_as_output (bool, 默认为 False) — 是否使用输入图像尺寸作为输出图像尺寸,可用于单图像输入,例如图像编辑任务。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示要生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个或多个 torch 生成器,用于使生成过程具有确定性。
  • latents (torch.Tensor, 可选) — 预先生成的噪声潜在表示,从高斯分布中采样,用作图像生成的输入。可用于使用不同提示调整相同的生成。如果未提供,将使用提供的随机 generator 采样生成潜在张量。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。选择 PIL: PIL.Image.Imagenp.array
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ~pipelines.flux.FluxPipelineOutput 而不是普通元组。
  • 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 属性中列出的变量。

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

示例

>>> import torch
>>> from diffusers import OmniGenPipeline

>>> pipe = OmniGenPipeline.from_pretrained("Shitao/OmniGen-v1-diffusers", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")

>>> prompt = "A cat holding a sign that says hello world"
>>> # Depending on the variant being used, the pipeline call will slightly vary.
>>> # Refer to the pipeline documentation for more details.
>>> image = pipe(prompt, num_inference_steps=50, guidance_scale=2.5).images[0]
>>> image.save("output.png")

返回:ImagePipelineOutputtuple:如果 return_dictTrue,则返回 ImagePipelineOutput,否则返回一个 tuple,其中第一个元素是生成的图像列表。

disable_vae_slicing

< >

( )

禁用切片 VAE 解码。如果之前启用了 enable_vae_slicing,此方法将返回一步计算解码。

disable_vae_tiling

< >

( )

禁用平铺 VAE 解码。如果之前启用了 enable_vae_tiling,此方法将恢复一步计算解码。

enable_vae_slicing

< >

( )

启用切片 VAE 解码。启用此选项后,VAE 会将输入张量分片,分步计算解码。这有助于节省一些内存并允许更大的批次大小。

enable_vae_tiling

< >

( )

启用平铺 VAE 解码。启用此选项后,VAE 将把输入张量分割成瓦片,分多步计算编码和解码。这对于节省大量内存和处理更大的图像非常有用。

encode_input_images

< >

( input_pixel_values: typing.List[torch.Tensor] device: typing.Optional[torch.device] = None dtype: typing.Optional[torch.dtype] = None )

参数

  • input_pixel_values — 输入图像的归一化像素值
  • device

通过 VAE 获取输入图像的连续嵌入

返回:torch.Tensor

< > 在 GitHub 上更新