随着人工智能和深度学习的发展,多模态数据检索 逐渐成为热门技术,广泛应用于 图像搜索、语音识别、跨模态检索、推荐系统 等领域。传统的基于关键词或规则的检索方式已经难以满足智能应用的需求,因此,基于向量搜索的 近似最近邻(ANN)检索 成为主流方案。Milvus 作为一款 开源的向量数据库,可以高效地存储和检索 图像、文本、音频等多模态数据 的向量表示。本文将介绍 Milvus 如何处理多模态数据的向量搜索,以及如何构建高效的多模态检索系统。
在传统的检索系统中,通常使用 关键词匹配、规则匹配或基于 SQL 的查询,但这些方法在 图像、音频、文本 等非结构化数据上效果有限。向量搜索的优势包括:
Milvus 的多模态搜索流程通常包括 数据特征提取(Embedding)、向量存储(Milvus)、向量检索(ANN 搜索) 三个核心环节。
数据类型 | 预处理方式 | 特征提取模型 | 示例应用 |
---|---|---|---|
图像 | 归一化、尺寸调整 | ResNet、CLIP、DINO | 以图搜图、内容推荐 |
文本 | 分词、去停用词 | BERT、Sentence-BERT | 语义搜索、跨模态搜索 |
音频 | 采样、降噪 | Wav2Vec、VGGish | 语音检索、声纹识别 |
docker run -d --name milvus \
-p 19530:19530 \
milvusdb/milvus:latest
使用 ResNet50 提取图像特征并转换为向量:
from torchvision import models, transforms
from PIL import Image
import torch
# 加载 ResNet50 预训练模型
model = models.resnet50(pretrained=True)
model.eval()
# 图像预处理
def image_to_vector(image_path):
image = Image.open(image_path).convert("RGB")
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
])
image = transform(image).unsqueeze(0)
# 提取特征向量
with torch.no_grad():
vector = model(image).numpy().flatten()
return vector
使用 Sentence-BERT 将文本转换为向量:
from sentence_transformers import SentenceTransformer
# 加载 Sentence-BERT 模型
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
# 文本转换为向量
def text_to_vector(text):
vector = model.encode(text)
return vector
使用 VGGish 提取音频特征:
import librosa
import numpy as np
def audio_to_vector(audio_path):
y, sr = librosa.load(audio_path, sr=16000)
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40)
vector = np.mean(mfcc, axis=1)
return vector
from pymilvus import connections, FieldSchema, CollectionSchema, DataType, Collection
# 连接 Milvus
connections.connect(host="localhost", port="19530")
# 定义 Collection Schema
fields = [
FieldSchema(name="id", dtype=DataType.INT64, is_primary=True, auto_id=True),
FieldSchema(name="vector", dtype=DataType.FLOAT_VECTOR, dim=512), # 向量维度
]
schema = CollectionSchema(fields, description="Multimodal Search")
collection = Collection(name="multimodal_data", schema=schema)
# 插入数据
vectors = [image_to_vector("example.jpg")] # 可替换为 text_to_vector 或 audio_to_vector
collection.insert([vectors])
index_params = {
"index_type": "IVF_FLAT",
"metric_type": "L2",
"params": {"nlist": 128},
}
collection.create_index(field_name="vector", index_params=index_params)
query_vector = image_to_vector("query.jpg") # 也可以是 text_to_vector 或 audio_to_vector
search_params = {"metric_type": "L2", "params": {"nprobe": 10}}
results = collection.search([query_vector], anns_field="vector", param=search_params, limit=5)
for hits in results:
for hit in hits:
print(f"匹配 ID: {hit.id}, 相似度: {hit.distance}")
选择合适的索引:
预加载索引,提高查询速度:
collection.load()
批量查询,提高吞吐量:
search_results = collection.search(query_vectors, anns_field="vector", limit=10, batch_size=64)
多线程并发查询:
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
results = list(executor.map(lambda q: collection.search(q, anns_field="vector", limit=10), queries))
Milvus 结合深度学习特征提取模型,可以高效地处理 图像、文本、音频 的向量搜索,实现高精度的 多模态检索。
✅ 支持图像、文本、音频等多种模态数据。
✅ 高效存储向量数据,支持海量检索。
✅ 跨模态检索,如“文本查图片”、“语音查文本”。
✅ 支持 HNSW、IVF_FLAT、SCANN 等索引优化查询性能。
通过合理的 特征提取、索引优化、查询并行化,可以构建高效的 多模态向量搜索系统,提升 AI 应用的智能检索能力!
有什么问题和经验想分享?欢迎在评论区交流、点赞、收藏、关注!