Transformers documentation

PP-OCRv5_mobile_rec

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

该模型于 2026-03-19 贡献至 Hugging Face Transformers。

PP-OCRv5_mobile_rec

概述

PP-OCRv5_mobile_rec 是一款专为文本识别打造的轻量级模型,专注于在多语言文档和自然场景中对文本元素进行高效识别与理解。

模型架构

PP-OCRv5_mobile_rec 是 PP-OCRv5_rec 系列中的一员,该系列是 PaddleOCR 团队开发的最新一代文本识别模型。它旨在通过单一模型高效且准确地支持简体中文、繁体中文、英文、日文,以及手写体、竖排文本、拼音和生僻字等复杂文本场景的识别。在保持识别性能的同时,它还兼顾了推理速度和模型鲁棒性,为各种场景下的文档理解提供高效且精准的技术支持。

用法

单输入推理

以下示例演示了如何使用 AutoModel 通过 PP-OCRv5_mobile_rec 检测文本。

AutoModel
import requests
from PIL import Image

from transformers import AutoImageProcessor, AutoModelForTextRecognition


model_path="PaddlePaddle/PP-OCRv5_mobile_rec_safetensors"
model = AutoModelForTextRecognition.from_pretrained(model_path, device_map="auto")
image_processor = AutoImageProcessor.from_pretrained(model_path)

image = Image.open(requests.get("https://huggingface.co/datasets/hf-internal-testing/transformers-synthetic-assets/resolve/main/images/paddle_general_ocr_rec_001.png", stream=True).raw).convert("RGB")
inputs = image_processor(images=image, return_tensors="pt").to(model.device)
outputs = model(**inputs)

results = image_processor.post_process_text_recognition(outputs)

for result in results:
    print(result)

批量推理

以下是使用 AutoModel 和 PP-OCRv5_mobile_rec 实现此操作的方法:

AutoModel
import requests
from PIL import Image

from transformers import AutoImageProcessor, AutoModelForTextRecognition


model_path = "PaddlePaddle/PP-OCRv5_mobile_rec_safetensors"
model = AutoModelForTextRecognition.from_pretrained(model_path, device_map="auto")
image_processor = AutoImageProcessor.from_pretrained(model_path)

image = Image.open(requests.get("https://huggingface.co/datasets/hf-internal-testing/transformers-synthetic-assets/resolve/main/images/paddle_general_ocr_rec_001.png", stream=True).raw).convert("RGB")
inputs = image_processor(images=[image, image], return_tensors="pt").to(model.device)
outputs = model(**inputs)

results = image_processor.post_process_text_recognition(outputs)
for result in results:
    print(result)

PPOCRV5MobileRecForTextRecognition

class transformers.PPOCRV5MobileRecForTextRecognition

< >

( config: PPOCRV5MobileRecConfig )

参数

  • config (PPOCRV5MobileRecConfig) — 模型的配置类,包含模型的所有参数。使用配置文件进行初始化时,不会加载与模型关联的权重,仅加载配置。请查看 from_pretrained() 方法以加载模型权重。

用于文本识别任务的 PPOCRV5MobileRec 模型。

该模型继承自 PreTrainedModel。请查阅父类文档,了解该库为所有模型实现的通用方法(例如下载或保存、调整输入嵌入大小、剪枝头等)。

该模型也是 PyTorch torch.nn.Module 的子类。 请将其作为常规的 PyTorch Module 使用,有关通用用法和行为的所有事项,请参阅 PyTorch 文档。

forward

< >

( pixel_values: Tensor**kwargs: *<class 'transformers.utils.generic.TransformersKwargs'> ) → BaseModelOutputWithNoAttention or tuple(torch.Tensor)

参数

返回值

BaseModelOutputWithNoAttention 或 tuple(torch.Tensor)

BaseModelOutputWithNoAttention 或 torch.Tensor 元组(如果传入 return_dict=False 或 config.return_dict=False),包含根据配置(PPOCRV5MobileRecConfig)和输入而定的各种元素。

PPOCRV5MobileRecForTextRecognition 的 forward 方法重写了 __call__ 特殊方法。

尽管前向传播的实现需要在此函数中定义,但之后应调用 Module 实例,而不是直接调用此函数,因为前者会负责执行预处理和后处理步骤,而后者则会静默地忽略这些步骤。

  • last_hidden_state(形状为 (batch_size, num_channels, height, width) 的 torch.Tensor)— 模型最后一层输出的隐藏状态序列。

  • hidden_states (tuple(torch.Tensor),可选,当传入 output_hidden_states=True 或 config.output_hidden_states=True 时返回) — 一个 torch.Tensor 元组(如果模型具有嵌入层,则包含嵌入层输出的一个张量,以及每个层输出的一个张量),形状为 (batch_size, num_channels, height, width)。

    模型在每一层输出处的隐藏状态,以及可选的初始嵌入输出。

PPOCRV5MobileRecConfig

class transformers.PPOCRV5MobileRecConfig

< >

( transformers_version: str | None = Nonearchitectures: list[str] | None = Noneoutput_hidden_states: bool | None = Falsereturn_dict: bool | None = Truedtype: str | torch.dtype | None = Nonechunk_size_feed_forward: int = 0is_encoder_decoder: bool = Falseid2label: dict[int, str] | dict[str, str] | None = Nonelabel2id: dict[str, int] | dict[str, str] | None = Noneproblem_type: Literal['regression', 'single_label_classification', 'multi_label_classification'] | None = Nonehidden_act: str = 'silu'backbone_config: dict | transformers.configuration_utils.PreTrainedConfig | None = Nonehidden_size: int = 120mlp_ratio: float = 2.0depth: int = 2head_out_channels: int = 18385conv_kernel_size: list | None = Noneqkv_bias: bool = Truenum_attention_heads: int = 8attention_dropout: float | int = 0.0layer_norm_eps: float = 1e-06 )

参数

  • hidden_act (str,可选,默认为 silu) — 解码器中的非线性激活函数(函数或字符串)。例如,"gelu"、 "relu"、"silu" 等。
  • backbone_config (Union[dict, ~configuration_utils.PreTrainedConfig],可选) — 主干模型的配置。
  • hidden_size (int,可选,默认为 120) — 隐藏表示的维度。
  • mlp_ratio (float,可选,默认为 2.0) — MLP 隐藏层维度与嵌入维度的比值。
  • depth (int,可选,默认为 2) — 视觉编码器中 Transformer 层的数量。
  • head_out_channels (int, 可选,默认为 18385) — PPOCRV5MobileRecHead 的输出通道数,负责最终分类。
  • conv_kernel_size (list,可选) — 卷积核的大小。
  • qkv_bias (bool,可选,默认为 True) — 是否向查询、键和值添加偏置。
  • num_attention_heads (int,可选,默认为 8) — Transformer 解码器中每个注意力层的注意力头数量。
  • attention_dropout (Union[float, int],可选,默认为 0.0) — 注意力概率的 dropout 比率。
  • layer_norm_eps (float,可选,默认为 1e-06) — 层归一化层所使用的 epsilon 值。

这是用于存储 PPOCRV5MobileRecModel 配置的类。它根据指定的参数实例化 Pp Ocrv5 Mobile Rec 模型,并定义模型架构。使用默认值实例化配置将得到与 PaddlePaddle/PP-OCRv5_mobile_rec_safetensors 类似的配置。

配置对象继承自 PreTrainedConfig,可用于控制模型输出。有关更多信息,请参阅 PreTrainedConfig 的文档。

PPOCRV5MobileRecModel

class transformers.PPOCRV5MobileRecModel

< >

( config: PPOCRV5MobileRecConfig )

参数

  • config (PPOCRV5MobileRecConfig) — 模型的配置类,包含模型的所有参数。使用配置文件进行初始化时,不会加载与模型关联的权重,仅加载配置。请查看 from_pretrained() 方法以加载模型权重。

PPOCRV5MobileRec 模型,由 Backbone 和 Head 网络组成。

该模型继承自 PreTrainedModel。请查阅父类文档,了解该库为所有模型实现的通用方法(例如下载或保存、调整输入嵌入大小、剪枝头等)。

该模型也是 PyTorch torch.nn.Module 的子类。 请将其作为常规的 PyTorch Module 使用,有关通用用法和行为的所有事项,请参阅 PyTorch 文档。

forward

< >

( pixel_values: Tensor**kwargs: *<class 'transformers.utils.generic.TransformersKwargs'> ) → BaseModelOutputWithNoAttention or tuple(torch.Tensor)

参数

返回值

BaseModelOutputWithNoAttention 或 tuple(torch.Tensor)

BaseModelOutputWithNoAttention 或 torch.Tensor 元组(如果传入 return_dict=False 或 config.return_dict=False),包含根据配置(PPOCRV5MobileRecConfig)和输入而定的各种元素。

PPOCRV5MobileRecModel 的 forward 方法重写了 __call__ 特殊方法。

尽管前向传播的实现需要在此函数中定义,但之后应调用 Module 实例,而不是直接调用此函数,因为前者会负责执行预处理和后处理步骤,而后者则会静默地忽略这些步骤。

  • last_hidden_state(形状为 (batch_size, num_channels, height, width) 的 torch.Tensor)— 模型最后一层输出的隐藏状态序列。

  • hidden_states (tuple(torch.Tensor),可选,当传入 output_hidden_states=True 或 config.output_hidden_states=True 时返回) — 一个 torch.Tensor 元组(如果模型具有嵌入层,则包含嵌入层输出的一个张量,以及每个层输出的一个张量),形状为 (batch_size, num_channels, height, width)。

    模型在每一层输出处的隐藏状态,以及可选的初始嵌入输出。

PPOCRV5MobileRecModel

class transformers.PPOCRV5MobileRecModel

< >

( config: PPOCRV5MobileRecConfig )

参数

  • config (PPOCRV5MobileRecConfig) — 模型的配置类,包含模型的所有参数。使用配置文件进行初始化时,不会加载与模型关联的权重,仅加载配置。请查看 from_pretrained() 方法以加载模型权重。

PPOCRV5MobileRec 模型,由 Backbone 和 Head 网络组成。

该模型继承自 PreTrainedModel。请查阅父类文档,了解该库为所有模型实现的通用方法(例如下载或保存、调整输入嵌入大小、剪枝头等)。

该模型也是 PyTorch torch.nn.Module 的子类。 请将其作为常规的 PyTorch Module 使用,有关通用用法和行为的所有事项,请参阅 PyTorch 文档。

forward

< >

( pixel_values: Tensor**kwargs: *<class 'transformers.utils.generic.TransformersKwargs'> ) → BaseModelOutputWithNoAttention or tuple(torch.Tensor)

参数

返回值

BaseModelOutputWithNoAttention 或 tuple(torch.Tensor)

BaseModelOutputWithNoAttention 或 torch.Tensor 元组(如果传入 return_dict=False 或 config.return_dict=False),包含根据配置(PPOCRV5MobileRecConfig)和输入而定的各种元素。

PPOCRV5MobileRecModel 的 forward 方法重写了 __call__ 特殊方法。

尽管前向传播的实现需要在此函数中定义,但之后应调用 Module 实例,而不是直接调用此函数,因为前者会负责执行预处理和后处理步骤,而后者则会静默地忽略这些步骤。

  • last_hidden_state(形状为 (batch_size, num_channels, height, width) 的 torch.Tensor)— 模型最后一层输出的隐藏状态序列。

  • hidden_states (tuple(torch.Tensor),可选,当传入 output_hidden_states=True 或 config.output_hidden_states=True 时返回) — 一个 torch.Tensor 元组(如果模型具有嵌入层,则包含嵌入层输出的一个张量,以及每个层输出的一个张量),形状为 (batch_size, num_channels, height, width)。

    模型在每一层输出处的隐藏状态,以及可选的初始嵌入输出。

Update on GitHub