---
title: "Image Similarity with Hugging Face Datasets and Transformers"
description: "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."
url: "https://swe.quest/articles/image-similarity-with-hugging-face-datasets-and-transformers-cfa579cfca74f44c"
canonical: "https://huggingface.co/blog/image-similarity"
source: "Hugging Face"
published: "2023-01-16"
difficulty: "medium"
kind: "How-to"
reading_minutes: 7
tags: ["computer-vision","vector-search","search","python"]
---

# Image Similarity with Hugging Face Datasets and Transformers

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2023-01-16, 7 min read, How-to. Original: [https://huggingface.co/blog/image-similarity](https://huggingface.co/blog/image-similarity)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Computer vision](https://swe.quest/tags/computer-vision.md) (AI & ML)
- [Vector search](https://swe.quest/tags/vector-search.md) (Data & databases)
- [Search](https://swe.quest/tags/search.md) (Backend & APIs)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- PyTorch basics
- embeddings and cosine similarity ([easy posts on RAG & retrieval](https://swe.quest/tags/rag.md?difficulty=easy))
- Vision Transformers ([easy posts on Computer vision](https://swe.quest/tags/computer-vision.md?difficulty=easy))

## Keywords

Hugging Face Transformers, Hugging Face Datasets, Vision Transformer, FAISS, Beans dataset, locality-sensitive hashing

## Related posts

- [Image search with 🤗 datasets](https://swe.quest/articles/image-search-with-datasets-7a6fd7ae6c6c63ce.md): Hugging Face · 2022-03-16 · Medium · 12 min read · Vector search, Computer vision, Python, Search
  A 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.
- [Getting Started With Embeddings](https://swe.quest/articles/getting-started-with-embeddings-f83c278b13779a68.md): Hugging Face · 2022-06-23 · Medium · 11 min read · Search, Machine learning, Python, Vector search
  Embeddings turn text or images into fixed-length vectors that capture meaning, so semantic similarity becomes a distance calculation.
- [Implementing semantic image search with Amazon Titan and Supabase Vector](https://swe.quest/articles/implementing-semantic-image-search-with-amazon-titan-and-supabase-vector-07c13eba94b53a82.md): Supabase · 2024-03-26 · Medium · 5 min read · Vector search, PostgreSQL, Python, Computer vision, Cloud platforms
  Semantic 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.
- [Image replacement in Canva designs using reverse image search](https://swe.quest/articles/image-replacement-in-canva-designs-using-reverse-image-search-03653e5b614fb037.md): Canva Engineering · 2025-01-28 · Medium · 9 min read · Vector search, Computer vision, Search, System design
  Canva 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.
- [Efficient Visual Representation Learning And Evaluation](https://swe.quest/articles/efficient-visual-representation-learning-and-evaluation-5d9ca073957d3278.md): Etsy Code as Craft · 2024-06-07 · Hard · 10 min read · Computer vision, Recommendations & ranking, Model training, Evaluation, E-commerce
  Etsy 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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
