Note
Click here to download the full example code
1. Export trained GluonCV network to JSON¶
It is awesome if you are enjoy using GluonCV in Python for training and testing. At some point, you might ask: “Is it possible to deploy the existing models to somewhere out of Python environments?”
The answer is “Absolutely!”, and it’s super easy actually.
This article will show you how to export networks/models to be used somewhere other than Python.
import gluoncv as gcv
from gluoncv.utils import export_block
First of all, we need a network to play with, a pre-trained one is perfect
net = gcv.model_zoo.get_model('resnet18_v1', pretrained=True)
export_block('resnet18_v1', net, preprocess=True, layout='HWC')
print('Done.')
Out:
Downloading /root/.mxnet/models/resnet18_v1-a0666292.zip from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/models/resnet18_v1-a0666292.zip...
0%| | 0/42434 [00:00<?, ?KB/s]
0%| | 100/42434 [00:00<00:52, 799.68KB/s]
1%|1 | 506/42434 [00:00<00:18, 2259.47KB/s]
4%|4 | 1753/42434 [00:00<00:07, 5803.45KB/s]
17%|#6 | 7040/42434 [00:00<00:01, 21999.06KB/s]
31%|### | 13017/42434 [00:00<00:00, 34445.25KB/s]
50%|##### | 21256/42434 [00:00<00:00, 47811.17KB/s]
67%|######7 | 28544/42434 [00:00<00:00, 55457.95KB/s]
83%|########3 | 35368/42434 [00:00<00:00, 59337.18KB/s]
42435KB [00:00, 43650.32KB/s]
Done.
Hint
Use preprocess=True
will add a default preprocess layer above the network,
which will subtract mean [123.675, 116.28, 103.53], divide
std [58.395, 57.12, 57.375], and convert original image (B, H, W, C and range [0, 255]) to
tensor (B, C, H, W) as network input. This is the default preprocess behavior of all GluonCV
pre-trained models. With this preprocess head, you can use raw RGB image in C++ without
explicitly applying these operations.
The above code generates two files: xxxx.json and xxxx.params
Out:
['resnet18_v1-symbol.json', 'resnet18_v1-0000.params']
JSON file includes computational graph and params file includes pre-trained weights.
The exportable networks are not limited to image classification models. We can export detection and segmentation networks as well:
# YOLO
net = gcv.model_zoo.get_model('yolo3_darknet53_coco', pretrained=True)
export_block('yolo3_darknet53_coco', net)
# FCN
net = gcv.model_zoo.get_model('fcn_resnet50_ade', pretrained=True)
export_block('fcn_resnet50_ade', net)
# MaskRCNN
net = gcv.model_zoo.get_model('mask_rcnn_resnet50_v1b_coco', pretrained=True)
export_block('mask_rcnn_resnet50_v1b_coco', net)
Out:
Downloading /root/.mxnet/models/yolo3_darknet53_coco-09767802.zip from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/models/yolo3_darknet53_coco-09767802.zip...
0%| | 0/224190 [00:00<?, ?KB/s]
0%| | 102/224190 [00:00<04:40, 797.94KB/s]
0%| | 508/224190 [00:00<01:42, 2189.18KB/s]
1%| | 2184/224190 [00:00<00:30, 7162.90KB/s]
3%|3 | 7758/224190 [00:00<00:09, 23548.98KB/s]
7%|6 | 14640/224190 [00:00<00:05, 38269.95KB/s]
10%|9 | 22248/224190 [00:00<00:04, 50209.52KB/s]
13%|#3 | 30133/224190 [00:00<00:03, 59133.22KB/s]
17%|#6 | 37825/224190 [00:00<00:02, 64607.58KB/s]
20%|## | 45698/224190 [00:00<00:02, 68922.24KB/s]
24%|##3 | 53787/224190 [00:01<00:02, 72556.99KB/s]
27%|##7 | 61507/224190 [00:01<00:02, 73959.48KB/s]
31%|### | 69308/224190 [00:01<00:02, 75180.34KB/s]
34%|###4 | 76889/224190 [00:01<00:01, 75366.99KB/s]
38%|###7 | 84792/224190 [00:01<00:01, 76465.43KB/s]
41%|####1 | 92776/224190 [00:01<00:01, 77447.89KB/s]
45%|####4 | 100539/224190 [00:01<00:01, 76307.41KB/s]
49%|####8 | 108984/224190 [00:01<00:01, 78723.51KB/s]
52%|#####2 | 116871/224190 [00:01<00:01, 77722.21KB/s]
56%|#####5 | 124968/224190 [00:01<00:01, 78389.09KB/s]
59%|#####9 | 132816/224190 [00:02<00:01, 78382.50KB/s]
63%|######2 | 140661/224190 [00:02<00:01, 76579.59KB/s]
66%|######6 | 148332/224190 [00:02<00:01, 69287.50KB/s]
70%|######9 | 156747/224190 [00:02<00:00, 73369.48KB/s]
73%|#######3 | 164309/224190 [00:02<00:00, 74002.97KB/s]
77%|#######7 | 172629/224190 [00:02<00:00, 76640.69KB/s]
80%|######## | 180372/224190 [00:02<00:00, 76389.55KB/s]
84%|########3 | 188066/224190 [00:02<00:00, 76300.68KB/s]
88%|########7 | 196517/224190 [00:02<00:00, 78709.86KB/s]
91%|#########1| 204420/224190 [00:03<00:00, 77343.70KB/s]
95%|#########4| 212181/224190 [00:03<00:00, 77060.98KB/s]
98%|#########8| 220169/224190 [00:03<00:00, 77880.92KB/s]
100%|##########| 224190/224190 [00:03<00:00, 67850.24KB/s]
Downloading /root/.mxnet/models/fcn_resnet50_ade-3479525a.zip from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/models/fcn_resnet50_ade-3479525a.zip...
0%| | 0/128831 [00:00<?, ?KB/s]
0%| | 100/128831 [00:00<02:44, 780.55KB/s]
0%| | 505/128831 [00:00<00:59, 2169.61KB/s]
2%|1 | 2177/128831 [00:00<00:18, 6790.34KB/s]
6%|5 | 7385/128831 [00:00<00:05, 21559.64KB/s]
11%|# | 13635/128831 [00:00<00:03, 34569.89KB/s]
17%|#6 | 21329/128831 [00:00<00:02, 46431.54KB/s]
23%|##2 | 29117/128831 [00:00<00:01, 55934.25KB/s]
28%|##8 | 36303/128831 [00:00<00:01, 60735.86KB/s]
35%|###4 | 44870/128831 [00:01<00:01, 68241.33KB/s]
40%|#### | 51863/128831 [00:01<00:01, 68202.32KB/s]
46%|####5 | 59161/128831 [00:01<00:01, 69625.84KB/s]
52%|#####1 | 66497/128831 [00:01<00:00, 70737.68KB/s]
58%|#####8 | 74946/128831 [00:01<00:00, 74853.17KB/s]
64%|######4 | 82479/128831 [00:01<00:00, 73963.49KB/s]
70%|######9 | 90007/128831 [00:01<00:00, 74268.49KB/s]
76%|#######5 | 97709/128831 [00:01<00:00, 75084.79KB/s]
82%|########1 | 105481/128831 [00:01<00:00, 75869.74KB/s]
88%|########7 | 113082/128831 [00:01<00:00, 75685.34KB/s]
94%|#########4| 121217/128831 [00:02<00:00, 77214.22KB/s]
128832KB [00:02, 60766.94KB/s]
We are all set here. Please checkout the other tutorials of how to use the JSON and params files.
Total running time of the script: ( 0 minutes 22.950 seconds)