Closed raialvaro closed 2 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.
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!
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)
Ohhh! Thank so much! You're very kind!!
It works great. Good luck!
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)
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.
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!