在 eCommerce 里的应用中,我们可以对图像来进行搜索从而达到更好的应用体验。如果你之前阅读过我的文章 “Elasticsearch:如何使用 Elasticsearch 和 Python 构建面部识别系统”,可能对这个并不陌生。我们可以通过对图片的处理,把它变成向量,然后我们再进行向量搜索,从而达到搜索的目的。
在今天的 demo 中,我们来展示如何使用 Elasticsearch 来搜素图片。展示的代码可以在 GitHub - liu-xiao-guo/flask-elastic-nlp 找到。我们可以使用如下的命令来下载代码:
git clone https://github.com/liu-xiao-guo/flask-elastic-nlp
前提条件
你首先需要安装 Elasticsearch v8.3.0+ 及 Python v3.9+。你需要安装如下的5个 NLP 的模型:
- dslim/bert-base-ner
- sentence-transformers/clip-ViT-B-32-multilingual-v1
- distilbert-base-uncased-finetuned-sst-2-english
- bert-base-uncased
- sentence-transformers/msmarco-MiniLM-L-12-v3
- deepset/tinyroberta-squad2
Elasticsearch 资源
要并行运行所有模型,你将需要约 21GB 的内存,因为模型已加载到内存中。
如果你的计算机没有足够的内存,那么你可以配置更少的内存,并且始终只同时运行 1 或 2 个模型,具体取决于您有多少可用内存。 要更改 docker-compose 的值,请转到 es-docker/.env 文件并更改 MEM_LIMIT。这个非常重要。如果你的电脑没有足够的内存,而你配置 21G,极有可能你的 docker 运行不起来。我们需要调整 .env 文件里的内存大小。你如果是在 macOS 上,你可以调整如下的内存配置:
安装 Elastic Stack
我们首先来安装 Elasticsearch 及 Kibana。我们进入到 es-docker 目录中:
1. $ pwd
2. /Users/liuxg/python/flask-elastic-nlp
3. $ cd es-docker/
4. $ ls -al
5. total 24
6. drwxr-xr-x 4 liuxg staff 128 Aug 23 14:42 .
7. drwxr-xr-x 18 liuxg staff 576 Aug 23 10:24 ..
8. -rw-r--r-- 1 liuxg staff 733 Aug 23 15:43 .env
9. -rw-r--r-- 1 liuxg staff 4599 Aug 23 14:52 docker-compose.yml
10. $ docker-compose up
在运行上面的命令之前,我们可以查看当前目录下的 .env 文件的配置。针对我的情况,我修改了最新的 Elastic Stack 版本为8.3.3。执行上面的命令后,它会自动下载 Elasticsearch 及 Kibana 的镜像,并启动 docker;
在这期间,如果你的安装挂掉了,极有可能是你的内存不够而造成的。你需要修改当前目录下的 .env 文章中的这个项:
MEM_LIMIT=21474836480
如果我的安装成功的话,我们可以使用如下的命令来查看:
curl -k -u elastic:changeme https://localhost:9200
`
1. $ curl -k -u elastic:changeme https://localhost:9200
2. {
3. "name" : "es01",
4. "cluster_name" : "elastic-nlp-8.3.3",
5. "cluster_uuid" : "RWAp5DntThedGCsN0Ae3Pw",
6. "version" : {
7. "number" : "8.3.3",
8. "build_flavor" : "default",
9. "build_type" : "docker",
10. "build_hash" : "801fed82df74dbe537f89b71b098ccaff88d2c56",
11. "build_date" : "2022-07-23T19:30:09.227964828Z",
12. "build_snapshot" : false,
13. "lucene_version" : "9.2.0",
14. "minimum_wire_compatibility_version" : "7.17.0",
15. "minimum_index_compatibility_version" : "7.0.0"
16. },
17. "tagline" : "You Know, for Search"
18. }
`
如果你看到上面的输出,则标明你的 Elasticsearch 的安装是成功的。我们也可以打开地址 http://localhost:5601 来登录 Kibana:
我们可以通过如下的命令来查看运行中的 docker 容器:
docker ps
1. $ docker ps
2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3. a2393f9bd322 docker.elastic.co/kibana/kibana:8.3.3 "/bin/tini -- /usr/l…" 13 minutes ago Up 13 minutes (healthy) 0.0.0.0:5601->5601/tcp elastic-nlp_kibana_1
4. 4ba6f28c6492 docker.elastic.co/elasticsearch/elasticsearch:8.3.3 "/bin/tini -- /usr/l…" 13 minutes ago Up 13 minutes (healthy) 0.0.0.0:9200->9200/tcp, 9300/tcp elastic-nlp_es01_1
从上面的输出中,我们可以看出来有两个容器在运行:一个是 Elasticsearch 而另外一个是 Kibana。
配资 Python env
为了避免我们的 Python 安装和系统的安装混在一起,我们执行如下的命令:
1. $ cd flask-elastic-nlp
2. $ python3 -m venv .venv
3. $ source .venv/bin/activate
4. $ pip install -r requirements.txt
在安装 requirements.txt 中的 Python 包时,我们可能会遇到有些安装包不存在,这个主要看你是从哪里进行安装的。我对 requirements.txt 做了一点修改。它最终的内容是这样的:
requirements.txt
`
1. flask==2.0.2
2. wtforms==3.0.1
3. flask-wtf==1.0.1
4. python-dotenv==0.19.2
5. Werkzeug~=2.0.3
6. sentence-transformers~=2.2.0
7. ftfy~=6.1.1
8. # image-embeddings
9. pandas~=1.3.5
10. elasticsearch~=8.3.3
11. Pillow~=9.0.1
12. tqdm~=4.62.3
13. # eland
14. eland~=8.2.0
15. torch~=1.11.0
16. torchvision~=0.13.1
17. transformers~=4.19.2
`
这样我们的 Python 环境就配置好了。
上传 NLP 模型
让我们将模型加载到应用程序中。 我们使用 eland python 客户端来加载模型。 有关更多详细信息,请遵循文档。我们也可以参考文章 “Elasticsearch:使用向量搜索来查询及比较文字 - NLP text embedding” 来上传模型。在上面安装 Python 的过程中,我们已经安装好 eland。我们可以直接使用如下的命令来上传模型:
1. # wait until each model is loaded and started. If you do not have enough memory, you will see errors sometimes confusing
2. $ eland_import_hub_model --url https://elastic:changeme@localhost:9200 --hub-model-id dslim/bert-base-NER --task-type ner --start --insecure
3. $ eland_import_hub_model --url https://elastic:changeme@127.0.0.1:9200 --hub-model-id sentence-transformers/clip-ViT-B-32-multilingual-v1 --task-type text_embedding --start --insecure
4. $ eland_import_hub_model --url https://elastic:changeme@127.0.0.1:9200 --hub-model-id distilbert-base-uncased-finetuned-sst-2-english --task-type text_classification --start --insecure
5. $ eland_import_hub_model --url https://elastic:changeme@127.0.0.1:9200 --hub-model-id bert-base-uncased --task-type fill_mask --start --insecure
6. $ eland_import_hub_model --url https://elastic:changeme@127.0.0.1:9200 --hub-model-id sentence-transformers/msmarco-MiniLM-L-12-v3 --task-type text_embedding --start --insecure
7. $ eland_import_hub_model --url https://elastic:changeme@127.0.0.1:9200 --hub-model-id deepset/tinyroberta-squad2 --task-type question_answering --start --insecure
我们在项目的根目录中分别打入上面的命令:
我们回到 Kibana 的界面:
我们按照同样的方法把剩下的4个模型都上传到 Elasticsearch 中:
这样我们最终看到所有的模型都被上传了:
如果你在屏幕中看到缺少某些模型并看到一条消息。 需要 ML 作业和经过训练的模型同步,继续并单击链接以同步模型。
导入数据索引
我们还需要在烧瓶应用程序中使用的数据索引。 在此过程中,脚本还将从 Unsplash 下载数据集。
确保设置了 Python 环境。
1. $ cd embeddings
2. $ python3 build-datasets.py --es_host "https://127.0.0.1:9200" --es_user "elastic" --es_password "changeme" --no-verify_certs --delete_existing
等运行完上面的命令后,我们会发现:
这里的 blogs 也就是我们在另外一篇文章 “Elasticsearch:使用向量搜索来查询及比较文字 - NLP text embedding” 中使用到的索引。 我们可以通过如下的命令来查看新生成的索引:
GET _cat/indices
上面的命令返回:
1. yellow open image-embeddings sO3-86hmQ_e9fg1uaYw9Xw 1 1 19833 0 197.4mb 197.4mb
2. yellow open blogs JiZuoqCFQkapgMgpeM0D-Q 1 1 326 0 3.8mb 3.8mb
3. yellow open les-miserable-embedded ZExnlOVnShC5P_2wdIdvhw 1 1 14021 0 107mb 107mb
从上面的输出中,我们可以看到有三个索引被生成。我们可以通过如下的命令来查看每个索引的 mapping:
GET image-embeddings/_mapping
`
1. {
2. "image-embeddings": {
3. "mappings": {
4. "properties": {
5. "ai_description": {
6. "type": "text"
7. },
8. "exif_camera_make": {
9. "type": "keyword"
10. },
11. "exif_camera_model": {
12. "type": "keyword"
13. },
14. "exif_iso": {
15. "type": "integer"
16. },
17. "image_embedding": {
18. "type": "dense_vector",
19. "dims": 512,
20. "index": true,
21. "similarity": "cosine"
22. },
23. "photo_description": {
24. "type": "text"
25. },
26. "photo_id": {
27. "type": "keyword"
28. },
29. "photo_image_url": {
30. "type": "keyword"
31. },
32. "photo_url": {
33. "type": "keyword"
34. },
35. "photographer_first_name": {
36. "type": "keyword"
37. },
38. "photographer_last_name": {
39. "type": "keyword"
40. },
41. "photographer_username": {
42. "type": "keyword"
43. }
44. }
45. }
46. }
47. }
`
从上面,我们可以看出来 image_embedding 字段是一个 512 维度的 dense_vector 字段。它可以进行向量搜索。
运行 Flash App
确保设置了 Python 环境。
1. # In the main directory
2. # !!! configure file `.env` with values pointing to your Elasticsearch cluster
3. $ flask run --port=5001
4. # Access URL `127.0.0.1:5001`
我们在电脑的浏览器中打开 http://localhost:5001:
当我们输入 “A mountain covered in snow” 时,我们可以看到上面显示的画面。我们也可以使用上面的另外一个例子 “Valentine day flowers”,我们可以看到如下的搜索结果:
我打入了一个中文的句子 “我喜欢北京”:
我们也可以搜索一个相同的图片,比如我们搜索如下的图片:
我们进行搜索:
从上面,我们可以看到被搜索出来的图片。