Note
Click here to download the full example code
1. Getting Started with FCN Pre-trained Models¶
This is a quick demo of using GluonCV FCN model on PASCAL VOC dataset. 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¶
download the example image
url = 'https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/segmentation/voc_examples/1.jpg'
filename = 'example.jpg'
gluoncv.utils.download(url, filename)
Out:
Downloading example.jpg from https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/segmentation/voc_examples/1.jpg...
0%| | 0/24 [00:00<?, ?KB/s]
100%|##########| 24/24 [00:00<00:00, 11662.99KB/s]
load the image
img = image.imread(filename)
from matplotlib import pyplot as plt
plt.imshow(img.asnumpy())
plt.show()
normalize the image using dataset mean
Load the pre-trained model and make prediction¶
get pre-trained model
model = gluoncv.model_zoo.get_model('fcn_resnet101_voc', pretrained=True)
Out:
Downloading /root/.mxnet/models/resnet101_v1s-bd93a83c.zip from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/models/resnet101_v1s-bd93a83c.zip...
0%| | 0/100120 [00:00<?, ?KB/s]
0%| | 101/100120 [00:00<02:01, 822.78KB/s]
1%| | 508/100120 [00:00<00:43, 2313.59KB/s]
2%|2 | 2174/100120 [00:00<00:13, 7494.88KB/s]
7%|7 | 7485/100120 [00:00<00:03, 23397.08KB/s]
14%|#3 | 13569/100120 [00:00<00:02, 35844.96KB/s]
22%|##1 | 21878/100120 [00:00<00:01, 49694.96KB/s]
31%|### | 30614/100120 [00:00<00:01, 61341.20KB/s]
38%|###8 | 38117/100120 [00:00<00:00, 65534.44KB/s]
45%|####5 | 45383/100120 [00:00<00:00, 67702.96KB/s]
53%|#####2 | 52645/100120 [00:01<00:00, 69192.76KB/s]
60%|###### | 60464/100120 [00:01<00:00, 71242.72KB/s]
69%|######8 | 68796/100120 [00:01<00:00, 74709.63KB/s]
77%|#######6 | 76924/100120 [00:01<00:00, 76586.12KB/s]
85%|########4 | 84930/100120 [00:01<00:00, 77623.11KB/s]
93%|#########2| 93082/100120 [00:01<00:00, 78787.56KB/s]
100121KB [00:01, 60169.24KB/s]
Downloading /root/.mxnet/models/fcn_resnet101_voc-12c2b9b3.zip from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/models/fcn_resnet101_voc-12c2b9b3.zip...
0%| | 0/197332 [00:00<?, ?KB/s]
0%| | 713/197332 [00:00<00:32, 6019.02KB/s]
2%|1 | 3620/197332 [00:00<00:11, 17077.96KB/s]
6%|5 | 11124/197332 [00:00<00:04, 41267.10KB/s]
10%|9 | 19268/197332 [00:00<00:03, 56266.43KB/s]
14%|#3 | 27197/197332 [00:00<00:02, 64272.42KB/s]
18%|#8 | 35948/197332 [00:00<00:02, 71345.17KB/s]
23%|##2 | 44545/197332 [00:00<00:02, 76018.37KB/s]
27%|##6 | 52534/197332 [00:00<00:01, 77228.92KB/s]
31%|###1 | 61368/197332 [00:00<00:01, 80665.47KB/s]
35%|###5 | 69482/197332 [00:01<00:02, 51085.40KB/s]
39%|###8 | 76908/197332 [00:01<00:02, 56108.35KB/s]
42%|####2 | 83673/197332 [00:01<00:02, 54654.53KB/s]
47%|####6 | 92345/197332 [00:01<00:01, 62310.44KB/s]
51%|##### | 100241/197332 [00:01<00:01, 64432.00KB/s]
55%|#####5 | 108838/197332 [00:01<00:01, 70037.42KB/s]
59%|#####9 | 117065/197332 [00:01<00:01, 73351.23KB/s]
64%|######3 | 125602/197332 [00:01<00:00, 76705.05KB/s]
68%|######7 | 133555/197332 [00:02<00:00, 75112.36KB/s]
72%|#######1 | 142021/197332 [00:02<00:00, 77812.37KB/s]
76%|#######5 | 149960/197332 [00:02<00:00, 65174.54KB/s]
80%|######## | 158552/197332 [00:02<00:00, 70465.03KB/s]
85%|########4 | 167545/197332 [00:02<00:00, 75663.15KB/s]
89%|########9 | 175755/197332 [00:02<00:00, 77439.02KB/s]
93%|#########3| 184167/197332 [00:02<00:00, 79328.51KB/s]
98%|#########7| 192819/197332 [00:02<00:00, 80870.78KB/s]
197333KB [00:02, 67221.14KB/s]
make prediction using single scale
Add color pallete for visualization
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
mmask = mpimg.imread('output.png')
plt.imshow(mmask)
plt.show()
- More Examples
Total running time of the script: ( 0 minutes 11.295 seconds)