開始使用免費開始

使用 CLIP 的零樣本學習

你將使用零樣本學習來分類一張來自 rajuptvs/ecommerce_products_clip 資料集的影像。這個資料集包含約 2k 張商品圖片與對應的描述:

Image of a woman modeling a dress

我們已為你載入資料集(dataset)、CLIPProcessor(processor)與 CLIPModel(model),以及一組類別清單:

categories = ["shirt", "trousers", "shoes", "dress", "hat", 
              "bag", "watch", "glasses", "jacket", "belt"]

本練習屬於課程

使用 Hugging Face 的多模態模型

檢視課程

練習說明

  • 使用 processor 前處理 categoriesdataset 中索引為 999 的影像;啟用 padding。
  • 將解包後的 inputs 傳入 model
  • 使用 .logits_per_image 屬性與 .softmax() 方法計算各類別的機率。
  • 使用 probscategories 找出最可能的類別。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Preprocess the categories and image 
inputs = ____(text=____, images=____, return_tensors="pt", padding=____)

# Process the unpacked inputs with the model
outputs = ____

# Calculate the probabilities of each category
probs = outputs.____.____(dim=1)

# Find the most likely category
category = categories[probs.____.item()]
print(f"Predicted category: {category}")
編輯並執行程式碼