fuzailpalnak / building-footprint-segmentation

Building footprint segmentation from satellite and aerial imagery
https://fuzailpalnak-buildingextraction-appbuilding-extraction-s-ov1rp9.streamlitapp.com/
Apache License 2.0
134 stars 32 forks source link

Use weight file #23

Closed raialvaro closed 2 years ago

raialvaro commented 3 years ago

Hello my friend!

your work is amazing! Congratulations.

I need your help. I wish I could use the pre-trained weights. Also, how can I use the code to be able to use some test images and verify that it performs the segmentation correctly!

Thank so much!

fuzailpalnak commented 3 years ago

ThankYou!!! Of course, you can use pre-trained weights along with the code. You could also check the segmentation performance on https://building-extraction-demo.herokuapp.com/, where this branch is deployed.

raialvaro commented 3 years ago

Shows fantastic results. Thanks!!

I want to use this model on my local machine and using the "RefineNet" pretrained weights that it has in the repository, what code should I use?

Thank so much!

fuzailpalnak commented 3 years ago

Use this script to run locally with the RefineNet weights

import cv2
from building_footprint_segmentation.seg.binary.models import ReFineNet
from building_footprint_segmentation.helpers.normalizer import min_max_image_net
from building_footprint_segmentation.utils.py_network import (
    to_input_image_tensor,
    add_extra_dimension,
    convert_tensor_to_numpy,
    load_parallel_model
)
from building_footprint_segmentation.utils.operations import handle_image_size
from torch.utils import model_zoo

MAX_SIZE = 384
MODEL_URL = "https://github.com/fuzailpalnak/building-footprint-segmentation/releases/download/alpha/refine.zip"

def get_model():
    refine_net = ReFineNet()
    state_dict = model_zoo.load_url(MODEL_URL, progress=True, map_location="cpu")
    refine_net.load_state_dict(state_dict)
    return refine_net

def extract(original_image, model):

    original_height, original_width = original_image.shape[:2]

    if (original_height, original_width) != (MAX_SIZE, MAX_SIZE):
        original_image = handle_image_size(original_image, (MAX_SIZE, MAX_SIZE))

    # Apply Normalization
    normalized_image = min_max_image_net(img=original_image)

    tensor_image = add_extra_dimension(to_input_image_tensor(normalized_image))

    with torch.no_grad():
        # Perform prediction
        prediction = model(tensor_image)
        prediction = prediction.sigmoid()

    prediction_binary = convert_tensor_to_numpy(prediction[0]).reshape(
        (MAX_SIZE, MAX_SIZE)
    )

    prediction_3_channels = cv2.cvtColor(prediction_binary, cv2.COLOR_GRAY2RGB)

    dst = cv2.addWeighted(
        original_image,
        1,
        (prediction_3_channels * (0, 255, 0)).astype(np.uint8),
        0.4,
        0,
    )
    return prediction_binary, prediction_3_channels, dst

def run(image_path):
    original_image = cv2.imread(image_path)
    original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)

    model = get_model()
    # PARALLELIZE the model if gpu available
    # model = load_parallel_model(model)

    prediction_binary, prediction_3_channels, dst = extract(original_image, model)
raialvaro commented 3 years ago

Ohhh! Thank so much! You're very kind!!

It works great. Good luck!

JuliaWasala commented 2 years ago

Hi Fuzail, I tried doing this with the massachusets buildings dataset cropped to 256x256 pixels, but I get the following error: `--------------------------------------------------------------------------- ValueError Traceback (most recent call last)

in ----> 1 pred_bin,_,dst = run(input,model) in run(image_path, model) 5 # model = load_parallel_model(model) 6 ----> 7 return extract(original_image, model) 8 in extract(original_image, model) 6 7 if (original_height, original_width) != (MAX_SIZE, MAX_SIZE): ----> 8 original_image = handle_image_size(original_image, (MAX_SIZE, MAX_SIZE)) 9 10 # Apply Normalization ~/amrefenv/lib/python3.6/site-packages/building_footprint_segmentation/utils/operations.py in handle_image_size(input_image, dimension) 236 237 elif dimension > (h, w): --> 238 limit = get_pad_limit(dimension, (h, w)) 239 input_image = pad_image(input_image, limit) 240 ~/amrefenv/lib/python3.6/site-packages/building_footprint_segmentation/utils/operations.py in get_pad_limit(model_input_dimension, image_input_dimension) 133 """ 134 model_height, model_width = model_input_dimension --> 135 image_height, image_width, _ = image_input_dimension 136 137 limit = (model_height - image_height) // 2 ValueError: not enough values to unpack (expected 3, got 2)` I get the same when i upload an image to your online app. Do you know how to fix this?
fuzailpalnak commented 2 years ago

Its an error on https://github.com/fuzailpalnak/building-footprint-segmentation/blob/8c9880975c4db41045a81cd4780cf541d4a23727/building_footprint_segmentation/utils/operations.py#L238, the function parameter is a 2d tuple and internally it expects a 3d tuple, this issue is with the pad limit, i will try to fix this as soon as i get time, in the mean time, you can try images larger than 384x384, that will work.