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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Forest Fire Detection FirenetCNN
|
| 3 |
+
emoji: π₯
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: pink
|
| 6 |
sdk: gradio
|
|
|
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
| 9 |
+
license: mit
|
| 10 |
+
python_version: "3.12"
|
| 11 |
+
short_description: Forest fire and smoke detection with CNN + Grad-CAM
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# Forest Fire Detection Using FirenetCNN and XAI Techniques
|
| 15 |
+
|
| 16 |
+
[](https://deepwiki.com/OpelSpeedster/Forest-Fire-Detection-Using-FirenetCNN-and-XAI-Techniques)
|
| 17 |
+
|
| 18 |
+
This project implements a Convolutional Neural Network (CNN) to detect and classify forest fires from images and videos. The model leverages transfer learning with the MobileNetV2 architecture and is trained to distinguish between three classes: 'fire', 'smoke', and 'no_fire'.
|
| 19 |
+
|
| 20 |
+
To enhance model interpretability and trustworthiness, the project incorporates Explainable AI (XAI) using Grad-CAM (Gradient-weighted Class Activation Mapping). This technique generates heatmaps that visualize the specific regions in an image the model focuses on to make its predictions.
|
| 21 |
+
|
| 22 |
+
## Key Features
|
| 23 |
+
* **Multi-Class Classification:** Classifies input into 'fire', 'smoke', or 'no_fire' categories.
|
| 24 |
+
* **Transfer Learning:** Utilizes a pre-trained MobileNetV2 model, fine-tuned for the specific task of fire detection.
|
| 25 |
+
* **Data Augmentation:** Employs various image augmentation techniques (rotation, shifting, shearing, zooming, and flipping).
|
| 26 |
+
* **Versatile Prediction:** Capable of performing predictions on static images, pre-recorded videos, and live webcam feeds.
|
| 27 |
+
* **Explainable AI (XAI):** Implements Grad-CAM to produce heatmaps, providing visual insight into the model's decisions.
|
| 28 |
+
* **Web Interface:** Gradio-based web application for easy deployment and demo.
|
| 29 |
+
* **Docker Support:** Containerized deployment for production use.
|
| 30 |
+
|
| 31 |
+
## Model Performance
|
| 32 |
+
The model was evaluated on a test set of 405 images, achieving an overall accuracy of 82%.
|
| 33 |
+
|
| 34 |
+
```
|
| 35 |
+
precision recall f1-score support
|
| 36 |
+
|
| 37 |
+
fire 0.92 0.81 0.86 121
|
| 38 |
+
no_fire 0.76 0.98 0.86 146
|
| 39 |
+
smoke 0.84 0.67 0.75 138
|
| 40 |
+
|
| 41 |
+
accuracy 0.82 405
|
| 42 |
+
macro avg 0.84 0.82 0.82 405
|
| 43 |
+
weighted avg 0.83 0.82 0.82 405
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
## Project Structure
|
| 47 |
+
|
| 48 |
+
```
|
| 49 |
+
βββ src/ # Python package (core functionality)
|
| 50 |
+
β βββ __init__.py # Package exports
|
| 51 |
+
β βββ model.py # Model definition and utilities
|
| 52 |
+
β βββ gradcam.py # Grad-CAM implementation
|
| 53 |
+
β βββ inference.py # Unified inference engine
|
| 54 |
+
β βββ training.py # Training pipeline
|
| 55 |
+
βββ models/ # Trained model files
|
| 56 |
+
β βββ FirenetCNN1.h5 # Primary trained model
|
| 57 |
+
β βββ FirenetCNN.h5 # Alternative model version
|
| 58 |
+
β βββ firenet_model.h5 # Base model
|
| 59 |
+
βββ app.py # Gradio web application
|
| 60 |
+
βββ config.py # Project configuration
|
| 61 |
+
βββ Dockerfile # Docker build file
|
| 62 |
+
βββ docker-compose.yml # Docker Compose configuration
|
| 63 |
+
βββ pyproject.toml # Python package configuration
|
| 64 |
+
βββ requirements.txt # Dependencies
|
| 65 |
+
βββ Fire_PredCopy.ipynb # Original training notebook (reference)
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
## Installation & Setup
|
| 69 |
+
|
| 70 |
+
### Prerequisites
|
| 71 |
+
- Python 3.10+
|
| 72 |
+
- [uv](https://docs.astral.sh/uv/) (recommended) or pip
|
| 73 |
+
- A webcam for live detection (optional)
|
| 74 |
+
|
| 75 |
+
### Option 1: Using uv (Recommended)
|
| 76 |
+
|
| 77 |
+
```bash
|
| 78 |
+
# Clone the repository
|
| 79 |
+
git clone https://github.com/OpelSpeedster/Forest-Fire-Detection-Using-FirenetCNN-and-XAI-Techniques.git
|
| 80 |
+
cd Forest-Fire-Detection-Using-FirenetCNN-and-XAI-Techniques
|
| 81 |
+
|
| 82 |
+
# Install dependencies
|
| 83 |
+
uv pip install -r requirements.txt
|
| 84 |
+
|
| 85 |
+
# Run the application
|
| 86 |
+
uv run python app.py
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
### Option 2: Using pip
|
| 90 |
+
|
| 91 |
+
```bash
|
| 92 |
+
# Clone and install
|
| 93 |
+
git clone https://github.com/OpelSpeedster/Forest-Fire-Detection-Using-FirenetCNN-and-XAI-Techniques.git
|
| 94 |
+
cd Forest-Fire-Detection-Using-FirenetCNN-and-XAI-Techniques
|
| 95 |
+
|
| 96 |
+
pip install -r requirements.txt
|
| 97 |
+
python app.py
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
### Option 3: Docker
|
| 101 |
+
|
| 102 |
+
```bash
|
| 103 |
+
# Build and run with Docker Compose
|
| 104 |
+
docker compose up --build
|
| 105 |
+
|
| 106 |
+
# Or build manually
|
| 107 |
+
docker build -t fire-detection .
|
| 108 |
+
docker run -p 7860:7860 -v ./models:/app/models fire-detection
|
| 109 |
+
```
|
| 110 |
+
|
| 111 |
+
### Download the Dataset
|
| 112 |
+
|
| 113 |
+
This project uses the [Forest Fire Classifier Dataset](https://www.kaggle.com/datasets/google-brain/forest-fire-detection-from-satellite-images). Download and structure as:
|
| 114 |
+
|
| 115 |
+
```
|
| 116 |
+
data/
|
| 117 |
+
βββ forestfire-classifier-dataset/
|
| 118 |
+
βββ train/
|
| 119 |
+
β βββ fire/
|
| 120 |
+
β βββ nofire/
|
| 121 |
+
β βββ smoke/
|
| 122 |
+
βββ val/
|
| 123 |
+
β βββ fire/
|
| 124 |
+
β βββ nofire/
|
| 125 |
+
β βββ smoke/
|
| 126 |
+
βββ test/
|
| 127 |
+
βββ fire/
|
| 128 |
+
βββ nofire/
|
| 129 |
+
βββ smoke/
|
| 130 |
+
```
|
| 131 |
+
|
| 132 |
+
**Note:** The dataset folder is named `nofire` (without underscore), which matches the trained model's class ordering.
|
| 133 |
+
|
| 134 |
+
## Usage
|
| 135 |
+
|
| 136 |
+
### Web Interface
|
| 137 |
+
|
| 138 |
+
```bash
|
| 139 |
+
uv run python app.py
|
| 140 |
+
```
|
| 141 |
+
|
| 142 |
+
This launches a Gradio web interface at http://localhost:7860 with:
|
| 143 |
+
|
| 144 |
+
1. **π· Image Classification** - Upload images for fire/smoke/no_fire detection with Grad-CAM visualization
|
| 145 |
+
2. **π₯ Video Analysis** - Upload videos for frame-by-frame analysis with class distribution statistics
|
| 146 |
+
3. **πΉ Webcam Inference** - Live webcam detection (via Python API)
|
| 147 |
+
4. **π Model Information** - Architecture details and performance metrics
|
| 148 |
+
|
| 149 |
+
### Python API
|
| 150 |
+
|
| 151 |
+
```python
|
| 152 |
+
from src.inference import FireNetInference
|
| 153 |
+
|
| 154 |
+
# Initialize inference engine
|
| 155 |
+
engine = FireNetInference("models/FirenetCNN1.h5")
|
| 156 |
+
|
| 157 |
+
# Predict on a single image
|
| 158 |
+
result = engine.predict_image("path/to/image.jpg")
|
| 159 |
+
print(f"Prediction: {result['label']} ({result['confidence']*100:.2f}%)")
|
| 160 |
+
|
| 161 |
+
# Process a video
|
| 162 |
+
stats = engine.predict_video("path/to/video.mp4", output_path="output.mp4")
|
| 163 |
+
print(f"Processed {stats['processed_frames']} frames")
|
| 164 |
+
|
| 165 |
+
# Grad-CAM demo for all classes
|
| 166 |
+
demo = engine.create_gradcam_demo_image("path/to/image.jpg")
|
| 167 |
+
```
|
| 168 |
+
|
| 169 |
+
### Training
|
| 170 |
+
|
| 171 |
+
```bash
|
| 172 |
+
# Train a new model
|
| 173 |
+
uv run python -m src.training \
|
| 174 |
+
--train-dir data/forestfire-classifier-dataset/train \
|
| 175 |
+
--val-dir data/forestfire-classifier-dataset/val \
|
| 176 |
+
--model-path models/FirenetCNN.keras \
|
| 177 |
+
--epochs 100
|
| 178 |
+
|
| 179 |
+
# With fine-tuning
|
| 180 |
+
uv run python -m src.training \
|
| 181 |
+
--train-dir data/forestfire-classifier-dataset/train \
|
| 182 |
+
--val-dir data/forestfire-classifier-dataset/val \
|
| 183 |
+
--epochs 50 --fine-tune --fine-tune-epochs 20
|
| 184 |
+
```
|
| 185 |
+
|
| 186 |
+
### Evaluation
|
| 187 |
+
|
| 188 |
+
```python
|
| 189 |
+
from src.inference import FireNetInference
|
| 190 |
+
|
| 191 |
+
results = FireNetInference.evaluate_model_on_dataset(
|
| 192 |
+
"models/FirenetCNN1.h5",
|
| 193 |
+
"data/forestfire-classifier-dataset/test",
|
| 194 |
+
output_report="evaluation_report.txt"
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
print(f"Accuracy: {results['accuracy']:.2f}")
|
| 198 |
+
print(f"F1 Score: {results['weighted_avg_f1']:.2f}")
|
| 199 |
+
```
|
| 200 |
+
|
| 201 |
+
## Deploying to Hugging Face Spaces (ZeroGPU)
|
| 202 |
+
|
| 203 |
+
This app is preconfigured to deploy as a Gradio Space with [ZeroGPU](https://huggingface.co/docs/hub/spaces-zerogpu) hardware.
|
| 204 |
+
|
| 205 |
+
1. Create a new Space at https://huggingface.co/new-space with **SDK: Gradio**.
|
| 206 |
+
2. Upload only the files the Space needs (skip large media/notebooks/office docs):
|
| 207 |
+
```bash
|
| 208 |
+
pip install -U "huggingface_hub[cli]"
|
| 209 |
+
huggingface-cli login
|
| 210 |
+
huggingface-cli upload <your-username>/<space-name> . --repo-type=space \
|
| 211 |
+
--include "app.py" "config.py" "requirements.txt" "packages.txt" "README.md" \
|
| 212 |
+
--include "src/**" "models/FirenetCNN1.h5"
|
| 213 |
+
```
|
| 214 |
+
3. In the Space's **Settings** tab, set **Hardware** to **ZeroGPU**.
|
| 215 |
+
4. The app loads `models/FirenetCNN1.h5` by default (override with the `MODEL_PATH` variable/secret in Space Settings if you rename it).
|
| 216 |
+
|
| 217 |
+
**Note on TensorFlow + ZeroGPU:** Hugging Face's ZeroGPU is officially validated for PyTorch workloads. This app still requests a ZeroGPU slot per prediction via `@spaces.GPU`, and TensorFlow will use the GPU automatically if it's visible inside that worker process; if not, TensorFlow transparently falls back to CPU (no crash), so the Space stays fully functional either way.
|
| 218 |
+
|
| 219 |
+
## Model Files
|
| 220 |
+
|
| 221 |
+
| File | Format | Size | Description |
|
| 222 |
+
|------|--------|------|-------------|
|
| 223 |
+
| `FirenetCNN1.h5` | HDF5 | ~24 MB | Primary trained model |
|
| 224 |
+
| `FirenetCNN.h5` | HDF5 | ~24 MB | Alternative version |
|
| 225 |
+
| `firenet_model.h5` | HDF5 | ~2 MB | Base model |
|
| 226 |
+
|
| 227 |
+
The inference engine tries `.keras` format first, then falls back to `.h5`.
|
| 228 |
+
|
| 229 |
+
## Class Labels
|
| 230 |
+
|
| 231 |
+
| Index | Label | Description |
|
| 232 |
+
|-------|-------|-------------|
|
| 233 |
+
| 0 | `fire` | Active fire detected |
|
| 234 |
+
| 1 | `no_fire` | No fire detected |
|
| 235 |
+
| 2 | `smoke` | Smoke detected |
|
| 236 |
+
|
| 237 |
+
## Key Technical Details
|
| 238 |
+
|
| 239 |
+
- **Architecture:** MobileNetV2 + custom classifier head (GlobalAveragePooling2D β Dense(1024) β Dropout(0.5) β Dense(3))
|
| 240 |
+
- **Input Size:** 224x224x3
|
| 241 |
+
- **Grad-CAM Layer:** `out_relu` (last convolutional layer of MobileNetV2)
|
| 242 |
+
- **Preprocessing:** Rescaling to [0, 1], no mean subtraction
|
| 243 |
+
|
| 244 |
+
## License
|
| 245 |
+
|
| 246 |
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
| 247 |
+
|
| 248 |
+
## Credit
|
| 249 |
+
|
| 250 |
+
The original model was trained by Vishal S V. This version provides a modern, deployable interface for the FirenetCNN model with Gradio web app, Docker support, and comprehensive Python API.
|