시작하기무료로 시작하기

CV 파인튜닝: 모델 클래스

이 연습 문제에서는 사전 학습된 모델을 불러와, 원래 ImageNet 학습에 사용된 1000개 클래스 대신 Stanford Cars 데이터셋의 새로운 자동차 모델 유형 분류에 맞게 출력층을 조정해 보겠습니다. 이 데이터셋에는 자동차 이미지와 레이블이 포함되어 있어요.

데이터셋(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=____
)
코드 편집 및 실행