43
loading...
This website collects cookies to deliver better user experience
gdown
to download and unzip the data from Google Drive.$ pip3 install gdown
$ gdown "https://drive.google.com/uc?id=1bg1RtUjeZlOfV2BiA2nf7sn5Jec9b-9I"
$ unzip -q image_dataset.zip
dataset
for the image dataset and query
for the query images.pip
in this tutorial. We also support installing Towhee via conda
as well as from source; check out this page for more information.$ pip3 install towhee
docker-compose
to install Milvus standalone. Before installing Milvus (see the official Milvus installation guide), make sure you have the necessary prerequisites.# download the latest docker-compose file
$ wget https://github.com/milvus-io/milvus/releases/download/v2.0.0-pre-ga/milvus-standalone-docker-compose.yml -O docker-compose.yml
# start the Milvus service
$ docker-compose up -d
# check the state of the containers
$ docker-compose ps
$ pip3 install pymilvus==2.0.0rc9
image-embedding
category will reveal all image embedding pipelines that Towhee offers. We also provide a summary of popular image embedding pipelines here.from towhee import pipeline
embedding_pipeline = pipeline('towhee/image-embedding-resnet50')
image-embedding
Towhee pipelines output an embedding vector given an image path.import numpy as np
import os
from pathlib import Path
dataset_path = './image_dataset/dataset/'
images = []
vectors = []
for img_path in Path(dataset_path).glob('*'):
vec = embedding_pipeline(str(img_path))
norm_vec = vec / np.linalg.norm(vec)
vectors.append(norm_vec.tolist())
images.append(str(img_path.resolve()))
import pymilvus as milvus
collection_name = 'reverse_image_search'
vec_dim = len(vectors[0])
# connect to local Milvus service
milvus.connections.connect(host='127.0.0.1', port=19530)
# create collection
id_field = milvus.FieldSchema(name="id", dtype=milvus.DataType.INT64, is_primary=True, auto_id=True)
vec_field = milvus.FieldSchema(name="vec", dtype=milvus.DataType.FLOAT_VECTOR, dim=vec_dim)
schema = milvus.CollectionSchema(fields=[id_field, vec_field])
collection = milvus.Collection(name=collection_name, schema=schema)
# insert data to Milvus
res = collection.insert([vectors])
collection.load()
img_dict = {}
# maintain mappings between primary keys and the original images for image retrieval
for i, key in enumerate(res.primary_keys):
img_dict[key] = images[i]
query_img_path = './image_dataset/query/'
query_images = []
query_vectors = []
top_k = 5
for img_path in Path(query_img_path).glob('*'):
vec = embedding_pipeline(str(img_path))
norm_vec = vec / np.linalg.norm(vec)
query_vectors.append(norm_vec.tolist())
query_images.append(str(img_path.resolve()))
query_results = collection.search(data=query_vectors, anns_field="vec", param={"metric_type": 'L2'}, limit=top_k)
import matplotlib.pyplot as plt
from PIL import Image
for i in range(len(query_results)):
results = query_results[i]
query_file = query_images[i]
result_files = [img_dict[result.id] for result in results]
distances = [result.distance for result in results]
fig_query, ax_query = plt.subplots(1,1, figsize=(5,5))
ax_query.imshow(Image.open(query_file))
ax_query.set_title("Searched Image\n")
ax_query.axis('off')
fig, ax = plt.subplots(1,len(result_files),figsize=(20,20))
for x in range(len(result_files)):
ax[x].imshow(Image.open(result_files[x]))
ax[x].set_title('dist: ' + str(distances[x])[0:5])
ax[x].axis('off')