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]
  4%|4         | 1768/42434 [00:00<00:02, 14247.02KB/s]
  8%|7         | 3193/42434 [00:00<00:05, 6593.28KB/s]
 21%|##        | 8710/42434 [00:00<00:01, 19286.42KB/s]
 33%|###2      | 13992/42434 [00:00<00:01, 23417.42KB/s]
 40%|###9      | 16966/42434 [00:00<00:01, 20976.77KB/s]
 48%|####8     | 20540/42434 [00:01<00:01, 18327.88KB/s]
 68%|######7   | 28711/42434 [00:01<00:00, 30717.58KB/s]
 77%|#######7  | 32829/42434 [00:01<00:00, 25792.27KB/s]
 85%|########5 | 36211/42434 [00:01<00:00, 26562.14KB/s]
 93%|#########2| 39452/42434 [00:02<00:00, 15889.58KB/s]
42435KB [00:02, 20247.10KB/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%|          | 93/224190 [00:00<05:36, 666.15KB/s]
  0%|          | 516/224190 [00:00<01:48, 2057.71KB/s]
  1%|          | 2190/224190 [00:00<00:33, 6589.30KB/s]
  3%|3         | 7177/224190 [00:00<00:10, 20366.98KB/s]
  6%|6         | 13954/224190 [00:00<00:06, 33500.16KB/s]
  9%|9         | 20761/224190 [00:00<00:04, 43811.96KB/s]
 12%|#2        | 27388/224190 [00:00<00:03, 49558.13KB/s]
 16%|#5        | 35192/224190 [00:00<00:03, 57963.08KB/s]
 19%|#8        | 42055/224190 [00:01<00:03, 59359.71KB/s]
 22%|##2       | 49498/224190 [00:01<00:02, 63751.38KB/s]
 25%|##5       | 56715/224190 [00:01<00:02, 64400.31KB/s]
 29%|##8       | 64272/224190 [00:01<00:02, 67638.41KB/s]
 32%|###1      | 71579/224190 [00:01<00:02, 67252.28KB/s]
 35%|###5      | 79195/224190 [00:01<00:02, 69825.74KB/s]
 39%|###8      | 86365/224190 [00:01<00:02, 68085.21KB/s]
 42%|####1     | 94061/224190 [00:01<00:01, 70637.36KB/s]
 45%|####5     | 101437/224190 [00:01<00:01, 69240.71KB/s]
 49%|####8     | 109261/224190 [00:01<00:01, 71816.48KB/s]
 52%|#####1    | 116573/224190 [00:02<00:01, 72015.33KB/s]
 55%|#####5    | 123801/224190 [00:02<00:01, 71338.56KB/s]
 59%|#####8    | 131597/224190 [00:02<00:01, 72089.92KB/s]
 62%|######1   | 138818/224190 [00:02<00:01, 71114.26KB/s]
 65%|######5   | 146370/224190 [00:02<00:01, 72395.18KB/s]
 69%|######8   | 153620/224190 [00:02<00:01, 70340.21KB/s]
 72%|#######2  | 161540/224190 [00:02<00:00, 72904.06KB/s]
 75%|#######5  | 168852/224190 [00:02<00:00, 70899.58KB/s]
 79%|#######8  | 176607/224190 [00:02<00:00, 72818.60KB/s]
 82%|########2 | 183913/224190 [00:03<00:00, 70744.49KB/s]
 86%|########5 | 191728/224190 [00:03<00:00, 72877.14KB/s]
 89%|########8 | 199042/224190 [00:03<00:00, 70067.85KB/s]
 92%|#########2| 206874/224190 [00:03<00:00, 72425.46KB/s]
 96%|#########5| 214154/224190 [00:03<00:00, 69307.35KB/s]
 99%|#########8| 221285/224190 [00:03<00:00, 69872.84KB/s]
100%|##########| 224190/224190 [00:03<00:00, 61883.38KB/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%|          | 274/128831 [00:00<00:58, 2192.27KB/s]
  1%|1         | 1410/128831 [00:00<00:20, 6223.02KB/s]
  5%|4         | 5944/128831 [00:00<00:06, 19572.03KB/s]
 11%|#1        | 14231/128831 [00:00<00:02, 40998.66KB/s]
 15%|#5        | 19913/128831 [00:00<00:02, 46153.99KB/s]
 21%|##1       | 27526/128831 [00:00<00:01, 55676.04KB/s]
 26%|##6       | 33953/128831 [00:00<00:01, 58358.15KB/s]
 32%|###1      | 40795/128831 [00:00<00:01, 60895.72KB/s]
 38%|###7      | 48788/128831 [00:00<00:01, 66678.51KB/s]
 43%|####3     | 55735/128831 [00:01<00:01, 67521.18KB/s]
 50%|####9     | 63855/128831 [00:01<00:00, 68650.56KB/s]
 56%|#####5    | 71901/128831 [00:01<00:00, 72100.32KB/s]
 62%|######1   | 79385/128831 [00:01<00:00, 72903.05KB/s]
 68%|######8   | 87938/128831 [00:01<00:00, 76638.81KB/s]
 74%|#######4  | 95632/128831 [00:01<00:00, 76090.83KB/s]
 81%|########  | 103918/128831 [00:01<00:00, 77771.13KB/s]
 87%|########6 | 111711/128831 [00:01<00:00, 76753.93KB/s]
 93%|#########2| 119400/128831 [00:01<00:00, 76119.36KB/s]
 99%|#########9| 127623/128831 [00:02<00:00, 77916.82KB/s]
128832KB [00:02, 63342.40KB/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 23.844 seconds)

Gallery generated by Sphinx-Gallery