Skip to content

Instantly share code, notes, and snippets.

@comaniac
Created August 7, 2020 17:14
Show Gist options
  • Save comaniac/0962717b437900fd0db42bd6ad14a9cd to your computer and use it in GitHub Desktop.
Save comaniac/0962717b437900fd0db42bd6ad14a9cd to your computer and use it in GitHub Desktop.
import numpy as np
import tvm
from tvm import relay
from tvm.contrib import graph_runtime
from tvm.relay.backend import compile_engine
import gluoncv as gcv
model_name = 'MobileNet1.0'
shape = (1, 3, 224, 224)
target = 'llvm -mcpu=skylake-avx512'
dev_ctx = tvm.cpu(0)
net = gcv.model_zoo.get_model(model_name, pretrained=True)
mod, params = relay.frontend.from_mxnet(net, shape={'data': shape})
compile_engine.get().clear()
with relay.build_config(opt_level=3):
graph, lib, params = relay.build_module.build(mod, target=target, params=params)
print('Evaluating...')
runtime = graph_runtime.create(graph, lib, dev_ctx)
runtime.set_input('data',
tvm.nd.array(np.random.uniform(size=shape).astype('float32')))
runtime.set_input(**params)
ftimer = runtime.module.time_evaluator('run', dev_ctx, number=100, repeat=3)
prof_res = np.array(ftimer().results) * 1000
print('Mean inference time (std dev): %.2f ms (%.2f ms)' %
(np.mean(prof_res), np.std(prof_res)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment