Transformers documentation

PPLCNetV4

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

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

PPLCNetV4

PPLCNetV4 仅作为骨干网络提供。官方尚未发布任何预训练图像分类检查点。

PPLCNetV4Config

class transformers.PPLCNetV4Config

< >

( 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 = Nonescale: float | int = 1.0block_configs: list | None = Nonestem_channels: list[int] | tuple[int, ...] = (3, 48, 96)reduction: int = 4hidden_act: str = 'relu'_out_features: list[str] | None = None_out_indices: list[int] | None = Nonenum_channels: int = 3stem_strides: collections.abc.Sequence[int | list[int] | tuple[int, ...]] = (2, 1, 1, 2, 1)stem_type: str = 'large'use_learnable_affine_block: bool = False )

参数

  • scale (float,可选,默认为 1.0) — 用于调整模型通道维度的缩放因子,可在不改变整体架构的情况下调整模型大小和计算成本(例如 0.25、0.5、1.0、1.5)。
  • block_configs (list[list[tuple]],可选,默认为 None) — 每个阶段中每个块的配置。每个元组包含: (kernel_size, in_channels, out_channels, stride, use_squeeze_excitation)。 如果为 None,则使用默认的 PP-LCNet 配置。
  • stem_channels (list[int], 可选, 默认为 [3, 48, 96]) — Stem 层的通道维度:
    • 第一个数字(3)是输入图像的通道数
    • 第二个数字(48)是中间茎通道数
    • 第三个数字(96)是输出茎通道数
  • reduction (int,可选,默认为 4) — 在 squeeze-and-excitation (SE) 块中用于特征通道维度的缩减因子,用于在保持特征表示能力的同时减少模型参数量和计算复杂度。
  • hidden_act (str,可选,默认为 relu) — 解码器中的非线性激活函数(函数或字符串)。例如,"gelu"、 "relu"、"silu" 等。
  • num_channels (int,可选,默认为 3) — 输入通道的数量。
  • stem_strides (Sequence[int | list[int] | tuple[int, ...]],可选,默认为 (2, 1, 1, 2, 1)) — Stem 层的步长模式。
  • stem_type (str, 可选, 默认为 large) — 要使用的 stem 层类型。可以是以下之一:
    • "large":标准 PP-LCNetV4 主干。
    • "small":通道维度更小的变体。
  • use_learnable_affine_block (bool,可选,默认为 False) — 是否在网络中使用 Learnable Affine Blocks (LAB)。 LAB 会在某些操作之后添加可学习的缩放和偏置参数。

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

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

PPLCNetV4Backbone

class transformers.PPLCNetV4Backbone

< >

( config: PPLCNetV4Config )

参数

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

用于特征提取的 PPLCNetV4 骨干模型。

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

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

forward

< >

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

参数

  • pixel_values(形状为 (batch_size, num_channels, image_size, image_size) 的 torch.Tensor)— 对应于输入图像的张量。可以使用 image_processor_class 获取像素值。有关详细信息,请参阅 image_processor_class.__call__(processor_class 使用 image_processor_class 来处理图像)。

返回值

BackboneOutput 或 tuple(torch.Tensor)

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

PPLCNetV4Backbone 的 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 之后的权重,用于在自注意力头中计算加权平均值。

示例:

>>> from transformers import PPLCNetV4Config, PPLCNetV4Backbone
>>> import torch

>>> config = PPLCNetV4Config()
>>> model = PPLCNetV4Backbone(config)

>>> pixel_values = torch.randn(1, 3, 224, 224)

>>> with torch.no_grad():
...     outputs = model(pixel_values)

>>> feature_maps = outputs.feature_maps
>>> list(feature_maps[-1].shape)
Update on GitHub