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
yaoht
YOLOX_MiGraphX
Commits
a5e2341e
Commit
a5e2341e
authored
Jan 08, 2024
by
yaoht
Browse files
fix cpp inputdata channel range problem
parent
e80d6c72
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
309 additions
and
442 deletions
+309
-442
Python/__pycache__/coco_classes.cpython-37.pyc
Python/__pycache__/coco_classes.cpython-37.pyc
+0
-0
Python/__pycache__/coco_classes.cpython-38.pyc
Python/__pycache__/coco_classes.cpython-38.pyc
+0
-0
Src/YOLOX.cpp
Src/YOLOX.cpp
+231
-293
Src/YOLOX.h
Src/YOLOX.h
+33
-30
Src/main.cpp
Src/main.cpp
+45
-119
No files found.
Python/__pycache__/coco_classes.cpython-37.pyc
0 → 100644
View file @
a5e2341e
File added
Python/__pycache__/coco_classes.cpython-38.pyc
0 → 100644
View file @
a5e2341e
File added
Src/YOLOX.cpp
View file @
a5e2341e
This diff is collapsed.
Click to expand it.
Src/YOLOX.h
View file @
a5e2341e
#ifndef __DETECTOR_YOLOX_H__
#ifndef __DETECTOR_YOLOX_H__
#define __DETECTOR_YOLOX_H__
#define __DETECTOR_YOLOX_H__
#include <migraphx/program.hpp>
#include <CommonDefinition.h>
#include <CommonDefinition.h>
#include <migraphx/program.hpp>
namespace
migraphxSamples
namespace
migraphxSamples
{
{
typedef
struct
_YOLOXParameter
typedef
struct
_YOLOXParameter
{
{
int
numberOfClasses
;
int
numberOfClasses
;
float
confidenceThreshold
;
float
confidenceThreshold
;
float
nmsThreshold
;
float
nmsThreshold
;
float
objectThreshold
;
float
objectThreshold
;
}
YOLOXParameter
;
}
YOLOXParameter
;
struct
Object
struct
Object
{
{
cv
::
Rect_
<
float
>
rect
;
cv
::
Rect_
<
float
>
rect
;
int
label
;
int
label
;
float
prob
;
float
prob
;
};
};
struct
GridAndStride
struct
GridAndStride
{
{
int
grid0
;
int
grid0
;
int
grid1
;
int
grid1
;
int
stride
;
int
stride
;
};
};
class
DetectorYOLOX
class
DetectorYOLOX
{
{
public:
public:
DetectorYOLOX
();
DetectorYOLOX
();
~
DetectorYOLOX
();
ErrorCode
Initialize
(
InitializationParameterOfDetector
initializationParameterOfDetector
,
bool
dynamic
);
~
DetectorYOLOX
();
void
decode_outputs
(
float
*
prob
,
std
::
vector
<
Object
>&
objects
,
float
scalew
,
float
scaleh
,
const
int
img_w
,
const
int
img_h
);
void
generate_grids_and_stride
(
std
::
vector
<
int
>&
strides
,
std
::
vector
<
GridAndStride
>&
grid_strides
);
void
generate_yolox_proposals
(
std
::
vector
<
GridAndStride
>
grid_strides
,
float
*
feat_blob
,
float
prob_threshold
,
std
::
vector
<
Object
>&
objects
);
void
qsort_descent_inplace
(
std
::
vector
<
Object
>&
faceobjects
,
int
left
,
int
right
);
void
qsort_descent_inplace
(
std
::
vector
<
Object
>&
objects
);
void
nms_sorted_bboxes
(
const
std
::
vector
<
Object
>&
faceobjects
,
std
::
vector
<
int
>&
picked
,
float
nms_threshold
);
inline
float
intersection_area
(
const
Object
&
a
,
const
Object
&
b
);
ErrorCode
Detect
(
const
cv
::
Mat
&
srcImage
,
std
::
vector
<
std
::
size_t
>
&
relInputShape
,
std
::
vector
<
ResultOfDetection
>
&
resultsOfDetection
,
bool
dynamic
);
ErrorCode
Initialize
(
InitializationParameterOfDetector
initializationParameterOfDetector
);
ErrorCode
Detect
(
const
cv
::
Mat
&
srcImage
,
const
std
::
vector
<
std
::
size_t
>
&
relInputShape
,
std
::
vector
<
ResultOfDetection
>
&
resultsOfDetection
);
private:
void
decode_outputs
(
float
*
prob
,
std
::
vector
<
Object
>
&
objects
,
float
scalew
,
float
scaleh
,
const
int
img_w
,
const
int
img_h
,
cv
::
Size
inputSize
);
void
generate_grids_and_stride
(
std
::
vector
<
int
>
&
strides
,
std
::
vector
<
GridAndStride
>
&
grid_strides
,
cv
::
Size
inputSize
);
void
generate_yolox_proposals
(
std
::
vector
<
GridAndStride
>
grid_strides
,
float
*
feat_blob
,
float
prob_threshold
,
std
::
vector
<
Object
>
&
objects
);
void
qsort_descent_inplace
(
std
::
vector
<
Object
>
&
faceobjects
,
int
left
,
int
right
);
void
qsort_descent_inplace
(
std
::
vector
<
Object
>
&
objects
);
void
nms_sorted_bboxes
(
const
std
::
vector
<
Object
>
&
faceobjects
,
std
::
vector
<
int
>
&
picked
,
float
nms_threshold
);
inline
float
intersection_area
(
const
Object
&
a
,
const
Object
&
b
);
private:
cv
::
FileStorage
configurationFile
;
cv
::
FileStorage
configurationFile
;
migraphx
::
program
net
;
migraphx
::
program
net
;
...
@@ -57,15 +62,13 @@ private:
...
@@ -57,15 +62,13 @@ private:
std
::
string
inputName
;
std
::
string
inputName
;
std
::
string
modelPath
;
std
::
string
modelPath
;
migraphx
::
shape
inputShape
;
migraphx
::
shape
inputShape
;
bool
useFP16
;
bool
useFP16
;
std
::
vector
<
std
::
string
>
classNames
;
std
::
vector
<
std
::
string
>
classNames
;
YOLOXParameter
yoloxParameter
;
YOLOXParameter
yoloxParameter
;
};
};
}
}
// namespace migraphxSamples
#endif
#endif
Src/main.cpp
View file @
a5e2341e
#include <Filesystem.h>
#include <SimpleLog.h>
#include <YOLOX.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <SimpleLog.h>
#include <Filesystem.h>
#include <YOLOX.h>
void
MIGraphXSamplesUsage
(
char
*
programName
)
void
MIGraphXSamplesUsage
(
char
*
programName
)
{
{
printf
(
"Usage : %s <index>
\n
"
,
programName
);
printf
(
"Usage : %s <index>
\n
"
,
programName
);
printf
(
"index:
\n
"
);
printf
(
"index:
\n
"
);
printf
(
"
\t
0) YOLOX sample.
\n
"
);
printf
(
"
\t
0) YOLOX sample.
\n
"
);
// printf("\t 1) YOLOX Dynamic sample.\n"); 暂不支持
}
}
void
Sample_YOLOX
();
void
Sample_YOLOX
();
void
Sample_YOLOX_Dynamic
();
int
main
(
int
argc
,
char
*
argv
[])
int
main
(
int
argc
,
char
*
argv
[])
{
{
if
(
argc
<
2
||
argc
>
2
)
{
if
(
argc
<
2
||
argc
>
2
)
{
MIGraphXSamplesUsage
(
argv
[
0
]);
MIGraphXSamplesUsage
(
argv
[
0
]);
return
-
1
;
return
-
1
;
}
}
if
(
!
strncmp
(
argv
[
1
],
"-h"
,
2
))
if
(
!
strncmp
(
argv
[
1
],
"-h"
,
2
))
{
{
MIGraphXSamplesUsage
(
argv
[
0
]);
MIGraphXSamplesUsage
(
argv
[
0
]);
return
0
;
return
0
;
}
}
switch
(
*
argv
[
1
])
switch
(
*
argv
[
1
])
{
{
case
'0'
:
{
case
'0'
:
Sample_YOLOX
();
{
break
;
Sample_YOLOX
();
}
break
;
case
'1'
:
{
}
break
;
case
'1'
:
}
{
default:
{
// Sample_YOLOX_Dynamic(); 暂不支持
MIGraphXSamplesUsage
(
argv
[
0
]);
break
;
break
;
}
}
default
:
{
MIGraphXSamplesUsage
(
argv
[
0
]);
break
;
}
}
}
return
0
;
return
0
;
}
}
void
Sample_YOLOX
()
void
Sample_YOLOX
()
{
{
// 创建YOLOX检测器
// 创建YOLOX检测器
migraphxSamples
::
DetectorYOLOX
detector
;
migraphxSamples
::
DetectorYOLOX
detector
;
migraphxSamples
::
InitializationParameterOfDetector
initParamOfDetectorYOLOX
;
migraphxSamples
::
InitializationParameterOfDetector
initParamOfDetectorYOLOX
;
initParamOfDetectorYOLOX
.
configFilePath
=
CONFIG_FILE
;
initParamOfDetectorYOLOX
.
configFilePath
=
CONFIG_FILE
;
migraphxSamples
::
ErrorCode
errorCode
=
detector
.
Initialize
(
initParamOfDetectorYOLOX
,
false
);
migraphxSamples
::
ErrorCode
errorCode
=
if
(
errorCode
!=
migraphxSamples
::
SUCCESS
)
detector
.
Initialize
(
initParamOfDetectorYOLOX
,
false
);
{
if
(
errorCode
!=
migraphxSamples
::
SUCCESS
)
{
LOG_ERROR
(
stdout
,
"fail to initialize detector!
\n
"
);
LOG_ERROR
(
stdout
,
"fail to initialize detector!
\n
"
);
exit
(
-
1
);
exit
(
-
1
);
}
}
LOG_INFO
(
stdout
,
"succeed to initialize detector
\n
"
);
LOG_INFO
(
stdout
,
"succeed to initialize detector
\n
"
);
// 读取测试图片
// 读取测试图片
cv
::
Mat
srcImage
=
cv
::
imread
(
"../Resource/Images/image_test.jpg"
,
1
);
cv
::
Mat
srcImage
=
cv
::
imread
(
"../Resource/Images/image_test.jpg"
,
1
);
// 静态推理固定尺寸
// 静态推理固定尺寸
std
::
vector
<
std
::
size_t
>
inputShape
=
{
1
,
3
,
640
,
640
};
std
::
vector
<
std
::
size_t
>
inputShape
=
{
1
,
3
,
640
,
640
};
// 推理
// 推理
std
::
vector
<
migraphxSamples
::
ResultOfDetection
>
predictions
;
std
::
vector
<
migraphxSamples
::
ResultOfDetection
>
predictions
;
double
time1
=
cv
::
getTickCount
();
double
time1
=
cv
::
getTickCount
();
detector
.
Detect
(
srcImage
,
inputShape
,
predictions
,
false
);
detector
.
Detect
(
srcImage
,
inputShape
,
predictions
,
false
);
double
time2
=
cv
::
getTickCount
();
double
time2
=
cv
::
getTickCount
();
double
elapsedTime
=
(
time2
-
time1
)
*
1000
/
cv
::
getTickFrequency
();
double
elapsedTime
=
(
time2
-
time1
)
*
1000
/
cv
::
getTickFrequency
();
LOG_INFO
(
stdout
,
"inference time:%f ms
\n
"
,
elapsedTime
);
LOG_INFO
(
stdout
,
"inference time:%f ms
\n
"
,
elapsedTime
);
// 获取推理结果
// 获取推理结果
LOG_INFO
(
stdout
,
"========== Detection Results ==========
\n
"
);
LOG_INFO
(
stdout
,
"========== Detection Results ==========
\n
"
);
for
(
int
i
=
0
;
i
<
predictions
.
size
();
++
i
)
for
(
int
i
=
0
;
i
<
predictions
.
size
();
++
i
)
{
{
migraphxSamples
::
ResultOfDetection
result
=
predictions
[
i
];
migraphxSamples
::
ResultOfDetection
result
=
predictions
[
i
];
cv
::
rectangle
(
srcImage
,
result
.
boundingBox
,
cv
::
Scalar
(
0
,
255
,
255
),
2
);
cv
::
rectangle
(
srcImage
,
result
.
boundingBox
,
cv
::
Scalar
(
0
,
255
,
255
),
2
);
std
::
string
label
=
cv
::
format
(
"%.2f"
,
result
.
confidence
);
std
::
string
label
=
cv
::
format
(
"%.2f"
,
result
.
confidence
);
label
=
result
.
className
+
" "
+
label
;
label
=
result
.
className
+
" "
+
label
;
int
left
=
predictions
[
i
].
boundingBox
.
x
;
int
left
=
predictions
[
i
].
boundingBox
.
x
;
int
top
=
predictions
[
i
].
boundingBox
.
y
;
int
top
=
predictions
[
i
].
boundingBox
.
y
;
int
baseLine
;
int
baseLine
;
cv
::
Size
labelSize
=
cv
::
getTextSize
(
label
,
cv
::
FONT_HERSHEY_SIMPLEX
,
0.5
,
1
,
&
baseLine
);
cv
::
Size
labelSize
=
cv
::
getTextSize
(
label
,
cv
::
FONT_HERSHEY_SIMPLEX
,
0.5
,
1
,
&
baseLine
);
top
=
max
(
top
,
labelSize
.
height
);
top
=
max
(
top
,
labelSize
.
height
);
cv
::
putText
(
srcImage
,
label
,
cv
::
Point
(
left
,
top
-
10
),
cv
::
FONT_HERSHEY_SIMPLEX
,
1
,
cv
::
Scalar
(
0
,
255
,
255
),
2
);
cv
::
putText
(
srcImage
,
label
,
cv
::
Point
(
left
,
top
-
10
),
cv
::
FONT_HERSHEY_SIMPLEX
,
1
,
cv
::
Scalar
(
0
,
255
,
255
),
2
);
LOG_INFO
(
stdout
,
"box:%d %d %d %d,label:%d,confidence:%f
\n
"
,
predictions
[
i
].
boundingBox
.
x
,
predictions
[
i
].
boundingBox
.
y
,
predictions
[
i
].
boundingBox
.
width
,
predictions
[
i
].
boundingBox
.
height
,
predictions
[
i
].
classID
,
predictions
[
i
].
confidence
);
LOG_INFO
(
stdout
,
"box:%d %d %d %d,label:%d,confidence:%f
\n
"
,
}
predictions
[
i
].
boundingBox
.
x
,
predictions
[
i
].
boundingBox
.
y
,
cv
::
imwrite
(
"Result.jpg"
,
srcImage
);
predictions
[
i
].
boundingBox
.
width
,
LOG_INFO
(
stdout
,
"Detection results have been saved to ./Result.jpg
\n
"
);
predictions
[
i
].
boundingBox
.
height
,
predictions
[
i
].
classID
,
predictions
[
i
].
confidence
);
}
void
Sample_YOLOX_Dynamic
()
{
// 创建YOLOX检测器
migraphxSamples
::
DetectorYOLOX
detector
;
migraphxSamples
::
InitializationParameterOfDetector
initParamOfDetectorYOLOX
;
initParamOfDetectorYOLOX
.
configFilePath
=
CONFIG_FILE
;
migraphxSamples
::
ErrorCode
errorCode
=
detector
.
Initialize
(
initParamOfDetectorYOLOX
,
true
);
if
(
errorCode
!=
migraphxSamples
::
SUCCESS
)
{
LOG_ERROR
(
stdout
,
"fail to initialize detector!
\n
"
);
exit
(
-
1
);
}
LOG_INFO
(
stdout
,
"succeed to initialize detector
\n
"
);
// 读取测试图像
std
::
vector
<
cv
::
Mat
>
srcImages
;
cv
::
String
folder
=
"../Resource/Images/DynamicPics"
;
std
::
vector
<
cv
::
String
>
imagePathList
;
cv
::
glob
(
folder
,
imagePathList
);
for
(
int
i
=
0
;
i
<
imagePathList
.
size
();
++
i
)
{
cv
::
Mat
srcImage
=
cv
::
imread
(
imagePathList
[
i
],
1
);
srcImages
.
push_back
(
srcImage
);
}
// 设置动态推理shape
std
::
vector
<
std
::
vector
<
std
::
size_t
>>
inputShapes
;
inputShapes
.
push_back
({
1
,
3
,
416
,
416
});
inputShapes
.
push_back
({
1
,
3
,
608
,
608
});
for
(
int
i
=
0
;
i
<
srcImages
.
size
();
++
i
)
{
// 推理
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
);
for
(
int
j
=
0
;
j
<
predictions
.
size
();
++
j
)
{
migraphxSamples
::
ResultOfDetection
result
=
predictions
[
j
];
cv
::
rectangle
(
srcImages
[
i
],
result
.
boundingBox
,
cv
::
Scalar
(
0
,
255
,
255
),
2
);
std
::
string
label
=
cv
::
format
(
"%.2f"
,
result
.
confidence
);
label
=
result
.
className
+
" "
+
label
;
int
left
=
predictions
[
j
].
boundingBox
.
x
;
int
top
=
predictions
[
j
].
boundingBox
.
y
;
int
baseLine
;
cv
::
Size
labelSize
=
cv
::
getTextSize
(
label
,
cv
::
FONT_HERSHEY_SIMPLEX
,
0.5
,
1
,
&
baseLine
);
top
=
max
(
top
,
labelSize
.
height
);
cv
::
putText
(
srcImages
[
i
],
label
,
cv
::
Point
(
left
,
top
-
10
),
cv
::
FONT_HERSHEY_SIMPLEX
,
1
,
cv
::
Scalar
(
0
,
255
,
255
),
2
);
LOG_INFO
(
stdout
,
"box:%d %d %d %d,label:%d,confidence:%f
\n
"
,
predictions
[
j
].
boundingBox
.
x
,
predictions
[
j
].
boundingBox
.
y
,
predictions
[
j
].
boundingBox
.
width
,
predictions
[
j
].
boundingBox
.
height
,
predictions
[
j
].
classID
,
predictions
[
j
].
confidence
);
}
std
::
string
imgName
=
cv
::
format
(
"Result%d.jpg"
,
i
);
cv
::
imwrite
(
imgName
,
srcImages
[
i
]);
LOG_INFO
(
stdout
,
"Detection results have been saved to ./Result%d.jpg
\n
"
,
i
);
}
}
cv
::
imwrite
(
"Result.jpg"
,
srcImage
);
LOG_INFO
(
stdout
,
"Detection results have been saved to ./Result.jpg
\n
"
);
}
}
\ No newline at end of file
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