Deploy Single Shot Multibox Detector(SSD) model

Author: Yao Wang Leyuan Wang

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 tvm import te

from matplotlib import pyplot as plt
from tvm import relay
from tvm.contrib import graph_executor
from tvm.contrib.download import download_testdata
from gluoncv import model_zoo, data, utils
/usr/local/lib/python3.7/dist-packages/gluoncv/__init__.py:40: UserWarning: Both `mxnet==1.6.0` and `torch==1.11.0+cpu` are installed. You might encounter increased GPU memory footprint if both framework are used at the same time.
  warnings.warn(f'Both `mxnet=={mx.__version__}` and `torch=={torch.__version__}` are installed. '

Preliminary and Set parameters

Note

We support compiling SSD on both CPUs and GPUs now.

To get best inference performance on CPU, change target argument according to your device and follow the Auto-tuning a Convolutional Network for x86 CPU to tune x86 CPU and Auto-tuning a Convolutional Network for ARM CPU for arm CPU.

To get best inference performance on Intel graphics, change target argument to 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 cuda; and for OPENCL-based GPUs, change target argument to opencl followed by device argument according to your device.

supported_model = [
    "ssd_512_resnet50_v1_voc",
    "ssd_512_resnet50_v1_coco",
    "ssd_512_resnet101_v2_voc",
    "ssd_512_mobilenet1.0_voc",
    "ssd_512_mobilenet1.0_coco",
    "ssd_300_vgg16_atrous_voc" "ssd_512_vgg16_atrous_coco",
]

model_name = supported_model[0]
dshape = (1, 3, 512, 512)

Download and pre-process demo image

im_fname = download_testdata(
    "https://github.com/dmlc/web-data/blob/main/" + "gluoncv/detection/street_small.jpg?raw=true",
    "street_small.jpg",
    module="data",
)
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)


def build(target):
    mod, params = relay.frontend.from_mxnet(block, {"data": dshape})
    with tvm.transform.PassContext(opt_level=3):
        lib = relay.build(mod, target, params=params)
    return lib
/usr/local/lib/python3.7/dist-packages/mxnet/gluon/block.py:1389: UserWarning: Cannot decide type for the following arguments. Consider providing them as input:
        data: None
  input_sym_arg_type = in_param.infer_type()[0]
Downloading /workspace/.mxnet/models/ssd_512_resnet50_v1_voc-9c8b225a.zip from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/models/ssd_512_resnet50_v1_voc-9c8b225a.zip...

  0%|          | 0/132723 [00:00<?, ?KB/s]
  5%|4         | 6621/132723 [00:00<00:01, 66200.28KB/s]
 12%|#1        | 15322/132723 [00:00<00:01, 78424.76KB/s]
 18%|#8        | 24203/132723 [00:00<00:01, 83156.64KB/s]
 25%|##4       | 33021/132723 [00:00<00:01, 85137.53KB/s]
 32%|###1      | 41833/132723 [00:00<00:01, 86205.82KB/s]
 38%|###8      | 50565/132723 [00:00<00:00, 86582.52KB/s]
 45%|####4     | 59355/132723 [00:00<00:00, 87010.98KB/s]
 51%|#####1    | 68057/132723 [00:00<00:00, 82893.66KB/s]
 58%|#####7    | 76925/132723 [00:00<00:00, 84639.42KB/s]
 65%|######4   | 85712/132723 [00:01<00:00, 85611.13KB/s]
 71%|#######1  | 94511/132723 [00:01<00:00, 86325.06KB/s]
 78%|#######7  | 103162/132723 [00:01<00:00, 86241.88KB/s]
 84%|########4 | 111983/132723 [00:01<00:00, 86830.05KB/s]
 91%|######### | 120743/132723 [00:01<00:00, 87059.10KB/s]
 98%|#########7| 129456/132723 [00:01<00:00, 87043.29KB/s]
100%|##########| 132723/132723 [00:01<00:00, 85342.70KB/s]

Create TVM runtime and do inference .. note:

Use target = "cuda -libs" to enable thrust based sort, if you
enabled thrust during cmake by -DUSE_THRUST=ON.
def run(lib, dev):
    # Build TVM runtime
    m = graph_executor.GraphModule(lib["default"](dev))
    tvm_input = tvm.nd.array(x.asnumpy(), device=dev)
    m.set_input("data", tvm_input)
    # 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 in ["llvm", "cuda"]:
    dev = tvm.device(target, 0)
    if dev.exist:
        lib = build(target)
        class_IDs, scores, bounding_boxs = run(lib, dev)
/workspace/python/tvm/driver/build_module.py:268: UserWarning: target_host parameter is going to be deprecated. Please pass in tvm.target.Target(target, host=target_host) instead.
  "target_host parameter is going to be deprecated. "

Display result

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

Total running time of the script: ( 2 minutes 20.458 seconds)

Gallery generated by Sphinx-Gallery