Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleDetection
提交
d7d3fbea
P
PaddleDetection
项目概览
s920243400
/
PaddleDetection
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleDetection
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
d7d3fbea
编写于
9月 11, 2020
作者:
W
wangxinxin08
提交者:
GitHub
9月 11, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify deploy inference code (#1394)
fix compile error in code modify code for inference
上级
399ed5a0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
92 addition
and
42 deletion
+92
-42
deploy/cpp/include/object_detector.h
deploy/cpp/include/object_detector.h
+7
-3
deploy/cpp/src/main.cc
deploy/cpp/src/main.cc
+35
-23
deploy/cpp/src/object_detector.cc
deploy/cpp/src/object_detector.cc
+50
-16
未找到文件。
deploy/cpp/include/object_detector.h
浏览文件 @
d7d3fbea
...
...
@@ -18,6 +18,7 @@
#include <vector>
#include <memory>
#include <utility>
#include <ctime>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
...
...
@@ -74,9 +75,12 @@ class ObjectDetector {
const
int
gpu_id
=
0
);
// Run predictor
void
Predict
(
const
cv
::
Mat
&
img
,
std
::
vector
<
ObjectResult
>*
result
);
void
Predict
(
const
cv
::
Mat
&
im
,
const
double
threshold
=
0.5
,
const
int
warmup
=
0
,
const
int
repeats
=
1
,
const
bool
run_benchmark
=
false
,
std
::
vector
<
ObjectResult
>*
result
=
nullptr
);
// Get Model Label list
const
std
::
vector
<
std
::
string
>&
GetLabelList
()
const
{
...
...
deploy/cpp/src/main.cc
浏览文件 @
d7d3fbea
...
...
@@ -29,6 +29,9 @@ DEFINE_bool(use_camera, false, "Use camera or not");
DEFINE_string
(
run_mode
,
"fluid"
,
"Mode of running(fluid/trt_fp32/trt_fp16)"
);
DEFINE_int32
(
gpu_id
,
0
,
"Device id of GPU to execute"
);
DEFINE_int32
(
camera_id
,
-
1
,
"Device id of camera to predict"
);
DEFINE_bool
(
run_benchmark
,
false
,
"Whether to predict a image_file repeatedly for benchmark"
);
DEFINE_double
(
threshold
,
0.5
,
"Threshold of score."
);
DEFINE_string
(
output_dir
,
"output"
,
"Directory of output visualization files."
);
void
PredictVideo
(
const
std
::
string
&
video_path
,
PaddleDetection
::
ObjectDetector
*
det
)
{
...
...
@@ -72,7 +75,7 @@ void PredictVideo(const std::string& video_path,
if
(
frame
.
empty
())
{
break
;
}
det
->
Predict
(
frame
,
&
result
);
det
->
Predict
(
frame
,
0.5
,
0
,
1
,
false
,
&
result
);
cv
::
Mat
out_im
=
PaddleDetection
::
VisualizeResult
(
frame
,
result
,
labels
,
colormap
);
for
(
const
auto
&
item
:
result
)
{
...
...
@@ -93,31 +96,40 @@ void PredictVideo(const std::string& video_path,
}
void
PredictImage
(
const
std
::
string
&
image_path
,
PaddleDetection
::
ObjectDetector
*
det
)
{
const
double
threshold
,
const
bool
run_benchmark
,
PaddleDetection
::
ObjectDetector
*
det
,
const
std
::
string
&
output_dir
=
"output"
)
{
// Open input image as an opencv cv::Mat object
cv
::
Mat
im
=
cv
::
imread
(
image_path
,
1
);
// Store all detected result
std
::
vector
<
PaddleDetection
::
ObjectResult
>
result
;
det
->
Predict
(
im
,
&
result
);
for
(
const
auto
&
item
:
result
)
{
printf
(
"class=%d confidence=%.4f rect=[%d %d %d %d]
\n
"
,
item
.
class_id
,
item
.
confidence
,
item
.
rect
[
0
],
item
.
rect
[
1
],
item
.
rect
[
2
],
item
.
rect
[
3
]);
if
(
run_benchmark
)
{
det
->
Predict
(
im
,
threshold
,
100
,
100
,
run_benchmark
,
&
result
);
}
else
{
det
->
Predict
(
im
,
0.5
,
0
,
1
,
run_benchmark
,
&
result
);
for
(
const
auto
&
item
:
result
)
{
printf
(
"class=%d confidence=%.4f rect=[%d %d %d %d]
\n
"
,
item
.
class_id
,
item
.
confidence
,
item
.
rect
[
0
],
item
.
rect
[
1
],
item
.
rect
[
2
],
item
.
rect
[
3
]);
}
// Visualization result
auto
labels
=
det
->
GetLabelList
();
auto
colormap
=
PaddleDetection
::
GenerateColorMap
(
labels
.
size
());
cv
::
Mat
vis_img
=
PaddleDetection
::
VisualizeResult
(
im
,
result
,
labels
,
colormap
);
std
::
vector
<
int
>
compression_params
;
compression_params
.
push_back
(
CV_IMWRITE_JPEG_QUALITY
);
compression_params
.
push_back
(
95
);
cv
::
imwrite
(
output_dir
+
"/output.jpg"
,
vis_img
,
compression_params
);
printf
(
"Visualized output saved as output.jpg
\n
"
);
}
// Visualization result
auto
labels
=
det
->
GetLabelList
();
auto
colormap
=
PaddleDetection
::
GenerateColorMap
(
labels
.
size
());
cv
::
Mat
vis_img
=
PaddleDetection
::
VisualizeResult
(
im
,
result
,
labels
,
colormap
);
std
::
vector
<
int
>
compression_params
;
compression_params
.
push_back
(
CV_IMWRITE_JPEG_QUALITY
);
compression_params
.
push_back
(
95
);
cv
::
imwrite
(
"output.jpg"
,
vis_img
,
compression_params
);
printf
(
"Visualized output saved as output.jpg
\n
"
);
}
int
main
(
int
argc
,
char
**
argv
)
{
...
...
@@ -139,10 +151,10 @@ int main(int argc, char** argv) {
PaddleDetection
::
ObjectDetector
det
(
FLAGS_model_dir
,
FLAGS_use_gpu
,
FLAGS_run_mode
,
FLAGS_gpu_id
);
// Do inference on input video or image
if
(
!
FLAGS_video_path
.
empty
()
or
FLAGS_use_camera
)
{
if
(
!
FLAGS_video_path
.
empty
()
||
FLAGS_use_camera
)
{
PredictVideo
(
FLAGS_video_path
,
&
det
);
}
else
if
(
!
FLAGS_image_path
.
empty
())
{
PredictImage
(
FLAGS_image_path
,
&
det
);
PredictImage
(
FLAGS_image_path
,
FLAGS_threshold
,
FLAGS_run_benchmark
,
&
det
,
FLAGS_output_dir
);
}
return
0
;
}
deploy/cpp/src/object_detector.cc
浏览文件 @
d7d3fbea
...
...
@@ -39,7 +39,7 @@ void ObjectDetector::LoadModel(const std::string& model_dir,
printf
(
"TensorRT int8 mode is not supported now, "
"please use 'trt_fp32' or 'trt_fp16' instead"
);
}
else
{
if
(
run_mode
!=
"trt_32"
)
{
if
(
run_mode
!=
"trt_
fp
32"
)
{
printf
(
"run_mode should be 'fluid', 'trt_fp32' or 'trt_fp16'"
);
}
}
...
...
@@ -56,6 +56,7 @@ void ObjectDetector::LoadModel(const std::string& model_dir,
}
config
.
SwitchUseFeedFetchOps
(
false
);
config
.
SwitchSpecifyInputNames
(
true
);
config
.
DisableGlogInfo
();
// Memory optimization
config
.
EnableMemoryOptim
();
predictor_
=
std
::
move
(
CreatePaddlePredictor
(
config
));
...
...
@@ -155,7 +156,11 @@ void ObjectDetector::Postprocess(
}
void
ObjectDetector
::
Predict
(
const
cv
::
Mat
&
im
,
std
::
vector
<
ObjectResult
>*
result
)
{
const
double
threshold
,
const
int
warmup
,
const
int
repeats
,
const
bool
run_benchmark
,
std
::
vector
<
ObjectResult
>*
result
)
{
// Preprocess image
Preprocess
(
im
);
// Prepare input tensor
...
...
@@ -182,24 +187,53 @@ void ObjectDetector::Predict(const cv::Mat& im,
}
}
// Run predictor
predictor_
->
ZeroCopyRun
();
// Get output tensor
auto
output_names
=
predictor_
->
GetOutputNames
();
auto
out_tensor
=
predictor_
->
GetOutputTensor
(
output_names
[
0
]);
std
::
vector
<
int
>
output_shape
=
out_tensor
->
shape
();
// Calculate output length
int
output_size
=
1
;
for
(
int
j
=
0
;
j
<
output_shape
.
size
();
++
j
)
{
output_size
*=
output_shape
[
j
];
for
(
int
i
=
0
;
i
<
warmup
;
i
++
)
{
predictor_
->
ZeroCopyRun
();
// Get output tensor
auto
output_names
=
predictor_
->
GetOutputNames
();
auto
out_tensor
=
predictor_
->
GetOutputTensor
(
output_names
[
0
]);
std
::
vector
<
int
>
output_shape
=
out_tensor
->
shape
();
// Calculate output length
int
output_size
=
1
;
for
(
int
j
=
0
;
j
<
output_shape
.
size
();
++
j
)
{
output_size
*=
output_shape
[
j
];
}
if
(
output_size
<
6
)
{
std
::
cerr
<<
"[WARNING] No object detected."
<<
std
::
endl
;
}
output_data_
.
resize
(
output_size
);
out_tensor
->
copy_to_cpu
(
output_data_
.
data
());
}
if
(
output_size
<
6
)
{
std
::
cerr
<<
"[WARNING] No object detected."
<<
std
::
endl
;
std
::
clock_t
start
=
clock
();
for
(
int
i
=
0
;
i
<
repeats
;
i
++
)
{
predictor_
->
ZeroCopyRun
();
// Get output tensor
auto
output_names
=
predictor_
->
GetOutputNames
();
auto
out_tensor
=
predictor_
->
GetOutputTensor
(
output_names
[
0
]);
std
::
vector
<
int
>
output_shape
=
out_tensor
->
shape
();
// Calculate output length
int
output_size
=
1
;
for
(
int
j
=
0
;
j
<
output_shape
.
size
();
++
j
)
{
output_size
*=
output_shape
[
j
];
}
if
(
output_size
<
6
)
{
std
::
cerr
<<
"[WARNING] No object detected."
<<
std
::
endl
;
}
output_data_
.
resize
(
output_size
);
out_tensor
->
copy_to_cpu
(
output_data_
.
data
());
}
output_data_
.
resize
(
output_size
);
out_tensor
->
copy_to_cpu
(
output_data_
.
data
());
std
::
clock_t
end
=
clock
();
float
ms
=
static_cast
<
float
>
(
end
-
start
)
/
CLOCKS_PER_SEC
/
repeats
*
1000.
;
printf
(
"Inference: %f ms per batch image
\n
"
,
ms
);
// Postprocessing result
Postprocess
(
im
,
result
);
if
(
!
run_benchmark
)
{
Postprocess
(
im
,
result
);
}
}
std
::
vector
<
int
>
GenerateColorMap
(
int
num_class
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录