使用 CLIP 的零样本学习
您将使用零样本学习来对 rajuptvs/ecommerce_products_clip 数据集中的一张图像进行分类。该数据集包含约 2k 张带有相关描述的商品图片:

系统已为您加载数据集(dataset)、CLIPProcessor(processor)和 CLIPModel(model),以及一个类别列表:
categories = ["shirt", "trousers", "shoes", "dress", "hat",
"bag", "watch", "glasses", "jacket", "belt"]
本练习是课程的一部分
使用 Hugging Face 的多模态模型
练习说明
- 使用
processor对categories和dataset中索引为999的图像进行预处理;启用 padding。 - 将解包后的
inputs传入model。 - 使用
.logits_per_image属性和.softmax()方法计算每个类别的概率。 - 利用
probs和categories找出最可能的类别。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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}")