Sub topic 17
Using CLIP Embeddings in Other Systems
Assume you know basic Python, PyTorch tensors, cosine similarity, and the CLIP image and text encoders. This chapter focuses on using already-trained CLIP embeddings inside larger applications.
A CLIP embedding is a vector that represents an image or a text description. Matching images and descriptions should have similar vectors. Because both types use the same space, an image embedding can be compared with a text embedding without training a new classifier for every label.
A typical downstream pipeline
A production system usually separates embedding creation from search or prediction:
- Load an image or text item.
- Run the matching CLIP encoder.
- Normalize the resulting vector.
- Store the vector with an identifier and metadata.
- Compare a new vector with stored vectors.
- Return the nearest items or pass the vector to another model.
For a catalog search system, you might embed product images and descriptions when products are added. At query time, embed the user's text and retrieve products with the highest cosine similarity.
Retrieval and semantic search
Retrieval uses vector similarity rather than exact words or visual hashes. A text query such as “a red vehicle on a snowy road” can find an image even when its filename contains none of those words.
import torch
import torch.nn.functional as F
# Each row is one stored image embedding.
image_embeddings = torch.tensor([
[0.9, 0.1, 0.0],
[0.1, 0.8, 0.2],
[0.2, 0.1, 0.9],
], dtype=torch.float32)
query_embedding = torch.tensor([[0.8, 0.2, 0.1]])
image_embeddings = F.normalize(image_embeddings, dim=1)
query_embedding = F.normalize(query_embedding, dim=1)
scores = query_embedding @ image_embeddings.T
top_scores, top_indices = scores[0].topk(k=2)
print("matching item indexes:", top_indices.tolist())
print("similarities:", top_scores.tolist())For large collections, do not compare the query with every vector. Use an approximate nearest-neighbor index such as FAISS or a vector database. Store the model name, preprocessing settings, and embedding dimension with each index. Vectors from incompatible models should not share an index.
Classification and recommendation
A downstream classifier can use CLIP vectors as input. A linear probe is a small model, often a single linear layer, trained on labeled examples while the CLIP encoder remains frozen. This tests whether the representation already contains information useful for the task.
A recommendation system can combine CLIP similarity with business signals such as price, availability, user history, or popularity. Keep these signals separate until ranking. A high visual similarity does not guarantee that an item is available or appropriate for the user.
Failure modes in applications
CLIP similarity is a relevance signal, not a complete decision rule.
- Similar-looking images may differ in important details such as model, size, or condition.
- Text prompts can encode cultural and dataset biases.
- Fine-grained attributes, counting, negation, and spatial relations may be unreliable.
- A similarity score is not automatically a probability. Choose thresholds with validation data.
- Sensitive images and queries require access control, retention rules, and careful logging.
Use a held-out evaluation set that resembles real traffic. Measure retrieval recall at a chosen cutoff, classification accuracy, and failures across important groups. Re-evaluate after changing the model, prompts, preprocessing, or index.
Exercise
Why should a vector index record the CLIP model version and preprocessing settings?
Show answer
Those choices determine the embedding space. Vectors produced by different configurations may not be comparable, so mixing them can produce incorrect search results.