bonlime / keras-deeplab-v3-plus

Keras implementation of Deeplab v3+ with pretrained weights
MIT License
1.35k stars 428 forks source link

How can I get the result images? #128

Open vickylibra opened 4 years ago

vickylibra commented 4 years ago

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

sonukiller commented 1 year ago

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)