colab上比较clip
!pip install torch torchvision ftfy regex !pip install git+https://github.com/openai/CLIP.git
import torch import clip from PIL import Image from io import BytesIO import requests # 加载模型 device = "cuda" if torch.cuda.is_available() else "cpu" model, preprocess = clip.load("ViT-B/32", device=device)
# 加载并预处理图片 image1 = preprocess(Image.open('img1.jpg')).unsqueeze(0).to(device) image2 = preprocess(Image.open('img2.jpg')).unsqueeze(0).to(device)
with torch.no_grad(): image_features1 = model.encode_image(image1) image_features2 = model.encode_image(image2) # 计算余弦相似度 cosine_similarity = torch.nn.CosineSimilarity(dim=1, eps=1e-6) similarity = cosine_similarity(image_features1, image_features2) print("Similarity score:", similarity.item())
前ICPC算法竞赛退役选手|现摸鱼ing