deploy_ssd_gluoncv.py 4.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
17 18 19 20
"""
Deploy Single Shot Multibox Detector(SSD) model
===============================================
**Author**: `Yao Wang <https://github.com/kevinthesun>`_
21
`Leyuan Wang <https://github.com/Laurawly>`_
22 23 24 25 26 27 28

This article is an introductory tutorial to deploy SSD models with TVM.
We will use GluonCV pre-trained SSD model and convert it to Relay IR
"""
import tvm

from matplotlib import pyplot as plt
29
from tvm.relay.testing.config import ctx_list
30 31
from tvm import relay
from tvm.contrib import graph_runtime
32
from tvm.contrib.download import download_testdata
33 34 35 36 37 38 39 40
from gluoncv import model_zoo, data, utils


######################################################################
# Preliminary and Set parameters
# ------------------------------
# .. note::
#
41
#   We support compiling SSD on both CPUs and GPUs now.
42 43 44 45
#
#   To get best inference performance on CPU, change
#   target argument according to your device and
#   follow the :ref:`tune_relay_x86` to tune x86 CPU and
46
#   :ref:`tune_relay_arm` for arm CPU.
47
#
48 49 50 51 52 53 54 55 56 57 58
#   To get best inference performance on Intel graphics,
#   change target argument to :code:`opencl -device=intel_graphics`.
#   But when using Intel graphics on Mac, target needs to 
#   be set to `opencl` only for the reason that Intel subgroup
#   extension is not supported on Mac.
#
#   To get best inference performance on CUDA-based GPUs,
#   change the target argument to :code:`cuda`; and for
#   OPENCL-based GPUs, change target argument to
#   :code:`opencl` followed by device argument according
#   to your device.
59 60 61 62 63

supported_model = [
    'ssd_512_resnet50_v1_voc',
    'ssd_512_resnet50_v1_coco',
    'ssd_512_resnet101_v2_voc',
64 65
    'ssd_512_mobilenet1.0_voc',
    'ssd_512_mobilenet1.0_coco',
66 67
    'ssd_300_vgg16_atrous_voc'
    'ssd_512_vgg16_atrous_coco',
68 69
]

70
model_name = supported_model[0]
71 72 73 74 75 76
dshape = (1, 3, 512, 512)
target_list = ctx_list()

######################################################################
# Download and pre-process demo image

77 78 79
im_fname = download_testdata('https://github.com/dmlc/web-data/blob/master/' +
                             'gluoncv/detection/street_small.jpg?raw=true',
                             'street_small.jpg', module='data')
80 81 82 83 84 85 86
x, img = data.transforms.presets.ssd.load_test(im_fname, short=512)

######################################################################
# Convert and compile model for CPU.

block = model_zoo.get_model(model_name, pretrained=True)

87
def build(target):
88
    mod, params = relay.frontend.from_mxnet(block, {"data": dshape})
89
    with relay.build_config(opt_level=3):
90
        graph, lib, params = relay.build(mod, target, params=params)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    return graph, lib, params

######################################################################
# Create TVM runtime and do inference

def run(graph, lib, params, ctx):
    # Build TVM runtime
    m = graph_runtime.create(graph, lib, ctx)
    tvm_input = tvm.nd.array(x.asnumpy(), ctx=ctx)
    m.set_input('data', tvm_input)
    m.set_input(**params)
    # execute
    m.run()
    # get outputs
    class_IDs, scores, bounding_boxs = m.get_output(0), m.get_output(1), m.get_output(2)
    return class_IDs, scores, bounding_boxs

for target, ctx in target_list:
109
    graph, lib, params = build(target)
110 111 112 113 114 115 116 117
    class_IDs, scores, bounding_boxs = run(graph, lib, params, ctx)

######################################################################
# Display result

ax = utils.viz.plot_bbox(img, bounding_boxs.asnumpy()[0], scores.asnumpy()[0],
                         class_IDs.asnumpy()[0], class_names=block.classes)
plt.show()