File size: 4,605 Bytes
fe89fbb
 
 
 
 
 
 
 
 
 
 
c17bef1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Fish Freshness Classifier
emoji: 🐟
colorFrom: blue
colorTo: green
sdk: gradio
sdk_version: 4.19.2
app_file: fish_freshness_app.py
pinned: false
---

# 🐟 Food Freshness Classification Using Deep Learning

This project aims to classify food images as **Fresh** or **Not Fresh** using deep learning and transfer learning with PyTorch. We explore various CNN architectures and hybrid ensemble models to achieve state-of-the-art accuracy.

---

## πŸ“ Project Structure

β”œβ”€β”€ models/
β”‚ β”œβ”€β”€ efficientnet_b0.py
β”‚ β”œβ”€β”€ efficientnet_b4.py
β”‚ β”œβ”€β”€ mobilenetv2.py
β”‚ β”œβ”€β”€ resnet50.py
β”‚ └── hybrid_fusion.py
β”‚
β”œβ”€β”€ train/
β”‚ β”œβ”€β”€ train_efficientnet.py
β”‚ β”œβ”€β”€ train_resnet50.py
β”‚ β”œβ”€β”€ train_mobilenetv2.py
β”‚ β”œβ”€β”€ train_efficientnet_b4.py
β”‚ └── train_hybrid_fusion.py
β”‚
β”œβ”€β”€ evaluate/
β”‚ β”œβ”€β”€ evaluate_efficientnet.py
β”‚ β”œβ”€β”€ evaluate_resnet50.py
β”‚ β”œβ”€β”€ evaluate_mobilenetv2.py
β”‚ β”œβ”€β”€ evaluate_efficientnet_b4.py
β”‚ β”œβ”€β”€ evaluate_hybrid_fusion.py
β”‚ └── compare_fish_models.py
β”‚
β”œβ”€β”€ utils/
β”‚ └── dataset_loader.py
β”‚
β”œβ”€β”€ data/
β”‚ β”œβ”€β”€ train_paths.npy
β”‚ β”œβ”€β”€ train_labels.npy
β”‚ β”œβ”€β”€ val_paths.npy
β”‚ β”œβ”€β”€ val_labels.npy
β”‚ β”œβ”€β”€ test_paths.npy
β”‚ └── test_labels.npy
β”‚
β”œβ”€β”€ results/
β”‚ β”œβ”€β”€ eval_metrics_.png
β”‚ β”œβ”€β”€ confusion_matrix_.png
β”‚ β”œβ”€β”€ model_metrics_summary.csv
β”‚ └── *.pth (saved models)
β”‚
β”œβ”€β”€ requirements.txt
└── README.md

---

## 🧠 Model Architectures

### βœ… Individual Models:
- **EfficientNetB0**
- **EfficientNetB4**
- **MobileNetV2**
- **ResNet50**

Each uses transfer learning:
- Initial training with `train_base=False` (frozen feature extractor)
- Followed by fine-tuning the deeper layers

### πŸ” Hybrid Model:
**EnhancedHybridFusionClassifier**
- Combines EfficientNetB0, ResNet50, and MobileNetV2
- Fuses feature embeddings via concatenation
- Classifier head with multiple dense layers
- Fine-tuned after head training

---

## πŸ§ͺ Evaluation and Comparison

Each model is evaluated on the same test set.

### Metrics:
- Accuracy
- Precision
- Recall
- F1 Score
- AUC (ROC)

### Visual Outputs:
- `confusion_matrix_*.png`
- `eval_metrics_*.png`
- ROC curves and metric bar charts (`compare_fish_models.py`)

Results are also exported to:
results/model_metrics_summary.csv

---

## πŸ—οΈ Training Details

- Optimizer: `Adam`
- Loss: `BCELoss`
- Scheduler: `ReduceLROnPlateau` (monitors val accuracy)
- Early stopping via logic inside training script
- Data augmentation during training:
  - Random horizontal flip
  - Rotation
  - Color jitter
- `num_workers` optimized based on CPU
- `pin_memory=True` for faster GPU transfer

---

## πŸ’Ύ Setup & Requirements

1. Clone the repository:
```bash
git clone <your-repo-url>
cd <repository-name>
```

2. Create and activate virtual environment:
```bash
# Windows
setup.bat

# Or manually:
python -m venv venv
venv\Scripts\activate  # Windows
source venv/bin/activate  # Linux/Mac
pip install -r requirements.txt
```

Requirements:
- Python 3.8+
- PyTorch 2.0+
- CUDA-compatible GPU (recommended)
- See requirements.txt for full list

## πŸš€ How to Run

1. Train a Model:
```bash
python train/train_efficientnet.py
```

2. Evaluate:
```bash
python evaluate/evaluate_efficientnet.py
```

3. Compare All Models:
```bash
python evaluate/compare_fish_models.py
```

## 🏁 Results Summary

| Model | Accuracy | Precision | Recall | F1 |
|-------|----------|-----------|--------|-------|
| EfficientNetB0 | 0.9819 | 0.9652 | 0.9978 | 0.9812 |
| EfficientNetB4 | 0.9797 | 0.9733 | 0.9843 | 0.9788 |
| MobileNetV2 | 0.9562 | 0.9410 | 0.9685 | 0.9546 |
| ResNet50 | 0.9765 | 0.9648 | 0.9865 | 0.9756 |
| HybridFusion | 0.9755 | 0.9627 | 0.9865 | 0.9745 |

## πŸ“Œ Notes

- Training logs are printed per epoch with loss/val accuracy
- Best models are saved as *_best.pth
- Hybrid model training uses transfer learning and feature fusion
- Comparison across architectures and training strategy is fair (same data, same pipeline)

## πŸ† Final Thoughts

This project shows the power of transfer learning and hybrid deep learning models in food quality assessment. The modular pipeline supports extension (e.g., Grad-CAM, more ensembling) and can serve as a template for similar classification tasks.

## Acknowledgments

- [Segment Anything Model (SAM)](https://github.com/facebookresearch/segment-anything)
- [EfficientNet Implementation](https://github.com/lukemelas/EfficientNet-PyTorch)