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, 15852.19KB/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]
  4%|4         | 7917/185766 [00:00<00:02, 79165.73KB/s]
  9%|8         | 15834/185766 [00:00<00:02, 64135.05KB/s]
 12%|#2        | 22424/185766 [00:00<00:02, 59693.84KB/s]
 16%|#6        | 29959/185766 [00:00<00:02, 65133.40KB/s]
 20%|#9        | 36602/185766 [00:00<00:02, 51801.36KB/s]
 24%|##4       | 44924/185766 [00:00<00:02, 58963.08KB/s]
 28%|##8       | 52163/185766 [00:00<00:02, 51183.45KB/s]
 31%|###1      | 57698/185766 [00:01<00:02, 49017.87KB/s]
 34%|###4      | 63409/185766 [00:01<00:02, 51005.86KB/s]
 37%|###7      | 69592/185766 [00:01<00:03, 35552.78KB/s]
 41%|####      | 75599/185766 [00:01<00:02, 40383.54KB/s]
 43%|####3     | 80442/185766 [00:01<00:02, 40727.04KB/s]
 47%|####6     | 86968/185766 [00:01<00:02, 38825.78KB/s]
 49%|####9     | 91909/185766 [00:01<00:02, 41127.15KB/s]
 54%|#####3    | 99984/185766 [00:02<00:01, 44710.73KB/s]
 56%|#####6    | 104700/185766 [00:02<00:02, 32293.71KB/s]
 59%|#####8    | 109002/185766 [00:02<00:02, 32821.92KB/s]
 63%|######2   | 116170/185766 [00:02<00:01, 40833.70KB/s]
 66%|######5   | 121764/185766 [00:02<00:01, 32040.15KB/s]
 68%|######7   | 125713/185766 [00:03<00:01, 31804.36KB/s]
 72%|#######2  | 133827/185766 [00:03<00:01, 41941.84KB/s]
 76%|#######5  | 140662/185766 [00:03<00:00, 47934.11KB/s]
 79%|#######8  | 146702/185766 [00:03<00:00, 50984.87KB/s]
 83%|########2 | 154044/185766 [00:03<00:00, 56796.70KB/s]
 86%|########6 | 160242/185766 [00:03<00:00, 45841.18KB/s]
 90%|########9 | 167142/185766 [00:03<00:00, 51239.91KB/s]
 94%|#########4| 175545/185766 [00:03<00:00, 59430.97KB/s]
 98%|#########8| 182732/185766 [00:03<00:00, 62699.50KB/s]
100%|##########| 185766/185766 [00:03<00:00, 46769.27KB/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 6.650 seconds)

Gallery generated by Sphinx-Gallery