-
Notifications
You must be signed in to change notification settings - Fork 33.1k
[Model] Add SLANet Model Support #45532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| <!--Copyright 2026 The HuggingFace Team. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations under the License. | ||
|
|
||
| ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be | ||
| rendered properly in your Markdown viewer. | ||
|
|
||
| --> | ||
| *This model was released on 2025-03-07 and added to Hugging Face Transformers on 2026-04-22.* | ||
|
|
||
| # SLANet | ||
|
|
||
| <div class="flex flex-wrap space-x-1"> | ||
| <img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white"> | ||
| </div> | ||
|
|
||
| ## Overview | ||
|
|
||
| **SLANet** and **SLANet_plus** are part of a series of dedicated lightweight models for table structure recognition, focusing on accurately recognizing table structures in documents and natural scenes. For more details about the SLANet series model, please refer to the [official documentation](https://www.paddleocr.ai/latest/en/version3.x/module_usage/table_structure_recognition.html). | ||
|
|
||
| ## Model Architecture | ||
|
|
||
| SLANet is a table structure recognition model developed by Baidu PaddlePaddle Vision Team. The model significantly improves the accuracy and inference speed of table structure recognition by adopting a CPU-friendly lightweight backbone network PP-LCNet, a high-low-level feature fusion module CSP-PAN, and a feature decoding module SLA Head that aligns structural and positional information. | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Single input inference | ||
|
|
||
| The example below demonstrates how to detect text with SLANet using the [`AutoModel`]. | ||
|
|
||
| <hfoptions id="usage"> | ||
| <hfoption id="AutoModel"> | ||
|
|
||
| ```py | ||
| from io import BytesIO | ||
|
|
||
| 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']) | ||
| ``` | ||
|
|
||
| </hfoption> | ||
| </hfoptions> | ||
|
|
||
| ## SLANetConfig | ||
|
|
||
| [[autodoc]] SLANetConfig | ||
|
|
||
| ## SLANetForTableRecognition | ||
|
|
||
| [[autodoc]] SLANetForTableRecognition | ||
|
|
||
| ## SLANetBackbone | ||
|
|
||
| [[autodoc]] SLANetBackbone | ||
|
|
||
| ## SLANetSLAHead | ||
|
|
||
| [[autodoc]] SLANetSLAHead | ||
|
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to add the slanext image processor to the auto mappings in Seems like the auto mappings didnt pick it up
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Copyright 2026 The HuggingFace Team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from ...utils import _LazyModule | ||
| from ...utils.import_utils import define_import_structure | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from .configuration_slanet import * | ||
| from .modeling_slanet import * | ||
| else: | ||
| import sys | ||
|
|
||
| _file = globals()["__file__"] | ||
| sys.modules[__name__] = _LazyModule(__name__, _file, define_import_structure(_file), module_spec=__spec__) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 | ||
| # This file was automatically generated from src/transformers/models/slanet/modular_slanet.py. | ||
| # Do NOT edit this file manually as any edits will be overwritten by the generation of | ||
| # the file from the modular. If any change should be done, please apply the change to the | ||
| # modular_slanet.py file directly. One of our CI enforces this. | ||
| # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 | ||
| # Copyright 2026 The PaddlePaddle Team and The HuggingFace Inc. team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| from huggingface_hub.dataclasses import strict | ||
|
|
||
| from ...backbone_utils import consolidate_backbone_kwargs_to_config | ||
| from ...configuration_utils import PreTrainedConfig | ||
| from ...utils import auto_docstring | ||
| from ..auto import AutoConfig | ||
|
|
||
|
|
||
| @auto_docstring(checkpoint="PaddlePaddle/SLANet_plus_safetensors") | ||
| @strict | ||
| class SLANetConfig(PreTrainedConfig): | ||
| r""" | ||
| post_conv_out_channels (`int`, *optional*, defaults to 96): | ||
| Number of output channels for the post-encoder convolution layer. | ||
| out_channels (`int`, *optional*, defaults to 50): | ||
| Vocabulary size for the table structure token prediction head, i.e., the number of distinct structure | ||
| tokens the model can predict. | ||
| hidden_size (`int`, *optional*, defaults to 256): | ||
| Dimensionality of the hidden states in the attention GRU cell and the structure/location prediction heads. | ||
| max_text_length (`int`, *optional*, defaults to 500): | ||
| Maximum number of autoregressive decoding steps (tokens) for the structure and location decoder. | ||
| csp_kernel_size (`int`, *optional*, defaults to 5): | ||
| The kernel size of the Cross Stage Partial (CSP) layer. | ||
| csp_num_blocks (`int`, *optional*, defaults to 1): | ||
| Number of blocks within the Cross Stage Partial (CSP) layer. | ||
| """ | ||
|
|
||
| model_type = "slanet" | ||
|
|
||
| sub_configs = {"backbone_config": AutoConfig} | ||
| post_conv_out_channels: int = 96 | ||
| out_channels: int = 50 | ||
| hidden_size: int = 256 | ||
| max_text_length: int = 500 | ||
| backbone_config: dict | PreTrainedConfig | None = None | ||
|
|
||
| hidden_act: str = "hardswish" | ||
| csp_kernel_size: int = 5 | ||
| csp_num_blocks: int = 1 | ||
|
|
||
| def __post_init__(self, **kwargs): | ||
| self.backbone_config, kwargs = consolidate_backbone_kwargs_to_config( | ||
| backbone_config=self.backbone_config, | ||
| default_config_type="pp_lcnet", | ||
| default_config_kwargs={ | ||
| "scale": 1, | ||
| "out_features": ["stage2", "stage3", "stage4", "stage5"], | ||
| "out_indices": [2, 3, 4, 5], | ||
| "divisor": 16, | ||
| }, | ||
| **kwargs, | ||
| ) | ||
| super().__post_init__(**kwargs) | ||
|
|
||
|
|
||
| __all__ = ["SLANetConfig"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat, already used the auto mappings :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done