Image Search With Milvus
Image Search with Milvus
In this notebook, we will show you how to use Milvus to search for similar images in a dataset. We will use a subset of the ImageNet dataset, then search for an image of an Afghan hound to demonstrate this.
Dataset Preparation
First, we need to load the dataset and unextract it for further processing.
Prequisites
To run this notebook, you need to have the following dependencies installed:
- pymilvus>=2.4.2
- timm
- torch
- numpy
- sklearn
- pillow
To run Colab, we provide the handy commands to install the necessary dependencies.
If you are using Google Colab, to enable dependencies just installed, you may need to restart the runtime (click on the "Runtime" menu at the top of the screen, and select "Restart session" from the dropdown menu).
Define the Feature Extractor
Then, we need to define a feature extractor which extracts embedding from an image using timm's ResNet-34 model.
Create a Milvus Collection
Then we need to create Milvus collection to store the image embeddings
As for the argument of
MilvusClient:
- Setting the
urias a local file, e.g../milvus.db, is the most convenient method, as it automatically utilizes Milvus Lite to store all data in this file.- If you have large scale of data, you can set up a more performant Milvus server on docker or kubernetes. In this setup, please use the server uri, e.g.
http://localhost:19530, as youruri.- If you want to use Zilliz Cloud, the fully managed cloud service for Milvus, adjust the
uriandtoken, which correspond to the Public Endpoint and Api key in Zilliz Cloud.
Insert the Embeddings to Milvus
We will extract embeddings of each image using the ResNet34 model and insert images from the training set into Milvus.
'query'
'results'
We can see that most of the images are from the same category as the search image, which is the Afghan hound. This means that we found similar images to the search image.
Quick Deploy
To learn about how to start an online demo with this tutorial, please refer to the example application.