CoolFace
Apppublic

almador2002/Cars196

sourceHugging Facemitupdated 4mo agoView on Hugging Face
0likes
App README

<div align="center">

πŸš— Cars196 β€” Visual Recommendation System

Find visually similar cars from an image or a text description

![Gradio](https://gradio.app) ![CLIP](https://huggingface.co/openai/clip-vit-base-patch32) ![Dataset](https://huggingface.co/datasets/pawlo2013/Cars196) ![Python](https://www.python.org/) ![License](https://opensource.org/licenses/MIT)

A CLIP-powered visual recommender that finds the 3 most similar cars to any input β€” using 512-dimensional joint image-text embeddings and cosine similarity.

</div>


🎯 What This Does

Upload a car photo or type a natural-language description β€” the app returns the 3 most visually similar cars from a dataset of ~8,000 vehicles spanning 196 fine-grained classes.

InputExampleWhat Happens
πŸ–ΌοΈ ImagePhoto of a red sports carCLIP encodes β†’ finds visually similar cars
πŸ“ Text"luxury black sedan"CLIP maps text into the same vector space β†’ matches images
🎯 OutputTop-3 cars with similarity scoresRanked by cosine similarity over 512-D embeddings

πŸ“Ή Presentation Video

<div align="center">

<!-- Replace YOURVIDEOID with the actual YouTube video ID after uploading --> ![Watch the Presentation](https://www.youtube.com/watch?v=YOURVIDEOID)

Click the thumbnail above to watch the full 5-minute walkthrough on YouTube

</div>


🧠 Tech Stack at a Glance

<div align="center">

ComponentChoiceWhy
Embedding ModelCLIP ViT-B/32Dual modality β€” handles both image and text queries from one model
Embedding Dim512Compact, fast similarity search
DatasetCars196196 fine-grained classes (Make + Model + Year)
ReductionPCA β†’ t-SNE β†’ UMAPCompared all three to find the best visualization
ClusteringK-Means + DBSCANBoth partition-based and density-based approaches
StorageParquet (car_embeddings.parquet)Fast, compressed columnar format
UIGradio 4.44.1Clean, instantly shareable
HostingHuggingFace SpacesFree, integrates natively with the ML ecosystem

</div>


πŸ—ΊοΈ The Full Journey

The complete analytical pipeline lives in Assignment_3_Cars196.ipynb. Here are the highlights.

πŸ“Š Part 1 β€” Dataset Exploration

Cars196 is a fine-grained classification benchmark containing 16,185 images of 196 car classes, where each class corresponds to a specific Make + Model + Year combination (e.g., "BMW 3 Series Sedan 2012"). We use the training split (~8,144 images).

[image]

πŸ” Part 2 β€” Exploratory Data Analysis

A deep dive into the dataset's structure β€” class balance, manufacturer distribution, body types, production years, and image dimensions.

[image]

Key findings

  • β€”πŸ“¦ Roughly balanced β€” ~40 images per class on average
  • β€”πŸ­ American and European manufacturers dominate the dataset
  • β€”πŸš™ Sedans, coupes, and SUVs are the most represented body types
  • β€”πŸ“… Cars range from 1991 to 2012 β€” with a heavy skew toward 2007-2012

[image]

[image]

🧬 Part 3 β€” Embeddings & Clustering

All ~8,000 car images were passed through CLIP ViT-B/32, producing L2-normalized 512-dimensional vectors.

[image]

3.1 β€” Dimensionality Reduction

Compared PCA, t-SNE, and UMAP side by side:

[image]

UMAP produced the most semantically meaningful clusters β€” visually similar cars (same body type, similar proportions) end up close together regardless of brand.

[image]

3.2 β€” Clustering

Applied two complementary clustering algorithms:

AlgorithmApproachResult
K-MeansPartition-based, K chosen via elbow + silhouette10 well-defined clusters
DBSCANDensity-based, finds K automaticallyDiscovers natural clusters + noise

[image]

[image]

[image]

3.3 β€” Cluster Interpretation

CLIP embeddings cluster cars primarily by:

  1. 1.πŸš— Body type / silhouette β€” the strongest signal (SUVs together, sedans together, sports cars together)
  2. 2.🎨 Visual style & proportions β€” low-slung sports cars separate from tall boxy SUVs
  3. 3.πŸ’‘ Color & lighting tendencies β€” backgrounds & palettes influence proximity
  4. 4.πŸ“ Size category β€” compact cars, mid-size sedans, and full-size vehicles form distinct groups

[image]

Verdict β€” are the clusters useful? Yes. They capture the visual feel of a car rather than just its brand name. A user looking for a sporty coupe naturally finds similar cars across BMW, Audi, and Porsche β€” exactly what a recommender should do.

🎯 Part 4 β€” The Recommendation Engine

User Input (image or text)
        ↓
    CLIP encoder
        ↓
   512-D vector (L2-normalized)
        ↓
   Cosine similarity vs. all 8,144 stored embeddings
        ↓
        ↓  ← single matrix multiplication
        ↓
    Top-3 ranked matches
Image Search Test

[image]

Text Search Tests

CLIP's killer feature: text and images live in the same vector space, so a phrase like "red sports car" can match images directly with no extra training.

[image]


🎁 Bonus Analyses

Beyond the assignment's core requirements, the notebook explores a few extras:

1. Body Type Similarity Heatmap

How similar are different body types in CLIP's vector space? Sedans and coupes are close; trucks and convertibles are far apart.

[image]

2. Embedding Interpolation

Linearly interpolating between a Sedan and a Convertible in the embedding space β€” and watching the nearest car at each step morph from one to the other.

[image]

3. Nearest Neighbor Exploration

For a random query car, what are its closest neighbors in embedding space?

[image]


βš–οΈ Ethical Considerations

A few things to keep in mind about this system:

  • β€”Dataset bias β€” Cars196 is heavily skewed toward American and European brands. Asian manufacturers (Toyota, Honda, Hyundai) are underrepresented, which biases recommendations toward Western models.
  • β€”Temporal bias β€” Most cars in the dataset are from 2007–2012. The system won't recognize modern (2020+) designs well.
  • β€”Environmental angle β€” A more responsible recommender could highlight fuel efficiency or electric alternatives. This system is purely visual β€” it doesn't consider environmental impact.
  • β€”Privacy β€” Uploaded images are processed in-memory and never stored. In a production setting, users should be explicitly informed about data handling.

πŸ“‚ Repository Contents

cars196-recommender/
β”œβ”€β”€ app.py                            # The Gradio application
β”œβ”€β”€ requirements.txt                  # Python dependencies (pinned for HF Spaces compatibility)
β”œβ”€β”€ README.md                         # ← You are here
β”œβ”€β”€ car_embeddings.parquet            # Pre-computed 512-D embeddings + metadata
β”œβ”€β”€ car_images.pkl                    # Compressed thumbnails for display
└── Assignment_3_Cars196.ipynb        # Full notebook (EDA β†’ embeddings β†’ clustering β†’ recommendations)

πŸ› οΈ How to Run Locally

bash
git clone https://huggingface.co/spaces/YOUR_USERNAME/cars196-recommender
cd cars196-recommender
pip install -r requirements.txt
python app.py

Then open http://localhost:7860 in your browser.


πŸ“š Acknowledgments & References

  • β€”CLIP β€” Radford et al., Learning Transferable Visual Models From Natural Language Supervision (paper)
  • β€”Cars196 / Stanford Cars β€” Krause et al., 3D Object Representations for Fine-Grained Categorization (paper)
  • β€”UMAP β€” McInnes et al., Uniform Manifold Approximation and Projection (paper)
  • β€”HuggingFace Datasets β€” for distributing Cars196 in a clean, accessible format
  • β€”Gradio β€” for making ML demos trivially easy to share

<div align="center">

Built for Assignment #3 β€” RecSys, Embeddings & Spaces

πŸ€— Hosted on HuggingFace Spaces β€’ 🧠 Powered by CLIP β€’ πŸ’œ Made with Gradio

</div>