!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())
标签:clip,similarity,image,torch,colab,device,import,比较 From: https://www.cnblogs.com/Anonytt/p/18278267