🚀 clip-vit-base-patch32-ko
這是一個韓語CLIP模型,通過Making Monolingual Sentence Embeddings Multilingual using Knowledge Distillation進行訓練,可用於圖像分類等任務。
🚀 快速開始
本模型可快速用於圖像分類任務,以下是使用示例。
📦 安裝指南
文檔未提及具體安裝步驟,可參考模型倉庫相關說明進行安裝。
💻 使用示例
基礎用法
import requests
import torch
from PIL import Image
from transformers import AutoModel, AutoProcessor
repo = "Bingsu/clip-vit-base-patch32-ko"
model = AutoModel.from_pretrained(repo)
processor = AutoProcessor.from_pretrained(repo)
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
inputs = processor(text=["고양이 두 마리", "개 두 마리"], images=image, return_tensors="pt", padding=True)
with torch.inference_mode():
outputs = model(**inputs)
logits_per_image = outputs.logits_per_image
probs = logits_per_image.softmax(dim=1)
>>> probs
tensor([[0.9926, 0.0074]])
高級用法
from transformers import pipeline
repo = "Bingsu/clip-vit-base-patch32-ko"
pipe = pipeline("zero-shot-image-classification", model=repo)
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
result = pipe(images=url, candidate_labels=["고양이 한 마리", "고양이 두 마리", "분홍색 소파에 드러누운 고양이 친구들"], hypothesis_template="{}")
>>> result
[{'score': 0.9456236958503723, 'label': '분홍색 소파에 드러누운 고양이 친구들'},
{'score': 0.05315302312374115, 'label': '고양이 두 마리'},
{'score': 0.0012233294546604156, 'label': '고양이 한 마리'}]
📚 詳細文檔
Tokenizer
分詞器是將韓語數據和英語數據按7:3的比例混合,通過原CLIP分詞器的.train_new_from_iterator
方法訓練得到的。
參考代碼:https://github.com/huggingface/transformers/blob/bc21aaca789f1a366c05e8b5e111632944886393/src/transformers/models/clip/modeling_clip.py#L661-L666
pooled_output = last_hidden_state[
torch.arange(last_hidden_state.shape[0]), input_ids.to(torch.int).argmax(dim=-1)
]
由於CLIP模型在計算pooled_output
時使用id最大的標記,因此eos標記必須是最後一個標記。
📄 許可證
本項目採用MIT許可證。