+
95
-

回答

不仅仅是图片,甚至视频、音频都能实现向量语义化检索,图像使用ImageNet-1k的validation数据集作为入库的图片数据集,将原始图片数据Embedding入库https://www.image-net.org/download.php

声音类的使用ESC-50数据集合 https://github.com/karolpiczak/ESC-50

图片embedding入库

import dashscope
from dashscope import MultiModalEmbedding
from dashvector import Client, Doc, DashVectorException

dashscope.api_key = '{your-dashscope-api-key}'

# 由于 ONE-PEACE 模型服务当前只支持 url 形式的图片、音频输入,因此用户需要将数据集提前上传到
# 公共网络存储(例如 oss/s3),并获取对应图片、音频的 url 列表。
# 该文件每行存储数据集单张图片的公共 url,与当前python脚本位于同目录下
IMAGENET1K_URLS_FILE_PATH = "imagenet1k-urls.txt"


def index_image():
    # 初始化 dashvector client
    client = Client(
      api_key='{your-dashvector-api-key}',
      endpoint='{your-dashvector-cluster-endpoint}'
    )

    # 创建集合:指定集合名称和向量维度, ONE-PEACE 模型产生的向量统一为 1536 维
    rsp = client.create('imagenet1k_val_embedding', 1536)
    if not rsp:
        raise DashVectorException(rsp.code, reason=rsp.message)

    # 调用 dashscope ONE-PEACE 模型生成图片 Embedding,并插入 dashvector
    collection = client.get('imagenet1k_val_embedding')
    with open(IMAGENET1K_URLS_FILE_PATH, 'r') as file:
        for i, line in enumerate(file):
            url = line.strip('\n')
            input = [{'image': url}]
            result = MultiModalEmbedding.call(model=MultiModalEmbedding.Models.multimodal_embedding_one_peace_v1,
                                              input=input,
                                              auto_truncation=True)
            if result.status_code != 200:
                print(f"ONE-PEACE failed to generate embedding of {url}, result: {result}")
                continue
            embedding = result.output["embedding"]
            collection.insert(
                Doc(
                    id=str(i),
                    vector=embedding,
                    fields={'image_url': url}
                )
            )
            if (i + 1) % 100 == 0:
                print(f"---- Succeeded to insert {i + 1} image embeddings")


if __name__ == '__main__':
    index_image()

2、通过文字检索图片

import dashscope
from dashscope import MultiModalEmbedding
from dashvector import Client
from urllib.request import urlopen
from PIL import Image

dashscope.api_key = '{your-dashscope-api-key}'


def show_image(image_list):
    for img in image_list:
        # 注意:show() 函数在 Linux 服务器上可能需要安装必要的图像浏览器组件才生效
        # 建议在支持 jupyter notebook 的服务器上运行该代码
        img.show()


def text_search(input_text):
    # 初始化 dashvector client
    client = Client(
      api_key='{your-dashvector-api-key}',
      endpoint='{your-dashvector-cluster-endpoint}'
    )

    # 获取上述入库的集合
    collection = client.get('imagenet1k_val_embedding')

    # 获取文本 query 的 Embedding 向量
    input = [{'text': input_text}]
    result = MultiModalEmbedding.call(model=MultiModalEmbedding.Models.multimodal_embedding_one_peace_v1,
                                      input=input,
                                      auto_truncation=True)
    if result.status_code != 200:
        raise Exception(f"ONE-PEACE failed to generate embedding of {input}, result: {result}")
    text_vector = result.output["embedding"]

    # DashVector 向量检索
    rsp = collection.query(text_vector, topk=3)
    image_list = list()
    for doc in rsp:
        img_url = doc.fields['image_url']
        img = Image.open(urlopen(img_url))
        image_list.append(img)
    return image_list


if __name__ == '__main__':
    """文本检索"""
    # 猫
    text_query = "cat"
    show_image(text_search(text_query))

音频

import dashscope
from dashscope import MultiModalEmbedding
from dashvector import Client
from urllib.request import urlopen
from PIL import Image

dashscope.api_key = '{your-dashscope-api-key}'


def show_image(image_list):
    for img in image_list:
        # 注意:show() 函数在 Linux 服务器上可能需要安装必要的图像浏览器组件才生效
        # 建议在支持 jupyter notebook 的服务器上运行该代码
        img.show()


def audio_search(input_audio):
    # 初始化 dashvector client
    client = Client(
      api_key='{your-dashvector-api-key}',
      endpoint='{your-dashvector-cluster-endpoint}'
    )

    # 获取上述入库的集合
    collection = client.get('imagenet1k_val_embedding')

    # 获取音频 query 的 Embedding 向量
    input = [{'audio': input_audio}]
    result = MultiModalEmbedding.call(model=MultiModalEmbedding.Models.multimodal_embedding_one_peace_v1,
                                      input=input,
                                      auto_truncation=True)
    if result.status_code != 200:
        raise Exception(f"ONE-PEACE failed to generate embedding of {input}, result: {result}")
    audio_vector = result.output["embedding"]

    # DashVector 向量检索
    rsp = collection.query(audio_vector, topk=3)
    image_list = list()
    for doc in rsp:
        img_url = doc.fields['image_url']
        img = Image.open(urlopen(img_url))
        image_list.append(img)
    return image_list


if __name__ == '__main__':
    """音频检索"""
    # 猫叫声
    audio_url = "http://proxima-internal.oss-cn-zhangjiakou.aliyuncs.com/audio-dataset/esc-50/1-47819-A-5.wav"
    show_image(audio_search(audio_url))

具体可参考这篇文章:https://help.aliyun.com/zh/dashscope/dashvector-one-peace-upgrade-multimodal-retrieva

网友回复

我知道答案,我要回答