Image Similarity Search
Using Deep Lake for image similarity search
How to Use Deep Lake as a Vector Store for Images
Deep Lake is a unique Vector Store because it supports storage of various data types including images, video, and audio. In this tutorial we show how to use Deep Lake to perform similarity search for images.
Creating the Vector Store
We will use ~5k images in the COCO Validation Dataset as a source of diverse images. First, let's download the data.
We must unzip the images and specify their parent folder below.
Next, let's define a ResNet18 PyTorch model to embed the images based on the output from the second-to-last layer. We use the torchvision
feature extractor to return the output of the avgpool
layer to the embedding
key, and we run on a GPU if available.
Let's define an embedding function that will embed a list of image filenames and return a list of embeddings. A transformation must be applied to the images so they can be fed into the model, including handling of grayscale images.
Now we can create the vector store for storing the data. The Vector Store does not have the default configuration with text
, embedding
, and metadata
tensors, so we use the tensor_params
input to define the structure of the Vector Store.
Finally, we can create a list of images from the source data and add it to the vector store.
We observe in the automatically printed summary that the Vector Store has tensors for the image
, their filename
, their embedding
, and an id
, with 5000 samples each. This summary is also available via vector_store.summary()
.
Similarity Search
Let's perform a similarity search on a reference image to find similar images in our Vector Store. First we download the image:
The similarity search will return data for the top k (defaults to 4) similar samples, including numpy arrays for the underlying images. We set exec_option
to compute_engine
, which evaluates the query using our performant Compute Engine. If the dataset is local, you should leave the default exec_option
("python"
).
The key-value pairs in the result contains the tensor as the key and a list of values for the data:
Since images can be quite large, and we may not want to return them as numpy arrays, so we use return_tensors
to specify that only the filename
and id
tensors should be returned:
Visualizing the Similarity Results
Instead of returning the results of the similarity search directly, we can use return_view = True
to get the Deep Lake dataset view, which is a lazy pointer to the underlying data that satisfies the similarity search (no data is retrieved locally).
We can then save the view and visualize it in the Deep Lake UI:
The images are all fairly similar to the reference image, so it looks like the similarity search worked well!
Congrats! You just used the Deep Lake VectorStore in for image similarity search! 🎉
Last updated