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_migraphx
Commits
4ec98d75
Commit
4ec98d75
authored
Nov 15, 2023
by
liucong
Browse files
修改yolov5工程格式
parent
84e926a8
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
8 additions
and
62 deletions
+8
-62
Doc/Tutorial_Python.md
Doc/Tutorial_Python.md
+1
-8
Python/YoloV5_infer_migraphx.py
Python/YoloV5_infer_migraphx.py
+4
-19
Src/YOLOV5.cpp
Src/YOLOV5.cpp
+3
-27
Src/main.cpp
Src/main.cpp
+0
-8
No files found.
Doc/Tutorial_Python.md
View file @
4ec98d75
...
...
@@ -54,18 +54,11 @@ class YOLOv5:
self.model = migraphx.parse_onnx(path, map_input_dims=maxInput)
# 获取模型输入/输出节点信息
print("inputs:")
inputs = self.model.get_inputs()
for key,value in inputs.items():
print("{}:{}".format(key,value))
print("outputs:")
outputs = self.model.get_outputs()
for key,value in outputs.items():
print("{}:{}".format(key,value))
# 获取模型的输入name
self.inputName =
"images"
self.inputName =
self.model.get_parameter_names()[0]
# 获取模型的输入尺寸
inputShape = inputShape=inputs[self.inputName].lens()
...
...
Python/YoloV5_infer_migraphx.py
View file @
4ec98d75
...
...
@@ -22,18 +22,11 @@ class YOLOv5:
self
.
model
=
migraphx
.
parse_onnx
(
path
,
map_input_dims
=
maxInput
)
# 获取模型输入/输出节点信息
print
(
"inputs:"
)
inputs
=
self
.
model
.
get_inputs
()
for
key
,
value
in
inputs
.
items
():
print
(
"{}:{}"
.
format
(
key
,
value
))
print
(
"outputs:"
)
outputs
=
self
.
model
.
get_outputs
()
for
key
,
value
in
outputs
.
items
():
print
(
"{}:{}"
.
format
(
key
,
value
))
# 获取模型的输入name
self
.
inputName
=
"images"
self
.
inputName
=
self
.
model
.
get_parameter_names
()[
0
]
# 获取模型的输入尺寸
inputShape
=
inputShape
=
inputs
[
self
.
inputName
].
lens
()
...
...
@@ -43,18 +36,11 @@ class YOLOv5:
else
:
self
.
model
=
migraphx
.
parse_onnx
(
path
)
# 获取模型输入/输出节点信息
print
(
"inputs:"
)
inputs
=
self
.
model
.
get_inputs
()
for
key
,
value
in
inputs
.
items
():
print
(
"{}:{}"
.
format
(
key
,
value
))
print
(
"outputs:"
)
outputs
=
self
.
model
.
get_outputs
()
for
key
,
value
in
outputs
.
items
():
print
(
"{}:{}"
.
format
(
key
,
value
))
# 获取模型的输入name
self
.
inputName
=
"images"
self
.
inputName
=
self
.
model
.
get_parameter_names
()[
0
]
# 获取模型的输入尺寸
inputShape
=
inputShape
=
inputs
[
self
.
inputName
].
lens
()
...
...
@@ -64,7 +50,6 @@ class YOLOv5:
# 模型编译
self
.
model
.
compile
(
t
=
migraphx
.
get_target
(
"gpu"
),
device_id
=
0
)
# device_id: 设置GPU设备,默认为0号设备
print
(
"Success to compile"
)
def
detect
(
self
,
image
,
input_shape
=
None
):
if
(
self
.
isDynamic
):
...
...
@@ -74,9 +59,8 @@ class YOLOv5:
input_img
=
self
.
prepare_input
(
image
)
# 执行推理
start
=
time
.
time
()
result
=
self
.
model
.
run
({
self
.
inputName
:
input_img
})
print
(
'net forward time: {:.4f}'
.
format
(
time
.
time
()
-
start
))
# 模型输出结果后处理
boxes
,
scores
,
class_ids
=
self
.
process_output
(
result
)
...
...
@@ -211,6 +195,7 @@ if __name__ == '__main__':
# 静态推理
if
args
.
staticInfer
:
yolov5_Static
(
args
.
imgPath
,
args
.
staticModelPath
,
args
.
objectThreshold
,
args
.
confThreshold
,
args
.
nmsThreshold
)
# 动态推理
if
args
.
dynamicInfer
:
yolov5_dynamic
(
args
.
imgFolderPath
,
args
.
dynamicModelPath
,
args
.
objectThreshold
,
args
.
confThreshold
,
args
.
nmsThreshold
)
...
...
Src/YOLOV5.cpp
View file @
4ec98d75
...
...
@@ -16,16 +16,14 @@ DetectorYOLOV5::DetectorYOLOV5()
DetectorYOLOV5
::~
DetectorYOLOV5
()
{
configurationFile
.
release
();
}
ErrorCode
DetectorYOLOV5
::
Initialize
(
InitializationParameterOfDetector
initializationParameterOfDetector
,
bool
dynamic
)
{
// 读取配置文件
std
::
string
configFilePath
=
initializationParameterOfDetector
.
configFilePath
;
if
(
Exists
(
configFilePath
)
==
false
)
if
(
!
Exists
(
configFilePath
))
{
LOG_ERROR
(
stdout
,
"no configuration file!
\n
"
);
return
CONFIG_FILE_NOT_EXIST
;
...
...
@@ -57,7 +55,7 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ
if
(
dynamic
)
{
// 加载模型
if
(
Exists
(
modelPath
)
==
false
)
if
(
!
Exists
(
modelPath
))
{
LOG_ERROR
(
stdout
,
"%s not exist!
\n
"
,
modelPath
.
c_str
());
return
MODEL_NOT_EXIST
;
...
...
@@ -69,18 +67,8 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ
LOG_INFO
(
stdout
,
"succeed to load model: %s
\n
"
,
GetFileName
(
modelPath
).
c_str
());
// 获取模型输入/输出节点信息
std
::
cout
<<
"inputs:"
<<
std
::
endl
;
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
inputs
=
net
.
get_inputs
();
for
(
auto
i
:
inputs
)
{
std
::
cout
<<
i
.
first
<<
":"
<<
i
.
second
<<
std
::
endl
;
}
std
::
cout
<<
"outputs:"
<<
std
::
endl
;
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
outputs
=
net
.
get_outputs
();
for
(
auto
i
:
outputs
)
{
std
::
cout
<<
i
.
first
<<
":"
<<
i
.
second
<<
std
::
endl
;
}
inputName
=
inputs
.
begin
()
->
first
;
inputShape
=
inputs
.
begin
()
->
second
;
int
N
=
inputShape
.
lens
()[
0
];
...
...
@@ -95,7 +83,7 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ
else
{
// 加载模型
if
(
Exists
(
modelPath
)
==
false
)
if
(
!
Exists
(
modelPath
))
{
LOG_ERROR
(
stdout
,
"%s not exist!
\n
"
,
modelPath
.
c_str
());
return
MODEL_NOT_EXIST
;
...
...
@@ -104,18 +92,8 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ
LOG_INFO
(
stdout
,
"succeed to load model: %s
\n
"
,
GetFileName
(
modelPath
).
c_str
());
// 获取模型输入/输出节点信息
std
::
cout
<<
"inputs:"
<<
std
::
endl
;
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
inputs
=
net
.
get_inputs
();
for
(
auto
i
:
inputs
)
{
std
::
cout
<<
i
.
first
<<
":"
<<
i
.
second
<<
std
::
endl
;
}
std
::
cout
<<
"outputs:"
<<
std
::
endl
;
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
outputs
=
net
.
get_outputs
();
for
(
auto
i
:
outputs
)
{
std
::
cout
<<
i
.
first
<<
":"
<<
i
.
second
<<
std
::
endl
;
}
inputName
=
inputs
.
begin
()
->
first
;
inputShape
=
inputs
.
begin
()
->
second
;
int
N
=
inputShape
.
lens
()[
0
];
...
...
@@ -170,9 +148,7 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ
classNames
.
resize
(
yolov5Parameter
.
numberOfClasses
);
}
return
SUCCESS
;
}
ErrorCode
DetectorYOLOV5
::
Detect
(
const
cv
::
Mat
&
srcImage
,
std
::
vector
<
std
::
size_t
>
&
relInputShape
,
std
::
vector
<
ResultOfDetection
>
&
resultsOfDetection
,
bool
dynamic
)
...
...
Src/main.cpp
View file @
4ec98d75
...
...
@@ -71,11 +71,7 @@ void Sample_YOLOV5()
// 推理
std
::
vector
<
migraphxSamples
::
ResultOfDetection
>
predictions
;
double
time1
=
cv
::
getTickCount
();
detector
.
Detect
(
srcImage
,
inputShape
,
predictions
,
false
);
double
time2
=
cv
::
getTickCount
();
double
elapsedTime
=
(
time2
-
time1
)
*
1000
/
cv
::
getTickFrequency
();
LOG_INFO
(
stdout
,
"inference time:%f ms
\n
"
,
elapsedTime
);
// 获取推理结果
LOG_INFO
(
stdout
,
"========== Detection Results ==========
\n
"
);
...
...
@@ -135,11 +131,7 @@ void Sample_YOLOV5_Dynamic()
{
// 推理
std
::
vector
<
migraphxSamples
::
ResultOfDetection
>
predictions
;
double
time1
=
cv
::
getTickCount
();
detector
.
Detect
(
srcImages
[
i
],
inputShapes
[
i
],
predictions
,
true
);
double
time2
=
cv
::
getTickCount
();
double
elapsedTime
=
(
time2
-
time1
)
*
1000
/
cv
::
getTickFrequency
();
LOG_INFO
(
stdout
,
"inference image%d time:%f ms
\n
"
,
i
,
elapsedTime
);
// 获取推理结果
LOG_INFO
(
stdout
,
"========== Detection Image%d Results ==========
\n
"
,
i
);
...
...
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