Diffusers documentation

Ovis-Image

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.43.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Ovis-Image

concepts

Ovis-Image 是一个 7B 的文本到图像模型,专为高质量文本渲染进行了优化,旨在严格的计算约束下高效运行。

来自阿里巴巴集团的 Ovis-Image Technical Report,作者为 Guo-Hua Wang、Liangfu Cao、Tianyu Cui、Minghao Fu、Xiaohao Chen、Pengxin Zhan、Jianshan Zhao、Lan Li、Bowen Fu、Jiaqi Liu、Qing-Guo Chen。

该论文的摘要如下:

我们推出了 Ovis-Image,这是一款 7B 规模的文本生成图像模型,专为高质量文本渲染而优化,旨在严格的计算约束下高效运行。Ovis-Image 基于我们此前的 Ovis-U1 框架构建,将基于扩散的视觉解码器与更强大的 Ovis 2.5 多模态主干网络相结合,并采用以文本为核心的训练流程,将大规模预训练与精心设计的后训练优化相结合。尽管架构紧凑,Ovis-Image 的文本渲染性能与 Qwen-Image 等显著更大的开源模型相当,并接近 Seedream 和 GPT4o 等闭源系统。至关重要的是,该模型可以在单张显存适中的高端 GPU 上部署,从而缩小了前沿文本渲染水平与实际部署之间的差距。我们的结果表明,将强大的多模态主干网络与精心设计的、以文本为重点的训练方案相结合,足以实现可靠的双语文本渲染,而无需依赖超大或专有模型。

亮点:

  • 在紧凑的 7B 规模下实现强大的文本渲染:Ovis-Image 是一个 7B 的文本到图像模型,其文本渲染质量可与 Qwen-Image 等更大的 20B 级系统相媲美,并在以文本为主的场景中能与 GPT4o 等领先的闭源模型竞争,同时其规模足够小,可在广泛可获取的硬件上运行。
  • 在文本密集且对排版敏感的提示词上实现高保真度:该模型在处理要求语言内容与渲染字体紧密对齐的提示词时表现出色(例如海报、横幅、标志、UI 原型、信息图表),能够在多种字体、尺寸和宽高比下生成清晰可读、拼写正确且语义一致的文字,同时不损害整体视觉质量。
  • 效率与可部署性:凭借 7B 参数规模和精简的架构,Ovis-Image 可在单张显存适中的高端 GPU 上运行,支持低延迟的交互式使用,并能扩展至批量生产级服务,从而在数十亿参数模型难以落地的应用场景中,提供接近前沿水平的文本渲染能力。

此流水线由 Ovis-Image Team 贡献。原始代码库可在此处找到。

可用模型:

模型推荐的 dtype
AIDC-AI/Ovis-Image-7Btorch.bfloat16

有关更多信息,请参阅此集合。

OvisImagePipeline

class diffusers.OvisImagePipeline

< >

( scheduler: FlowMatchEulerDiscreteSchedulervae: AutoencoderKLtext_encoder: Qwen3Modeltokenizer: Qwen2Tokenizertransformer: OvisImageTransformer2DModel )

参数

  • transformer (OvisImageTransformer2DModel) — 用于对编码后的图像潜变量进行去噪的条件 Transformer (MMDiT) 架构。
  • scheduler (FlowMatchEulerDiscreteScheduler) — 用于与 transformer 配合,对编码后的图像潜变量进行去噪的调度器。
  • vae (AutoencoderKL) — 变分自编码器(VAE)模型,用于将图像编码为潜在表示,以及从潜在表示解码回图像。
  • text_encoder (Qwen3Model) — Qwen3Model 类的文本编码器。
  • tokenizer(Qwen2TokenizerFast)— Qwen2TokenizerFast 类的分词器。

用于文本到图像生成的 Ovis-Image 流水线。

参考:https://github.com/AIDC-AI/Ovis-Image

__call__

< >

( prompt: str | list[str] = Nonenegative_prompt: str | list[str] = ''guidance_scale: float = 5.0height: int | None = Nonewidth: int | None = Nonenum_inference_steps: int = 50sigmas: list[float] | None = Nonenum_images_per_prompt: int | None = 1generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.FloatTensor] = Noneprompt_embeds: typing.Optional[torch.FloatTensor] = Nonenegative_prompt_embeds: typing.Optional[torch.FloatTensor] = Noneoutput_type: str | None = 'pil'return_dict: bool = Truejoint_attention_kwargs: dict[str, typing.Any] | None = Nonecallback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = Nonecallback_on_step_end_tensor_inputs: list[str] = ['latents']max_sequence_length: int = 256 ) → OvisImagePipelineOutput or tuple

参数

  • prompt (str 或 list[str],可选) — 用于引导图像生成的提示词或提示词列表。如果未定义,则必须改为传入 prompt_embeds。
  • negative_prompt (str 或 list[str],可选) — 用于不引导图像生成的提示词。如果未定义,则必须传入 negative_prompt_embeds。当不使用引导时会被忽略(即当 guidance_scale 不大于 1 时会被忽略)。
  • guidance_scale (float,可选,默认为 1.0) — 当 guidance_scale > 1 且提供了 negative_prompt 时,启用真正的无分类器引导(guidance scale)。
  • 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) — 去噪步数。更多的去噪步数通常能生成更高质量的图像,但代价是推理速度变慢。
  • sigmas (list[float],可选) — 用于在支持 sigmas 参数的调度器的 set_timesteps 方法中,为去噪过程指定自定义的 sigmas。如果未定义,则使用传入 num_inference_steps 时的默认行为。
  • num_images_per_prompt (int,可选,默认为 1) — 每个提示词生成的图像数量。
  • generator (torch.Generator 或 list[torch.Generator],可选) — 一个或一组 torch 生成器,用于使生成过程具有确定性。
  • latents (torch.FloatTensor, 可选) — 预先生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同的提示词对同一生成过程进行微调。如果未提供,将通过使用提供的随机generator进行采样来生成一个潜变量张量。
  • prompt_embeds (torch.FloatTensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示词加权。如果未提供,文本嵌入将从 prompt 输入参数生成。
  • negative_prompt_embeds (torch.FloatTensor, 可选) — 预生成的负向文本嵌入。可用于轻松调整文本输入,例如提示词加权。如果未提供,negative_prompt_embeds 将根据 negative_prompt 输入参数生成。
  • output_type (str,可选,默认为 "pil") — 生成图像的输出格式。可选值为 PIL:PIL.Image.Image 或 np.array。
  • return_dict (bool,可选,默认为 True) — 是否返回 ~pipelines.flux.FluxPipelineOutput 而不是普通元组。
  • joint_attention_kwargs (dict, 可选) — 一个 kwargs 字典,如果指定,将作为参数传递给 diffusers.models.attention_processor 中 self.processor 下定义的 AttentionProcessor。
  • 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,默认为 512)— 与 prompt 配合使用的最大序列长度。

返回值

OvisImagePipelineOutput 或 tuple

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

调用生成 pipeline 时调用的函数。

示例:

>>> import torch
>>> from diffusers import OvisImagePipeline

>>> pipe = OvisImagePipeline.from_pretrained("AIDC-AI/Ovis-Image-7B", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")
>>> prompt = 'A creative 3D artistic render where the text "OVIS-IMAGE" is written in a bold, expressive handwritten brush style using thick, wet oil paint. The paint is a mix of vibrant rainbow colors (red, blue, yellow) swirling together like toothpaste or impasto art. You can see the ridges of the brush bristles and the glossy, wet texture of the paint. The background is a clean artist's canvas. Dynamic lighting creates soft shadows behind the floating paint strokes. Colorful, expressive, tactile texture, 4k detail.'
>>> image = pipe(prompt, negative_prompt="", num_inference_steps=50, guidance_scale=5.0).images[0]
>>> image.save("ovis_image.png")

encode_prompt

< >

( prompt: str | list[str]negative_prompt: str | list[str] | None = Nonedo_classifier_free_guidance: bool = Truedevice: typing.Optional[torch.device] = Nonenum_images_per_prompt: int = 1max_sequence_length: int = 256prompt_embeds: typing.Optional[torch.FloatTensor] = Nonenegative_prompt_embeds: typing.Optional[torch.FloatTensor] = None )

参数

  • prompt (str 或 list[str],可选) — 要编码的提示
  • negative_prompt (str 或 list[str],可选) — 用于不引导图像生成的提示词。仅在 do_classifier_free_guidance 为 True 时使用。如果未定义,则使用空字符串。
  • do_classifier_free_guidance (bool,可选,默认为 True) — 是否同时编码 negative_prompt 以用于 classifier-free guidance。
  • device — (torch.device): torch 设备
  • num_images_per_prompt (int) — 每个提示词应生成的图像数量
  • max_sequence_length (int,可选,默认为 256) — 用于 prompt 的最大序列长度。
  • prompt_embeds (torch.FloatTensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示词加权。如果未提供,文本嵌入将从 prompt 输入参数生成。
  • negative_prompt_embeds (torch.FloatTensor, 可选) — 预生成的负向文本嵌入。如果未提供,则从 negative_prompt 生成。

OvisImagePipelineOutput

class diffusers.pipelines.ovis_image.OvisImagePipelineOutput

< >

( images: 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 数组表示扩散流水线生成的去噪图像。

Ovis-Image 流水线的输出类。

Update on GitHub