Transformers documentation
UVDoc
该模型于 2026-03-21 贡献至 Hugging Face Transformers。
UVDoc
概述
UVDoc 文本图像校正的主要目的是对图像进行几何变换,以纠正图像中的文档扭曲、倾斜和透视变形等问题。
用法
单输入推理
以下示例演示了如何使用 AutoImageProcessor 和 UVDocModel 通过 UVDoc 对文档图像进行校正。
import requests
from PIL import Image
from transformers import AutoImageProcessor, AutoModel
model_path = "PaddlePaddle/UVDoc_safetensors"
model = AutoModel.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_doc_test.jpg", stream=True).raw)
inputs = image_processor(images=image, return_tensors="pt").to(model.device)
outputs = model(**inputs)
result = image_processor.post_process_document_rectification(outputs.last_hidden_state, inputs["original_images"])
print(result)批量推理
以下是如何使用 UVDoc 执行批量文档校正:
import requests
from PIL import Image
from transformers import AutoImageProcessor, AutoModel
model_path = "PaddlePaddle/UVDoc_safetensors"
model = AutoModel.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_doc_test.jpg", stream=True).raw)
inputs = image_processor(images=[image, image], return_tensors="pt").to(model.device)
outputs = model(**inputs)
result = image_processor.post_process_document_rectification(outputs.last_hidden_state, inputs["original_images"])
print(result)UVDocConfig
类 transformers.UVDocConfig
< source >( 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 = Nonebackbone_config: dict | transformers.configuration_utils.PreTrainedConfig | None = Nonehidden_act: str = 'prelu'padding_mode: str = 'reflect'kernel_size: int = 5bridge_connector: list[int] | tuple[int, ...] = (128, 128)out_point_positions2D: Sequence = ((128, 32), (32, 2)) )
参数
- backbone_config (
Union[dict, ~configuration_utils.PreTrainedConfig],可选) — 主干模型的配置。 - hidden_act (
str,可选,默认为prelu) — 解码器中的非线性激活函数(函数或字符串)。例如,"gelu"、"relu"、"silu"等。 - padding_mode (
str,可选,默认为"reflect") — 卷积层的填充模式。支持的模式包括"reflect"、"constant"和"replicate"。 - kernel_size (
int,可选,默认为 5) — 骨干网络中卷积层的卷积核大小。 - bridge_connector (
list[int] | tuple[int, ...],可选,默认为(128, 128)) — 桥接连接器的配置,格式为 [in_channels, out_channels]。 - out_point_positions2D (
Sequence[list[int] | tuple[int, ...]],可选,默认为((128, 32), (32, 2))) — 输出点位置 2D 层的配置,格式为 [in_channels, out_channels]。
这是用于存储 UVDocModel 配置的类。它根据指定的参数实例化 Uvdoc 模型,并定义模型架构。使用默认值实例化配置,将得到与 PaddlePaddle/UVDoc_safetensors 类似的配置。
配置对象继承自 PreTrainedConfig,可用于控制模型输出。有关更多信息,请参阅 PreTrainedConfig 的文档。
UVDocModel
class transformers.UVDocModel
< source >( config: UVDocConfig )
参数
- config (UVDocConfig) — 模型的配置类,包含模型的所有参数。使用配置文件进行初始化时,不会加载与模型关联的权重,仅加载配置。请查看 from_pretrained() 方法以加载模型权重。
该模型以原始文档图像(像素值)作为输入,通过 UVDoc 主干网络进行处理以预测空间变换参数, 并输出校正后的文档图像张量。
该模型继承自 PreTrainedModel。请查阅父类文档,了解该库为所有模型实现的通用方法(例如下载或保存、调整输入嵌入大小、剪枝头等)。
该模型也是 PyTorch torch.nn.Module 的子类。 请将其作为常规的 PyTorch Module 使用,有关通用用法和行为的所有事项,请参阅 PyTorch 文档。
forward
< source >( pixel_values: Tensor**kwargs: *<class 'transformers.utils.generic.TransformersKwargs'> ) → BaseModelOutputWithNoAttention or tuple(torch.Tensor)
参数
- pixel_values (
torch.Tensor,形状为(batch_size, num_channels, image_size, image_size)) — 与输入图像对应的张量。可以使用 UVDocImageProcessor 获取像素值。有关详细信息,请参阅 UVDocImageProcessor.call()(processor_class使用 UVDocImageProcessor 处理图像)。
返回值
BaseModelOutputWithNoAttention 或 tuple(torch.Tensor)
BaseModelOutputWithNoAttention 或 torch.Tensor 元组(如果传入 return_dict=False 或 config.return_dict=False),包含根据配置(UVDocConfig)和输入而定的各种元素。
UVDocModel 的 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)。模型在每一层输出处的隐藏状态,以及可选的初始嵌入输出。
UVDocBackboneConfig
class transformers.UVDocBackboneConfig
< source >( 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 = None_out_features: list[str] | None = None_out_indices: list[int] | None = Noneresnet_head: Sequence = ((3, 32), (32, 32))resnet_configs: Sequence = (((32, 32, 1, False), (32, 32, 3, False), (32, 32, 3, False)), ((32, 64, 1, True), (64, 64, 3, False), (64, 64, 3, False), (64, 64, 3, False)), ((64, 128, 1, True), (128, 128, 3, False), (128, 128, 3, False), (128, 128, 3, False), (128, 128, 3, False), (128, 128, 3, False)))stage_configs: Sequence = (((128, 1),), ((128, 2),), ((128, 5),), ((128, 8), (128, 3), (128, 2)), ((128, 12), (128, 7), (128, 4)), ((128, 18), (128, 12), (128, 6)))kernel_size: int = 5 )
参数
- resnet_head (
Sequence[list[int] | tuple[int, ...]],可选,默认为((3, 32), (32, 32))) — ResNet 头部层的配置,格式为 [in_channels, out_channels]。 - resnet_configs (
Sequence[Sequence[tuple[int, int, int, bool] | list[int | bool]]],可选,默认为(((32, 32, 1, False), -- (32, 32, 3, False), (32, 32, 3, False)), ((32, 64, 1, True), (64, 64, 3, False), (64, 64, 3, False), (64, 64, 3, False)), ((64, 128, 1, True), (128, 128, 3, False), (128, 128, 3, False), (128, 128, 3, False), (128, 128, 3, False), (128, 128, 3, False)))): ResNet 各阶段的配置,格式为 [in_channels, out_channels, dilation_value, downsample]。 - stage_configs (
Sequence[Sequence[tuple[int, …] | list[int]]], 可选,默认为(((128, 1),), ((128, 2),), -- ((128, 5),), ((128, 8),(128, 3),(128, 2),), ((128, 12), (128, 7), (128, 4),), ((128, 18), (128, 12), (128, 6),),)): 桥接模块各阶段的配置,格式为 [in_channels, dilation_value]。 每个内部序列对应一个桥接块,外部序列则按桥接阶段对块进行分组。 - kernel_size (
int,可选,默认为5) — 卷积核的大小。
这是用于存储 UVDocModel 配置的类。它根据指定的参数实例化 Uvdoc 模型,并定义模型架构。使用默认值实例化配置,将得到与 PaddlePaddle/UVDoc_safetensors 类似的配置。
配置对象继承自 PreTrainedConfig,可用于控制模型输出。有关更多信息,请参阅 PreTrainedConfig 的文档。
UVDocBackbone
class transformers.UVDocBackbone
< source >( config: UVDocBackboneConfig )
参数
- config (UVDocBackboneConfig) — 模型配置类,包含模型的所有参数。使用配置文件进行初始化时,不会加载与模型关联的权重,仅加载配置。请查看 from_pretrained() 方法以加载模型权重。
用于特征提取的 UVDoc 骨干模型。
该模型继承自 PreTrainedModel。请查阅父类文档,了解该库为所有模型实现的通用方法(例如下载或保存、调整输入嵌入大小、剪枝头等)。
该模型也是 PyTorch torch.nn.Module 的子类。 请将其作为常规的 PyTorch Module 使用,有关通用用法和行为的所有事项,请参阅 PyTorch 文档。
forward
< source >( pixel_values: Tensor**kwargs: *<class 'transformers.utils.generic.TransformersKwargs'> ) → BackboneOutput or tuple(torch.Tensor)
参数
- pixel_values (
torch.Tensor,形状为(batch_size, num_channels, image_size, image_size)) — 与输入图像对应的张量。可以使用 UVDocImageProcessor 获取像素值。有关详细信息,请参阅 UVDocImageProcessor.call()(processor_class使用 UVDocImageProcessor 处理图像)。
返回值
BackboneOutput 或 tuple(torch.Tensor)
BackboneOutput 或 torch.Tensor 元组(如果传入 return_dict=False 或 config.return_dict=False),其包含的元素取决于配置(UVDocConfig)和输入。
UVDocBackbone 的 forward 方法重写了 __call__ 特殊方法。
尽管前向传播的实现需要在此函数中定义,但之后应调用
Module实例,而不是直接调用此函数,因为前者会负责执行预处理和后处理步骤,而后者则会静默地忽略这些步骤。
feature_maps(形状为
(batch_size, num_channels, height, width)的tuple(torch.Tensor))— 各阶段的特征图。hidden_states (
tuple(torch.Tensor),可选,在传入output_hidden_states=True或config.output_hidden_states=True时返回) — 一个torch.Tensor元组(包含嵌入层的输出以及每一层的输出),形状为(batch_size, sequence_length, hidden_size)或(batch_size, num_channels, height, width),具体取决于骨干网络。模型在每个阶段输出处的隐藏状态,以及初始嵌入输出。
attentions (
tuple(torch.Tensor),可选,当传入output_attentions=True或config.output_attentions=True时返回) — 一个torch.Tensor元组(每层一个),形状为(batch_size, num_heads, sequence_length, sequence_length)。仅当主干网络使用注意力机制时适用。注意力权重,即经过注意力 softmax 之后的权重,用于在自注意力头中计算加权平均值。
UVDocBridge
UVDocImageProcessor
class transformers.UVDocImageProcessor
< source >( **kwargs: *<class 'transformers.processing_utils.ImagesKwargs'> )
参数
- do_convert_rgb (
bool,kwargs,可选) — 是否将图像转换为 RGB。 - do_resize (
bool,kwargs,可选,默认为True) — 是否调整图像大小。 - size (
Annotated[int | list[int] | tuple[int, ...] | dict[str, int] | None, None], kwargs,默认为{'height' -- 712, 'width': 488}): 描述输入到模型的最大尺寸。 - default_to_square (
bool,kwargs,可选,默认为True) — 当 size 为 int 时,是否在调整大小时默认使用正方形图像。 - crop_size (
Annotated[int | list[int] | tuple[int, ...] | dict[str, int] | None, None], kwargs) — 应用center_crop后输出图像的尺寸。 - resample (
Annotated[Union[int, PILImageResampling, NoneType], None], kwargs,默认为2) — 如果调整图像大小,则使用的重采样滤波器。它可以是枚举PILImageResampling中的一个值。仅当do_resize设置为True时才会生效。 - do_rescale (
bool,kwargs,可选,默认为True) — 是否对图像进行重新缩放。 - rescale_factor (
float,kwargs,可选,默认为0.00392156862745098) — 当do_rescale设置为True时,用于对图像进行缩放的缩放因子。 - do_normalize (
bool,kwargs,可选) — 是否对图像进行归一化。 - image_mean (
Union[float, list[float], tuple[float, ...]], kwargs, 可选) — 用于归一化的图像均值。仅当do_normalize设置为True时生效。 - image_std (
Union[float, list[float], tuple[float, ...]], kwargs, 可选) — 用于归一化的图像标准差。仅当do_normalize设置为True时生效。 - do_pad (
bool, kwargs, 可选) — 是否对图像进行填充。填充操作会将图像填充至批次中的最大尺寸,或填充至每张图像的固定正方形尺寸。具体的填充策略取决于模型。 - pad_size (
Annotated[int | list[int] | tuple[int, ...] | dict[str, int] | None, None], kwargs) — 用于填充图像的{"height": int, "width" int}尺寸。必须大于为预处理提供的任何图像尺寸。如果未提供pad_size,图像将被填充到批次中最大的高度和宽度。仅在do_pad=True.时应用。 - do_center_crop (
bool,kwargs,可选) — 是否对图像进行中心裁剪。 - data_format (
Union[str, ~image_utils.ChannelDimension], kwargs, 可选) — 仅支持ChannelDimension.FIRST。为兼容旧版处理器而添加。 - input_data_format (
Union[str, ~image_utils.ChannelDimension], kwargs, 可选) — 输入图像的通道维度格式。如果未设置,则从输入图像中推断通道维度格式。可以是以下之一:"channels_first"或ChannelDimension.FIRST:图像采用 (num_channels, height, width) 格式。"channels_last"或ChannelDimension.LAST:图像采用 (height, width, num_channels) 格式。"none"或ChannelDimension.NONE:图像为 (height, width) 格式。
- device (
Annotated[Union[str, torch.device, NoneType], None], kwargs) — 用于处理视频的设备。如果未设置,则从输入视频中推断设备。 - return_tensors (
Annotated[str | ~utils.generic.TensorType | None, None], kwargs) — 如果设置为'pt',则返回堆叠的张量,否则返回张量列表。 - disable_grouping (
bool,kwargs,可选) — 是否禁用按尺寸对图像进行分组,从而逐个处理图像而非批量处理。 如果为 None,则当图像位于 CPU 上时设置为 True,否则设置为 False。此选择基于经验观察,详见此处:https://github.com/huggingface/transformers/pull/38157 - image_seq_length (
int, kwargs, optional) — 输入中每张图片所使用的图像 token 数量。 为向后兼容而添加,但在未来的模型中应将其设置为处理器属性。 如果设置为'pt',则返回堆叠的张量,否则返回张量列表。 - disable_grouping (
bool,kwargs,可选) — 是否禁用按尺寸对图像进行分组,从而逐个处理图像而非批量处理。 如果为 None,则当图像位于 CPU 上时设置为 True,否则设置为 False。此选择基于经验观察,详见此处:https://github.com/huggingface/transformers/pull/38157 - image_seq_length (
int, kwargs, optional) — 输入中每张图片所使用的图像 token 数量。 为向后兼容而添加,但在未来的模型中应将其设置为处理器属性。 如果设置为'pt',则返回堆叠的张量,否则返回张量列表。 - disable_grouping (
bool,kwargs,可选) — 是否禁用按尺寸对图像进行分组,从而逐个处理图像而非批量处理。 如果为 None,则当图像位于 CPU 上时设置为 True,否则设置为 False。此选择基于经验观察,详见此处:https://github.com/huggingface/transformers/pull/38157 - image_seq_length (
int, kwargs, optional) — 输入中每张图片所使用的图像 token 数量。 为向后兼容而添加,但在未来的模型中应将其设置为处理器属性。 如果设置为'pt',则返回堆叠的张量,否则返回张量列表。 - disable_grouping (
bool,kwargs,可选) — 是否禁用按尺寸对图像进行分组,从而逐个处理图像而非批量处理。 如果为 None,则当图像位于 CPU 上时设置为 True,否则设置为 False。此选择基于经验观察,详见此处:https://github.com/huggingface/transformers/pull/38157 - image_seq_length (
int, kwargs, optional) — 输入中每张图片所使用的图像 token 数量。 为向后兼容而添加,但在未来的模型中应将其设置为处理器属性。 如果设置为'pt',则返回堆叠的张量,否则返回张量列表。 - disable_grouping (
bool,kwargs,可选) — 是否禁用按尺寸对图像进行分组,从而逐个处理图像而非批量处理。 如果为 None,则当图像位于 CPU 上时设置为 True,否则设置为 False。此选择基于经验观察,详见此处:https://github.com/huggingface/transformers/pull/38157 - image_seq_length (
int, kwargs, optional) — 输入中每张图片所使用的图像 token 数量。 为向后兼容而添加,但在未来的模型中应将其设置为处理器属性。 如果设置为'pt',则返回堆叠的张量,否则返回张量列表。 - disable_grouping (
bool,kwargs,可选) — 是否禁用按尺寸对图像进行分组,从而逐个处理图像而非批量处理。 如果为 None,则当图像位于 CPU 上时设置为 True,否则设置为 False。此选择基于经验观察,详见此处:https://github.com/huggingface/transformers/pull/38157 - image_seq_length (
int, kwargs, optional) — 输入中每张图片所使用的图像 token 数量。 为向后兼容而添加,但在未来的模型中应将其设置为处理器属性。 如果设置为'pt',则返回堆叠的张量,否则返回张量列表。 - disable_grouping (
bool,kwargs,可选) — 是否禁用按尺寸对图像进行分组,从而逐个处理图像而非批量处理。 如果为 None,则当图像位于 CPU 上时设置为 True,否则设置为 False。此选择基于经验观察,详见此处:https://github.com/huggingface/transformers/pull/38157 - image_seq_length (
int, kwargs, optional) — 输入中每张图片所使用的图像 token 数量。 为向后兼容而添加,但在未来的模型中应将其设置为处理器属性。 如果设置为'pt',则返回堆叠的张量,否则返回张量列表。 - disable_grouping (
bool,kwargs,可选) — 是否禁用按尺寸对图像进行分组,从而逐个处理图像而非批量处理。 如果为 None,则当图像位于 CPU 上时设置为 True,否则设置为 False。此选择基于经验观察,详见此处:https://github.com/huggingface/transformers/pull/38157 - image_seq_length (
int,kwargs,可选) — 输入中每张图片所使用的图像 token 数量。 为向后兼容而添加,但在未来的模型中,应将其设置为处理器的属性。 - Returns 如果设置为
'pt',则返回堆叠的张量,否则返回张量列表。 —
构建一个 UVDocImageProcessor 图像处理器。
disable_grouping (bool, kwargs, 可选):
是否禁用按尺寸对图像进行分组,从而逐个处理图像而非批量处理。
如果为 None,则当图像位于 CPU 上时设置为 True,否则设置为 False。此选择基于经验观察,详情见此处:https://github.com/huggingface/transformers/pull/38157 image_seq_length (int, kwargs, 可选):
输入中每张图像所使用的图像 token 数量。
为向后兼容而添加,但在未来的模型中应将其设置为处理器属性。
post_process_document_rectification
< source >( prediction: Tensororiginal_images: listscale: float = 255.0 ) → List of dictionaries containing rectified images. Each dictionary has
对文档校正的预测结果进行后处理,将其转换为校正后的图像。