電腦視覺微調:模型類別
在這個練習中,你將載入預訓練模型,並調整輸出,以適應來自 Stanford Cars 資料集 的汽車車型新分類,而不是原本 ImageNet 訓練所使用的 1000 個類別。此資料集包含已標註的汽車影像。
已載入資料集(dataset),以及來自 transformers 的 AutoModelForImageClassification。資料集已經過篩選,只包含三種車型。
本練習屬於課程
使用 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=____
)