Transformers documentation

SLANet

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

and get access to the augmented documentation experience

to get started

该模型于 2026-04-22 贡献至 Hugging Face Transformers。

SLANet

概述

SLANet 和 SLANet_plus 是一系列专为表格结构识别设计的轻量级模型,专注于准确识别文档和自然场景中的表格结构。有关 SLANet 系列模型的更多详细信息,请参阅官方文档。

模型架构

SLANet 是由百度 PaddlePaddle 视觉团队开发的表格结构识别模型。该模型采用了适用于 CPU 的轻量级骨干网络 PP-LCNet、高低层特征融合模块 CSP-PAN 以及对齐结构与位置信息的特征解码模块 SLA Head,从而显著提升了表格结构识别的准确率和推理速度。

用法

单输入推理

下面的示例演示了如何使用 AutoModel 通过 SLANet 检测文本。

AutoModel
from io import BytesIO

from huggingface_hub.utils import httpx
from PIL import Image

from transformers import AutoImageProcessor, AutoModelForTableRecognition


model_path="PaddlePaddle/SLANet_plus_safetensors"
model = AutoModelForTableRecognition.from_pretrained(model_path, device_map="auto")
image_processor = AutoImageProcessor.from_pretrained(model_path)

image = Image.open(BytesIO(httpx.get(image_url).content))
inputs = image_processor(images=image, return_tensors="pt").to(model.device)
outputs = model(**inputs)

results = image_processor.post_process_table_recognition(outputs)

print(result['structure'])
print(result['structure_score'])

SLANetConfig

class transformers.SLANetConfig

< >

( 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 = Nonepost_conv_out_channels: int = 96out_channels: int = 50hidden_size: int = 256max_text_length: int = 500backbone_config: dict | transformers.configuration_utils.PreTrainedConfig | None = Nonehidden_act: str = 'hardswish'csp_kernel_size: int = 5csp_num_blocks: int = 1 )

参数

  • post_conv_out_channels (int,可选,默认为 96) — 编码器后卷积层的输出通道数。
  • out_channels (int,可选,默认为 50) — 表格结构 token 预测头的词表大小,即模型可预测的不同结构 token 的数量。
  • hidden_size (int,可选,默认为 256) — 注意力 GRU 单元以及结构/位置预测头中隐藏状态的维度。
  • max_text_length (int,可选,默认值为 500) — 结构和位置解码器自回归解码步骤(token)的最大数量。
  • backbone_config (Union[dict, ~configuration_utils.PreTrainedConfig],可选) — 主干模型的配置。
  • hidden_act (str,可选,默认为 hardswish) — 解码器中的非线性激活函数(函数或字符串)。例如,"gelu"、 "relu"、"silu" 等。
  • csp_kernel_size (int, 可选,默认为 5) — Cross Stage Partial (CSP) 层的卷积核大小。
  • csp_num_blocks (int,可选,默认为 1) — Cross Stage Partial (CSP) 层内的块数。

这是用于存储 SlanetModel 配置的类。它根据指定的参数实例化 Slanet 模型,并定义模型架构。使用默认值实例化配置将得到与 PaddlePaddle/SLANet_plus_safetensors 类似的配置。

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

SLANetForTableRecognition

class transformers.SLANetForTableRecognition

< >

( config: SLANetConfig )

参数

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

用于表格识别任务的 SLANet Table Recognition 模型。封装了核心的 SLANetPreTrainedModel,并返回与 Transformers 表格识别 API 兼容的输出。

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

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

forward

< >

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

参数

返回值

SLANetForTableRecognitionOutput 或 tuple(torch.Tensor)

SLANetForTableRecognitionOutput 或 torch.Tensor 元组(如果传入 return_dict=False 或 config.return_dict=False),其包含的元素取决于配置(SLANetConfig)和输入。

SLANetForTableRecognition 的 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)。

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

  • head_hidden_states (tuple(torch.Tensor),可选,在传入 output_hidden_states=True 或 config.output_hidden_states=True 时返回) — SLANetSLAHead 在每个预测步骤的隐藏状态,最多可包含 self.config.max_text_length 个状态(取决于提前退出)。

  • head_attentions (tuple(torch.Tensor),可选,在传入 output_attentions=True 或 config.output_attentions=True 时返回) — SLANetSLAHead 在每个预测步骤的注意力,最多可包含 self.config.max_text_length 个注意力(取决于提前退出)。

SLANetBackbone

class transformers.SLANetBackbone

< >

( config: SLANetConfig )

forward

< >

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

参数

  • hidden_states (torch.Tensor) — 该层的输入,形状为 `(batch, seq_len, embed_dim)`

返回值

BaseModelOutputWithNoAttention 或 tuple(torch.Tensor)

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

SLANetBackbone 的 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)。

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

SLANetSLAHead

class transformers.SLANetSLAHead

< >

( config: dict | None = None**kwargs )

Update on GitHub