In [1]:
!pip install mxnet


Collecting mxnet
  Downloading https://files.pythonhosted.org/packages/87/bb/b7989f03555c7dbafa8c0faf806c436791f21ec24c05ed0cf521c1911001/mxnet-1.2.1-py2.py3-none-manylinux1_x86_64.whl (23.9MB)
    100% |████████████████████████████████| 23.9MB 1.4MB/s 
Requirement already satisfied: requests<2.19.0,>=2.18.4 in /usr/local/lib/python3.6/dist-packages (from mxnet) (2.18.4)
Requirement already satisfied: numpy<1.15.0,>=1.8.2 in /usr/local/lib/python3.6/dist-packages (from mxnet) (1.14.5)
Collecting graphviz<0.9.0,>=0.8.1 (from mxnet)
  Downloading https://files.pythonhosted.org/packages/53/39/4ab213673844e0c004bed8a0781a0721a3f6bb23eb8854ee75c236428892/graphviz-0.8.4-py2.py3-none-any.whl
Requirement already satisfied: idna<2.7,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests<2.19.0,>=2.18.4->mxnet) (2.6)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests<2.19.0,>=2.18.4->mxnet) (2018.4.16)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests<2.19.0,>=2.18.4->mxnet) (1.22)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests<2.19.0,>=2.18.4->mxnet) (3.0.4)
Installing collected packages: graphviz, mxnet
Successfully installed graphviz-0.8.4 mxnet-1.2.1

In [3]:
!pip install gluoncv


Collecting gluoncv
  Downloading https://files.pythonhosted.org/packages/1a/98/8712d939471d5c9200b82e197515ba2b05258c87dbe50f79eb27ae451335/gluoncv-0.2.0-py2.py3-none-any.whl (136kB)
    100% |████████████████████████████████| 143kB 4.6MB/s 
Requirement already satisfied: scipy in /usr/local/lib/python3.6/dist-packages (from gluoncv) (0.19.1)
Requirement already satisfied: Pillow in /usr/local/lib/python3.6/dist-packages (from gluoncv) (4.0.0)
Requirement already satisfied: matplotlib in /usr/local/lib/python3.6/dist-packages (from gluoncv) (2.1.2)
Collecting tqdm (from gluoncv)
  Downloading https://files.pythonhosted.org/packages/7d/e6/19dfaff08fcbee7f3453e5b537e65a8364f1945f921a36d08be1e2ff3475/tqdm-4.24.0-py2.py3-none-any.whl (43kB)
    100% |████████████████████████████████| 51kB 5.8MB/s 
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from gluoncv) (1.14.5)
Requirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages (from gluoncv) (2.18.4)
Requirement already satisfied: olefile in /usr/local/lib/python3.6/dist-packages (from Pillow->gluoncv) (0.45.1)
Requirement already satisfied: six>=1.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib->gluoncv) (1.11.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->gluoncv) (2.2.0)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib->gluoncv) (0.10.0)
Requirement already satisfied: pytz in /usr/local/lib/python3.6/dist-packages (from matplotlib->gluoncv) (2018.5)
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->gluoncv) (2.5.3)
Requirement already satisfied: idna<2.7,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests->gluoncv) (2.6)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests->gluoncv) (2018.4.16)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests->gluoncv) (3.0.4)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests->gluoncv) (1.22)
Installing collected packages: tqdm, gluoncv
Successfully installed gluoncv-0.2.0 tqdm-4.24.0

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

In [5]:
url = 'https://github.com/zhanghang1989/image-data/blob/master/encoding/' + \
    'segmentation/ade20k/ADE_val_00001142.jpg?raw=true'
filename = 'ade20k_example.jpg'
gluoncv.utils.download(url, filename)


Downloading ade20k_example.jpg from https://github.com/zhanghang1989/image-data/blob/master/encoding/segmentation/ade20k/ADE_val_00001142.jpg?raw=true...
100%|██████████| 52/52 [00:00<00:00, 2169.52KB/s]
Out[5]:
'ade20k_example.jpg'

In [6]:
img = image.imread(filename)

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



In [0]:
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)

In [8]:
model = gluoncv.model_zoo.get_model('psp_resnet50_ade', pretrained=True)


Model file is not found. Downloading.
Downloading /content/.mxnet/models/resnet50_v1b-e263a986.zip from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/models/resnet50_v1b-e263a986.zip...
92684KB [00:03, 29211.68KB/s]                           
/usr/local/lib/python3.6/dist-packages/mxnet/gluon/block.py:413: UserWarning: load_params is deprecated. Please use load_parameters.
  warnings.warn("load_params is deprecated. Please use load_parameters.")
Model file is not found. Downloading.
Downloading /content/.mxnet/models/psp_resnet50_ade-3133bd42.zip from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/models/psp_resnet50_ade-3133bd42.zip...
178237KB [00:05, 31962.49KB/s]                            

In [0]:
output = model.demo(img)
predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()

In [0]:
from gluoncv.utils.viz import get_color_pallete
import matplotlib.image as mpimg
mask = get_color_pallete(predict, 'ade20k')
mask.save('output.png')

In [11]:
mmask = mpimg.imread('output.png')
plt.imshow(mmask)
plt.show()



In [0]: