Steps you must follow to use DeepLab V3+ model for semantic segmentation

Here are the steps that must be followed to be able to use the model to segment an image:

  1. First, clone or download the repository from https://github.com/bonlime/keras-deeplab-v3-plus.
  2. Extract the ZIP file downloaded to the keras-deeplab-v3-plus-master folder.
  3. Navigate to the keras-deeplab-v3-plus-master folder; the following code needs to be run from inside the directory.

Before running the following code block, create an input folder and an empty output folder. Save your images you want to segment inside the input folder. The following code block shows how to use the Deeplabv3+ in Python to do semantic segmentation:

#os.chdir('keras-deeplab-v3-plus-master') # go to keras-deeplab-v3-plusmaster
from matplotlib import pyplot as pylab
import cv2 # used for resize
import numpy as np
from model import Deeplabv3
deeplab_model = Deeplabv3()
pathIn = 'input' # path for the input image
pathOut = 'output' # output path for the segmented image
img = pylab.imread(pathIn + "/cycle.jpg")
w, h, _ = img.shape
ratio = 512. / np.max([w,h])
resized = cv2.resize(img,(int(ratio*h),int(ratio*w)))
resized = resized / 127.5 - 1.
pad_x = int(512 - resized.shape[0])
resized2 = np.pad(resized,((0,pad_x),(0,0),(0,0)),mode='constant')
res = deeplab_model.predict(np.expand_dims(resized2,0))
labels = np.argmax(res.squeeze(),-1)
pylab.figure(figsize=(20,20))
pylab.imshow(labels[:-pad_x], cmap='inferno'), pylab.axis('off'), pylab.colorbar()
pylab.show()
pylab.savefig(pathOut + "\segmented.jpg", bbox_inches='tight', pad_inches=0)
pylab.close()
#os.chdir('..')

The following photo is the input image fed to the DeepLab v3+ model:

The following screenshot shows the output created after semantic segmentation of the previous photo, obtained by running the preceding code:

You can obtain the labels of the segments and create an overlay with yet another input image, as shown in the following diagram:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.76.237