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%|          | 101/42434 [00:00<00:54, 783.62KB/s]
  1%|1         | 505/42434 [00:00<00:19, 2158.91KB/s]
  5%|5         | 2178/42434 [00:00<00:05, 7070.87KB/s]
 18%|#8        | 7731/42434 [00:00<00:01, 23302.87KB/s]
 31%|###1      | 13275/42434 [00:00<00:00, 33703.84KB/s]
 46%|####5     | 19329/42434 [00:00<00:00, 42186.95KB/s]
 64%|######4   | 27303/42434 [00:00<00:00, 52352.30KB/s]
 83%|########3 | 35315/42434 [00:00<00:00, 47911.03KB/s]
42435KB [00:01, 39428.08KB/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

import glob
print(glob.glob('*.json') + glob.glob('*.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%|          | 87/224190 [00:00<05:38, 661.75KB/s]
  0%|          | 516/224190 [00:00<01:42, 2181.93KB/s]
  1%|          | 2181/224190 [00:00<00:32, 6934.25KB/s]
  3%|3         | 7545/224190 [00:00<00:09, 22363.41KB/s]
  6%|5         | 13010/224190 [00:00<00:06, 32709.83KB/s]
  9%|9         | 20284/224190 [00:00<00:04, 45282.65KB/s]
 12%|#1        | 25925/224190 [00:00<00:04, 48727.60KB/s]
 15%|#4        | 32757/224190 [00:00<00:04, 43469.58KB/s]
 18%|#7        | 40069/224190 [00:01<00:03, 51122.62KB/s]
 21%|##        | 46667/224190 [00:01<00:03, 55122.20KB/s]
 23%|##3       | 52497/224190 [00:01<00:03, 45763.98KB/s]
 27%|##7       | 60791/224190 [00:01<00:02, 54869.78KB/s]
 30%|###       | 67740/224190 [00:01<00:02, 58645.75KB/s]
 34%|###3      | 75823/224190 [00:01<00:02, 64638.87KB/s]
 37%|###6      | 82875/224190 [00:01<00:02, 66275.28KB/s]
 41%|####      | 90959/224190 [00:01<00:01, 70418.91KB/s]
 44%|####3     | 98219/224190 [00:01<00:01, 70898.75KB/s]
 47%|####7     | 105463/224190 [00:02<00:02, 55436.60KB/s]
 51%|#####     | 113602/224190 [00:02<00:01, 61082.94KB/s]
 54%|#####3    | 120615/224190 [00:02<00:01, 63405.47KB/s]
 57%|#####7    | 128890/224190 [00:02<00:01, 68024.24KB/s]
 61%|######    | 136077/224190 [00:02<00:01, 67403.85KB/s]
 64%|######3   | 143051/224190 [00:02<00:01, 42103.50KB/s]
 66%|######6   | 148582/224190 [00:03<00:01, 40185.64KB/s]
 70%|#######   | 156999/224190 [00:03<00:01, 47341.09KB/s]
 73%|#######3  | 163828/224190 [00:03<00:01, 49131.20KB/s]
 76%|#######5  | 170115/224190 [00:03<00:01, 52254.83KB/s]
 79%|#######8  | 177056/224190 [00:03<00:00, 56460.72KB/s]
 82%|########1 | 183173/224190 [00:03<00:00, 42048.57KB/s]
 84%|########3 | 188209/224190 [00:03<00:01, 33999.74KB/s]
 86%|########5 | 192375/224190 [00:04<00:01, 30924.50KB/s]
 87%|########7 | 195998/224190 [00:04<00:00, 28579.50KB/s]
 89%|########8 | 199208/224190 [00:04<00:01, 20079.90KB/s]
 91%|#########1| 204810/224190 [00:04<00:00, 25896.99KB/s]
 94%|#########4| 211341/224190 [00:04<00:00, 33298.52KB/s]
 96%|#########6| 215720/224190 [00:04<00:00, 32655.46KB/s]
100%|#########9| 223244/224190 [00:05<00:00, 41730.54KB/s]
100%|##########| 224190/224190 [00:05<00:00, 42552.87KB/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]
  1%|1         | 1317/128831 [00:00<00:10, 11780.82KB/s]
  4%|3         | 5102/128831 [00:00<00:04, 26404.52KB/s]
  9%|9         | 11704/128831 [00:00<00:02, 43268.22KB/s]
 13%|#3        | 17150/128831 [00:00<00:02, 47569.55KB/s]
 18%|#8        | 23432/128831 [00:00<00:01, 52972.66KB/s]
 23%|##3       | 29950/128831 [00:00<00:01, 57075.93KB/s]
 28%|##7       | 35686/128831 [00:01<00:03, 29113.55KB/s]
 31%|###1      | 40019/128831 [00:01<00:02, 29640.12KB/s]
 37%|###6      | 47655/128831 [00:01<00:02, 38848.27KB/s]
 41%|####      | 52681/128831 [00:01<00:01, 39836.49KB/s]
 46%|####6     | 59513/128831 [00:01<00:01, 46525.32KB/s]
 51%|#####     | 65523/128831 [00:01<00:01, 44621.67KB/s]
 55%|#####4    | 70533/128831 [00:01<00:01, 41892.46KB/s]
 58%|#####8    | 75111/128831 [00:01<00:01, 36971.41KB/s]
 64%|######3   | 81905/128831 [00:02<00:01, 36866.93KB/s]
 69%|######9   | 89537/128831 [00:02<00:00, 45333.86KB/s]
 76%|#######5  | 97357/128831 [00:02<00:00, 53044.79KB/s]
 80%|########  | 103231/128831 [00:02<00:00, 40936.32KB/s]
 84%|########3 | 108102/128831 [00:02<00:00, 40991.66KB/s]
 89%|########9 | 114680/128831 [00:02<00:00, 38701.15KB/s]
 92%|#########2| 118968/128831 [00:02<00:00, 37511.01KB/s]
 95%|#########5| 122989/128831 [00:03<00:00, 32595.17KB/s]
128832KB [00:03, 39623.65KB/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 26.704 seconds)

Gallery generated by Sphinx-Gallery