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%|          | 93/42434 [00:00<01:05, 646.14KB/s]
  1%|1         | 518/42434 [00:00<00:21, 1989.40KB/s]
  5%|5         | 2177/42434 [00:00<00:06, 6312.36KB/s]
 16%|#6        | 6929/42434 [00:00<00:01, 19107.33KB/s]
 31%|###       | 12953/42434 [00:00<00:00, 30120.50KB/s]
 47%|####7     | 19953/42434 [00:00<00:00, 41844.51KB/s]
 62%|######2   | 26327/42434 [00:00<00:00, 45922.45KB/s]
 78%|#######8  | 33182/42434 [00:00<00:00, 52404.33KB/s]
 97%|#########6| 41031/42434 [00:01<00:00, 56999.93KB/s]
42435KB [00:01, 38462.74KB/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]
  3%|3         | 7534/224190 [00:00<00:02, 75336.48KB/s]
  7%|6         | 15232/224190 [00:00<00:02, 70415.70KB/s]
 10%|9         | 22299/224190 [00:00<00:04, 50365.72KB/s]
 12%|#2        | 27823/224190 [00:00<00:05, 37098.65KB/s]
 15%|#5        | 33865/224190 [00:00<00:04, 42683.42KB/s]
 19%|#8        | 41538/224190 [00:00<00:03, 51362.55KB/s]
 22%|##1       | 48410/224190 [00:00<00:03, 54869.70KB/s]
 25%|##5       | 56093/224190 [00:01<00:02, 60841.86KB/s]
 28%|##8       | 63550/224190 [00:01<00:02, 64458.36KB/s]
 31%|###1      | 70359/224190 [00:01<00:02, 64638.49KB/s]
 35%|###4      | 77985/224190 [00:01<00:02, 67968.34KB/s]
 38%|###7      | 84980/224190 [00:01<00:02, 66648.23KB/s]
 41%|####1     | 92708/224190 [00:01<00:01, 69703.61KB/s]
 45%|####4     | 99793/224190 [00:01<00:01, 67932.09KB/s]
 48%|####7     | 107439/224190 [00:01<00:01, 70382.28KB/s]
 51%|#####1    | 114549/224190 [00:01<00:01, 68504.07KB/s]
 54%|#####4    | 121684/224190 [00:01<00:01, 69319.27KB/s]
 58%|#####7    | 129465/224190 [00:02<00:01, 71489.67KB/s]
 61%|######    | 136649/224190 [00:02<00:01, 68097.31KB/s]
 64%|######4   | 143512/224190 [00:02<00:02, 33539.99KB/s]
 66%|######6   | 148759/224190 [00:03<00:03, 20687.38KB/s]
 70%|######9   | 156516/224190 [00:03<00:02, 27415.65KB/s]
 73%|#######2  | 162811/224190 [00:03<00:01, 32514.40KB/s]
 75%|#######5  | 168250/224190 [00:03<00:01, 33471.15KB/s]
 78%|#######8  | 175821/224190 [00:03<00:01, 40320.63KB/s]
 81%|########  | 181275/224190 [00:04<00:01, 29144.37KB/s]
 83%|########2 | 185579/224190 [00:04<00:01, 26992.69KB/s]
 86%|########6 | 193215/224190 [00:04<00:00, 35283.03KB/s]
 88%|########8 | 198067/224190 [00:04<00:00, 27460.62KB/s]
 92%|#########1| 205943/224190 [00:04<00:00, 36030.60KB/s]
 95%|#########4| 211963/224190 [00:04<00:00, 39901.20KB/s]
 98%|#########7| 219441/224190 [00:04<00:00, 47314.01KB/s]
100%|##########| 224190/224190 [00:05<00:00, 44803.54KB/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%|          | 102/128831 [00:00<02:37, 818.33KB/s]
  0%|          | 510/128831 [00:00<00:56, 2265.38KB/s]
  2%|1         | 2176/128831 [00:00<00:17, 7327.33KB/s]
  6%|6         | 7758/128831 [00:00<00:05, 23976.92KB/s]
 11%|#         | 13853/128831 [00:00<00:03, 36140.82KB/s]
 17%|#6        | 21453/128831 [00:00<00:02, 46861.61KB/s]
 23%|##2       | 29253/128831 [00:00<00:01, 56343.83KB/s]
 28%|##7       | 35721/128831 [00:00<00:01, 58869.03KB/s]
 34%|###4      | 43901/128831 [00:00<00:01, 65797.95KB/s]
 39%|###9      | 50596/128831 [00:01<00:01, 65618.33KB/s]
 45%|####5     | 58532/128831 [00:01<00:01, 69728.59KB/s]
 52%|#####1    | 66505/128831 [00:01<00:00, 72722.83KB/s]
 57%|#####7    | 73827/128831 [00:01<00:00, 72329.25KB/s]
 64%|######4   | 82695/128831 [00:01<00:00, 75347.49KB/s]
 71%|#######   | 91141/128831 [00:01<00:00, 78021.67KB/s]
 77%|#######6  | 98959/128831 [00:01<00:00, 76152.20KB/s]
 83%|########3 | 107381/128831 [00:01<00:00, 77712.47KB/s]
 89%|########9 | 115166/128831 [00:01<00:00, 77742.75KB/s]
 95%|#########5| 122950/128831 [00:02<00:00, 77059.86KB/s]
128832KB [00:02, 61738.68KB/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 24.129 seconds)

Gallery generated by Sphinx-Gallery