利用transformers提取图片特征,存储到Pinecone数据库

news/2025/3/15 7:37:20/

一、前言

1、连接Pinecone数据库,或者创建pinecone数据库

2、加载或下载CLIP模型

3、加载图片

4、利用transformers提取图片向量特征

5、存储到Pincecone数据库中

二、代码示例

import pinecone
import torch
import numpy as np
from PIL import Image
from transformers import CLIPProcessor, CLIPModel# Initialize the Pinecone client
pinecone.init(api_key="Your-API-KEY", environment="Your-environment")# 如果没有先注册,地址:https://www.pinecone.io/(需要梯子)# 连接数据库
index_name = "img-index"
print(index_name)
index = pinecone.Index(index_name=index_name)# 加载或下载CLIP模型
model_path = r"D:\Desktop\\clip_model.pt"
model_name = 'openai/clip-vit-base-patch16'
try:# 尝试从本地路径加载模型clip_model = torch.load(model_path)clip_processor = CLIPProcessor.from_pretrained(model_name)
except FileNotFoundError:# 如果本地未找到模型,则下载并保存模型(需要梯子)clip_processor = CLIPProcessor.from_pretrained(model_name)clip_model = CLIPModel.from_pretrained(model_name)torch.save(clip_model, model_path)# 加载图片
image_path = r'D:\Desktop\tp.png'
print(image_path)# Load the image and text
text = ["img"]# Preprocess the image and text
inputs = clip_processor(text=text, images=Image.open(image_path), return_tensors="pt", padding=True, truncation=True)# Forward pass through the model
with torch.no_grad():outputs = clip_model(**inputs)# Get the image and text embeddings
# image_embeds 是图像在模型嵌入空间中的向量表示,用于计算图像之间的相似度或在其他图像相关任务中使用:512
# logits_per_image 是用于图像分类任务的预测得分,表示图像在不同类别上的概率分布:20
image_vectors = outputs.image_embeds.numpy().tolist()print(image_vectors[0])vectors = [("vec22",  # Vector IDimage_vectors,  # Dense vector values{"path": image_path}  # Vector metadata)
]
print(vectors)# 将数据存储
upsert_response = index.upsert(vectors=vectors,namespace="img-namespace"
)


http://www.ppmy.cn/news/947716.html

相关文章

题目1417:变形金刚

题目描述: 看过变形金刚的人一定记得这样一个场景,机器人在攻击人类的时候,可以上天入地,并且都如履平地。 聪明的人类很快就想到,可不可以也利用地下的攻势来跟机器人进行周旋。很快,人类就在地下建立了几…

Canvas变形金刚

这里是html代码 <canvas width500 height500 id"cas"></canvas> 这里是css代码 *{margin: 0;padding: 0; } canvas{/*border: 1px solid;*/position: absolute;margin-left: 50%;left: -250px; } 这里是js代码 onload function () {var cas document.q…

变形金刚实验调试

ValueError _, term_width os.popen(stty size, r).read().split() ValueError: not enough values to unpack (expected 2, got 0)resolution _, term_width os.popen(stty size, r).read().split() term_width int(term_width) windows下运行需要注释掉上面两行并添加下面…

变形金刚年史

2019独角兽企业重金招聘Python工程师标准>>> 数千万年前&#xff0c;宇宙中的智慧生命五面怪在宇宙中入侵了一个星球——塞博坦&#xff08;cybertron&#xff09;&#xff0c;在这个星球上&#xff0c;五面怪建造了方便自己制造的星球核心计算机魔力神球&#xff0…

变形金刚图纸_变形金刚救援

变形金刚图纸 使用神经网络生成数据到文本语言 (Data to text language generation with neural networks) In my last post, I elaborated upon my modelling approach, for the use case of RDF-to-text generation. The task is part of a larger open source project to wh…

java变形金刚中文版_[原创]RC4、Base64魔改看雪CTF-变形金刚学习笔记

前言 最近在研究某某app的数据库&#xff0c;发现自己在so层的调试比较薄弱&#xff0c;专门找了看雪的CTF-变形金刚来学习。希望在用ida调试so方面有所突破。 利用国庆期间整理成笔记。技术不成熟或许描述的不够清晰请大伙见谅。 也拜读了几位大佬的文章。 工具准备ida7.0 Tra…

1024分辨率《变形金刚3》BD中英双字 高清 1080P 9G   720P 6G 下载

高清 1080P 9G 720P 6G 下载" title="1024分辨率《变形金刚3》BD中英双字 高清 1080P 9G 720P 6G 下载">

操作系统(王道)- 操作系统的特征

一、操作系统的特征 1&#xff09;并发 并发和并行的概念区分 并发的概念&#xff1a; 2&#xff09;共享 共享的概念&#xff1a; 并发和共享的关系&#xff1a; 3&#xff09;虚拟 虚拟特性&#xff1a; 空分、时分复用技术 空分复用技术&#xff1a; 时分复用技术&#xff…