Spaces:
Sleeping
Sleeping
File size: 9,041 Bytes
3aef74e 4d77859 3aef74e 43124a6 12955c7 43124a6 12955c7 43124a6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
---
title: Food-101 Classifier
emoji: ๐
colorFrom: yellow
colorTo: red
sdk: gradio
app_file: scripts/app.py
---

[](https://www.python.org/)[](https://pytorch.org/)[](LICENSE)
# ๐ฝ๏ธ 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
- [๐ฝ๏ธ Food-101 Image Classification with EfficientNetV2-S and PyTorch Lightning](#๏ธ-food-101-image-classification-with-efficientnetv2-s-and-pytorch-lightning)
- [๐ Table of Contents](#-table-of-contents)
- [๐ฏ Project Highlights](#-project-highlights)
- [๐ก Real-World Applications](#-real-world-applications)
- [๐งซ Experimental Results](#-experimental-results)
- [๐ Final Results](#-final-results)
- [๐ฌ Performance Analysis and Error Diagnosis](#-performance-analysis-and-error-diagnosis)
- [๐ค Lowest-Performing Classes](#-lowest-performing-classes)
- [Root Cause Analysis of Misclassifications](#root-cause-analysis-of-misclassifications)
- [Future Work](#future-work)
- [๐งช Methodology and Experimental Process](#-methodology-and-experimental-process)
- [๐ Repository Structure](#-repository-structure)
- [๐ Getting Started](#-getting-started)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Usage](#usage)
- [๐ป Technologies Used](#-technologies-used)
---
## ๐ฏ 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%** |

*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.

Check out my [Food101 Gradio Demo](https://huggingface.co/spaces/your-username/food101-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
```bash
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:**
```bash
git clone https://github.com/Deathshot78/Food101-Classification
cd Food101-Classification
```
2. **Install the dependencies:**
```bash
pip install -r requirements.txt
```
### Usage
Run training with a subset for quick testing:
```bash
python main.py
```
### ๐ป Technologies Used
- Python
- PyTorch
- PyTorch Lightning
- TorchMetrics
- Gradio
- Matplotlib & Seaborn
|