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