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, 11967.23KB/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%|          | 91/185766 [00:00<04:15, 727.53KB/s]
  0%|          | 512/185766 [00:00<01:21, 2263.39KB/s]
  1%|1         | 2176/185766 [00:00<00:25, 7224.68KB/s]
  4%|4         | 7806/185766 [00:00<00:07, 23910.64KB/s]
  7%|7         | 13821/185766 [00:00<00:04, 35757.34KB/s]
 11%|#1        | 21177/185766 [00:00<00:03, 47790.73KB/s]
 15%|#5        | 28083/185766 [00:00<00:02, 54438.01KB/s]
 19%|#9        | 35315/185766 [00:00<00:02, 52622.34KB/s]
 22%|##1       | 40759/185766 [00:01<00:02, 50273.08KB/s]
 26%|##5       | 47697/185766 [00:01<00:02, 55504.20KB/s]
 29%|##9       | 54360/185766 [00:01<00:02, 55688.29KB/s]
 32%|###2      | 60074/185766 [00:01<00:02, 56087.52KB/s]
 35%|###5      | 65762/185766 [00:01<00:02, 45563.29KB/s]
 40%|###9      | 73653/185766 [00:01<00:02, 53779.25KB/s]
 43%|####2     | 79459/185766 [00:01<00:02, 49618.86KB/s]
 46%|####5     | 84758/185766 [00:01<00:02, 39834.39KB/s]
 48%|####8     | 89289/185766 [00:02<00:02, 41044.22KB/s]
 51%|#####1    | 95014/185766 [00:02<00:02, 44932.60KB/s]
 54%|#####3    | 99878/185766 [00:02<00:02, 38802.40KB/s]
 57%|#####7    | 106392/185766 [00:02<00:01, 45010.49KB/s]
 60%|#####9    | 111326/185766 [00:02<00:02, 36229.54KB/s]
 65%|######4   | 119948/185766 [00:02<00:01, 47250.49KB/s]
 69%|######8   | 127428/185766 [00:02<00:01, 53846.75KB/s]
 73%|#######2  | 135045/185766 [00:02<00:00, 59535.69KB/s]
 77%|#######7  | 143483/185766 [00:03<00:00, 66167.35KB/s]
 81%|########1 | 151028/185766 [00:03<00:00, 68725.52KB/s]
 86%|########5 | 159652/185766 [00:03<00:00, 73677.36KB/s]
 90%|######### | 167311/185766 [00:03<00:00, 74144.75KB/s]
 94%|#########4| 175478/185766 [00:03<00:00, 75507.91KB/s]
 99%|#########8| 183876/185766 [00:03<00:00, 77971.55KB/s]
100%|##########| 185766/185766 [00:03<00:00, 51468.22KB/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.333 seconds)

Gallery generated by Sphinx-Gallery