-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun_mobile.py
98 lines (82 loc) · 2.43 KB
/
run_mobile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import sys
from time import time
import numpy as np
import onnx
import tvm
from tvm.contrib import graph_runtime, rpc
from tvm.contrib import util, ndk, rpc_proxy
import nnvm.frontend
import nnvm.compiler
from models.YOLOv2_tiny.model import postprocess
exec_gpu = False
opt_level = 0
num_iter = 100
dtype = np.float32
onnx_graph = onnx.load('./models/YOLOv2_tiny/YOLOv2_tiny.onnx')
#print(onnx_graph)
n = 352
org_img = Image.open('./data/test.jpg')
org_img = org_img.resize((n, n))
img = np.asarray(org_img).astype(np.float32).copy()
img = img.transpose(2,0,1)
img /= 255.
img = img[np.newaxis,:]
input_name = 'input_0'
data_shape = img.shape
out_shape = (1,125,n//32,n//32)
# GET model from frameworks
# change xyz to supported framework name.
sym, params = nnvm.frontend.from_onnx(onnx_graph)
#print(sym.debug_str()
# connect to the proxy
# Set to be address of tvm proxy.
proxy_host = os.environ["TVM_ANDROID_RPC_PROXY_HOST"]
proxy_port = 9090
key = "android"
print('RPC Connecting...')
remote = rpc.connect(proxy_host, proxy_port, key=key)
print('RPC Connected')
arch = "arm64"
if exec_gpu:
# Mobile GPU
target = 'opencl'
target_host = "llvm -target=%s-linux-android" % arch
ctx = remote.cl(0)
else:
# Mobile CPU
target = "llvm -target=%s-linux-android" % arch
target_host = None
ctx = remote.cpu(0)
print('Build Graph...')
with nnvm.compiler.build_config(opt_level=opt_level, add_pass=None):
graph, lib, params = nnvm.compiler.build(sym, target, {input_name: data_shape}, params=params, target_host=target_host)
print("-------compute graph-------")
print(graph.ir())
so_name = "YOLOv2_tiny-aarch64.so"
temp = util.tempdir()
path_so = temp.relpath(so_name)
#path_so = so_name
#print(path_so)
#print('show opencl kernel')
#print(lib.imported_modules[0].get_source())
lib.export_library(path_so, ndk.create_shared)
print('DEPLOY: Shared Library Uploading...')
remote.upload(path_so)
rlib = remote.load_module(so_name)
### run on remote device
img = tvm.nd.array(img.astype(dtype), ctx)
rmodule = graph_runtime.create(graph, rlib, ctx)
rmodule.set_input('input_0', img)
rmodule.set_input(**params)
#start = time()
print('Execute')
rmodule.run()
output = tvm.nd.empty(out_shape, ctx=ctx)
output = rmodule.get_output(0, output).asnumpy()
postprocess(output[0], org_img)
plt.savefig('output-nnvm-android.png')
print('Benchmark')
ftimer = rmodule.module.time_evaluator("run", ctx, num_iter)
prof_res = ftimer()
print(prof_res)