---
license: apache-2.0
sdk: docker
pinned: true
short_description: Detect deforestation from satellite imagery.
---
# ๐ณ Deforestation Detection using Satellite Imagery
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.
---
## ๐ Project Objective
> **Can an automated model built on satellite imagery help environmental organizations monitor illegal deforestation in near real-time?**
We aim to answer this by building a robust deep learning pipeline that:
- Collects satellite imagery for selected regions and years.
- Trains a U-Net segmentation model on forest/non-forest (or deforested) labels.
- Allows users to input coordinates + year and visualize predictions on a Leaflet map.
---
## ๐ฐ๏ธ Data Collection & Preprocessing
We use **Google Earth Engine (GEE)** to fetch Sentinel-2 or Landsat-8 imagery and NDVI indices.
### Data Sources:
- **Imagery**: Landsat-8 (30m resolution), optionally Sentinel-2 (10m resolution).
- **Labels**: Derived using NDVI and/or forest loss layers from Global Forest Change dataset.
### Preprocessing:
- Region-wise time-lapse collection.
- NDVI-based labeling: Thresholding NDVI to identify vegetation loss.
- Tiling large images into patches.
- Saving as `.npy` and `.tfrecord` formats for model training.
---
## ๐ง Model Architecture
The model uses a **U-Net architecture** implemented in **TensorFlow**, designed for image segmentation tasks.
- **Input**: Satellite image patches (RGB or NDVI composite).
- **Output**: Binary mask indicating deforested areas.
### Features:
- Batch normalization and dropout regularization
- Dice coefficient + binary cross-entropy loss
- Data augmentation (rotation, flipping, contrast)
---
## โ๏ธ Training
```bash
python train.py --epochs 50 --batch_size 16 --data_dir ./data --save_model ./models/unet_deforestation.h5
```
## ๐ Evaluation
The trained model is evaluated on a held-out test set using the following metrics:
- **IoU (Intersection over Union)**: Measures the overlap between predicted deforested area and ground truth.
- **Dice Coefficient**: Especially useful for imbalanced classes.
- **Precision & Recall**: To understand false positives and false negatives.
- **F1 Score**: Harmonic mean of precision and recall.
---
## ๐ Deployment
### Hugging Face Space ๐
Visit: [**Deforestation Detection on Hugging Face**](https://huggingface.co/spaces/ojasrohatgi/Deforestation-Detection)
Features:
- Interactive Leaflet map to select coordinates
- Year input to analyze temporal deforestation
- On-click image fetching and prediction overlay
- Downloadable prediction masks for offline use
### Backend Logic
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.
---
## ๐งพ Project Structure
```bash
Deforestation-Detection/
โ
โโโ app/ # Streamlit-based frontend interface
โ โโโ main.py # Streamlit UI & user interaction logic
โ โโโ map_component.html # Leaflet map integration for coordinate input
โ โโโ overlay.py # Image overlay + visualization helpers
โ โโโ utils.py # Patching, preprocessing, and GEE interaction
โ
โโโ model/ # Trained model artifacts
โ โโโ unet_deforestation.h5 # Final trained U-Net model
โ
โโโ earth_engine/ # Scripts for working with GEE
โ โโโ export_data.py # Downloads satellite images + NDVI labels
โ
โโโ training/ # Model architecture and training logic
โ โโโ train.py # Model training loop
โ โโโ evaluate.py # Evaluation script for test data
โ โโโ metrics.py # IoU, Dice, and other evaluation metrics
โ โโโ unet_model.py # Custom U-Net implementation
โ
โโโ data/ # Data directory (images, masks, TFRecords)
โ โโโ train/ # Training image and mask patches
โ โโโ val/ # Validation image and mask patches
โ โโโ test/ # Test image and mask patches
โ
โโโ requirements.txt # Python dependencies
โโโ README.md # This file
โโโ .gitattributes / .gitignore # GitHub config files
```
## ๐งช How to Run Locally
### 1. Clone the repository
```bash
git clone [https://github.com/Ojas-Rohatgi/Deforestation-Detection](https://github.com/Ojas-Rohatgi/Deforestation-Detection)
cd Deforestation-Detection
```
### 2. Set up a virtual environment
```bash
python -m venv venv
source venv/bin/activate ย # On Windows: venv\Scripts\activate
pip install -r requirements.txt
```
### 3. Prepare data
Ensure you have satellite imagery and labeled NDVI masks stored in the `data/` folder.
You can generate training data using Earth Engine export scripts in `earth_engine/export_data.py`.
### 4. Train the model
```bash
python training/train.py --epochs 50 --batch\_size 16 --data\_dir ./data --save\_model ./model/unet\_deforestation.h5
```
You can configure the number of epochs, batch size, and save path as needed.
### 5. Run the app
```bash
cd app
streamlit run main.py
```
This will launch a web interface where you can:
* Select a region using the interactive Leaflet map
* Input a year (e.g., 2020)
* Fetch satellite imagery and get prediction overlays
---
## โ๏ธ Deployment
This app is live at:
๐ [Hugging Face Spaces โ ojasrohatgi/Deforestation-Detection](https://huggingface.co/spaces/ojasrohatgi/Deforestation-Detection)
### Features:
* ๐ Interactive map for selecting location
* ๐
Year input for time-based analysis
* ๐ท Automatic image fetch via GEE
* ๐ง Real-time inference with trained U-Net
* ๐ผ๏ธ Visual overlay of predictions
* โฌ๏ธ Option to download the prediction mask
---
## โ๏ธ Tech Stack
| Component | Tools Used |
| ---------------- | -------------------------------------- |
| Model | TensorFlow 2.x, Keras, U-Net |
| Data Source | Google Earth Engine (Landsat-8, NDVI) |
| Frontend | Flask, Leaflet.js |
| Image Processing | NumPy, OpenCV, Matplotlib, PIL |
| Deployment | Hugging Face Spaces, GitHub |
| Output Formats | PNG masks, NumPy arrays, TFRecords |
---
## ๐ Future Roadmap
* [ ] Multi-year change detection (e.g., forest loss between 2015โ2023)
* [ ] Visual heatmaps of deforestation severity
* [ ] Region-specific fine-tuning for Southeast Asia, Amazon, and Africa
* [ ] Support for multispectral and SAR imagery
* [ ] Webhooks/API integration for automated NGO alerts
---
## ๐โโ๏ธ Author
**Ojas Rohatgi**
Final Year B.Tech โ Computer Science (Data Science & AI)
SRM University, Sonepat, Haryana, India
* ๐ GitHub: [Ojas-Rohatgi](https://github.com/Ojas-Rohatgi)
* ๐ Hugging Face: [ojasrohatgi](https://huggingface.co/ojasrohatgi)
---
## ๐ License
This project is licensed under the **MIT License**.
See the [LICENSE](LICENSE) file for full license text.
---
## โญ๏ธ Support This Project
If you found this useful:
* ๐ Star this repository on GitHub
* ๐ Share the Hugging Face app with your network
* ๐ข Report issues or suggest improvements in the Issues tab