Transformers documentation

RegNet

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

and get access to the augmented documentation experience

to get started

该模型于 2020-03-30 在 HF papers 上发表,并于 2022-04-07 贡献至 Hugging Face Transformers。

RegNet

概述

RegNet 模型由 Ilija Radosavovic、Raj Prateek Kosaraju、Ross Girshick、Kaiming He 和 Piotr Dollár 在 Designing Network Design Spaces 中提出。

作者设计了搜索空间以执行神经架构搜索(NAS)。他们首先从一个高维搜索空间开始,然后根据当前搜索空间采样出的表现最佳的模型,通过经验性地施加约束来迭代地缩小搜索空间。

该论文的摘要如下:

在本工作中,我们提出了一种新的网络设计范式。我们的目标是推动对网络设计的理解,并发现能够跨不同场景泛化的设计原则。我们不再专注于设计单个网络实例,而是设计用于参数化网络群体的网络设计空间。整体过程类似于经典的手动网络设计,但提升到了设计空间层面。利用我们的方法,我们探索了网络设计的结构方面,并构建了一个由简单、规则的网络组成的低维设计空间,我们将其称为 RegNet。RegNet 参数化的核心洞察出人意料地简单:优秀网络的宽度和深度可以用一个量化线性函数来解释。我们对 RegNet 设计空间进行了分析,得出了与当前网络设计实践不符的有趣发现。RegNet 设计空间提供了简单且快速的网络,在广泛的浮点运算量(flop)范围内表现良好。在可比的训练设置和浮点运算量下,RegNet 模型的性能优于流行的 EfficientNet 模型,同时在 GPU 上的速度最高可达 5 倍。

该模型由 Francesco 贡献。原始代码可在此处找到。

The huge 10B model from Self-supervised Pretraining of Visual Features in the Wild, trained on one billion Instagram images, is available on the hub

资源

以下是官方的 Hugging Face 和社区(以 🌎 标识)资源列表,可帮助您快速上手 RegNet。

Image Classification

如果您有兴趣提交资源以收录在此处,请随时发起 Pull Request,我们将对其进行审核!理想情况下,该资源应展示一些新内容,而不是重复现有资源。

RegNetConfig

类 transformers.RegNetConfig

< >

( 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 = Nonenum_channels: int = 3embedding_size: int = 32hidden_sizes: list[int] | tuple[int, ...] = (128, 192, 512, 1088)depths: list[int] | tuple[int, ...] = (2, 6, 12, 2)groups_width: int = 64layer_type: str = 'y'hidden_act: str = 'relu'downsample_in_first_stage: bool = True )

参数

  • num_channels (int,可选,默认为 3) — 输入通道的数量。
  • embedding_size (int,可选,默认为 32) — 嵌入和隐藏状态的维度。
  • hidden_sizes (Union[list[int], tuple[int, ...]],可选,默认为 (128, 192, 512, 1088)) — 模型每个阶段的维度(隐藏层大小)。
  • depths (Union[list[int], tuple[int, ...]],可选,默认为 (2, 6, 12, 2)) — Transformer 中每一层的深度。
  • groups_width (int,可选,默认为 64) — 每个阶段的组宽度。
  • layer_type (str, optional, defaults to "y") — The layer to use, it can be either "x" or “y”. An xlayer is a ResNet's BottleNeck layer withreductionfixed to1. While a ylayer is ax` but with squeeze and excitation. Please refer to the paper for a detailed explanation of how these layers were constructed.
  • hidden_act (str,可选,默认为 relu) — 解码器中的非线性激活函数(函数或字符串)。例如,"gelu"、 "relu"、"silu" 等。
  • downsample_in_first_stage(bool,可选,默认为 False)— 如果为 True,第一阶段将使用 stride 为 2 的方式对输入进行下采样。

这是用于存储 RegNetModel 配置的类。它根据指定的参数实例化 Regnet 模型,并定义模型架构。使用默认值实例化配置将得到与 facebook/regnet-y-040 类似的配置。

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

示例:

>>> from transformers import RegNetConfig, RegNetModel

>>> # Initializing a RegNet regnet-y-40 style configuration
>>> configuration = RegNetConfig()
>>> # Initializing a model from the regnet-y-40 style configuration
>>> model = RegNetModel(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config

RegNetModel

class transformers.RegNetModel

< >

( config: RegNetConfig )

参数

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

未添加任何特定头部的裸 Regnet 模型,直接输出原始隐藏状态。

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

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

forward

< >

( pixel_values: Tensoroutput_hidden_states: bool | None = Nonereturn_dict: bool | None = None**kwargs ) → BaseModelOutputWithPoolingAndNoAttention or tuple(torch.Tensor)

参数

  • pixel_values (torch.Tensor,形状为 (batch_size, num_channels, image_size, image_size)) — 与输入图像对应的张量。可以使用 ConvNextImageProcessor 获取像素值。有关详细信息,请参阅 ConvNextImageProcessor.call()(processor_class 使用 ConvNextImageProcessor 处理图像)。
  • output_hidden_states (bool,可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参阅返回张量中的 hidden_states。
  • return_dict (bool, 可选) — 是否返回 ModelOutput 而不是普通元组。

返回值

BaseModelOutputWithPoolingAndNoAttention 或 tuple(torch.Tensor)

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

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

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

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

  • pooler_output(形状为 (batch_size, hidden_size) 的 torch.Tensor)— 在空间维度上执行池化操作后的最后一层隐藏状态。

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

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

示例:

RegNetForImageClassification

class transformers.RegNetForImageClassification

< >

( config: RegNetConfig )

参数

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

带有图像分类头的 RegNet 模型(在池化特征之上添加一个线性层),例如用于 ImageNet。

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

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

forward

< >

( pixel_values: typing.Optional[torch.Tensor] = Nonelabels: typing.Optional[torch.LongTensor] = Noneoutput_hidden_states: bool | None = Nonereturn_dict: bool | None = None**kwargs ) → ImageClassifierOutputWithNoAttention or tuple(torch.Tensor)

参数

  • pixel_values (torch.Tensor,形状为 (batch_size, num_channels, image_size, image_size),可选) — 与输入图像对应的张量。可以使用 ConvNextImageProcessor 获取像素值。有关详细信息,请参阅 ConvNextImageProcessor.call()(processor_class 使用 ConvNextImageProcessor 处理图像)。
  • labels(形状为 (batch_size,) 的 torch.LongTensor,可选)— 用于计算图像分类/回归损失的标签。索引应在 [0, ..., config.num_labels - 1] 范围内。如果 config.num_labels > 1,则计算分类损失(交叉熵)。
  • output_hidden_states (bool,可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参阅返回张量中的 hidden_states。
  • return_dict (bool, 可选) — 是否返回 ModelOutput 而不是普通元组。

返回值

ImageClassifierOutputWithNoAttention 或 tuple(torch.Tensor)

一个 ImageClassifierOutputWithNoAttention 或 torch.Tensor 元组(当传入 return_dict=False 或 config.return_dict=False 时),其包含的元素取决于配置(RegNetConfig)和输入。

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

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

  • loss (torch.Tensor,形状为 (1,),可选,当提供 labels 时返回) — 分类损失(如果 config.num_labels==1,则为回归损失)。
  • logits(形状为 (batch_size, config.num_labels) 的 torch.Tensor)— 分类(或当 config.num_labels==1 时为回归)得分(SoftMax 之前)。
  • hidden_states (tuple(torch.Tensor),可选,在传入 output_hidden_states=True 或 config.output_hidden_states=True 时返回) — 一个 torch.Tensor 元组(如果模型具有嵌入层,则包含嵌入层输出的一个张量,以及每个阶段输出的一个张量),形状为 (batch_size, num_channels, height, width)。模型在每个阶段输出处的隐藏状态(也称为特征图)。

示例:

>>> from transformers import AutoImageProcessor, RegNetForImageClassification
>>> import torch
>>> from datasets import load_dataset

>>> dataset = load_dataset("huggingface/cats-image")
>>> image = dataset["test"]["image"][0]

>>> image_processor = AutoImageProcessor.from_pretrained("facebook/regnet-y-040")
>>> model = RegNetForImageClassification.from_pretrained("facebook/regnet-y-040")

>>> inputs = image_processor(image, return_tensors="pt")

>>> with torch.no_grad():
...     logits = model(**inputs).logits

>>> # model predicts one of the 1000 ImageNet classes
>>> predicted_label = logits.argmax(-1).item()
>>> print(model.config.id2label[predicted_label])
...
Update on GitHub