1 Star 0 Fork 0

duanyunhong/feature similarity calculation

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
clip_official.py 622 Bytes
一键复制 编辑 原始数据 按行查看 历史
duanyunhong 提交于 2024-11-02 17:54 +08:00 . input 0-1, cal_sim use all channel flatten
import torch
import clip
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("Lenna_(test_image).png")).unsqueeze(0).to(device)
text = clip.tokenize(["a diagram", "a dog", "a cat"]).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 probs:", probs) # prints: [[0.9927937 0.00421068 0.00299572]]
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/duanyunhong/feature-similarity-calculation.git
git@gitee.com:duanyunhong/feature-similarity-calculation.git
duanyunhong
feature-similarity-calculation
feature similarity calculation
master

搜索帮助