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%|          | 102/42434 [00:00<00:53, 798.29KB/s]
  1%|1         | 509/42434 [00:00<00:18, 2214.23KB/s]
  5%|5         | 2190/42434 [00:00<00:05, 7228.35KB/s]
 17%|#7        | 7335/42434 [00:00<00:01, 22207.23KB/s]
 32%|###1      | 13529/42434 [00:00<00:00, 35162.83KB/s]
 52%|#####1    | 22008/42434 [00:00<00:00, 50933.57KB/s]
 67%|######7   | 28615/42434 [00:00<00:00, 55658.41KB/s]
 87%|########6 | 36881/42434 [00:00<00:00, 61811.06KB/s]
42435KB [00:00, 44005.96KB/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%|          | 103/224190 [00:00<05:43, 652.02KB/s]
  0%|          | 512/224190 [00:00<02:07, 1752.85KB/s]
  1%|          | 1916/224190 [00:00<00:44, 5001.35KB/s]
  2%|2         | 5211/224190 [00:00<00:16, 13134.19KB/s]
  5%|4         | 11049/224190 [00:00<00:08, 26419.10KB/s]
  7%|7         | 16473/224190 [00:00<00:06, 32547.12KB/s]
 10%|#         | 23301/224190 [00:00<00:04, 42693.81KB/s]
 13%|#3        | 30184/224190 [00:00<00:03, 50235.79KB/s]
 16%|#6        | 36126/224190 [00:01<00:03, 50295.93KB/s]
 19%|#8        | 42096/224190 [00:01<00:03, 52964.72KB/s]
 22%|##1       | 49114/224190 [00:01<00:03, 57929.08KB/s]
 25%|##4       | 55306/224190 [00:01<00:03, 55927.58KB/s]
 28%|##7       | 62181/224190 [00:01<00:02, 59551.93KB/s]
 31%|###       | 68651/224190 [00:01<00:02, 61032.08KB/s]
 33%|###3      | 75077/224190 [00:01<00:02, 58508.03KB/s]
 36%|###6      | 81755/224190 [00:01<00:02, 60840.86KB/s]
 40%|###9      | 88664/224190 [00:01<00:02, 63209.51KB/s]
 42%|####2     | 95048/224190 [00:02<00:02, 59646.91KB/s]
 46%|####5     | 102189/224190 [00:02<00:01, 62951.27KB/s]
 49%|####8     | 108802/224190 [00:02<00:01, 63860.53KB/s]
 51%|#####1    | 115247/224190 [00:02<00:01, 60787.66KB/s]
 54%|#####4    | 122102/224190 [00:02<00:01, 62979.30KB/s]
 58%|#####7    | 128941/224190 [00:02<00:01, 64531.96KB/s]
 60%|######    | 135478/224190 [00:02<00:01, 63781.51KB/s]
 63%|######3   | 141891/224190 [00:02<00:01, 61506.15KB/s]
 66%|######6   | 148877/224190 [00:02<00:01, 63889.92KB/s]
 69%|######9   | 155762/224190 [00:03<00:01, 65326.30KB/s]
 72%|#######2  | 162326/224190 [00:03<00:01, 61561.34KB/s]
 75%|#######5  | 169018/224190 [00:03<00:00, 62976.70KB/s]
 78%|#######8  | 175954/224190 [00:03<00:00, 64156.00KB/s]
 81%|########1 | 182408/224190 [00:03<00:00, 61653.66KB/s]
 84%|########4 | 189197/224190 [00:03<00:00, 63420.21KB/s]
 87%|########7 | 195985/224190 [00:03<00:00, 64704.09KB/s]
 90%|######### | 202488/224190 [00:03<00:00, 61018.37KB/s]
 93%|#########3| 209319/224190 [00:03<00:00, 62979.99KB/s]
 96%|#########6| 216053/224190 [00:03<00:00, 64227.71KB/s]
 99%|#########9| 222519/224190 [00:04<00:00, 61414.31KB/s]
100%|##########| 224190/224190 [00:04<00:00, 54463.21KB/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%|          | 500/128831 [00:00<00:31, 4046.42KB/s]
  2%|2         | 2582/128831 [00:00<00:11, 11407.24KB/s]
  7%|6         | 8804/128831 [00:00<00:03, 31793.87KB/s]
 12%|#1        | 15118/128831 [00:00<00:02, 43212.19KB/s]
 18%|#7        | 22615/128831 [00:00<00:02, 51293.72KB/s]
 24%|##4       | 31062/128831 [00:00<00:01, 61804.39KB/s]
 29%|##9       | 37527/128831 [00:00<00:01, 62689.54KB/s]
 36%|###5      | 46167/128831 [00:00<00:01, 66753.53KB/s]
 42%|####2     | 54246/128831 [00:00<00:01, 70900.70KB/s]
 48%|####7     | 61397/128831 [00:01<00:00, 69913.85KB/s]
 54%|#####4    | 69896/128831 [00:01<00:00, 72282.77KB/s]
 60%|######    | 77883/128831 [00:01<00:00, 74482.94KB/s]
 66%|######6   | 85471/128831 [00:01<00:00, 74891.50KB/s]
 73%|#######2  | 93712/128831 [00:01<00:00, 77108.87KB/s]
 79%|#######8  | 101442/128831 [00:01<00:00, 75673.03KB/s]
 85%|########4 | 109027/128831 [00:01<00:00, 41665.40KB/s]
 89%|########9 | 114932/128831 [00:02<00:00, 39877.07KB/s]
 93%|#########3| 120122/128831 [00:02<00:00, 36336.80KB/s]
 99%|#########8| 127452/128831 [00:02<00:00, 43401.83KB/s]
128832KB [00:02, 52607.99KB/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 25.457 seconds)

Gallery generated by Sphinx-Gallery