前面写了几篇记录,都是在记录解决了哪些基本问题。在经过一系列铺垫之后,终于又来到milvus bootcamp这个示例项目面前,继续摸索其如何使用。
此bootcamp中提供的milvus的docker版本过低,因此我又从其他地方找来最新docker版本,如下:
//docker-compose.yml
version: '3.5'
services:
etcd:
container_name: milvus-etcd
image: quay.io/coreos/etcd:v3.5.5
environment:
- ETCD_AUTO_COMPACTION_MODE=revision
- ETCD_AUTO_COMPACTION_RETENTION=1000
- ETCD_QUOTA_BACKEND_BYTES=4294967296
- ETCD_SNAPSHOT_COUNT=50000
volumes:
- ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/etcd:/etcd
command: etcd -advertise-client-urls=http://127.0.0.1:2379 -listen-client-urls http://0.0.0.0:2379 --data-dir /etcd
minio:
container_name: milvus-minio
image: minio/minio:RELEASE.2022-03-17T06-34-49Z
environment:
MINIO_ACCESS_KEY: minioadmin
MINIO_SECRET_KEY: minioadmin
volumes:
- ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/minio:/minio_data
command: minio server /minio_data
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
interval: 30s
timeout: 20s
retries: 3
standalone:
container_name: milvus-standalone
image: milvusdb/milvus:v2.2.8
command: ["milvus", "run", "standalone"]
environment:
ETCD_ENDPOINTS: etcd:2379
MINIO_ADDRESS: minio:9000
volumes:
- ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/milvus:/var/lib/milvus
ports:
- "19530:19530"
- "9091:9091"
depends_on:
- "etcd"
- "minio"
networks:
default:
name: milvus
代码运行过程中出现异常,从http://127.0.0.1:5000/docs界面中操作FAST API接口出现异常。通过跟踪出错堆栈,修改代码如下:
//bootcamp/solutions/image/reverse_image_search/quick_deploy/server/src/milvus_helpers.py
diff --git a/solutions/image/reverse_image_search/quick_deploy/server/src/milvus_helpers.py b/solutions/image/reverse_image_search/quick_deploy/server/src/milvus_helpers.py
index 4f6e379b..9480e805 100644
--- a/solutions/image/reverse_image_search/quick_deploy/server/src/milvus_helpers.py
+++ b/solutions/image/reverse_image_search/quick_deploy/server/src/milvus_helpers.py
@@ -60,6 +60,7 @@ def insert(self, collection_name, vectors):
# Batch insert vectors to milvus collection
try:
self.create_collection(collection_name)
+ self.create_index(collection_name)
data = [vectors]
self.set_collection(collection_name)
mr = self.collection.insert(data)
@@ -76,6 +77,7 @@ def create_index(self, collection_name):
# Create IVF_FLAT index on milvus collection
try:
self.set_collection(collection_name)
+ self.collection.release()
default_index = {"index_type": "IVF_SQ8", "metric_type": METRIC_TYPE, "params": {"nlist": 16384}}
status = self.collection.create_index(field_name="embedding", index_params=default_index)
if not status.code:
@@ -103,6 +105,7 @@ def search_vectors(self, collection_name, vectors, top_k):
# Search vector in milvus collection
try:
self.set_collection(collection_name)
+ self.collection.load()
search_params = {"metric_type": METRIC_TYPE, "params": {"nprobe": 16}}
# data = [vectors]
res = self.collection.search(vectors, anns_field="embedding", param=search_params, limit=top_k)
系统启动参考bootcamp中README进行即可,我在这里再简单描述一下。首先,启动docker-compose.yml中的容器,然后按照README中的描述启动一个mysql:5.7容器。最后使用python3 src/main.py启动服务端。服务端启动之后,就可以在浏览器中打开http://127.0.0.1:5000/docs,使用FAST API接口来操作来测试milvus服务。
此接口就是测试一下从界面中显示图片。系统假定图像素材目录在当前机器的/data目录下。在GET接口中首先点击"Try it out"之后,在img_path中输入一个图像路径,比如/data/dog_01.jpg,然后再点击Execute按钮,在下方就能显示出相应路径的图像。每次执行一个命令时,在python3 src/main.py所在的终端界面中,都要打印出服务端执行的日志。
此接口我的理解就是统计当前在/data/目录下图像的数量。
此接口是向milvus中添加/data目录下的图像素材。在Request body中填写如下参数:
{
"Table": "reverse_img_search",
"File": "/data"
}
其中,reverse_img_search是milvus概念中的collection name,同时也是mysql中的表名。File字段就是填写要加载的图像所在目录。
执行成功之后,可以登陆mysql中查看
sudo mysql -h127.00.1 -uroot -p
show databases;
use mysql;
show tables;
select * from reverse_img_search;
+--------------------+------------------------+
| milvus_id | image_path |
+--------------------+------------------------+
| 441440473896720459 | /data/aeroplane_01.jpg |
| 441440473896720460 | /data/aeroplane_02.jpg |
| 441440473896720461 | /data/dog_01.jpg |
| 441440473896720462 | /data/1.png |
+--------------------+------------------------+
4 rows in set (0.00 sec)
此接口即是以图搜图,在table_name中填写前面load时的collection name:reverse_img_search,在img中选择一个本地图像,界面中会显示搜索结果如下:
[
[
"/data/dog_01.jpg",
0
],
[
"/data/aeroplane_02.jpg",
0
],
[
"/data/1.png",
1.6162450313568115
],
[
"/data/aeroplane_01.jpg",
1.6162450313568115
]
]
即返回collection中每个图像与当前本地图像的相似度。
此接口来测试图像数量,也是在table_name中输入reverse_img_search,会在界面中返回数量。
这里只是测试服务端API接口,即直接对milvus服务进行本地操作,后续要从client端来测试milvus。