Daniel kiani
Update README.md
4d77859 verified

A newer version of the Gradio SDK is available: 6.2.0

Upgrade
metadata
title: Food-101 Classifier
emoji: ๐Ÿ”
colorFrom: yellow
colorTo: red
sdk: gradio
app_file: scripts/app.py

Food101 Classification Banner

PythonPyTorchMade with MLLicense: MIT

๐Ÿฝ๏ธ Food-101 Image Classification with EfficientNetV2-S and PyTorch Lightning

This repository contains the code for an end-to-end deep learning project to classify 101 food categories from the challenging Food-101 dataset. The project demonstrates a systematic approach to model selection, fine-tuning, and hyperparameter optimization, achieving a final validation accuracy of 85.4% on the full dataset.

The entire training and evaluation pipeline is built using modern, reproducible practices with PyTorch Lightning.


๐Ÿ“‘ Table of Contents


๐ŸŽฏ Project Highlights

  • High-Performance Model โšก: Utilizes a pre-trained EfficientNetV2-S, selected for its excellent balance of accuracy and computational efficiency suitable for potential edge deployment.
  • Reproducible Pipeline ๐Ÿ”„: Encapsulates the entire workflowโ€”from data loading to training and evaluationโ€”in a clean and organized LightningModule and DataModule.
  • Efficient Experimentation โฑ๏ธ: Overcame hardware limitations by implementing dataset subsetting for rapid prototyping.
  • Advanced Fine-Tuning ๐Ÿ› ๏ธ: Implemented a robust fine-tuning strategy, unfreezing the final three blocks of the feature extractor and using the Adam optimizer with a CosineAnnealingLR scheduler for stable convergence.
  • In-Depth Analysis ๐Ÿ”Ž: Went beyond simple accuracy by calculating and logging per-class F1-scores and accuracies, enabling a deep dive into the model's strengths and weaknesses.
  • Live Deployment ๐Ÿ“บ: The final model is deployed and accessible as an interactive Gradio web application on Hugging Face Spaces.

๐Ÿ’ก Real-World Applications

Beyond being a technical challenge, this food classification model serves as a foundation for numerous real-world applications in health, hospitality, and smart home technology.

  • Health and Nutrition Tracking

    • Automated Calorie Counting: Users can snap a photo of their meal, and an app can automatically identify each food item to provide an instant estimate of calories, macros, and other nutritional information.
    • Dietary Management: Assists individuals with allergies or specific dietary needs (e.g., diabetes, gluten-free) by helping them identify and log their food intake accurately.
  • Restaurant and Hospitality Tech

    • Self-Checkout Systems: In cafeterias or quick-service restaurants, a camera-based system could identify all items on a tray to automate the billing process, reducing queues and improving efficiency.
      • Interactive Menus: Allow diners to point their phone at a dish to get more information, such as ingredients, allergen warnings, or customer reviews.
  • Smart Home and Appliances

    • Smart Refrigerators: A fridge equipped with a camera could identify leftover dishes, suggest recipes based on available food, and help track food spoilage to reduce waste.

๐Ÿงซ Experimental Results

This project followed an iterative approach. The table below summarizes the key experiments and their outcomes, showing the progression from the initial baseline to the final model.

Model Training Strategy Data % Key Hyperparameters Final Val Accuracy
EfficientNet-B2 Simple fine-tune (last block) 50% lr=1e-4 ~64%
EfficientNet-B2 Unfreeze last 3 blocks 50% lr=1e-3 82.0%
EfficientNet-B2 Two-Stage Fine-Tuning 50% lr1=1e-3, lr2=1e-5 Performance Degraded
EfficientNetV2-S Unfreeze last 3 blocks 50% lr=1e-4 (Tuned) 82.4%
EfficientNetV2-S Unfreeze last 3 blocks and more advanced transforms 50% lr=1e-4 (Tuned) ~82.4% Pretty much the same Performance
EfficientNetV2-S Unfreeze last 3 blocks 100% lr=1e-4 (Tuned) 85.4%

๐Ÿ“Š Final Results

After systematically iterating on model architecture and hyperparameters, the final model achieved the following performance on the full Food-101 validation set:

Metric Score
Validation Accuracy 85.4%

Confusion Matrix Plot Here is the Confusion Matrix on the Test set. (you can find this plot in the assets section)

This model is deployed and accessible as an interactive Gradio web application on Hugging Face Spaces.

Gradio

Check out my Food101 Gradio Demo.


๐Ÿ”ฌ Performance Analysis and Error Diagnosis

Beyond the aggregate accuracy, a per-class analysis was conducted to identify the model's specific limitations and diagnose the root causes of misclassifications.

The model performed exceptionally well on many classes but struggled with a distinct set of categories, primarily due to visual ambiguity and high variability in appearance.

๐Ÿค Lowest-Performing Classes

The following five classes had the lowest validation accuracy:

Class Name Index Validation Accuracy
shrimp_and_grits 93 44.0%
ravioli 77 59.2%
apple_pie 0 61.6%
huevos_rancheros 56 63.2%
falafel 36 63.6%

Root Cause Analysis of Misclassifications

  • High Intra-Class Variation: The model struggled with dishes that have no single, consistent appearance.
  • Fine-Grained Confusion: Errors occurred between visually similar classes like ravioli vs. dumplings.
  • Ambiguous Features: Foods like falafel resemble many small fried dishes, making classification tricky.

Future Work

Improvements could include:

  • Detailed confusion matrix analysis ๐Ÿ”
  • More aggressive data augmentation ๐Ÿ“ˆ
  • Larger architectures for fine-grained recognition ๐Ÿ‹๏ธ
  • Training for longer ๐Ÿ‹๏ธ

๐Ÿงช Methodology and Experimental Process

Steps taken in the project:

  1. Baseline Establishment ๐Ÿ โ€“ EfficientNet-B2 achieved ~64%.
  2. Architecture Selection ๐Ÿ—๏ธ โ€“ EfficientNetV2-S chosen for balance of accuracy and size.
  3. Transforms Selection ๐ŸŽจ โ€“ TrivialAugmentWide + RandomResizedCrop, RandAugment, etc.
  4. Fine-Tuning Strategy ๐Ÿ”ง โ€“ Final 3 blocks unfrozen for training.
  5. Final Model Training ๐Ÿ† โ€“ Full dataset, Adam, CosineAnnealingLR, EarlyStopping โ†’ 85.4%.

๐Ÿ“ Repository Structure

food-101-classification/
โ”œโ”€โ”€ data/
โ”œโ”€โ”€ logs/
โ”œโ”€โ”€ scripts/
โ”‚   โ”œโ”€โ”€ main.py
โ”‚   โ”œโ”€โ”€ models.py
โ”‚   โ”œโ”€โ”€ class_names.py
โ”‚   โ”œโ”€โ”€ app.py
โ”‚   โ””โ”€โ”€ prepare_data.py
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ README.md

๐Ÿš€ Getting Started

Prerequisites

  • Python 3.10+ ๐Ÿ
  • PyTorch ๐Ÿ”ฅ
  • CUDA-enabled GPU (recommended) ๐ŸŽฎ

Installation

  1. Clone the repository:

    git clone https://github.com/Deathshot78/Food101-Classification
    cd Food101-Classification
    
  2. Install the dependencies:

    pip install -r requirements.txt
    

Usage

Run training with a subset for quick testing:

python main.py

๐Ÿ’ป Technologies Used

  • Python

  • PyTorch

  • PyTorch Lightning

  • TorchMetrics

  • Gradio

  • Matplotlib & Seaborn