Auto-translation used

Image Search Engine Demo based on ResNet50 and Cosine Similarity Search

🎯 Goal: demonstrating the logic of an image search engine using ResNet50 for feature extraction and cosine similarity for searching similar images.

The idea behind of this Image Search Algorithm is to calculate similarities between a given image and all other images from the main dataset for returning the indices of the top k candidates.

For evaluating similarity score between 2 images, we need to convert all images into features ~ set of numeric values called embeddings / vectors. We use ResNet50 for extracting features from images, shortly about ResNet50.

ResNet50’s combination of computational efficiency, pre-training on a large-scale dataset (ImageNet), and versatility make it an excellent choice for feature extraction in a wide range of computer vision tasks. Architecture of ResNet50 (credits: WikiMedia)

Cosine similarity is used for evaluating the similarity score between two images because it effectively measures the alignment of their feature vectors, handles high-dimensional and sparse data well, and is computationally efficient. Formula of Cosine Similarity (credits: Wikipedia):  

Image Search function Code:

def search_similar_images(query_image_index, image_features, image_files, k=5):
    query_feature = image_features[query_image_index].reshape(1, -1)
    similarities = cosine_similarity(query_feature, image_features).flatten()
    top_k_indices = similarities.argsort()[-k-1:-1][::-1]  # ~ exclude the query image itself
    return top_k_indices

References:

1. https://www.tensorflow.org/api_docs/python/tf/keras/applications/resnet

2. https://en.wikipedia.org/wiki/Cosine_similarity

Kaggle Notebook: https://www.kaggle.com/code/armanzhalgasbayev/image-search-engine-demo-resnet-cossim