• Tutorials

Image Search with Milvus

Open In Colab

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.

!unzip -q -o


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.

$ pip install pymilvus --upgrade
$ pip install timm

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.

import torch
from PIL import Image
import timm
from sklearn.preprocessing import normalize
from import resolve_data_config
from import create_transform

class FeatureExtractor:
    def __init__(self, modelname):
        # Load the pre-trained model
        self.model = timm.create_model(
            modelname, pretrained=True, num_classes=0, global_pool="avg"

        # Get the input size required by the model
        self.input_size = self.model.default_cfg["input_size"]

        config = resolve_data_config({}, model=modelname)
        # Get the preprocessing function provided by TIMM for the model
        self.preprocess = create_transform(**config)

    def __call__(self, imagepath):
        # Preprocess the input image
        input_image ="RGB")  # Convert to RGB if needed
        input_image = self.preprocess(input_image)

        # Convert the image to a PyTorch tensor and add a batch dimension
        input_tensor = input_image.unsqueeze(0)

        # Perform inference
        with torch.no_grad():
            output = self.model(input_tensor)

        # Extract the feature vector
        feature_vector = output.squeeze().numpy()

        return normalize(feature_vector.reshape(1, -1), norm="l2").flatten()

Create a Milvus Collection

Then we need to create Milvus collection to store the image embeddings

from pymilvus import MilvusClient

# Set up a Milvus client
client = MilvusClient(uri="example.db")
# Create a collection in quick setup mode
if client.has_collection(collection_name="image_embeddings"):

As for the argument of MilvusClient:

  • Setting the uri as 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 your uri.
  • If you want to use Zilliz Cloud, the fully managed cloud service for Milvus, adjust the uri and token, 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.

import os

extractor = FeatureExtractor("resnet34")

root = "./train"
insert = True
if insert is True:
    for dirpath, foldername, filenames in os.walk(root):
        for filename in filenames:
            if filename.endswith(".JPEG"):
                filepath = dirpath + "/" + filename
                image_embedding = extractor(filepath)
                    {"vector": image_embedding, "filename": filepath},
from IPython.display import display

query_image = "./test/Afghan_hound/n02088094_4261.JPEG"

results =
    search_params={"metric_type": "COSINE"},
images = []
for result in results:
    for hit in result[:10]:
        filename = hit["entity"]["filename"]
        img =
        img = img.resize((150, 150))

width = 150 * 5
height = 150 * 2
concatenated_image ="RGB", (width, height))

for idx, img in enumerate(images):
    x = idx % 5
    y = idx // 5
    concatenated_image.paste(img, (x * 150, y * 150))
display(, 150)))

query result


Results 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.