Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i get each label's coordinates on segmentation map? #124

Closed
Baek2back opened this issue Nov 6, 2019 · 5 comments
Closed

How can i get each label's coordinates on segmentation map? #124

Baek2back opened this issue Nov 6, 2019 · 5 comments

Comments

@Baek2back
Copy link

Baek2back commented Nov 6, 2019

import numpy as np
from PIL import Image
from matplotlib import pyplot as plt

from model import Deeplabv3

# Generates labels using most basic setup.  Supports various image sizes.  Returns image labels in same format
# as original image.  Normalization matches MobileNetV2

trained_image_width=512 
mean_subtraction_value=127.5
image = np.array(Image.open('imgs/image1.jpg'))

# resize to max dimension of images from training dataset
w, h, _ = image.shape
ratio = float(trained_image_width) / np.max([w, h])
resized_image = np.array(Image.fromarray(image.astype('uint8')).resize((int(ratio * h), int(ratio * w))))

# apply normalization for trained dataset images
resized_image = (resized_image / mean_subtraction_value) - 1.

# pad array to square image to match training images
pad_x = int(trained_image_width - resized_image.shape[0])
pad_y = int(trained_image_width - resized_image.shape[1])
resized_image = np.pad(resized_image, ((0, pad_x), (0, pad_y), (0, 0)), mode='constant')

# make prediction
deeplab_model = Deeplabv3()
res = deeplab_model.predict(np.expand_dims(resized_image, 0))
labels = np.argmax(res.squeeze(), -1)

# remove padding and resize back to original image
if pad_x > 0:
    labels = labels[:-pad_x]
if pad_y > 0:
    labels = labels[:, :-pad_y]
labels = np.array(Image.fromarray(labels.astype('uint8')).resize((h, w)))

plt.imshow(labels)
plt.show()

I run this code and get segmentation map
But, I want to get result like https://github.com/bonlime/keras-deeplab-v3-plus/blob/master/imgs/seg_results2.png and each label's coordinates on image.
How can i do that?

@bonlime
Copy link
Owner

bonlime commented Nov 6, 2019

check original TF Deeplab implementation, they have code to turn segmentation maps to that kind of images. As far as I remember, it was in one of the ipynb files

@bonlime bonlime closed this as completed Nov 6, 2019
@Baek2back
Copy link
Author

Thanks a lot. I solved that!

@praisethemoon
Copy link

Hey @Baek2back, could please share how you managed to solve that? I am having the same issue.

@praisethemoon
Copy link

@Baek2back Much appreciated! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants