Hub 文档
在 Hugging Face 上使用 OpenCLIP
加入 Hugging Face 社区
并获得增强的文档体验
开始使用
在 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 模型,您可以单击模型卡片中的“在 OpenCLIP 中使用”,您将获得可用的代码片段!



