.. _sphx_glr_build_examples_segmentation_demo_fcn.py: 1. Getting Started with FCN Pre-trained Models ============================================== This is a quick demo of using GluonCV FCN model. Please follow the `installation guide <../index.html>`_ to install MXNet and GluonCV if not yet. .. code-block:: python 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 ----------------- download the example image .. code-block:: python url = 'https://raw.githubusercontent.com/zhanghang1989/gluon-vision-figures/master/voc_examples/1.jpg' filename = 'example.jpg' gluoncv.utils.download(url, filename) load the image .. code-block:: python with open(filename, 'rb') as f: img = image.imdecode(f.read()) from matplotlib import pyplot as plt plt.imshow(img.asnumpy()) plt.show() .. image:: /build/examples_segmentation/images/sphx_glr_demo_fcn_001.png :align: center normalize the image using dataset mean .. code-block:: python transform_fn = transforms.Compose([ transforms.ToTensor(), transforms.Normalize([.485, .456, .406], [.229, .224, .225]) ]) img = transform_fn(img) img = img.expand_dims(0).as_in_context(ctx) Load the pre-trained model and make prediction ---------------------------------------------- get pre-trained model .. code-block:: python model = gluoncv.model_zoo.get_model('fcn_resnet101_voc', pretrained=True) make prediction using single scale .. code-block:: python output = model.evaluate(img) predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy() Add color pallete for visualization .. code-block:: python from gluoncv.utils.viz import get_color_pallete import matplotlib.image as mpimg mask = get_color_pallete(predict, 'pascal_voc') mask.save('output.png') show the predicted mask .. code-block:: python mmask = mpimg.imread('output.png') plt.imshow(mmask) plt.show() .. image:: /build/examples_segmentation/images/sphx_glr_demo_fcn_002.png :align: center More Examples ------------- .. image:: https://raw.githubusercontent.com/zhanghang1989/gluon-vision-figures/master/voc_examples/4.jpg :width: 45% .. image:: https://raw.githubusercontent.com/zhanghang1989/gluon-vision-figures/master/voc_examples/4.png :width: 45% .. image:: https://raw.githubusercontent.com/zhanghang1989/gluon-vision-figures/master/voc_examples/5.jpg :width: 45% .. image:: https://raw.githubusercontent.com/zhanghang1989/gluon-vision-figures/master/voc_examples/5.png :width: 45% .. image:: https://raw.githubusercontent.com/zhanghang1989/gluon-vision-figures/master/voc_examples/6.jpg :width: 45% .. image:: https://raw.githubusercontent.com/zhanghang1989/gluon-vision-figures/master/voc_examples/6.png :width: 45% **Total running time of the script:** ( 0 minutes 5.013 seconds) .. only :: html .. container:: sphx-glr-footer .. container:: sphx-glr-download :download:`Download Python source code: demo_fcn.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: demo_fcn.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_