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