<jupyter_start><jupyter_text>Deploy Single Shot Multibox Detector(SSD) model===============================================**Author**: `<NAME> `_`<NAME> `_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<jupyter_code>import tvm
from tvm import te

from matplotlib import pyplot as plt
from tvm import relay
from tvm.contrib import graph_runtime
from tvm.contrib.download import download_testdata
from gluoncv import model_zoo, data, utils<jupyter_output><empty_output><jupyter_text>Preliminary and Set parameters------------------------------NoteWe 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 `tune_relay_x86` to tune x86 CPU and  `tune_relay_arm` for arm CPU.  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.<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>Download and pre-process demo image<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>Convert and compile model for CPU.<jupyter_code>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<jupyter_output><empty_output><jupyter_text>Create TVM runtime and do inference<jupyter_code>def run(lib, ctx):
    # Build TVM runtime
    m = graph_runtime.GraphModule(lib["default"](ctx))
    tvm_input = tvm.nd.array(x.asnumpy(), ctx=ctx)
    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"]:
    ctx = tvm.context(target, 0)
    if ctx.exist:
        lib = build(target)
        class_IDs, scores, bounding_boxs = run(lib, ctx)<jupyter_output><empty_output><jupyter_text>Display result<jupyter_code>ax = utils.viz.plot_bbox(
    img,
    bounding_boxs.asnumpy()[0],
    scores.asnumpy()[0],
    class_IDs.asnumpy()[0],
    class_names=block.classes,
)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12300123001230012, 'fraction_numerical': 0.01937269372693727, 'mean_word_length': 4.979779411764706, 'pattern_counts': {'":': 1, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '196793', 'license': 'Apache-2.0', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1112, 'n_words': 277, 'path': '_downloads/9a21b42e66b4b1c56e9e651ee1609fee/deploy_ssd_gluoncv.ipynb', 'repo_name': 'areusch/tvm-docs-refactor', 'size': '3259'}