Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
yolov5_tvm
Commits
281c48da
Commit
281c48da
authored
Oct 24, 2023
by
zhangqha
Browse files
add tvm fp16 yolov5s example
parent
3000d881
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
0 deletions
+66
-0
yolo_half.py
yolo_half.py
+66
-0
yolov5s_half.onnx
yolov5s_half.onnx
+0
-0
No files found.
yolo_half.py
0 → 100644
View file @
281c48da
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
))
yolov5s_half.onnx
0 → 100644
View file @
281c48da
File added
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment