Upload folder using huggingface_hub
Browse files- .gitattributes +9 -0
- Building_Segmentation.ipynb +0 -0
- README.md +23 -7
- __pycache__/demo.cpython-310.pyc +0 -0
- __pycache__/utils.cpython-310.pyc +0 -0
- building_segmentation_model.keras +3 -0
- demo.py +36 -0
- images/22678945_15.png +3 -0
- images/22828930_15.png +3 -0
- images/23429080_15.png +3 -0
- images/23729035_15.png +3 -0
- images/24179065_15.png +3 -0
- mask.png +3 -0
- mask2.png +3 -0
- out.png +0 -0
- requirements.txt +6 -0
- task_definition.png +3 -0
- utils.py +75 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,12 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
building_segmentation_model.keras filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
images/22678945_15.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
images/22828930_15.png filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
images/23429080_15.png filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
images/23729035_15.png filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
images/24179065_15.png filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
mask.png filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
mask2.png filter=lfs diff=lfs merge=lfs -text
|
| 44 |
+
task_definition.png filter=lfs diff=lfs merge=lfs -text
|
Building_Segmentation.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
README.md
CHANGED
|
@@ -1,12 +1,28 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: indigo
|
| 5 |
-
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.24.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: building_footprint_segmentation
|
| 3 |
+
app_file: demo.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
sdk_version: 4.24.0
|
|
|
|
|
|
|
| 6 |
---
|
| 7 |
+
A U-Net model for segmenting buildings from satellite imagery
|
| 8 |
|
| 9 |
+
A binary segmentation mask (of the same height and width with the input image) should be created The segmentation mask should have a value of 1 at pixels where there is a building and 0 at other pixels.
|
| 10 |
+
|
| 11 |
+
The figure below showcases the input and output image expected. In the mask pixels that correspond to pixels in the input image are white and background is black.
|
| 12 |
+
|
| 13 |
+

|
| 14 |
+
|
| 15 |
+
# Data
|
| 16 |
+
The data used in this project is sourced from [Road and Building Detection Datasets](https://www.cs.toronto.edu/~vmnih/data/) with the following citation:
|
| 17 |
+
|
| 18 |
+
```
|
| 19 |
+
@phdthesis{MnihThesis,
|
| 20 |
+
author = {Volodymyr Mnih},
|
| 21 |
+
title = {Machine Learning for Aerial Image Labeling},
|
| 22 |
+
school = {University of Toronto},
|
| 23 |
+
year = {2013}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
For the ease of use, relevant parts of this dataset was sourced from [kaggle.com](https://www.kaggle.com/datasets/balraj98/massachusetts-buildings-dataset)
|
__pycache__/demo.cpython-310.pyc
ADDED
|
Binary file (817 Bytes). View file
|
|
|
__pycache__/utils.cpython-310.pyc
ADDED
|
Binary file (2.06 kB). View file
|
|
|
building_segmentation_model.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:468bf3edbc57bff66868ca0dc86d351f7a6f938c80f4c20b7e4da7fc5058a74d
|
| 3 |
+
size 89891608
|
demo.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import utils
|
| 3 |
+
from keras.models import load_model
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
model = load_model("building_segmentation_model.keras")
|
| 8 |
+
|
| 9 |
+
def segment(img):
|
| 10 |
+
mask = utils.predict(model, img)
|
| 11 |
+
mask = mask[..., 0]
|
| 12 |
+
|
| 13 |
+
# Set masked regions to blue with 70% transparency
|
| 14 |
+
blue_color = np.array([255, 0, 0], dtype=np.uint8) # Red color
|
| 15 |
+
transparency = 0.7
|
| 16 |
+
|
| 17 |
+
masked_image = img.copy()
|
| 18 |
+
masked_image[mask == 1] = masked_image[mask == 1] * (1 - transparency) + transparency * blue_color
|
| 19 |
+
|
| 20 |
+
return masked_image
|
| 21 |
+
|
| 22 |
+
demo = gr.Interface(
|
| 23 |
+
fn = segment,
|
| 24 |
+
inputs=[gr.Image()],
|
| 25 |
+
outputs=[gr.Image()],
|
| 26 |
+
examples=[
|
| 27 |
+
"images/22678945_15.png",
|
| 28 |
+
"images/22828930_15.png",
|
| 29 |
+
"images/23429080_15.png",
|
| 30 |
+
"images/23729035_15.png",
|
| 31 |
+
"images/24179065_15.png",
|
| 32 |
+
]
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
demo.launch()
|
images/22678945_15.png
ADDED
|
Git LFS Details
|
images/22828930_15.png
ADDED
|
Git LFS Details
|
images/23429080_15.png
ADDED
|
Git LFS Details
|
images/23729035_15.png
ADDED
|
Git LFS Details
|
images/24179065_15.png
ADDED
|
Git LFS Details
|
mask.png
ADDED
|
Git LFS Details
|
mask2.png
ADDED
|
Git LFS Details
|
out.png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.24.0
|
| 2 |
+
keras==2.15.0
|
| 3 |
+
matplotlib==3.7.1
|
| 4 |
+
matplotlib==3.5.1
|
| 5 |
+
numpy==1.23.5
|
| 6 |
+
numpy==1.21.5
|
task_definition.png
ADDED
|
Git LFS Details
|
utils.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
def calculate_padding(image, patch_size=256):
|
| 5 |
+
"""
|
| 6 |
+
Calculates required padding amounts
|
| 7 |
+
|
| 8 |
+
ARGS:
|
| 9 |
+
image: Image to be padded (height, width, channels)
|
| 10 |
+
patch_size: Stride of the patches
|
| 11 |
+
|
| 12 |
+
RETURNS:
|
| 13 |
+
padding: Padding amounts (top, bottom, left, right)
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
height, width = image.shape[-3:-1]
|
| 17 |
+
|
| 18 |
+
# Calculate Desired shape
|
| 19 |
+
xx = (width // patch_size +1) * patch_size
|
| 20 |
+
yy = (height // patch_size +1) * patch_size
|
| 21 |
+
|
| 22 |
+
# Calculate padding amounts
|
| 23 |
+
x_pad = xx - width
|
| 24 |
+
y_pad = yy - height
|
| 25 |
+
|
| 26 |
+
x0 = x_pad // 2
|
| 27 |
+
x1 = x_pad - x0
|
| 28 |
+
y0 = y_pad // 2
|
| 29 |
+
y1 = y_pad - y0
|
| 30 |
+
|
| 31 |
+
return (y0, y1, x0, x1)
|
| 32 |
+
|
| 33 |
+
def apply_padding(image, patch_size=256):
|
| 34 |
+
"""
|
| 35 |
+
Adds padding to the image to make patches of equal size that covers the entire image
|
| 36 |
+
|
| 37 |
+
ARGS:
|
| 38 |
+
image: Image to be padded
|
| 39 |
+
patch_size: Stride of the patches
|
| 40 |
+
|
| 41 |
+
RETURNS:
|
| 42 |
+
padded_image: Padded image
|
| 43 |
+
"""
|
| 44 |
+
width, height = image.shape[:2]
|
| 45 |
+
pad_y0, pad_y1, pad_x0, pad_x1 = calculate_padding(image, patch_size)
|
| 46 |
+
|
| 47 |
+
return np.pad(image, ((pad_y0, pad_y1), (pad_x0, pad_x1), (0, 0)), mode='constant')
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def predict(model, img, threshold=0.5, patch_size=256):
|
| 51 |
+
"""
|
| 52 |
+
model: a keras model
|
| 53 |
+
img: a np array representing the image (expects float dtype and values between 0 and 1)
|
| 54 |
+
"""
|
| 55 |
+
padded = apply_padding(img, patch_size)
|
| 56 |
+
|
| 57 |
+
number_of_rows, number_of_columns = math.ceil(img.shape[0] /patch_size), math.ceil(img.shape[1] /patch_size)
|
| 58 |
+
# fig, axes = plt.subplots(number_of_rows, number_of_columns, figsize=(20, 20))
|
| 59 |
+
prediction = np.zeros((number_of_rows * patch_size, number_of_columns * patch_size, 1), dtype="float32")
|
| 60 |
+
|
| 61 |
+
for i in range(0, number_of_rows):
|
| 62 |
+
for j in range(0, number_of_columns):
|
| 63 |
+
# Convert to float
|
| 64 |
+
slc = padded[i*patch_size:(i+1)*patch_size, j*patch_size:(j+1)*patch_size] / 256.0
|
| 65 |
+
pred = model.predict(np.expand_dims(slc, axis = 0), verbose = 0)
|
| 66 |
+
# axes[i][j].imshow(pred[0])
|
| 67 |
+
prediction[i*patch_size:(i+1)*patch_size, j*patch_size:(j+1)*patch_size] = pred[0]
|
| 68 |
+
|
| 69 |
+
# Threshold the results
|
| 70 |
+
prediction = (prediction > threshold).astype(np.uint8)
|
| 71 |
+
|
| 72 |
+
# Reverse padding
|
| 73 |
+
y0, y1, x0, x1 = calculate_padding(img, patch_size)
|
| 74 |
+
preds = prediction[y0:-y1, x0:-x1]
|
| 75 |
+
return preds
|