john5050 commited on
Commit
181c6f6
Β·
unverified Β·
2 Parent(s): e68f6e0c9d0316

Merge pull request #2 from Shivendrapratapsingh306/main

Browse files
Files changed (1) hide show
  1. README.md +219 -0
README.md ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # πŸ—οΈ Illegal Construction Detector
2
+
3
+ > **AI-powered satellite/aerial image analysis for detecting buildings in restricted zones using semantic segmentation.**
4
+
5
+ [![Python](https://img.shields.io/badge/Python-3.8%2B-blue?logo=python)](https://www.python.org/)
6
+ [![PyTorch](https://img.shields.io/badge/PyTorch-2.x-EE4C2C?logo=pytorch)](https://pytorch.org/)
7
+ [![Flask](https://img.shields.io/badge/Flask-API-black?logo=flask)](https://flask.palletsprojects.com/)
8
+ [![License](https://img.shields.io/badge/License-See%20LICENSE-green)](#license)
9
+
10
+ ---
11
+
12
+ ## πŸ“Œ Overview
13
+
14
+ This project uses a deep learning segmentation model to automatically identify buildings in aerial or satellite imagery and flag those that fall within user-defined restricted/zoning areas. It is designed for urban planning, land administration, and regulatory compliance workflows.
15
+
16
+ **Key capabilities:**
17
+ - Semantic segmentation of buildings from high-resolution aerial images
18
+ - Patch-based inference for large images of arbitrary size
19
+ - Zoning mask overlay to classify buildings as **legal** or **illegal**
20
+ - REST API backend (Flask) with base64 image response for easy frontend integration
21
+ - Visual overlay output highlighting illegal structures in red and legal structures in green
22
+
23
+ ---
24
+
25
+ ## 🧠 Model Architecture
26
+
27
+ | Component | Detail |
28
+ |---|---|
29
+ | Architecture | U-Net |
30
+ | Encoder | EfficientNet-B3 (ImageNet pretrained) |
31
+ | Loss Function | Dice Loss (binary) |
32
+ | Optimizer | Adam (lr = 1e-4) |
33
+ | LR Scheduler | ReduceLROnPlateau (patience=3, factor=0.5) |
34
+ | Input | RGB patches of 256 Γ— 256 px |
35
+ | Output | Binary segmentation mask (building / non-building) |
36
+
37
+ The model was trained on the [Massachusetts Buildings Dataset](https://www.kaggle.com/datasets/balraj98/massachusetts-buildings-dataset) for 30 epochs with augmentations including horizontal/vertical flips, random 90Β° rotation, and brightness/contrast jitter.
38
+
39
+ ---
40
+
41
+ ## πŸ—‚οΈ Repository Structure
42
+
43
+ ```
44
+ β”œβ”€β”€ app.py # Flask API server
45
+ β”œβ”€β”€ best_model.pth # Trained model weights
46
+ β”œβ”€β”€ requirements.txt # Python dependencies
47
+ β”œβ”€β”€ sourcecode.ipynb # Model training notebook (Kaggle)
48
+ β”œβ”€β”€ inference_script.ipynb # Standalone inference & visualization notebook
49
+ β”œβ”€β”€ static/
50
+ β”‚ └── index.html # Frontend UI (served by Flask)
51
+ └── README.md
52
+ ```
53
+
54
+ ---
55
+
56
+ ## βš™οΈ Installation
57
+
58
+ ### Prerequisites
59
+ - Python 3.8+
60
+ - CUDA-compatible GPU *(optional but recommended)*
61
+
62
+ ### Steps
63
+
64
+ ```bash
65
+ # 1. Clone the repository
66
+ git clone https://github.com/your-username/illegal-construction-detector.git
67
+ cd illegal-construction-detector
68
+
69
+ # 2. Create and activate a virtual environment
70
+ python -m venv venv
71
+ source venv/bin/activate # Windows: venv\Scripts\activate
72
+
73
+ # 3. Install dependencies
74
+ pip install -r requirements.txt
75
+
76
+ # 4. Place the model weights in the project root
77
+ # Ensure best_model.pth is present (download from Releases or train your own)
78
+ ```
79
+
80
+ ---
81
+
82
+ ## πŸš€ Usage
83
+
84
+ ### Option 1 β€” Flask Web API
85
+
86
+ ```bash
87
+ python app.py
88
+ ```
89
+
90
+ The server starts at `http://localhost:5000`. Navigate there in your browser to use the UI, or send requests directly:
91
+
92
+ ```bash
93
+ curl -X POST http://localhost:5000/predict \
94
+ -F "image=@/path/to/aerial_image.png"
95
+ ```
96
+
97
+ **Response JSON:**
98
+
99
+ ```json
100
+ {
101
+ "verdict": "ILLEGAL CONSTRUCTION DETECTED",
102
+ "illegal_count": 3,
103
+ "legal_count": 12,
104
+ "total_count": 15,
105
+ "illegal_percent": 8.42,
106
+ "device": "cuda",
107
+ "original": "<base64_png>",
108
+ "mask": "<base64_png>",
109
+ "overlay": "<base64_png>"
110
+ }
111
+ ```
112
+
113
+ ### Option 2 β€” Jupyter Notebook Inference
114
+
115
+ Open `inference_script.ipynb` and update the following variables:
116
+
117
+ ```python
118
+ MODEL_PATH = "best_model.pth" # path to weights
119
+ IMAGE_PATH = "test_image.png" # path to your aerial image
120
+ ```
121
+
122
+ Run all cells to generate the building mask, zoning overlay, and illegal building highlights.
123
+
124
+ ---
125
+
126
+ ## πŸ—ΊοΈ Zoning Configuration
127
+
128
+ By default, the right half of any image is treated as a restricted zone. To customise this, modify the `create_zoning_mask` function in `app.py` or the inference notebook:
129
+
130
+ ```python
131
+ def create_zoning_mask(shape):
132
+ h, w = shape
133
+ zoning = np.zeros((h, w), dtype=np.uint8)
134
+ # Example: mark a rectangular region as restricted
135
+ zoning[100:400, 200:600] = 1
136
+ return zoning
137
+ ```
138
+
139
+ Any building whose pixels overlap with the restricted zone (`zoning == 1`) is classified as **illegal**.
140
+
141
+ ---
142
+
143
+ ## πŸ‹οΈ Training
144
+
145
+ The full training pipeline is available in `sourcecode.ipynb`, designed to run on Kaggle with GPU acceleration.
146
+
147
+ **Dataset:** [Massachusetts Buildings Dataset](https://www.kaggle.com/datasets/balraj98/massachusetts-buildings-dataset)
148
+
149
+ **Training configuration:**
150
+
151
+ ```python
152
+ PATCH_SIZE = 256
153
+ BATCH_SIZE = 8
154
+ EPOCHS = 30
155
+ LEARNING_RATE = 1e-4
156
+ ```
157
+
158
+ **Augmentations:** Horizontal flip, vertical flip, random 90Β° rotation, brightness/contrast jitter
159
+
160
+ **Metrics tracked:** Dice Loss Β· IoU Score
161
+
162
+ To retrain, update `BASE_PATH` in the notebook to your dataset location and run all cells. The best-performing checkpoint (highest IoU) is saved as `best_model.pth`.
163
+
164
+ ---
165
+
166
+ ## πŸ“Š Results
167
+
168
+ | Metric | Value |
169
+ |---|---|
170
+ | Validation IoU | Tracked per epoch |
171
+ | Inference mode | Patch-based (256Γ—256), supports arbitrary image sizes |
172
+ | Supported formats | PNG, JPG, TIFF |
173
+
174
+ ---
175
+
176
+ ## πŸ”§ Dependencies
177
+
178
+ ```
179
+ flask
180
+ torch
181
+ torchvision
182
+ segmentation-models-pytorch
183
+ albumentations
184
+ opencv-python
185
+ pillow
186
+ numpy
187
+ ```
188
+
189
+ Install all with:
190
+ ```bash
191
+ pip install -r requirements.txt
192
+ ```
193
+
194
+ > **Note for Windows users:** If you encounter a `fbgemm.dll` error when importing PyTorch, ensure you have the [Microsoft Visual C++ Redistributable](https://aka.ms/vs/17/release/vc_redist.x64.exe) installed.
195
+
196
+ ---
197
+
198
+ ## 🀝 Contributing
199
+
200
+ Contributions are welcome. Please open an issue first to discuss proposed changes, then submit a pull request.
201
+
202
+ 1. Fork the repository
203
+ 2. Create a feature branch (`git checkout -b feature/your-feature`)
204
+ 3. Commit your changes (`git commit -m 'Add your feature'`)
205
+ 4. Push and open a Pull Request
206
+
207
+ ---
208
+
209
+ ## πŸ“„ License
210
+
211
+ See the [LICENSE](LICENSE) file for details.
212
+
213
+ ---
214
+
215
+ ## πŸ™ Acknowledgements
216
+
217
+ - [Massachusetts Buildings Dataset](https://www.kaggle.com/datasets/balraj98/massachusetts-buildings-dataset) by Balraj Ashwath
218
+ - [segmentation-models-pytorch](https://github.com/qubvel/segmentation_models.pytorch) by Pavel Iakubovskii
219
+ - [Albumentations](https://albumentations.ai/) augmentation library