Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,17 +5,13 @@ from PIL import Image
|
|
| 5 |
import numpy as np
|
| 6 |
import segmentation_models_pytorch as smp
|
| 7 |
|
| 8 |
-
# =========================
|
| 9 |
# DEVICE
|
| 10 |
-
# =========================
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
# LOAD MODEL
|
| 15 |
-
# =========================
|
| 16 |
model = smp.UnetPlusPlus(
|
| 17 |
encoder_name="resnet34",
|
| 18 |
-
encoder_weights=None,
|
| 19 |
in_channels=3,
|
| 20 |
classes=1,
|
| 21 |
activation=None
|
|
@@ -27,36 +23,35 @@ model.load_state_dict(
|
|
| 27 |
|
| 28 |
model.eval()
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
# PREPROCESS
|
| 32 |
-
# =========================
|
| 33 |
transform = transforms.Compose([
|
| 34 |
transforms.Resize((256, 256)),
|
| 35 |
transforms.ToTensor()
|
| 36 |
])
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
# PREDICTION
|
| 40 |
-
# =========================
|
| 41 |
def predict(image):
|
| 42 |
-
|
|
|
|
| 43 |
x = transform(image).unsqueeze(0).to(device)
|
| 44 |
|
| 45 |
with torch.no_grad():
|
| 46 |
pred = model(x)[0][0].cpu().numpy()
|
| 47 |
|
| 48 |
mask = (pred > 0.5).astype(np.uint8) * 255
|
| 49 |
-
return mask
|
| 50 |
|
| 51 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# UI
|
| 53 |
-
# =========================
|
| 54 |
demo = gr.Interface(
|
| 55 |
fn=predict,
|
| 56 |
-
inputs=gr.Image(type="
|
| 57 |
outputs=gr.Image(type="numpy"),
|
| 58 |
title="🏗️ Building Extraction (U-Net++)",
|
| 59 |
description="Upload satellite/drone image to detect buildings"
|
| 60 |
)
|
| 61 |
|
| 62 |
-
demo.launch(ssr_mode=False)
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import segmentation_models_pytorch as smp
|
| 7 |
|
|
|
|
| 8 |
# DEVICE
|
|
|
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
+
# MODEL
|
|
|
|
|
|
|
| 12 |
model = smp.UnetPlusPlus(
|
| 13 |
encoder_name="resnet34",
|
| 14 |
+
encoder_weights=None,
|
| 15 |
in_channels=3,
|
| 16 |
classes=1,
|
| 17 |
activation=None
|
|
|
|
| 23 |
|
| 24 |
model.eval()
|
| 25 |
|
| 26 |
+
# TRANSFORM
|
|
|
|
|
|
|
| 27 |
transform = transforms.Compose([
|
| 28 |
transforms.Resize((256, 256)),
|
| 29 |
transforms.ToTensor()
|
| 30 |
])
|
| 31 |
|
| 32 |
+
# PREDICT
|
|
|
|
|
|
|
| 33 |
def predict(image):
|
| 34 |
+
original_size = image.size # save size
|
| 35 |
+
|
| 36 |
x = transform(image).unsqueeze(0).to(device)
|
| 37 |
|
| 38 |
with torch.no_grad():
|
| 39 |
pred = model(x)[0][0].cpu().numpy()
|
| 40 |
|
| 41 |
mask = (pred > 0.5).astype(np.uint8) * 255
|
|
|
|
| 42 |
|
| 43 |
+
# resize back
|
| 44 |
+
mask = Image.fromarray(mask).resize(original_size)
|
| 45 |
+
|
| 46 |
+
return np.array(mask)
|
| 47 |
+
|
| 48 |
# UI
|
|
|
|
| 49 |
demo = gr.Interface(
|
| 50 |
fn=predict,
|
| 51 |
+
inputs=gr.Image(type="pil"), # 🔥 FIX HERE
|
| 52 |
outputs=gr.Image(type="numpy"),
|
| 53 |
title="🏗️ Building Extraction (U-Net++)",
|
| 54 |
description="Upload satellite/drone image to detect buildings"
|
| 55 |
)
|
| 56 |
|
| 57 |
+
demo.launch(ssr_mode=False)
|