roqueselopeta's picture
Update README with complete project documentation
fe89fbb
---
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)