7. Test with ICNet Pre-trained Models for Multi-Human Parsing

This is a quick demo of using GluonCV ICNet model for multi-human parsing on real-world images. Please follow the installation guide to install MXNet and GluonCV if not yet.

import mxnet as mx
from mxnet import image
from mxnet.gluon.data.vision import transforms
import gluoncv
# using cpu
ctx = mx.cpu(0)

Prepare the image

Let’s first download the example image,

url = 'https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/segmentation/mhpv1_examples/1.jpg'
filename = 'mhp_v1_example.jpg'
gluoncv.utils.download(url, filename, True)

Out:

Downloading mhp_v1_example.jpg from https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/segmentation/mhpv1_examples/1.jpg...

  0%|          | 0/84 [00:00<?, ?KB/s]
85KB [00:00, 14419.25KB/s]

Then we load the image and visualize it,

img = image.imread(filename)

from matplotlib import pyplot as plt
plt.imshow(img.asnumpy())
plt.show()
demo icnet

We normalize the image using dataset mean and standard deviation,

from gluoncv.data.transforms.presets.segmentation import test_transform
img = test_transform(img, ctx)

Load the pre-trained model and make prediction

Next, we get a pre-trained model from our model zoo,

model = gluoncv.model_zoo.get_model('icnet_resnet50_mhpv1', pretrained=True)

Out:

Downloading /root/.mxnet/models/icnet_resnet50_mhpv1-873d381a.zip from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/models/icnet_resnet50_mhpv1-873d381a.zip...

  0%|          | 0/185766 [00:00<?, ?KB/s]
  0%|          | 92/185766 [00:00<04:40, 661.23KB/s]
  0%|          | 518/185766 [00:00<01:30, 2051.49KB/s]
  1%|1         | 2179/185766 [00:00<00:28, 6517.44KB/s]
  4%|3         | 7086/185766 [00:00<00:08, 20013.91KB/s]
  7%|7         | 13542/185766 [00:00<00:05, 33903.21KB/s]
 11%|#1        | 20471/185766 [00:00<00:03, 44807.52KB/s]
 14%|#4        | 26633/185766 [00:00<00:03, 49691.15KB/s]
 17%|#7        | 31897/185766 [00:00<00:03, 43310.39KB/s]
 20%|#9        | 36563/185766 [00:01<00:03, 40737.39KB/s]
 23%|##2       | 42245/185766 [00:01<00:03, 44955.38KB/s]
 27%|##6       | 49396/185766 [00:01<00:02, 52194.93KB/s]
 30%|##9       | 54872/185766 [00:01<00:03, 33523.15KB/s]
 32%|###1      | 59226/185766 [00:01<00:03, 33987.85KB/s]
 34%|###4      | 63336/185766 [00:01<00:03, 32299.56KB/s]
 37%|###7      | 68966/185766 [00:01<00:03, 37566.57KB/s]
 39%|###9      | 73260/185766 [00:02<00:03, 30103.28KB/s]
 43%|####3     | 80278/185766 [00:02<00:02, 38479.20KB/s]
 47%|####6     | 86968/185766 [00:02<00:02, 38813.54KB/s]
 49%|####9     | 91384/185766 [00:02<00:02, 36963.38KB/s]
 51%|#####1    | 95651/185766 [00:02<00:02, 36643.49KB/s]
 54%|#####3    | 99563/185766 [00:02<00:02, 36852.37KB/s]
 56%|#####5    | 103426/185766 [00:02<00:02, 35088.90KB/s]
 58%|#####7    | 107056/185766 [00:03<00:02, 28958.16KB/s]
 59%|#####9    | 110176/185766 [00:03<00:03, 20784.08KB/s]
 62%|######2   | 115216/185766 [00:03<00:02, 25265.01KB/s]
 64%|######3   | 118215/185766 [00:03<00:03, 20193.52KB/s]
 65%|######4   | 120679/185766 [00:03<00:03, 18496.45KB/s]
 67%|######6   | 123970/185766 [00:04<00:03, 20101.71KB/s]
 71%|#######   | 131441/185766 [00:04<00:01, 31145.65KB/s]
 75%|#######4  | 138810/185766 [00:04<00:01, 39739.13KB/s]
 78%|#######7  | 144413/185766 [00:04<00:00, 43620.14KB/s]
 82%|########1 | 152227/185766 [00:04<00:00, 43303.90KB/s]
 85%|########4 | 156981/185766 [00:04<00:00, 32454.01KB/s]
 88%|########8 | 163595/185766 [00:04<00:00, 38281.65KB/s]
 91%|######### | 168120/185766 [00:05<00:00, 32108.57KB/s]
 93%|#########2| 172644/185766 [00:05<00:00, 28814.79KB/s]
 95%|#########4| 175979/185766 [00:05<00:00, 29390.64KB/s]
 99%|#########8| 183517/185766 [00:05<00:00, 39144.18KB/s]
100%|##########| 185766/185766 [00:05<00:00, 33020.61KB/s]

We directly make semantic predictions on the image,

output = model.predict(img)
predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()

In the end, we add color pallete for visualizing the predicted mask,

from gluoncv.utils.viz import get_color_pallete
import matplotlib.image as mpimg
mask = get_color_pallete(predict, 'mhpv1')
mask.save('output.png')
mmask = mpimg.imread('output.png')
plt.imshow(mmask)
plt.show()
demo icnet

Total running time of the script: ( 0 minutes 8.225 seconds)

Gallery generated by Sphinx-Gallery