# Ovis-Image

![concepts](https://github.com/AIDC-AI/Ovis-Image/blob/main/docs/imgs/ovis_image_case.png)

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

来自阿里巴巴集团的 [Ovis-Image Technical Report](https://arxiv.org/abs/2511.22982)，作者为 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 贡献。原始代码库可在[此处](https://github.com/AIDC-AI/Ovis-Image)找到。

可用模型：

| 模型 | 推荐的 dtype |
|:-----:|:-----------------:|
| [`AIDC-AI/Ovis-Image-7B`](https://huggingface.co/AIDC-AI/Ovis-Image-7B) | `torch.bfloat16` |

有关更多信息，请参阅[此](https://huggingface.co/collections/AIDC-AI/ovis-image)集合。

## OvisImagePipeline[[diffusers.OvisImagePipeline]]

#### diffusers.OvisImagePipeline[[diffusers.OvisImagePipeline]]

```python
diffusers.OvisImagePipeline(scheduler: FlowMatchEulerDiscreteScheduler, vae: AutoencoderKL, text_encoder: Qwen3Model, tokenizer: Qwen2Tokenizer, transformer: OvisImageTransformer2DModel)
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/pipelines/ovis_image/pipeline_ovis_image.py#L129)

**参数：**

transformer ([OvisImageTransformer2DModel](/docs/diffusers/v0.43.0/zh/api/models/ovisimage_transformer2d#diffusers.OvisImageTransformer2DModel))：用于对编码后的图像潜变量进行去噪的条件 Transformer（MMDiT）架构。

scheduler ([FlowMatchEulerDiscreteScheduler](/docs/diffusers/v0.43.0/zh/api/schedulers/flow_match_euler_discrete#diffusers.FlowMatchEulerDiscreteScheduler))：用于与 `transformer` 配合，对编码后的图像潜变量进行去噪的调度器。

vae ([AutoencoderKL](/docs/diffusers/v0.43.0/zh/api/models/autoencoderkl#diffusers.AutoencoderKL))：变分自编码器（VAE）模型，用于将图像编码为潜在表示，并从潜在表示解码回图像。

text_encoder (`Qwen3Model`)：[Qwen3Model](https://huggingface.co/docs/transformers/en/model_doc/qwen3#transformers.Qwen3Model) 类的文本编码器。

tokenizer (`Qwen2TokenizerFast`)：[Qwen2TokenizerFast](https://huggingface.co/docs/transformers/en/model_doc/qwen2#transformers.Qwen2TokenizerFast) 类的分词器。

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

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

#### __调用__[[diffusers.OvisImagePipeline.__call__]]

```python
__call__(prompt: str | list[str] = None, negative_prompt: str | list[str] = '', guidance_scale: float = 5.0, height: int | None = None, width: int | None = None, num_inference_steps: int = 50, sigmas: list[float] | None = None, num_images_per_prompt: int | None = 1, generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = None, latents: typing.Optional[torch.FloatTensor] = None, prompt_embeds: typing.Optional[torch.FloatTensor] = None, negative_prompt_embeds: typing.Optional[torch.FloatTensor] = None, output_type: str | None = 'pil', return_dict: bool = True, joint_attention_kwargs: dict[str, typing.Any] | None = None, callback_on_step_end: typing.Optional[typing.Callable[[int, int], NoneType]] = None, callback_on_step_end_tensor_inputs: list[str] = ['latents'], max_sequence_length: int = 256)
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/pipelines/ovis_image/pipeline_ovis_image.py#L457)

**参数：**

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]`, *可选*) : 用于支持在 `set_timesteps` 方法中接受 `sigmas` 参数的调度器的去噪过程的自定义 sigmas。如果未定义，将使用传入 `num_inference_steps` 时的默认行为。

num_images_per_prompt (`int`，*可选*，默认为 1)：每个提示词生成的图像数量。

generator (`torch.Generator` 或 `list[torch.Generator]`，*可选*)：一个或一组 [torch 生成器](https://pytorch.org/docs/stable/generated/torch.Generator.html)，用于使生成过程具有确定性。

latents (`torch.FloatTensor`, *可选*) : 预先生成的噪声潜变量，从高斯分布中采样得到，用作图像生成的输入。可用于以不同的提示词微调同一生成过程。如果未提供，将通过使用提供的随机`generator`进行采样来生成潜变量张量。

prompt_embeds (`torch.FloatTensor`，*可选*)：预生成的文本嵌入。可用于轻松调整文本输入，*例如*提示词加权。如果未提供，则将从 `prompt` 输入参数生成文本嵌入。

negative_prompt_embeds (`torch.FloatTensor`, *可选*) : 预生成的负向文本嵌入。可用于轻松调整文本输入，*例如*提示词权重。如果未提供，negative_prompt_embeds 将根据 `negative_prompt` 输入参数生成。

output_type (`str`，*可选*，默认为 `"pil"`)：生成图像的输出格式。可选 [PIL](https://pillow.readthedocs.io/en/stable/)：`PIL.Image.Image` 或 `np.array`。

return_dict (`bool`，*可选*，默认为 `True`)：是否返回 `~pipelines.flux.FluxPipelineOutput` 而不是普通元组。

joint_attention_kwargs (`dict`, *可选*) : 一个 kwargs 字典，如果指定，将作为参数传递给 [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py) 中 `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](/docs/diffusers/v0.43.0/zh/api/pipelines/ovis_image#diffusers.pipelines.ovis_image.OvisImagePipelineOutput) 或 `tuple`

如果 `return_dict` 为 True，则返回 [OvisImagePipelineOutput](/docs/diffusers/v0.43.0/zh/api/pipelines/ovis_image#diffusers.pipelines.ovis_image.OvisImagePipelineOutput)，否则返回 `tuple`。当返回元组时，第一个元素是一个包含生成图像的列表。

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

示例：
```py
>>> 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[[diffusers.OvisImagePipeline.encode_prompt]]

```python
encode_prompt(prompt: str | list[str], negative_prompt: str | list[str] | None = None, do_classifier_free_guidance: bool = True, device: typing.Optional[torch.device] = None, num_images_per_prompt: int = 1, max_sequence_length: int = 256, prompt_embeds: typing.Optional[torch.FloatTensor] = None, negative_prompt_embeds: typing.Optional[torch.FloatTensor] = None)
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/pipelines/ovis_image/pipeline_ovis_image.py#L269)

**参数：**

prompt (`str` 或 `list[str]`，*可选*)：要编码的提示

negative_prompt (`str` 或 `list[str]`，*可选*)：用于引导图像生成的负面提示词。仅在 `do_classifier_free_guidance` 为 `True` 时使用。如果未定义，则使用空字符串。

do_classifier_free_guidance (`bool`，*可选*，默认为 `True`)：是否同时为无分类器引导编码 `negative_prompt`。

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[[diffusers.pipelines.ovis_image.OvisImagePipelineOutput]]

#### diffusers.pipelines.ovis_image.OvisImagePipelineOutput[[diffusers.pipelines.ovis_image.OvisImagePipelineOutput]]

```python
diffusers.pipelines.ovis_image.OvisImagePipelineOutput(images: list[PIL.Image.Image, numpy.ndarray])
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/pipelines/ovis_image/pipeline_output.py#L24)

**参数：**

images (`list[PIL.Image.Image]` 或 `np.ndarray`)：长度为 `batch_size` 的去噪 PIL 图像列表，或形状为 `(batch_size, height, width, num_channels)` 的 numpy 数组。PIL 图像或 numpy 数组表示扩散流水线生成的去噪图像。

Ovis-Image 流水线的输出类。

