Spaces:
Sleeping
Sleeping
File size: 1,222 Bytes
b585c6d e8d9048 b585c6d c5fbcc3 b585c6d c5fbcc3 b585c6d e8d9048 b585c6d c5fbcc3 b585c6d c5fbcc3 b585c6d c5fbcc3 e8d9048 b585c6d e8d9048 b585c6d c5fbcc3 b585c6d c5fbcc3 b585c6d e8d9048 b585c6d c5fbcc3 | 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 | import gradio as gr
import torch
import torchvision.transforms as transforms
from PIL import Image
import numpy as np
import segmentation_models_pytorch as smp
# DEVICE
device = "cuda" if torch.cuda.is_available() else "cpu"
# MODEL
model = smp.UnetPlusPlus(
encoder_name="resnet34",
encoder_weights=None,
in_channels=3,
classes=1,
activation=None
).to(device)
model.load_state_dict(
torch.load("buildings_unetpp_model.pth", map_location=device)
)
model.eval()
# TRANSFORM
transform = transforms.Compose([
transforms.Resize((256, 256)),
transforms.ToTensor()
])
# PREDICT
def predict(image):
original_size = image.size # save size
x = transform(image).unsqueeze(0).to(device)
with torch.no_grad():
pred = model(x)[0][0].cpu().numpy()
mask = (pred > 0.5).astype(np.uint8) * 255
# resize back
mask = Image.fromarray(mask).resize(original_size)
return np.array(mask)
# UI
demo = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"), # 🔥 FIX HERE
outputs=gr.Image(type="numpy"),
title="🏗️ Building Extraction (U-Net++)",
description="Upload satellite/drone image to detect buildings"
)
demo.launch(ssr_mode=False) |