Sources indicate that in this post, you'll learn to build an image similarity system with πŸ€— Transformers. Finding out the similarity between a query image and potential candidates is an vital use case for information retrieval systems, such as reverse image search, for example. All the system is trying to answer is that, given a query image and a set of candidate images, which images are the most similar to the query image.

We'll leverage the πŸ€— datasets library as it seamlessly supports parallel processing which will come in handy when building this system.

Although the post uses a ViT-based model (nateraw/vit-base-beans) and a particular dataset (Beans), it can be extended to use other models supporting vision modality and other image datasets. Some notable models you could try:.

Also, the approach presented in the post can potentially be extended to other modalities as well.

To study the fully working image-similarity system, you can refer to the Colab Notebook linked at the beginning. (Related: How to Start a Side Business While Working Full-Time...)

Recent findings suggest that to build this system, we first need to define how we want to compute the similarity between two images. One widely popular practice is to compute dense representations (embeddings) of the given images and then use the cosine similarity metric to determine how similar the two images are. (Related: Hawaiian Interisland Awards β€” Hop Between Islands...)

For this post, we'll use β€œembeddings” to represent images in vector space. This gives us a nice way to meaningfully compress the high-dimensional pixel space of images (224 x 224 x 3, for example) to something much lower dimensional (768, for example). The primary advantage of doing this is the reduced computation time in the subsequent steps.

According to recent reports, to compute the embeddings from the images, we'll use a vision model that has some understanding of how to represent the input images in the vector space. This type of model is also commonly referred to as image encoder.

For loading the model, we leverage the AutoModel class. It provides an interface for us to load any compatible model checkpoint from the Hugging Face Hub. Alongside the model, we also load the processor associated with the model for data preprocessing.

In this case, the checkpoint was obtained by fine-tuning a Vision Transformer based model on the beans dataset.

Some questions that might arise here:.

Q1: Why did we not use AutoModelForImageClassification?

This is because we want to obtain dense representations of the images and not discrete categories, which are what AutoModelForImageClassification would have provided.

Q2: Why this checkpoint in particular?

As mentioned earlier, we're using a specific dataset to build the system. So, instead of using a generalist model (like the ones trained on the ImageNet-1k dataset, for example), it's better to use a model that has been fine-tuned on the dataset being used. That way, the underlying model better understands the input images.

Note that you can also use a checkpoint that was obtained through self-supervised pre-training. The checkpoint doesn't necessarily have to come from supervised learning. In fact, if pre-trained well, self-supervised models can yield impressive retrieval performance.

Now that we have a model for computing the embeddings, we need some candidate images to query against.

Industry observers note that in some time, we'll be building hash tables mapping the candidate images to hashes. During the query time, we'll use these hash tables. We'll talk more about hash tables in the respective section but for now, to have a set of candidate images, we will use the train split of the beans dataset.

This is how a single sample from the training split looks like:.

The dataset has three features:.

To demonstrate the image similarity system, we'll use 100 samples from the candidate image dataset to keep the overall runtime short.

Below, you can find a pictorial overview of the process underlying fetching similar images.

Breaking down the above figure a bit, we have:.

We can write a simple utility and map() it to our dataset of candidate images to compute the embeddings efficiently.

And we can map extract_embeddings() like so:.

Next, for convenience, we create a list containing the identifiers of the candidate images.

According to recent reports, we'll use the matrix of the embeddings of all the candidate images for computing the similarity scores with a query image. We have already computed the candidate image embeddings. In the next cell, we just gather them together in a matrix.

It has been revealed that we'll use cosine similarity to compute the similarity score in between two embedding vectors. We'll then use it to fetch similar candidate samples given a query sample.

In a recent development, given all the utilities, we're equipped to do a similarity search. Let's have a query image from the test split of the beans dataset:.

Seems like our system got the right set of similar images. When visualized, we'd get:.

We now have a working image similarity system. But in reality, you'll be dealing with a lot more candidate images. Taking that into consideration, our current procedure has got multiple drawbacks:.

In a recent development, if we can somehow reduce the dimensionality of the embeddings without disturbing their meaning, we can still maintain a good trade-off between speed and retrieval quality. The accompanying Colab Notebook of this post implements and demonstrates utilities for achieving this with random projection and locality-sensitive hashing.

πŸ€— Datasets offers direct integrations with FAISS which further simplifies the process of building similarity systems. Let's say you've already extracted the embeddings of the candidate images (the beans dataset) and stored them inside a feature called embeddings. You can now easily use the add_faiss_index() of the dataset to build a dense index:.

Once the index is built, dataset_with_embeddings can be used to retrieve the nearest examples given query embeddings with get_nearest_examples():.

In a recent development, the method returns scores and corresponding candidate examples. To know more, you can check out the official documentation and this notebook.

Finally, you can try out the following Space that builds a mini image similarity application:.

In this post, we ran through a quickstart for building image similarity systems. If you found this post interesting, we highly recommend building on top of the concepts we discussed here so you can get more comfortable with the inner workings.

Still looking to learn more? Here are some additional resources that might be useful for you:.

β†’ Read original article