Image Similarity with Hugging Face Datasets and Transformers
An image similarity system can be built by embedding images with a fine-tuned Vision Transformer (CLS token from the last hidden state) and ranking candidates by cosine similarity against a query embedding. The walkthrough uses π€ Transformers' AutoModel and π€ Datasets' batched map() to embed a 100-image subset of the Beans dataset, then retrieves top-k matches from the test split. For scale, brute-force search over 768-d embeddings becomes costly in memory and compute, so dimensionality reduction via random projection and locality-sensitive hashing, or a FAISS index through Datasets' add_faiss_index(), is recommended.
Related posts
- Image search with π€ datasets
Hugging Face mediumAdeptA text-to-image search over 10,000 British Library book embellishments can be built by loading the images with the π€ datasets ImageFolder loader and embedding them with the CLIP ViT-B/32 model via sentence_transformers. The embeddings are indexed with datasets' built-in add_faiss_index, and text prompts are queried with get_nearest_examples. push_to_hub stores intermediate datasets so work can move between machines, such as a laptop and a Colab GPU. Results for prompts like "a steam engine" or "a musical instrument" are not always correct but are usually reasonable, which is enough to search images by semantic content.
- Getting Started With Embeddings
Hugging Face mediumAdeptEmbeddings turn text or images into fixed-length vectors that capture meaning, so semantic similarity becomes a distance calculation. A walkthrough builds a small FAQ matcher: it embeds 13 Medicare FAQs into 384-dimensional vectors with sentence-transformers/all-MiniLM-L6-v2 through the Hugging Face Inference API, hosts the vectors as a CSV dataset on the Hub, then embeds a user query and ranks the top 5 FAQs by cosine similarity with util.semantic_search. Practical notes include retrying the first API call while the model loads, and using FAISS via add_faiss_index as an alternative search path.
- Implementing semantic image search with Amazon Titan and Supabase Vector
Supabase mediumAdeptSemantic image search can be built in Python by generating 1024-dimensional embeddings for images with the Amazon Titan multimodal embedding model (amazon.titan-embed-image-v1) via Amazon Bedrock, then storing and querying them in Supabase Vector (Postgres with pgvector) using the vecs client. The walkthrough covers Poetry project setup, base64-encoding images for the Bedrock API, upserting embeddings with metadata into a vecs collection and creating an index. A text query is embedded with the same model and matched against image vectors with a metadata filter, and the top result is displayed with matplotlib. The same approach supports reverse image search.
- Image replacement in Canva designs using reverse image search
Canva Engineering mediumAdeptCanva built a reverse image search system to replace IP-restricted images in templates, after ruling out its recommendation engine, perceptual hashing, metadata text search and AI-generated images. Five embedding models (DINOv2, CLIP, ViTMAE, DreamSim, CaiT) plus a GPT-4o-description-to-CLIP approach were compared qualitatively on 50,000 images using Faiss and a subject/color/composition similarity hierarchy, and DINOv2 won. Embeddings for 150M+ images live in a third-party external vector database instead of in memory, which keeps up with library changes and supports aspect-ratio filtering. Results are strong for photos but weak for text, symbols and cartoons, yet pilots showed designers replacing images 4.5x faster.
- Efficient Visual Representation Learning And Evaluation
Etsy Code as Craft hardMasterEtsy compares EfficientNetB0 and the EfficientFormer-l3 hybrid vision transformer as backbones for image embeddings that power visual search and visually similar recommendations, fine-tuned with a four-task multitask setup on two 16GB P100 GPUs. Embeddings are evaluated after each epoch with brute-force nearest-neighbor Recall@K callbacks on intra-item, review-photo and ad-click datasets, plus an experimental set where Stable Diffusion turns text search queries into image queries. EfficientNetB0 (~5M params, 1.7ms on iPhone 12) offers the best latency, while EfficientFormer-l3 (~30M params, 2.7ms) gives up to +17% Recall@5 and +0.65% CTR in A/B tests. The Stable Diffusion pipeline was sped up with token merging and fp16 to about 500 images per hour on one T4.