File size: 7,832 Bytes
5339b55
4694583
5339b55
4694583
 
5339b55
4694583
5339b55
4694583
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
---
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.

<p align="center">
  <img src="Final Output.png" width="70%"/>
</p>

---

## πŸ“Œ 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