2

OpenAI CLIP模型新尝试,有开发者在Colab上实现近200万图片的精准检索匹配

 3 years ago
source link: https://www.jiqizhixin.com/articles/2021-01-27-3
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

本月初,OpenAI 同时发布了两个连接文本与图像的神经网络:DALL·E 和 CLIP。其中,DALL·E 可以基于文本直接生成图像,CLIP 能够完成图像与文本类别的匹配。这两个模型发布之后,机器学习社区出现了一些复现 DALL·E 模型的尝试,如开发者 Phil Wang 的 GitHub 项目 DALLE-pytorch,短短二十余天就收获了 1.9k star。

DALLE-pytorch 项目地址:https://github.com/lucidrains/DALLE-pytorch

近日,又有开发者创建了一个利用 CLIP 模型搜索文本精准匹配图片的项目。该项目所有图片出自 Unsplash 数据集,大约有 200 万张,利用 CLIP 模型进行处理。项目既可以在给定的免费谷歌 Colab notebook 中运行,也可以在用户自己的机器上运行。

aiIriaR.png!mobile

项目地址:https://github.com/haltakov/natural-language-image-search#two-dogs-playing-in-the-snow

在具体实现上,项目作者在 Colab Notebook 上将所有图片处理成了预计算特征向量,然后找到与自然语言搜索查询(即输入的文本)最匹配的图片。

项目作者提供了一些效果展示图,如输入搜索词条「"Two dogs playing in the snow"」,就会出现以下几幅图片:

jeqYVru.png!mobile

摄自:Richard Burlton、Karl Anderson 和 Xuecheng Chen。

输入搜索词条「"The word love written on the wall"」,就会出现以下包含「love」的文本:

yEJfYjA.png!mobile

摄自:Genton Damian、Anna Rozwadowska 和 Jude Beck。

此外,你还可以选择搜索图片的数量,例如:通过修改「search_unslash(search_query, photo_features, photo_ids, 3)」,将该行中的 3 更改为想要的搜索结果数量。

如何实现?

谷歌 Colab notebook 上运行

在给定的 Colab 会话中进行首次搜索的步骤如下:

1. 首先需要进入 Colab 界面;

2. 登录谷歌账户,单击右上角的「S」按钮即可完成此操作。注意:注册谷歌账户会影响隐私,比如你的谷歌搜索历史记录会记录在谷歌帐户中;

3. 单击单元格中的某个位置(三角形除外),该行显示「search_query = "Two dogs playing in the snow"」;

4. 单击菜单「Runtime→Run before」,等待执行结束;

5. 找到读取(或最初读取)「search_query = "Two dogs playing in the snow"」这一行,将「 "Two dogs playing in the snow"」改变成你想要的查询。例如:「search_query = "A clock with gold-colored numbers on a black background"」;

6.(可选)查找读取(或最初读取)的行「search_unslash(search_query, photo_features, photo_ids, 3)」。将该行中的 3 更改为想要的搜索结果数量;

7. 单击最初读取「search_query = "Two dogs playing in the snow"」这一行左侧的三角形, 等待搜索结果。

Colab 界面地址:https://colab.research.google.com/github/haltakov/natural-language-image-search/blob/main/colab/unsplash-image-search.ipynb#scrollTo=xbym_cYJJH6v

在 Colab 会话中执行更多搜索,请参考上述步骤 5-7。在 Google Colab 会话后,可以选择注销谷歌帐户,因为登录谷歌帐户会影响隐私。

本机上运行

想要在本机上实现该功能,首先需要安装必要的依赖项,安装代码如下:

pip install -r requirements.txt

如果想要运行所有的代码,打开Jupyternotebooks,按照以下编号顺序以及说明依次操作:

01-setup-clip.ipynb:设置环境,检查并准备 CLIP 代码;

02-download-unsplash-dataset.ipynb:从 Unsplash 数据集下载图片;

03-process-unsplash-dataset.ipynb:使用 CLIP 处理数据集中的所有图片;

04-search-image-dataset.ipynb:使用自然语言查询在数据集中检索图片;

05-search-image-api.ipynb:使用 Unsplash Search API 检索图片,CLIP 过滤搜索结果。

注意:只有精简版的 Unsplash 数据集是公开可用的。如果你想使用完整版,你需要申请(免费)访问权限;使用 Unsplash Search API 搜索图像不需要访问 Unsplash 数据集,但可能会产生糟糕的结果。

项目作者

rMFVR3U.png!mobile

项目作者 Vladimir Haltakov 取得了慕尼黑工业大学的计算机视觉博士,主要研究语义分割的上下文。他的主要研究兴趣在于计算机视觉与机器学习,包括多类别图像分割、3D 重建、场景理解、光流、条件随机场、概率图模型等。

目前,他就职于宝马集团(BMW Group),主要从事自动驾驶汽车的高精准定位研究和工作。

个人主页:https://haltakov.net/

参考链接:https://www.reddit.com/r/MachineLearning/comments/l52qe6/p_use_natural_language_queries_to_search_2/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK