当前位置: 代码网 > it编程>前端脚本>Python > Python中CLIP多模态模型的库的实现

Python中CLIP多模态模型的库的实现

2025年04月28日 Python 我要评论
clip(contrastive language–image pretraining)是 openai 提出的多模态模型,可以将图像和文本映射到同一个嵌入空间中,从而实现图文匹配、零样本

clip(contrastive language–image pretraining)是 openai 提出的 多模态模型,可以将图像和文本映射到同一个嵌入空间中,从而实现图文匹配、零样本分类、图文检索等任务。

虽然 openai 没有单独发布一个叫 clip 的官方 python 库,但社区版本如 open_clipclip from openaiclip-as-service 等都被广泛使用。以下主要介绍:

1. 安装 openai 官方 clip

pip install git+https://github.com/openai/clip.git

依赖:torchnumpypil

2. 快速使用示例

import clip
import torch
from pil import image

# 加载模型和预处理方法
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("vit-b/32", device=device)

# 加载图像并预处理
image = preprocess(image.open("cat.jpg")).unsqueeze(0).to(device)

# 编写文本描述
text = clip.tokenize(["a photo of a cat", "a photo of a dog"]).to(device)

# 提取特征并计算相似度
with torch.no_grad():
    image_features = model.encode_image(image)
    text_features = model.encode_text(text)
    logits_per_image, logits_per_text = model(image, text)
    probs = logits_per_image.softmax(dim=-1).cpu().numpy()

print("label probabilities:", probs)

3. 模型选项

支持的模型有:

  • "vit-b/32":最快,最常用
  • "vit-b/16":更大更准
  • "rn50""rn101":基于 resnet

4. 文本编码

text = ["a photo of a banana", "a dog", "a car"]
tokens = clip.tokenize(text).to(device)

with torch.no_grad():
    text_features = model.encode_text(tokens)

5. 图像编码

from pil import image

image = image.open("example.jpg")
image_input = preprocess(image).unsqueeze(0).to(device)

with torch.no_grad():
    image_features = model.encode_image(image_input)

6. 相似度比较

import torch.nn.functional as f

# 余弦相似度
similarity = f.cosine_similarity(image_features, text_features)
print(similarity)

7. 零样本图像分类

labels = ["a dog", "a cat", "a car"]
text_inputs = clip.tokenize([f"a photo of {label}" for label in labels]).to(device)

with torch.no_grad():
    text_features = model.encode_text(text_inputs)
    image_features = model.encode_image(image)

# 归一化
image_features /= image_features.norm(dim=-1, keepdim=true)
text_features /= text_features.norm(dim=-1, keepdim=true)

# 相似度得分
logits = (image_features @ text_features.t)
pred = logits.argmax().item()

print(f"predicted label: {labels[pred]}")

8. 与其他库对比

特性clipblip / flamingobert / gpt
图文对齐
多模态能力强(图像 + 文本)更强(支持生成)
零样本能力
适合任务图文检索、匹配、分类生成描述、问答、vqa语言任务

9. 更强大:open_clip

open_clip 是社区支持的更强版本,支持更多预训练模型(如 laion 提供的):

pip install open_clip_torch
import open_clip

model, preprocess, tokenizer = open_clip.create_model_and_transforms('vit-b-32', pretrained='laion2b_s34b_b79k')

10. 总结

功能方法
加载模型clip.load()
文本编码model.encode_text()
图像编码model.encode_image()
图文相似度model(image, text) 或余弦相似度
图像分类(零样本)文本描述嵌入后选最大相似度
支持模型"vit-b/32""vit-b/16" 等

clip 是现代多模态 ai 模型的典范,可广泛应用于图像检索、图文分类、图像问答、跨模态搜索等场景。它在“零样本”条件下也能表现良好,是构建通用图文理解系统的强大工具。

到此这篇关于python中clip多模态模型的库的实现的文章就介绍到这了,更多相关python clip多模态模型内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com