Deep Unsupervised Learning

CLIP Image Search

CLIPCosine similarityZero-shot
CLIP Image Search
Problem

Traditional image search needs labels or fine-tuning per task. CLIP maps images and text into a shared embedding space—can we build a semantic search engine over an entire validation split using only pre-trained weights, and how well does zero-shot classification work on fine-grained classes?

Dataset

Tiny-ImageNet validation set: 10,000 images across 200 classes (3×224×224 after preprocessing). Class names loaded from words.txt (WordNet synset descriptions). Images resized to 224×224 and tensorized. No training data used—CLIP ViT-B/32 runs in eval mode throughout.

Approach

Extract L2-normalized image embeddings for all 10,000 validation images in batches of 64. Build text embeddings for each class with the prompt template “a photo of a {class_name}” (using the first word of the WordNet description). At search time, rank images by cosine similarity (dot product of normalized vectors) to a text or image query embedding. Zero-shot classification assigns each image to the class with highest image–text similarity.

Training

No fine-tuning. One-time embedding extraction (~60 seconds for 10k images at ~2.6 it/s). Embeddings saved to tiny_imagenet_val_embeddings.pt for reuse. Top-K=5 for all retrieval queries.

My Work
  • Loaded openai/clip-vit-base-patch32 via HuggingFace CLIPModel and CLIPProcessor
  • Implemented batched extract_image_embeddings() with L2 normalization
  • Built load_wnid_to_words() to map class IDs to human-readable labels
  • Ran zero-shot classification via full image–text similarity matrix
  • Built search_text_to_image() for natural-language queries (dog, sports car, pasta, hiking, container ship)
  • Built search_image_to_image() using query images downloaded from Unsplash URLs
Evaluation
  • Zero-shot 200-way classification on all 10,000 Tiny-ImageNet validation images
  • Text-to-image top-5 retrieval for 5 queries: cute dog, red sports car, pasta, hiking, container ship
  • Image-to-image top-5 retrieval for 5 web query images (Unsplash URLs)
CLIP text-to-image search for a cute dog
Text query: "a photo of a cute dog" → top-5
CLIP text-to-image search for a red sports car
Text query: "a red sports car on the road" → top-5
CLIP text-to-image search for pasta
Text query: "a plate of pasta with sauce" → top-5
CLIP text-to-image search for hiking
Text query: "people hiking in the mountains" → top-5
CLIP text-to-image search for a container ship
Text query: "a large container ship at sea" → top-5
CLIP image-to-image search example 1
Image query 1 → top-5 similar images
CLIP image-to-image search example 2
Image query 2 → top-5 similar images
CLIP image-to-image search example 3
Image query 3 → top-5 similar images
CLIP image-to-image search example 4
Image query 4 → top-5 similar images
CLIP image-to-image search example 5
Image query 5 → top-5 similar images
Results
  • Zero-shot classification accuracy: 0.54% on Tiny-ImageNet val (200-way, fine-grained)
  • 10,000 image embeddings × 512 dimensions; 200 class text embeddings
  • Text-to-image and image-to-image top-5 retrieval demonstrated on 5 queries each
  • Embedding extraction: ~60s total for the full validation split
Takeaway

CLIP turns language and vision into shared geometry—search becomes nearest-neighbor in embedding space. Zero-shot accuracy is low on 200 fine-grained Tiny-ImageNet classes, but retrieval quality for broad natural-language queries is immediately useful. This reinforced how self-supervised multimodal pre-training enables flexible applications without task-specific training.

Work completed during Oxford summer coursework. This page shows only my own implementations and outputs; course materials are not redistributed.