复现系列-5:基于CLIP模型的跨模态视频搜索

667 阅读2分钟

最近一直在关注Jina,也在跟着官方文档学习一些基础知识,公众号里发了一篇教程,我也花了一些时间学习,中间遇到了一些问题,这里就给完整的记录下来。

准备一个测试视频

世界杯结束了,足球的话题热度依旧很高,我们就下载一个B站的足球相关的视频吧,并给命名为football.mp4

加载视频

from docarray import Document

video_data = Document(uri='football.mp4')
video_data.load_uri_to_video_tensor()

image.png 这里会展示关键帧,默认情况下5秒一个关键帧,如果需要按照视频内容的关联程度处理,可以参考

image.png

查看一下视频是否正确地加载了吧

import numpy
Document(tensor=numpy.rot90(numpy.flip(video_data.tensor[1400], 1))).display()

image.png

构建DocumentArray

from docarray import Document, DocumentArray
from numpy import rot90

keyframe_indices = video_data.tags['keyframe_indices']
keyframes = DocumentArray()
for idx in range(0, len(keyframe_indices) - 1):
    keyframe_number = keyframe_indices[idx]
    keyframe_tensor = rot90(video_data.tensor[keyframe_number], -1)
    clip_indices = {
        'start': str(keyframe_number),
        'end': str(keyframe_indices[idx + 1]),
    }
    keyframe = Document(tags=clip_indices, tensor=keyframe_tensor)
    keyframes.append(keyframe)

CLIP Server/Client

可以登录Jina Cloud使用官方提供的Server,也可以自己本地启动一个CLIP Server

这里我们本地启用一个CLIP Server。

#!/bin/bash
JINA_LOG_LEVEL=DEBUG python3.7 -m clip_server torch-flow.yml
jtype: Flow
version: '1'
with:
  port: 51000
executors:
  - name: clip_t
    replicas: 4
    uses:
      jtype: CLIPEncoder
      with:
        num_worker_preprocess: 8
        jit: True
        device: cpu
      metas:
        py_modules:
          - clip_server.executors.clip_torch

创建client并进行连接

from docarray import Document, DocumentArray
from clip_client import Client


url = "127.0.0.1:51000"
client = Client(f'grpc://{url}')
client.profile()

image.png

通过文本进行搜索

CLIP Server使用的模型是ViT-B-32::openai,只能使用英文来进行搜索,效果还是蛮好的。

image.png

其他问题

尝试切换过CLIP使用的模型,M-CLIP原理上是支持中文检索的,但是效果不佳,经过跟Jina工程师在Slack上的沟通,他们未来会增加Chinese-CLIP,而且很有希望是在春节之前,数着日子不超过十天了,期待一下,到时候会第一时间进行尝试。