开始使用免费开始使用

计算机视觉微调:模型类

在本练习中,您将加载一个预训练模型,并将其输出适配为新的汽车车型分类,使用的是 Stanford Cars 数据集,而不是原始 ImageNet 训练中使用的 1000 个类别。该数据集包含带标签的汽车图像。

数据集(dataset)和来自 transformersAutoModelForImageClassification 已加载。数据集已过滤,仅包含三种车型类型。

本练习是课程的一部分

使用 Hugging Face 的多模态模型

查看课程

练习说明

  • 从数据集中获取新的标签名称。
  • 在加载模型时,添加新的 id2label 映射。
  • 添加相应的 label2id 映射。
  • 添加所需的参数以更改类别数。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Obtain the new label names from the dataset
labels = dataset["train"].features["____"].____

label2id, id2label = dict(), dict()
for i, label in enumerate(labels):
    label2id[label] = str(i)
    id2label[str(i)] = label

model = AutoModelForImageClassification.from_pretrained(
    "google/mobilenet_v2_1.0_224",
    num_labels=len(labels),
    # Add the id2label mapping
    id2label=____,
    # Add the corresponding label2id mapping
    label2id=____,
    # Add the required flag to change the number of classes
    ignore_mismatched_sizes=____
)
编辑并运行代码