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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
license: apache-2.0
|
|
|
|
|
|
|
|
|
|
| 3 |
sdk: docker
|
| 4 |
+
pinned: true
|
| 5 |
+
short_description: Detect deforestation from satellite imagery.
|
| 6 |
---
|
| 7 |
+
# π³ Deforestation Detection using Satellite Imagery
|
| 8 |
|
| 9 |
+
This project aims to **detect deforestation from satellite images** using a **U-Net deep learning model**. It leverages automated satellite data collection, image segmentation techniques, and Earth Engine + TensorFlow pipelines to help environmental organizations monitor illegal deforestation in near real-time.
|
| 10 |
+
|
| 11 |
+
<p align="center">
|
| 12 |
+
<img src="Final Output.png" width="70%"/>
|
| 13 |
+
</p>
|
| 14 |
+
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
## π Project Objective
|
| 18 |
+
|
| 19 |
+
> **Can an automated model built on satellite imagery help environmental organizations monitor illegal deforestation in near real-time?**
|
| 20 |
+
|
| 21 |
+
We aim to answer this by building a robust deep learning pipeline that:
|
| 22 |
+
- Collects satellite imagery for selected regions and years.
|
| 23 |
+
- Trains a U-Net segmentation model on forest/non-forest (or deforested) labels.
|
| 24 |
+
- Allows users to input coordinates + year and visualize predictions on a Leaflet map.
|
| 25 |
+
|
| 26 |
+
---
|
| 27 |
+
|
| 28 |
+
## π°οΈ Data Collection & Preprocessing
|
| 29 |
+
|
| 30 |
+
We use **Google Earth Engine (GEE)** to fetch Sentinel-2 or Landsat-8 imagery and NDVI indices.
|
| 31 |
+
|
| 32 |
+
### Data Sources:
|
| 33 |
+
- **Imagery**: Landsat-8 (30m resolution), optionally Sentinel-2 (10m resolution).
|
| 34 |
+
- **Labels**: Derived using NDVI and/or forest loss layers from Global Forest Change dataset.
|
| 35 |
+
|
| 36 |
+
### Preprocessing:
|
| 37 |
+
- Region-wise time-lapse collection.
|
| 38 |
+
- NDVI-based labeling: Thresholding NDVI to identify vegetation loss.
|
| 39 |
+
- Tiling large images into patches.
|
| 40 |
+
- Saving as `.npy` and `.tfrecord` formats for model training.
|
| 41 |
+
|
| 42 |
+
---
|
| 43 |
+
|
| 44 |
+
## π§ Model Architecture
|
| 45 |
+
|
| 46 |
+
The model uses a **U-Net architecture** implemented in **TensorFlow**, designed for image segmentation tasks.
|
| 47 |
+
|
| 48 |
+
- **Input**: Satellite image patches (RGB or NDVI composite).
|
| 49 |
+
- **Output**: Binary mask indicating deforested areas.
|
| 50 |
+
|
| 51 |
+
### Features:
|
| 52 |
+
- Batch normalization and dropout regularization
|
| 53 |
+
- Dice coefficient + binary cross-entropy loss
|
| 54 |
+
- Data augmentation (rotation, flipping, contrast)
|
| 55 |
+
|
| 56 |
+
---
|
| 57 |
+
|
| 58 |
+
## βοΈ Training
|
| 59 |
+
|
| 60 |
+
```bash
|
| 61 |
+
python train.py --epochs 50 --batch_size 16 --data_dir ./data --save_model ./models/unet_deforestation.h5
|
| 62 |
+
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
## π Evaluation
|
| 66 |
+
|
| 67 |
+
The trained model is evaluated on a held-out test set using the following metrics:
|
| 68 |
+
|
| 69 |
+
- **IoU (Intersection over Union)**: Measures the overlap between predicted deforested area and ground truth.
|
| 70 |
+
- **Dice Coefficient**: Especially useful for imbalanced classes.
|
| 71 |
+
- **Precision & Recall**: To understand false positives and false negatives.
|
| 72 |
+
- **F1 Score**: Harmonic mean of precision and recall.
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
---
|
| 76 |
+
|
| 77 |
+
## π Deployment
|
| 78 |
+
|
| 79 |
+
### Hugging Face Space π
|
| 80 |
+
Visit: [**Deforestation Detection on Hugging Face**](https://huggingface.co/spaces/ojasrohatgi/Deforestation-Detection)
|
| 81 |
+
|
| 82 |
+
Features:
|
| 83 |
+
- Interactive Leaflet map to select coordinates
|
| 84 |
+
- Year input to analyze temporal deforestation
|
| 85 |
+
- On-click image fetching and prediction overlay
|
| 86 |
+
- Downloadable prediction masks for offline use
|
| 87 |
+
|
| 88 |
+
### Backend Logic
|
| 89 |
+
The backend takes user inputs (`lat, lon, year`), queries GEE for imagery, preprocesses it into patches, passes it to the trained U-Net model, and returns a combined prediction image with overlays.
|
| 90 |
+
|
| 91 |
+
---
|
| 92 |
+
|
| 93 |
+
## π§Ύ Project Structure
|
| 94 |
+
|
| 95 |
+
```bash
|
| 96 |
+
Deforestation-Detection/
|
| 97 |
+
β
|
| 98 |
+
βββ app/ # Streamlit-based frontend interface
|
| 99 |
+
β βββ main.py # Streamlit UI & user interaction logic
|
| 100 |
+
β βββ map_component.html # Leaflet map integration for coordinate input
|
| 101 |
+
β βββ overlay.py # Image overlay + visualization helpers
|
| 102 |
+
β βββ utils.py # Patching, preprocessing, and GEE interaction
|
| 103 |
+
β
|
| 104 |
+
βββ model/ # Trained model artifacts
|
| 105 |
+
β βββ unet_deforestation.h5 # Final trained U-Net model
|
| 106 |
+
β
|
| 107 |
+
βββ earth_engine/ # Scripts for working with GEE
|
| 108 |
+
β βββ export_data.py # Downloads satellite images + NDVI labels
|
| 109 |
+
β
|
| 110 |
+
βββ training/ # Model architecture and training logic
|
| 111 |
+
β βββ train.py # Model training loop
|
| 112 |
+
β βββ evaluate.py # Evaluation script for test data
|
| 113 |
+
β βββ metrics.py # IoU, Dice, and other evaluation metrics
|
| 114 |
+
β βββ unet_model.py # Custom U-Net implementation
|
| 115 |
+
β
|
| 116 |
+
βββ data/ # Data directory (images, masks, TFRecords)
|
| 117 |
+
β βββ train/ # Training image and mask patches
|
| 118 |
+
β βββ val/ # Validation image and mask patches
|
| 119 |
+
β βββ test/ # Test image and mask patches
|
| 120 |
+
β
|
| 121 |
+
βββ requirements.txt # Python dependencies
|
| 122 |
+
βββ README.md # This file
|
| 123 |
+
βββ .gitattributes / .gitignore # GitHub config files
|
| 124 |
+
```
|
| 125 |
+
|
| 126 |
+
## π§ͺ How to Run Locally
|
| 127 |
+
|
| 128 |
+
### 1. Clone the repository
|
| 129 |
+
|
| 130 |
+
```bash
|
| 131 |
+
git clone [https://github.com/Ojas-Rohatgi/Deforestation-Detection](https://github.com/Ojas-Rohatgi/Deforestation-Detection)
|
| 132 |
+
cd Deforestation-Detection
|
| 133 |
+
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
### 2. Set up a virtual environment
|
| 137 |
+
|
| 138 |
+
```bash
|
| 139 |
+
python -m venv venv
|
| 140 |
+
source venv/bin/activate Β # On Windows: venv\Scripts\activate
|
| 141 |
+
pip install -r requirements.txt
|
| 142 |
+
|
| 143 |
+
```
|
| 144 |
+
|
| 145 |
+
### 3. Prepare data
|
| 146 |
+
|
| 147 |
+
Ensure you have satellite imagery and labeled NDVI masks stored in the `data/` folder.
|
| 148 |
+
You can generate training data using Earth Engine export scripts in `earth_engine/export_data.py`.
|
| 149 |
+
|
| 150 |
+
### 4. Train the model
|
| 151 |
+
|
| 152 |
+
```bash
|
| 153 |
+
python training/train.py --epochs 50 --batch\_size 16 --data\_dir ./data --save\_model ./model/unet\_deforestation.h5
|
| 154 |
+
|
| 155 |
+
```
|
| 156 |
+
You can configure the number of epochs, batch size, and save path as needed.
|
| 157 |
+
|
| 158 |
+
### 5. Run the app
|
| 159 |
+
|
| 160 |
+
```bash
|
| 161 |
+
cd app
|
| 162 |
+
streamlit run main.py
|
| 163 |
+
|
| 164 |
+
```
|
| 165 |
+
This will launch a web interface where you can:
|
| 166 |
+
|
| 167 |
+
* Select a region using the interactive Leaflet map
|
| 168 |
+
* Input a year (e.g., 2020)
|
| 169 |
+
* Fetch satellite imagery and get prediction overlays
|
| 170 |
+
|
| 171 |
+
---
|
| 172 |
+
|
| 173 |
+
## βοΈ Deployment
|
| 174 |
+
|
| 175 |
+
This app is live at:
|
| 176 |
+
π [Hugging Face Spaces β ojasrohatgi/Deforestation-Detection](https://huggingface.co/spaces/ojasrohatgi/Deforestation-Detection)
|
| 177 |
+
|
| 178 |
+
### Features:
|
| 179 |
+
|
| 180 |
+
* π Interactive map for selecting location
|
| 181 |
+
* π
Year input for time-based analysis
|
| 182 |
+
* π· Automatic image fetch via GEE
|
| 183 |
+
* π§ Real-time inference with trained U-Net
|
| 184 |
+
* πΌοΈ Visual overlay of predictions
|
| 185 |
+
* β¬οΈ Option to download the prediction mask
|
| 186 |
+
|
| 187 |
+
---
|
| 188 |
+
|
| 189 |
+
## βοΈ Tech Stack
|
| 190 |
+
|
| 191 |
+
| Component | Tools Used |
|
| 192 |
+
| ---------------- | -------------------------------------- |
|
| 193 |
+
| Model | TensorFlow 2.x, Keras, U-Net |
|
| 194 |
+
| Data Source | Google Earth Engine (Landsat-8, NDVI) |
|
| 195 |
+
| Frontend | Flask, Leaflet.js |
|
| 196 |
+
| Image Processing | NumPy, OpenCV, Matplotlib, PIL |
|
| 197 |
+
| Deployment | Hugging Face Spaces, GitHub |
|
| 198 |
+
| Output Formats | PNG masks, NumPy arrays, TFRecords |
|
| 199 |
+
|
| 200 |
+
---
|
| 201 |
+
|
| 202 |
+
## π Future Roadmap
|
| 203 |
+
|
| 204 |
+
* [ ] Multi-year change detection (e.g., forest loss between 2015β2023)
|
| 205 |
+
* [ ] Visual heatmaps of deforestation severity
|
| 206 |
+
* [ ] Region-specific fine-tuning for Southeast Asia, Amazon, and Africa
|
| 207 |
+
* [ ] Support for multispectral and SAR imagery
|
| 208 |
+
* [ ] Webhooks/API integration for automated NGO alerts
|
| 209 |
+
|
| 210 |
+
---
|
| 211 |
+
|
| 212 |
+
## πββοΈ Author
|
| 213 |
+
|
| 214 |
+
**Ojas Rohatgi**
|
| 215 |
+
Final Year B.Tech β Computer Science (Data Science & AI)
|
| 216 |
+
SRM University, Sonepat, Haryana, India
|
| 217 |
+
|
| 218 |
+
* π GitHub: [Ojas-Rohatgi](https://github.com/Ojas-Rohatgi)
|
| 219 |
+
* π Hugging Face: [ojasrohatgi](https://huggingface.co/ojasrohatgi)
|
| 220 |
+
|
| 221 |
+
---
|
| 222 |
+
|
| 223 |
+
## π License
|
| 224 |
+
|
| 225 |
+
This project is licensed under the **MIT License**.
|
| 226 |
+
See the [LICENSE](LICENSE) file for full license text.
|
| 227 |
+
|
| 228 |
+
---
|
| 229 |
+
|
| 230 |
+
## βοΈ Support This Project
|
| 231 |
+
|
| 232 |
+
If you found this useful:
|
| 233 |
+
|
| 234 |
+
* π Star this repository on GitHub
|
| 235 |
+
* π Share the Hugging Face app with your network
|
| 236 |
+
* π’ Report issues or suggest improvements in the Issues tab
|