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

![Food101 Classification Banner](assets/banner.png)

[![Python](https://img.shields.io/badge/Python-3.10-blue?logo=python)](https://www.python.org/)[![PyTorch](https://img.shields.io/badge/PyTorch-2.7.1-EE4C2C?logo=pytorch)](https://pytorch.org/)![Made with ML](https://img.shields.io/badge/Made%20with-ML-blueviolet?logo=openai)[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](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%** |

![Confusion Matrix Plot](assets/confusion_matrix.png)
*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](assets/gradio.png)

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