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 the result images? #128

Open
vickylibra opened this issue Dec 4, 2019 · 1 comment
Open

How can I get the result images? #128

vickylibra opened this issue Dec 4, 2019 · 1 comment

Comments

@vickylibra
Copy link

After using predict(), I get labels now, but how can I get the labeled images? Sorry, I'm new in this, thanks!

@sonukiller
Copy link

sonukiller commented Feb 5, 2023

Hi,
After you use model.predict(img), you will get logits. You have to use argmax and then you will get the label. Now you have the labels, you can directly plot them plt.imshow(labels)

In the page itself, the author has given following code, use it:
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)

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

2 participants