Skip to content

Commit

Permalink
add deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhang committed Feb 19, 2019
1 parent 77ee4d2 commit baef62b
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -102,3 +102,5 @@ venv.bak/

# mypy
.mypy_cache/
*.DS_Store
*.swp
43 changes: 43 additions & 0 deletions 05_deployment/cpp_inference.ipynb
@@ -0,0 +1,43 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. GluonCV C++ Inference Demo\n================================\n\nThis is a demo tutorial which illustrates how to use existing GluonCV\nmodels in c++ environments given exported JSON and PARAMS files.\n\nPlease checkout `Export Network <sphx_glr_build_examples_deployment_export_network.py>` for instructions of how to export pre-trained models.\n\nDemo usage\n----------\n\n.. code:: bash\n\n # with yolo3_darknet53_voc-0000.params and yolo3_darknet53-symbol.json on disk\n ./gluoncv-detect yolo3_darknet53_voc demo.jpg\n\n.. figure:: https://user-images.githubusercontent.com/3307514/45458507-d76ff600-b6a8-11e8-92e1-0b1966e4344f.jpg\n :alt: demo\n\n demo\n\nUsage:\n\n::\n\n SYNOPSIS\n ./gluoncv-detect <model file> <image file> [-o <outfile>] [--class-file <classfile>] [-e\n <epoch>] [--gpu <gpu>] [-q] [--no-disp] [-t <thresh>]\n\n OPTIONS\n -o, --output <outfile>\n output image, by default no output\n\n --class-file <classfile>\n plain text file for class names, one name per line\n\n -e, --epoch <epoch>\n Epoch number to load parameters, by default is 0\n\n --gpu <gpu> Which gpu to use, by default is -1, means cpu only.\n -q, --quite Quite mode, no screen output\n --no-disp Do not display image\n -t, --thresh <thresh>\n Visualize threshold, from 0 to 1, default 0.3.\n\nSource Code and Build Instructions\n----------------------------------\n\nThe C++ demo code and build instructions are available in our repository `scripts\n<https://github.com/dmlc/gluon-cv/tree/master/scripts/deployment/cpp-inference>`_.\n\n.. hint::\n\n Prebuilt binaries are `available <https://github.com/dmlc/gluon-cv/tree/master/scripts/deployment/cpp-inference#download-prebuilt-binaries>`_ for Linus, Mac OS and Windows.\n\n And you can also build MXNet from `source <https://github.com/dmlc/gluon-cv/tree/master/scripts/deployment/cpp-inference#build-from-source>`_ to support C++ inference demo.\n\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
129 changes: 129 additions & 0 deletions 05_deployment/export_network.ipynb
@@ -0,0 +1,129 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Export trained GluonCV network to JSON\n============================================\n\nIt is awesome if you are enjoy using GluonCV in Python for training and testing.\nAt some point, you might ask: \"Is it possible to deploy the existing models to somewhere out of Python environments?\"\n\nThe answer is \"Absolutely!\", and it's super easy actually.\n\nThis article will show you how to export networks/models to be used somewhere other than Python.\n\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import gluoncv as gcv\nfrom gluoncv.utils import export_block"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First of all, we need a network to play with, a pre-trained one is perfect\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"net = gcv.model_zoo.get_model('resnet18_v1', pretrained=True)\nexport_block('resnet18_v1', net, preprocess=True, layout='HWC')\nprint('Done.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. hint::\n\n Use ``preprocess=True`` will add a default preprocess layer above the network,\n which will subtract mean [123.675, 116.28, 103.53], divide\n std [58.395, 57.12, 57.375], and convert original image (B, H, W, C and range [0, 255]) to\n tensor (B, C, H, W) as network input. This is the default preprocess behavior of all GluonCV\n pre-trained models. With this preprocess head, you can use raw RGB image in C++ without\n explicitly applying these operations.\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The above code generates two files: xxxx.json and xxxx.params\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import glob\nprint(glob.glob('*.json') + glob.glob('*.params'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"JSON file includes computational graph and params file includes pre-trained weights.\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The exportable networks are not limited to image classification models.\nWe can export detection and segmentation networks as well:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# YOLO\nnet = gcv.model_zoo.get_model('yolo3_darknet53_coco', pretrained=True)\nexport_block('yolo3_darknet53_coco', net)\n\n# FCN\nnet = gcv.model_zoo.get_model('fcn_resnet50_ade', pretrained=True)\nexport_block('fcn_resnet50_ade', net)\n\n# MaskRCNN\nnet = gcv.model_zoo.get_model('mask_rcnn_resnet50_v1b_coco', pretrained=True)\nexport_block('mask_rcnn_resnet50_v1b_coco', net)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are all set here. Please checkout the other tutorials of how to use the JSON and params files.\n\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -15,13 +15,13 @@ Agenda
| Time | Title | Slides | Notebooks |
|-------------|------------------------------------------------------------------------|-----------|------------|
| 8:30-9:15 | Welcome and AWS Setup | [link][0] | [link][01] |
| 9:15-10:00 | Deep Learning and Gluon Basics (NDArray, AutoGrad, Libraries) | [link][ ] | [link][11],[link][12] |
| 9:15-10:00 | Deep Learning and Gluon Basics (NDArray, AutoGrad, Libraries) | | [link][11],[link][12] |
| 10:00-10:30 | Coffe Break | | |
| 10:30-11:45 | Bags of Tricks for Image Classification (ResNet, MobileNet, Inception) | [link][2] | [link][21] |
| 12:00-2:00 | Lunch and Break | | |
| 2:00-3:45 | Understanding Object Detectors (SSD, Faster RCNN, YOLOV3) | [link][3] | [link][31] |
| 3:45-5:00 | Semantic segmentation algorithms (FCN, PSPNet, DeepLabV3) | [link][4] | [link][41] |
| 5:00-5:30 | Painless Deployment (C++, TVM) | [link][ ] | [link][ ] |
| 5:00-5:30 | Painless Deployment (C++, TVM) | | [link][51],[link][52] |
| 5:30-5:45 | Q&A and Closing | | |


Expand All @@ -36,3 +36,5 @@ Agenda
[21]: https://github.com/zhanghang1989/ICCV19-GluonCV/blob/master/02_classification/ImageClassification.ipynb
[31]: https://github.com/zhanghang1989/ICCV19-GluonCV/blob/master/03_detection/ObjectDetection.ipynb
[41]: https://github.com/zhanghang1989/ICCV19-GluonCV/blob/master/04_segmentation/SemanticSegmentation.ipynb
[51]: https://github.com/zhanghang1989/ICCV19-GluonCV/blob/master/05_deployment/export_network.ipynb
[52]: https://github.com/zhanghang1989/ICCV19-GluonCV/blob/master/05_deployment/cpp_inference.ipynb

0 comments on commit baef62b

Please sign in to comment.