在 Hugging Face 使用 OpenCLIP
OpenCLIP 是 OpenAI CLIP 的开源实现。
在 Hub 上探索 OpenCLIP
你可以通过在模型页面左侧筛选来找到 OpenCLIP 模型。
托管在 Hub 上的 OpenCLIP 模型有一个包含模型有用信息的模型卡片。由于 OpenCLIP Hugging Face Hub 集成,你可以用几行代码加载 OpenCLIP 模型。你还可以使用推理端点部署这些模型。
安装
要开始使用,你可以遵循 OpenCLIP 安装指南。 你也可以通过 pip 使用以下一行安装:
$ pip install open_clip_torch
使用现有模型
所有 OpenCLIP 模型都可以轻松地从 Hub 加载:
import open_clip
model, preprocess = open_clip.create_model_from_pretrained('hf-hub:laion/CLIP-ViT-g-14-laion2B-s12B-b42K')
tokenizer = open_clip.get_tokenizer('hf-hub:laion/CLIP-ViT-g-14-laion2B-s12B-b42K')
加载后,你可以编码图像和文本以进行零样本图像分类:
import torch
from PIL import Image
import requests
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
image = Image.open(requests.get(url, stream=True).raw)
image = preprocess(image).unsqueeze(0)
text = tokenizer(["a diagram", "a dog", "a cat"])
with torch.no_grad(), torch.cuda.amp.autocast():
image_features = model.encode_image(image)
text_features = model.encode_text(text)
image_features /= image_features.norm(dim=-1, keepdim=True)
text_features /= text_features.norm(dim=-1, keepdim=True)
text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
print("Label probs:", text_probs)
它输出每个可能类别的概率:
Label probs: tensor([[0.0020, 0.0034, 0.9946]])
如果你想加载特定的 OpenCLIP 模型,可以在模型卡片中点击 Use in OpenCLIP,你将获得一个可用的代码片段!



