MulaiMulai sekarang secara gratis

CV fine-tuning: model classes

In this exercise, you will load the pretrained model, and adapt the output to accommodate a new classification of car model types from the Stanford Cars dataset instead of the 1000 classes used for the original ImageNet training. The dataset contains labelled images of cars.

The dataset has been loaded (dataset), as has AutoModelForImageClassification from transformers. The dataset has been filtered so that three model types are included.

Latihan ini adalah bagian dari kursus

Multi-Modal Models with Hugging Face

Lihat Kursus

Petunjuk latihan

  • Obtain the new label names from the dataset
  • Add the new id2label mapping while loading the model.
  • Add the corresponding label2id mapping.
  • Add the required flag to change the number of classes.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# 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=____
)
Edit dan Jalankan Kode