Visual Search System
Return visually similar images for an uploaded or cropped photo using learned embeddings and approximate nearest neighbour search.
- Read time
- 10 min
- Diagrams
- 2 architecture
- Key metric
- Recall@k
- Difficulty
- Medium

Step 01Problem Statement
Can you describe the problem a Visual Search System aims to solve?
A Visual Search System is designed to help users discover images that are visually similar to a given query image. For instance, if a user uploads a picture of a specific type of furniture, the system should return other images of similar furniture. The core challenge is to effectively identify and retrieve relevant images from a vast database based purely on visual characteristics, without relying on textual metadata. The system should also support searching based on a cropped portion of an image and present results ranked by visual similarity.
Step 02Identify Metrics
What metrics would you use to evaluate the performance and success of a Visual Search System?
Evaluating a visual search system requires both offline and online metrics.
Offline Metrics (Model Evaluation):
- Recall@k: This measures the proportion of relevant items (similar images) that are retrieved within the top
kresults. It's crucial for ensuring that the system doesn't miss highly similar items. For example, if there are 10 truly similar images and the system returns 7 of them in the top 100, Recall@100 would be 0.7. - Precision@k: This measures the proportion of retrieved items within the top
kresults that are actually relevant. High precision means fewer irrelevant results are shown. For example, if the system returns 100 images and 80 are truly similar, Precision@100 would be 0.8. - Mean Average Precision (MAP): A popular metric for ranking problems, MAP considers both precision and recall across different recall levels. It's the mean of the average precision scores for each query.
- Normalized Discounted Cumulative Gain (NDCG): This metric evaluates the ranking quality, considering the position of relevant items. Highly relevant items ranked higher contribute more to the score.
- Embedding Space Metrics: Metrics like intra-class distance (distance between embeddings of similar images) and inter-class distance (distance between embeddings of dissimilar images) can be used to evaluate the quality of the learned embeddings. Ideally, intra-class distances should be small, and inter-class distances should be large.
Online Metrics (System Performance & User Experience):
- Click-Through Rate (CTR): The percentage of times users click on a recommended image after a search. A higher CTR indicates better relevance.
- Engagement Rate: Measures user interactions beyond clicks, such as saves, shares, or time spent viewing results.
- Latency: The time taken for the system to return search results. As specified, this should be very low (e.g., less than 200ms).
- User Satisfaction: Can be measured through A/B testing, surveys, or implicit feedback signals.
Step 03Train and Evaluate Model
How would you approach training and evaluating the machine learning model for visual search, from data preparation to model selection?
The core of a visual search system is representation learning, where images are transformed into numerical vectors called embeddings. Similar images should have embeddings that are close to each other in a high-dimensional space.
Data Preparation
-
Data Collection: Gather a large dataset of images. This includes user-uploaded images and potentially publicly available image datasets. Crucially, we need labels indicating visual similarity. This can be derived from user interactions (e.g., users clicking on similar images, saving them together) or manual annotation.
-
Data Engineering:
- Image Data: Store images with metadata such as
ID,Owner ID,Upload time, andManual tags. An example schema is provided in Table 1.
- Image Data: Store images with metadata such as
| ID | Owner ID | Upload time | Manual tags |
|---|---|---|---|
| 1 | 8 | 1658451341 | Zebra |
| 2 | 5 | 1658451841 | Pasta, Food, Kitchen |
| 3 | 19 | 1658821820 | Children, Family, Party |
Table 1: Image Data Schema
- User Data: Store user demographic attributes like
ID,Username,Age,Gender,City,Country,Email. An example schema is provided in Table 2.
| ID | Username | Age | Gender | City | Country | |
|---|---|---|---|---|---|---|
| 1 | johnduo | 26 | M | San Jose | USA | john@gmail.com |
| 2 | hs2008 | 49 | M | Paris | France | hsieh@gmail.com |
| 3 | alexish | 16 | F | Rio | Brazil | alexh@yahoo.com |
Table 2: User Data Schema
- User-Image Interactions: Record user interactions such as impressions and clicks, including
User ID,Query image ID,Displayed image ID,Position in the displayed list,Interaction type,Location, andTimestamp. This data is vital for generating implicit feedback and creating training labels for similarity. An example schema is provided in Table 3.
| User ID | Query image ID | Displayed image ID | Position in the displayed list | Interaction type | Location (lat, long) | Timestamp |
|---|---|---|---|---|---|---|
| 8 | 2 | 6 | 1 | Click | 38.8951 -77.0364 | 1658450539 |
| 6 | 3 | 9 | 2 | Click | 38.8951 -77.0364 | 1658451341 |
| 91 | 5 | 1 | 2 | Impression | 41.9241 -89.0389 | 1658451365 |
Table 3: User-Image Interaction Data Schema
-
Feature Engineering (Image Preprocessing):
- Resizing: All images must be resized to a fixed dimension (e.g., 224x224 pixels) to serve as input for convolutional neural networks (CNNs).
- Scaling: Pixel values are typically scaled to a range like [0, 1] by dividing by 255.
- Normalization: Z-score normalization (mean 0, standard deviation 1) can further standardize pixel values, which often helps model convergence.
- Data Augmentation: Techniques like random cropping, rotation, flipping, and color jittering can be applied to increase the diversity of the training data and improve model generalization.
Model Training
-
Model Architecture: A common approach is to use a deep Convolutional Neural Network (CNN) as an encoder to generate image embeddings. Pre-trained models like ResNet, Inception, or EfficientNet are excellent starting points due to their strong feature extraction capabilities from large datasets like ImageNet.
-
Loss Function:
- Triplet Loss: This is a widely used loss function for learning embeddings. It aims to ensure that an anchor image's embedding is closer to a positive sample's embedding (visually similar) than to a negative sample's embedding (visually dissimilar) by a certain margin. The loss is defined as:
L = max(0, d(a, p) - d(a, n) + margin), wheredis a distance metric (e.g., Euclidean distance),ais the anchor,pis the positive, andnis the negative sample. - Contrastive Loss: Similar to triplet loss, it pulls similar samples closer and pushes dissimilar samples apart.
- ArcFace/CosFace Loss: These are often used in facial recognition but can be adapted for general visual similarity tasks, focusing on maximizing angular or cosine similarity between embeddings of similar images.
- Triplet Loss: This is a widely used loss function for learning embeddings. It aims to ensure that an anchor image's embedding is closer to a positive sample's embedding (visually similar) than to a negative sample's embedding (visually dissimilar) by a certain margin. The loss is defined as:
-
Training Data Generation (Pairs/Triplets): For triplet loss, triplets (anchor, positive, negative) need to be generated. Positive pairs can be derived from user interaction data (e.g., images clicked after searching for a query image, or images saved together). Negative pairs can be randomly sampled or, more effectively, hard negative mining can be employed to select negative samples that are visually similar to the anchor but semantically different, forcing the model to learn finer distinctions.
-
Optimization: Use optimizers like Adam or SGD with momentum. Learning rate schedules (e.g., cosine decay, step decay) and regularization techniques (e.g., dropout, weight decay) are important for stable training.
Model Evaluation
- Offline Evaluation: After training, evaluate the model on a held-out test set using the offline metrics mentioned above (Recall@k, Precision@k, MAP, NDCG). This helps assess the model's ability to generate good embeddings.
- A/B Testing: Deploy the new model to a small subset of users and compare its online metrics (CTR, engagement) against the existing system. This is the most reliable way to determine real-world impact.
- Human Evaluation: For subjective tasks like visual similarity, human evaluators can provide valuable feedback on the quality and relevance of search results.
Step 04Design the System
Walk me through the high-level and detailed system design for a Visual Search System, including all major components.
A Visual Search System typically consists of an offline processing pipeline for embedding generation and an online serving pipeline for real-time search.
High-Level Architecture
At a high level, the system can be divided into two main parts:
- Offline Indexing Pipeline: This pipeline processes all images in the database, extracts their features using the trained ML model, and stores these features (embeddings) in a searchable index.
- Online Query Pipeline: When a user submits a query image, this pipeline extracts its features, queries the index for similar embeddings, retrieves the corresponding images, and returns them to the user.
Loading diagram…
Detailed System Design
Let's break down the components in more detail:
-
Image Storage:
- Raw Image Storage: A distributed, highly available storage system (e.g., S3, HDFS) to store raw image files. Images are typically stored in their original format and resolution.
- Metadata Database: A relational database (e.g., PostgreSQL, MySQL) or NoSQL database (e.g., Cassandra) to store image metadata (ID, owner, upload time, tags) and user data.
-
Offline Indexing Pipeline:
- Image Ingestion: A mechanism (e.g., Kafka, SQS) to handle new image uploads. When a new image is uploaded, it's added to a queue for processing.
- Feature Extraction Service: A distributed processing system (e.g., Spark, Flink) that reads new images from the queue, preprocesses them (resizing, normalization), and feeds them to the Image Embedding Model.
- Image Embedding Model: The trained deep learning model (CNN) that takes an image as input and outputs its fixed-size embedding vector. This model is typically deployed on GPUs for efficient inference.
- Embedding Storage: A specialized database for storing high-dimensional vectors. This could be a vector database (e.g., Milvus, Pinecone) or a distributed key-value store (e.g., Cassandra) with custom indexing.
- Approximate Nearest Neighbor (ANN) Index: For a large number of embeddings (billions), exact nearest neighbor search is too slow. An ANN index (e.g., Faiss, Annoy, HNSW) is built on top of the embeddings to allow for fast, approximate similarity searches. This index is periodically updated or rebuilt as new embeddings are added.
-
Online Query Pipeline:
- API Gateway/Load Balancer: Handles incoming user requests for visual search.
- Query Preprocessing: Takes the user's query image (or cropped region), preprocesses it (resizing, normalization) to match the input requirements of the embedding model.
- Query Embedding Service: Uses the same Image Embedding Model (or a lightweight version) to generate an embedding for the query image. This service needs to be highly optimized for low latency.
- ANN Search Service: Receives the query embedding and performs a fast lookup in the ANN index to find the
kmost similar image embeddings. This service is critical for search speed. - Result Retrieval & Ranking: Retrieves the full image metadata (from the Metadata Database) for the
kcandidate images returned by the ANN search. It then performs a final re-ranking based on exact distance calculations (if ANN was approximate) and potentially other factors (e.g., freshness, popularity, content moderation scores). - Image Delivery Service: Serves the actual image files (e.g., from a CDN) to the user's device.
Loading diagram…
Step 05Scale the Design
How would you ensure this Visual Search System can scale to handle billions of images and millions of users?
Scaling a visual search system involves optimizing both the offline indexing and online query pipelines.
-
Distributed Storage: Use distributed file systems (HDFS) or object storage (S3) for raw images to handle petabytes of data. Metadata databases should be sharded or use NoSQL solutions like Cassandra for horizontal scalability.
-
Distributed Processing for Feature Extraction: The Feature Extraction Service must be highly parallelized. Technologies like Apache Spark or Flink can process large batches of images across a cluster of machines. GPUs are essential for accelerating the Image Embedding Model inference, and these can be managed in a distributed fashion (e.g., Kubernetes with GPU nodes).
-
Scalable Embedding Storage and ANN Index:
- Vector Databases: Specialized vector databases (Milvus, Pinecone, Weaviate) are built for storing and querying billions of high-dimensional vectors efficiently. They often handle sharding and indexing automatically.
- Distributed ANN Libraries: Libraries like Faiss or Annoy can be used to build and query ANN indexes across multiple machines. The index itself can be sharded, with each shard responsible for a subset of the embedding space.
- Hierarchical Indexing: For extremely large datasets, a multi-level indexing strategy can be employed, where a coarse-grained index quickly narrows down the search space, followed by a finer-grained search within the candidate subset.
-
Low-Latency Query Embedding Service: Deploy the Query Embedding Service on dedicated inference servers with GPUs or specialized AI accelerators (TPUs) to ensure sub-millisecond latency for embedding generation. Model quantization or pruning can reduce model size and improve inference speed.
-
Caching: Implement caching at various levels:
- Query Result Cache: Cache results for popular or recent queries.
- Embedding Cache: Cache embeddings of frequently queried images.
- Metadata Cache: Cache metadata for frequently accessed images.
-
Load Balancing and Microservices: Use load balancers to distribute traffic across multiple instances of each service (API Gateway, Query Embedding Service, ANN Search Service, Result Retrieval). Decouple components into microservices to allow independent scaling and deployment.
-
Asynchronous Processing: For non-critical operations (e.g., updating the ANN index, logging interactions), use message queues to decouple producers and consumers, preventing bottlenecks in the critical path.
-
Monitoring and Alerting: Implement robust monitoring for all system components, including data pipelines, model performance, service latency, and error rates. Set up automated alerts to quickly detect and respond to issues.
-
MLOps and A/B Testing: Establish robust MLOps practices for continuous integration, continuous delivery, and continuous training of models. This ensures that models are always up-to-date with the latest data and can adapt to evolving user behavior and image content. A/B testing is crucial for safely experimenting with new models and features and measuring their impact on key online metrics.
Finished reading?
Track your progress across all system design questions.