Spaces:
Sleeping
Sleeping
Matan Kriel commited on
Commit ยท
cd514b7
1
Parent(s): 1d15dd1
changed code and read me
Browse filesFiles changed (3)
- .DS_StoreAdded
- Assignment_3_Food_Match.ipynbAdded
- README.md+81-60
123452345677891011101112131614151619171819202122232425262728262728293031323334353634353637383938404142404344454642474448494650485152505352545554555656575859606158596061626364636465666767686970717269707374757677787379808175828378848586878283888990858687889192939495909697939498991001019899100101102102103104105106107108109110111112113114115116117118119120121122123124125126127128107108109129130131132133134135114115116117118136137138139---title: Food Matchemoji: ๐colorFrom: pinkcolorTo: greentitle: Food Matcher AI (SigLIP Edition)emoji: ๐colorFrom: greencolorTo: yellowsdk: gradiosdk_version: 6.1.0sdk_version: 5.0.0app_file: app.pypinned: falselicense: mitshort_description: Trained model to detect and recommend similar foods.---
# ๐ Visual Dish Matcher AI
**A computer vision app that suggests dishes based on visual/text similarity.****A computer vision app that suggests recipes and dishes based on visual similarity using Google's SigLIP model.**
## ๐ฏ Project OverviewThis project explores the power of **Vector Embeddings** in building recommendation systems. Unlike traditional filters (e.g., "Show me Italian food"), this app uses **OpenAI's CLIP model** to "see" the food. It converts images into mathematical vectors and finds matches based on visual contentโtexture, color, shape, and ingredients.This project builds a **Visual Search Engine** for food. Instead of relying on text labels (which can be inaccurate or missing), we use **Vector Embeddings** to find dishes that look similar.
**Key Features:*** **Multimodal Search:** Find food using an image *or* a text description.* **Advanced Data Cleaning:** Automated detection of blurry or low-quality images.* **Model Comparison:** A scientific comparison between **OpenAI CLIP** and **Google SigLIP** to choose the best engine.
**Live Demo:** [Click "App" tab above to view]
---
## ๐ ๏ธ Tech Stack* **Model:** OpenAI CLIP (`clip-vit-base-patch32`)* **Frameworks:** PyTorch, Transformers, Datasets (Hugging Face)* **Interface:** Gradio* **Model:** Google SigLIP (`google/siglip-base-patch16-224`)* **Frameworks:** PyTorch, Transformers, Gradio, Datasets* **Data Engineering:** OpenCV (Feature Extraction), NumPy* **Data Storage:** Parquet (via Git LFS)* **Visualization:** Matplotlib, Seaborn, Scikit-Learn (t-SNE/PCA)
---
## DataSet - Food101
Food-101 dataset, a popular benchmark for fine-grained image classification. Unlike "clean" studio datasets, Food-101 contains real-world images taken in various lighting conditions, angles, and noise levels, making it highly representative of photos users typically upload to social media or food apps.## ๐ Part 1: Data Analysis & Cleaning**Dataset:** [Food-101 (ETH Zurich)](https://huggingface.co/datasets/ethz/food101) (Subset of 5,000 images).
Key Features:### 1. Exploratory Data Analysis (EDA)Before any modeling, we analyzed the raw data to ensure quality and balance.
101 Categories: Covers a wide range of international dishes, including Sushi, Pizza, Hamburger, Pad Thai, Baklava, and Chocolate Mousse.* **Class Balance Check:** We verified that our random subset of 5,000 images maintained a healthy distribution across the 101 food categories (approx. 50 images per class).* **Image Dimensions:** We visualized the width and height distribution to identify unusually small or large images.* **Outlier Detection:** We plotted the distribution of **Aspect Ratios** and **Brightness Levels**.
"In the Wild" Data: Images are not perfectly centered or lit; they contain background noise (plates, cutlery, restaurant tables), challenging the model to focus on the food itself.
Project Subset: To ensure computational efficiency for this assignment, a randomized stratified subset of 5,000 images was selected from the training split.
Data Structure:
Input: RGB Images (various aspect ratios, resized during processing).
Labels: 101 unique Integer IDs mapped to human-readable Class Names.
---
## ๐ Part 1: Data Exploration (EDA)**Dataset:** [Food-101 (ETH Zurich)](https://huggingface.co/datasets/ethz/food101)To ensure computational efficiency for the assignment, I utilized a randomized subset of **5,000 images** spanning 101 categories.### 2. Data CleaningBased on the plots above, **we deleted "bad" images** that were:* Too Dark (Avg Pixel Intensity < 20)* Too Bright/Washed out (Avg Pixel Intensity > 245)* Extreme Aspect Ratios (Too stretched or squashed, AR > 3.0)
### 1. Data CleaningBefore training, the dataset underwent rigorous cleaning:* **Format Correction:** Converted distinct Grayscale images to RGB to ensure compatibility with the CLIP model.* **Outlier Detection:** Analyzed image brightness and aspect ratios to identify and flag low-quality or distorted images (e.g., pitch-black photos or extreme panoramas).
### 2. Image DistributionWe verified the class balance to ensure the model wasn't biased toward specific categories.---
## โ๏ธ Part 2: Model Comparison (CLIP vs. SigLIP vs metaclip)To ensure the best search results, we ran a "Challenger" test between three leading multimodal models.
### The Contestants:1. **Baseline:** OpenAI CLIP (`clip-vit-base-patch32`)2. **Challenger:** Google SigLIP (`siglip-base-patch16-224`)3. **Challenger:** Facebook MetaCLIP": ("facebook/metaclip-b32-400m)
### 3. Dimensionality AnalysisWe analyzed the width vs. height of the dataset to verify that most images were standard sizes suitable for resizing.### The Evaluation:We compared them using **Silhouette Scores** (measuring how distinct the food clusters are) and a visual "Taste Test" (checking nearest neighbors for specific dishes).
* **Metric:** Silhouette Score* **Winner:** **Google SigLIP** (Produced cleaner, more distinct clusters and better visual matches).
**Visual Comparison:**We queried both models with the same image to see which returned more accurate similar foods.
### Outlier Detection

---
## ๐ง Part 2: Embeddings & ClusteringThe core of the "Visual Matcher" is the embedding space. We generated 512-dimensional vectors for every image in the training set.## ๐ง Part 3: Embeddings & ClusteringUsing the winning model (**SigLIP**), We applied dimensionality reduction to visualize how the AI groups food concepts.
### Clustering AnalysisUsing **K-Means**, we grouped these vectors to see if the model could automatically discover food categories without being told the labels.* **Algorithm:** K-Means (k=50)* **Dimensionality Reduction:** t-SNE (to visualize 512D vectors in 2D)* **Algorithm:** K-Means Clustering (k=101 categories).* **Visualization:** * **PCA:** To see the global variance. * **t-SNE:** To see local groupings (e.g., "Sushi" clusters separately from "Burgers").
**Key Insight:** The model successfully grouped foods by visual properties. For example, "Red/Orange" foods (Pizza, Lasagna) formed distinct clusters separate from "Green" foods (Salads, Guacamole).


---
## ๐ Part 3: The ApplicationThe final product is a **Gradio** web application hosted on Hugging Face Spaces. It supports two modes of interaction:
1. **Image-to-Image:** The user uploads a photo (e.g., a burger). The app embeds the upload and calculates **Cosine Similarity** against the database to find the nearest visual neighbors.2. **Text-to-Image:** The user types a description (e.g., "Spicy Tacos"). The app uses CLIP's text encoder to find images that match the semantic meaning of the text.## ๐ Part 4: The ApplicationThe final product is a **Gradio** web application hosted on Hugging Face Spaces.
1. **Image-to-Image:** Upload a photo (e.g., a burger) -> The app embeds it using SigLIP -> Finds the nearest 3 visual matches.2. **Text-to-Image:** Type "Spicy Tacos" -> The app finds images matching that description.
## NoteThe application is running the clip model even though the sigLip model won, sigLip was to big to be run on the hugging face space free tier
### How to Run Locally1. **Clone the repository:** ```bash git clone [https://huggingface.co/spaces/YOUR_USERNAME/Food_Recommender](https://huggingface.co/spaces/YOUR_USERNAME/Food-Match) cd Food-Match ```2. **Install dependencies:** ```bash pip install -r requirements.txt ```3. **Run the app:** ```bash python app.py ```
---
## ๐ Repository Structure* `app.py`: Main application logic and Gradio interface.* `food_embeddings.parquet`: Pre-computed vector database (stored via Git LFS).* `requirements.txt`: Python dependencies.* `app.py`: Main application logic (Gradio + SigLIP).* `food_embeddings_siglip.parquet`: Pre-computed SigLIP vector database.* `requirements.txt`: Python dependencies (includes `sentencepiece`, `protobuf`).* `README.md`: Project documentation.
---
## โ๏ธ Author**[Matan Kriel]***Assignment #3: Embeddings, RecSys, and Spaces*---
## โ๏ธ Authors**Matan Kriel****Odeya Shmuel***Assignment #3: Embeddings, RecSys, and Spaces*.DS_StoreAdded
Assignment_3_Food_Match.ipynbAdded
README.md+81-60