yolo_half.py 2.36 KB
Newer Older
zhangqha's avatar
zhangqha committed
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
from tvm import testing
import onnx
#import onnxruntime
testing.utils.install_request_hook(depth=3)
# sphinx_gallery_end_ignore                                                                                                                                                                                                  
from PIL import Image
import numpy as np
from tvm.relay.transform import InferType, ToMixedPrecision
import tvm
from tvm import relay, auto_scheduler
import tvm.relay.testing
from tvm.contrib import graph_executor

'''

  prepare:
    vim /tvm-0.11-dev0/python/tvm/topi/rocm/conv2d.py +79
    set param data_type = 0
    export PYTHONPATH=/root/tvm-0.11-dev0/python:$PYTHONPATH
    export MIOPEN_DEBUG_CONV_IMPLICIT_GEMM=0
  
'''

img_data = np.random.rand(1,3,640,640).astype("float16")/255                                                                                                                             
input_name = "images"
shape_dict = {input_name: img_data.shape}
model_path = 'yolov5s_half.onnx'
onnx_model = onnx.load(model_path)
# Define the neural network and compilation target

batch_size = 1
layout = "NCHW"
target = "rocm -libs=miopen,rocblas"                                                                                                                                                     
#target = "rocm"     
dtype = "float16"
mod, params = relay.frontend.from_onnx(onnx_model, shape_dict, dtype=dtype)
# Compile with the history best

print("Compile...")
with tvm.transform.PassContext(opt_level=3):
    lib = relay.build(mod, target=target, params=params)
print('Compile success!')

dev = tvm.device(str(target), 0)
module = graph_executor.GraphModule(lib["default"](dev))
module.set_input(input_name, img_data)
module.run()
res = module.get_output(0)
print("res:{}, res shape:{}".format(res, res.shape))

# use onnxruntime verify tvm output
def verify(tvm_res):
    session = onnxruntime.InferenceSession(model_path,providers=['CPUExecutionProvider'] )
    input_name = session.get_inputs()[0].name
    output_name = session.get_outputs()[0].name
    output = session.run([output_name], {input_name: img_data})
    tvm.testing.assert_allclose(output[0], tvm_res.numpy(), rtol=1e-2, atol=1e-2)
    print("use onnxruntime verify successfully !")  

#verify(res)

print("Evaluate inference time cost...")
print(module.benchmark(dev, repeat=1, min_repeat_ms=500))