Optimum 文档

模型

您正在查看 main 版本,该版本需要从源码安装。如果您想要常规的 pip 安装,请查看最新的稳定版本 (v1.24.0)。
Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

模型

通用模型类

以下 Furiosa 类可用于实例化没有特定 head 的基础模型类。

FuriosaAIModel

class optimum.furiosa.FuriosaAIModel

< >

( model config: PretrainedConfig = None compute_metrics: typing.Optional[typing.Callable[[transformers.trainer_utils.EvalPrediction], typing.Dict]] = None label_names: typing.Optional[typing.List[str]] = None **kwargs )

evaluation_loop

< >

( dataset: Dataset )

参数

  • dataset (datasets.Dataset) — 用于评估步骤的数据集。

运行评估并返回指标和预测。

to

< >

( device: str )

使用指定的 device 进行推理。例如:“cpu” 或 “gpu”。device 可以是大写或小写。为了加速首次推理,在 .to() 之后调用 .compile()

计算机视觉

以下类可用于以下计算机视觉任务。

FuriosaAIModelForImageClassification

class optimum.furiosa.FuriosaAIModelForImageClassification

< >

( model = None config = None **kwargs )

参数

  • model (furiosa.runtime.model) — 是用于运行推理的主类。
  • config (transformers.PretrainedConfig) — PretrainedConfig 是模型配置类,包含模型的所有参数。使用配置文件初始化不会加载与模型关联的权重,仅加载配置。查看 ~furiosa.modeling.FuriosaAIBaseModel.from_pretrained 方法以加载模型权重。
  • device (str, 默认为 "CPU") — 模型将为其优化的设备类型。生成的编译模型将包含特定于此设备的节点。
  • furiosa_config (Optional[Dict], 默认为 None) — 包含与模型编译相关信息的字典。
  • compile (bool, 默认为 True) — 设置为 False 时,禁用加载步骤期间的模型编译。

用于图像分类任务的带有 ImageClassifierOutput 的 FuriosaAI 模型。

此模型继承自 optimum.furiosa.FuriosaAIBaseModel。查看超类文档,了解库为其所有模型实现的通用方法(例如下载或保存)

forward

< >

( pixel_values: typing.Union[torch.Tensor, numpy.ndarray] **kwargs )

参数

  • pixel_values (torch.Tensor) — 与当前批次中的图像对应的像素值。像素值可以使用 AutoFeatureExtractor 从编码图像中获得。

FuriosaAIModelForImageClassification forward 方法,覆盖了 __call__ 特殊方法。

虽然 forward 传递的配方需要在该函数中定义,但之后应该调用 Module 实例而不是此函数,因为前者负责运行预处理和后处理步骤,而后者会静默地忽略它们。

使用 transformers.pipelines 进行图像分类的示例

>>> from transformers import AutoFeatureExtractor, pipeline
>>> from optimum.furiosa import FuriosaAIModelForImageClassification

>>> preprocessor = AutoFeatureExtractor.from_pretrained("microsoft/resnet50")
>>> model = FuriosaAIModelForImageClassification.from_pretrained("microsoft/resnet50", export=True, input_shape_dict="dict('pixel_values': [1, 3, 224, 224])", output_shape_dict="dict("logits": [1, 1000])",)
>>> pipe = pipeline("image-classification", model=model, feature_extractor=preprocessor)
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> outputs = pipe(url)