Skip to content

Commit 0138ea8

Browse files
author
Fangchang Ma
committed
add code for running on TX2
1 parent 6ea77b2 commit 0138ea8

File tree

8 files changed

+127
-0
lines changed

8 files changed

+127
-0
lines changed

Diff for: deploy/data/depth.npy

196 KB
Binary file not shown.

Diff for: deploy/data/depth.png

38 KB
Loading

Diff for: deploy/data/pred.npy

196 KB
Binary file not shown.

Diff for: deploy/data/pred.png

18.4 KB
Loading

Diff for: deploy/data/rgb.npy

1.15 MB
Binary file not shown.

Diff for: deploy/data/rgb.png

109 KB
Loading

Diff for: deploy/data/visualize.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import numpy as np
2+
import matplotlib as mp
3+
4+
mp.use("pdf")
5+
import matplotlib.pyplot as plt
6+
7+
cmap = plt.cm.viridis
8+
9+
def colored_depthmap(depth, d_min=None, d_max=None):
10+
if d_min is None:
11+
d_min = np.min(depth)
12+
if d_max is None:
13+
d_max = np.max(depth)
14+
depth_relative = (depth - d_min) / (d_max - d_min)
15+
return 255 * cmap(depth_relative)[:,:,:3] # HWC
16+
17+
def save_rgb_image(rgb_npy_fp, filename):
18+
rgb_np = np.load(rgb_npy_fp)
19+
rgb_scaled = 255 * np.transpose(np.squeeze(rgb_np), (0,1,2)) # HWC
20+
mp.image.imsave('rgb.png', rgb_scaled.astype('uint8'))
21+
22+
def save_depth_image(depth_npy_fp, filename):
23+
depth_np = np.load(depth_npy_fp)
24+
depth_np_color = colored_depthmap(depth_np)
25+
mp.image.imsave('depth.png', depth_np_color.astype('uint8'))
26+
27+
def save_pred_image(pred_npy_fp, filename):
28+
pred_np = np.load(pred_npy_fp)
29+
pred_np_2d = pred_np[0,0,:,:] # HW
30+
pred_np_color = colored_depthmap(pred_np_2d)
31+
mp.image.imsave('pred.png', pred_np_color.astype('uint8'))
32+
33+
save_rgb_image('rgb.npy', 'rgb.png')
34+
save_depth_image('depth.npy', 'depth.png')
35+
save_pred_image('pred.npy', 'pred.png')

Diff for: deploy/tx2_run_tvm.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import tvm
2+
import numpy as np
3+
import argparse
4+
import os
5+
import time
6+
7+
def run_model(model_dir, input_fp, output_fp, warmup_trials, run_trials, cuda, try_randin):
8+
# import compiled graph
9+
print("=> [TVM on TX2] using model files in {}".format(model_dir))
10+
assert(os.path.isdir(model_dir))
11+
12+
print("=> [TVM on TX2] loading model lib and ptx")
13+
loaded_lib = tvm.module.load(os.path.join(model_dir, "deploy_lib.o"))
14+
if cuda:
15+
dev_lib = tvm.module.load(os.path.join(model_dir, "deploy_cuda.ptx"))
16+
loaded_lib.import_module(dev_lib)
17+
18+
print("=> [TVM on TX2] loading model graph and params")
19+
loaded_graph = open(os.path.join(model_dir,"deploy_graph.json")).read()
20+
loaded_params = bytearray(open(os.path.join(model_dir, "deploy_param.params"), "rb").read())
21+
22+
print("=> [TVM on TX2] creating TVM runtime module")
23+
fcreate = tvm.get_global_func("tvm.graph_runtime.create")
24+
ctx = tvm.gpu(0) if cuda else tvm.cpu(0)
25+
gmodule = fcreate(loaded_graph, loaded_lib, ctx.device_type, ctx.device_id)
26+
set_input, get_output, run = gmodule["set_input"], gmodule["get_output"], gmodule["run"]
27+
28+
print("=> [TVM on TX2] feeding inputs and params into TVM module")
29+
rgb_np = np.load(input_fp) # HWC
30+
x = np.zeros([1,3,224,224]) # NCHW
31+
x[0,:,:,:] = np.transpose(rgb_np, (2,0,1))
32+
set_input('0', tvm.nd.array(x.astype('float32')))
33+
gmodule["load_params"](loaded_params)
34+
35+
print("=> [TVM on TX2] running TVM module, saving output")
36+
run() # not gmodule.run()
37+
out_shape = (1, 1, 224, 224)
38+
out = tvm.nd.empty(out_shape, "float32")
39+
get_output(0, out)
40+
np.save(output_fp, out.asnumpy())
41+
42+
print("=> [TVM on TX2] benchmarking: {} warmup, {} run trials".format(warmup_trials, run_trials))
43+
# run model several times as a warmup
44+
for i in range(warmup_trials):
45+
run()
46+
ctx.sync()
47+
48+
# profile runtime using TVM time evaluator
49+
ftimer = gmodule.time_evaluator("run", ctx, number=1, repeat=run_trials)
50+
profile_result = ftimer()
51+
profiled_runtime = profile_result[0]
52+
53+
print("=> [TVM on TX2] profiled runtime (in ms): {:.5f}".format(1000*profiled_runtime))
54+
55+
# try randomizing input
56+
if try_randin:
57+
randin_runtime = 0
58+
for i in range(run_trials):
59+
x = np.random.randn(1, 3, 224, 224)
60+
set_input('0', tvm.nd.array(x.astype('float32')))
61+
randin_ftimer = gmodule.time_evaluator("run", ctx, number=1, repeat=1)
62+
randin_profile_result = randin_ftimer()
63+
randin_runtime += randin_profile_result[0]
64+
randomized_input_runtime = randin_runtime/run_trials
65+
print("=> [TVM on TX2] with randomized input on every run, profiled runtime (in ms): {:.5f}".format(1000*randomized_input_runtime))
66+
67+
def main():
68+
parser = argparse.ArgumentParser()
69+
parser.add_argument('--model-dir', type=str, required=True,
70+
help='path to folder with TVM-compiled model files (required)')
71+
parser.add_argument('--input-fp', type=str, default='data/rgb.npy',
72+
help='numpy file containing input rgb data (default: data/rgb.npy')
73+
parser.add_argument('--output-fp', type=str, default='data/pred.npy',
74+
help='numpy file to store output prediction data (default: data/pred.npy')
75+
76+
77+
parser.add_argument('--warmup', type=int, default=10,
78+
help='number of inference warmup trials (default: 10)')
79+
parser.add_argument('--run', type=int, default=100,
80+
help='number of inference run trials (default: 100)')
81+
parser.add_argument('--cuda', type=bool, default=False,
82+
help='run with CUDA (default: False)')
83+
84+
parser.add_argument('--randin', type=bool, default=False,
85+
help='profile runtime while randomizing input on every run (default: False)')
86+
87+
args = parser.parse_args()
88+
run_model(args.model_dir, args.input_fp, args.output_fp, args.warmup, args.run, args.cuda, try_randin=args.randin)
89+
90+
if __name__ == '__main__':
91+
main()
92+

0 commit comments

Comments
 (0)