Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
504f32c5
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
大约 1 年 前同步成功
通知
115
Star
4999
Fork
1114
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
6
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleClas
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
6
合并请求
6
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
504f32c5
编写于
12月 15, 2021
作者:
W
Walter
提交者:
GitHub
12月 15, 2021
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1557 from RainFrost1/cpp_infer
分类Cpp添加打印label及topk功能
上级
3f215d87
e1716ded
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
109 addition
and
45 deletion
+109
-45
deploy/cpp/docs/imgs/cpp_infer_result.png
deploy/cpp/docs/imgs/cpp_infer_result.png
+0
-0
deploy/cpp/include/cls.h
deploy/cpp/include/cls.h
+2
-1
deploy/cpp/include/cls_config.h
deploy/cpp/include/cls_config.h
+20
-0
deploy/cpp/readme.md
deploy/cpp/readme.md
+1
-1
deploy/cpp/src/cls.cpp
deploy/cpp/src/cls.cpp
+18
-14
deploy/cpp/src/cls_config.cpp
deploy/cpp/src/cls_config.cpp
+17
-0
deploy/cpp/src/main.cpp
deploy/cpp/src/main.cpp
+35
-13
deploy/cpp/src/utility.cpp
deploy/cpp/src/utility.cpp
+14
-14
deploy/cpp/tools/build.sh
deploy/cpp/tools/build.sh
+2
-2
未找到文件。
deploy/cpp/docs/imgs/cpp_infer_result.png
查看替换文件 @
3f215d87
浏览文件 @
504f32c5
85.1 KB
|
W:
|
H:
73.4 KB
|
W:
|
H:
2-up
Swipe
Onion skin
deploy/cpp/include/cls.h
浏览文件 @
504f32c5
...
...
@@ -58,7 +58,8 @@ namespace PaddleClas {
void
LoadModel
(
const
std
::
string
&
model_path
,
const
std
::
string
&
params_path
);
// Run predictor
double
Run
(
cv
::
Mat
&
img
,
std
::
vector
<
double
>
*
times
);
void
Run
(
cv
::
Mat
&
img
,
std
::
vector
<
float
>
&
out_data
,
std
::
vector
<
int
>
&
idx
,
std
::
vector
<
double
>
&
times
);
private:
std
::
shared_ptr
<
Predictor
>
predictor_
;
...
...
deploy/cpp/include/cls_config.h
浏览文件 @
504f32c5
...
...
@@ -80,6 +80,20 @@ namespace PaddleClas {
this
->
benchmark
=
this
->
config_file
[
"Global"
][
"benchmark"
].
as
<
bool
>
();
else
this
->
benchmark
=
false
;
if
(
this
->
config_file
[
"PostProcess"
][
"Topk"
][
"topk"
].
IsDefined
())
this
->
topk
=
this
->
config_file
[
"PostProcess"
][
"Topk"
][
"topk"
].
as
<
int
>
();
if
(
this
->
config_file
[
"PostProcess"
][
"Topk"
][
"class_id_map_file"
]
.
IsDefined
())
this
->
class_id_map_path
=
this
->
config_file
[
"PostProcess"
][
"Topk"
][
"class_id_map_file"
]
.
as
<
std
::
string
>
();
if
(
this
->
config_file
[
"PostProcess"
][
"SavePreLabel"
][
"save_dir"
]
.
IsDefined
())
this
->
label_save_dir
=
this
->
config_file
[
"PostProcess"
][
"SavePreLabel"
][
"save_dir"
]
.
as
<
std
::
string
>
();
ReadLabelMap
();
}
YAML
::
Node
config_file
;
...
...
@@ -105,9 +119,15 @@ namespace PaddleClas {
float
scale
=
0.00392157
;
std
::
vector
<
float
>
mean
=
{
0.485
,
0.456
,
0.406
};
std
::
vector
<
float
>
std
=
{
0.229
,
0.224
,
0.225
};
int
topk
=
5
;
std
::
string
class_id_map_path
;
std
::
map
<
int
,
std
::
string
>
id_map
;
std
::
string
label_save_dir
;
void
PrintConfigInfo
();
void
ReadLabelMap
();
void
ReadYamlConfig
(
const
std
::
string
&
path
);
};
}
// namespace PaddleClas
deploy/cpp/readme.md
浏览文件 @
504f32c5
...
...
@@ -209,7 +209,7 @@ cp ../configs/inference_cls.yaml tools/
根据
[
python预测推理
](
../../docs/zh_CN/inference_deployment/python_deploy.md
)
的
`图像分类推理`
部分修改好
`tools`
目录下
`inference_cls.yaml`
文件。
`yaml`
文件的参数说明详见
[
python预测推理
](
../../docs/zh_CN/inference_deployment/python_deploy.md
)
。
请根据实际存放文件,修改好
`Global.infer_imgs`
、
`Global.inference_model_dir`
等参数。
请根据实际存放文件,修改好
`Global.infer_imgs`
、
`Global.inference_model_dir`
、
`PostProcess.Topk.topk`
、
`PostProcess.Topk.class_id_map_file`
等参数。
#### 2.3.2 执行
...
...
deploy/cpp/src/cls.cpp
浏览文件 @
504f32c5
...
...
@@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <algorithm>
#include <include/cls.h>
#include <numeric>
namespace
PaddleClas
{
...
...
@@ -52,7 +54,8 @@ namespace PaddleClas {
this
->
predictor_
=
CreatePredictor
(
config
);
}
double
Classifier
::
Run
(
cv
::
Mat
&
img
,
std
::
vector
<
double
>
*
times
)
{
void
Classifier
::
Run
(
cv
::
Mat
&
img
,
std
::
vector
<
float
>
&
out_data
,
std
::
vector
<
int
>
&
idx
,
std
::
vector
<
double
>
&
times
)
{
cv
::
Mat
srcimg
;
cv
::
Mat
resize_img
;
img
.
copyTo
(
srcimg
);
...
...
@@ -75,7 +78,6 @@ namespace PaddleClas {
input_t
->
CopyFromCpu
(
input
.
data
());
this
->
predictor_
->
Run
();
std
::
vector
<
float
>
out_data
;
auto
output_names
=
this
->
predictor_
->
GetOutputNames
();
auto
output_t
=
this
->
predictor_
->
GetOutputHandle
(
output_names
[
0
]);
std
::
vector
<
int
>
output_shape
=
output_t
->
shape
();
...
...
@@ -83,30 +85,32 @@ namespace PaddleClas {
std
::
multiplies
<
int
>
());
out_data
.
resize
(
out_num
);
idx
.
resize
(
out_num
);
output_t
->
CopyToCpu
(
out_data
.
data
());
auto
infer_end
=
std
::
chrono
::
system_clock
::
now
();
auto
postprocess_start
=
std
::
chrono
::
system_clock
::
now
();
int
maxPosition
=
max_element
(
out_data
.
begin
(),
out_data
.
end
())
-
out_data
.
begin
();
// int maxPosition =
// max_element(out_data.begin(), out_data.end()) - out_data.begin();
iota
(
idx
.
begin
(),
idx
.
end
(),
0
);
stable_sort
(
idx
.
begin
(),
idx
.
end
(),
[
&
out_data
](
int
i1
,
int
i2
)
{
return
out_data
[
i1
]
>
out_data
[
i2
];
});
auto
postprocess_end
=
std
::
chrono
::
system_clock
::
now
();
std
::
chrono
::
duration
<
float
>
preprocess_diff
=
preprocess_end
-
preprocess_start
;
times
->
push_back
(
double
(
preprocess_diff
.
count
()
*
1000
)
);
times
[
0
]
=
double
(
preprocess_diff
.
count
()
*
1000
);
std
::
chrono
::
duration
<
float
>
inference_diff
=
infer_end
-
infer_start
;
double
inference_cost_time
=
double
(
inference_diff
.
count
()
*
1000
);
times
->
push_back
(
inference_cost_time
)
;
times
[
1
]
=
inference_cost_time
;
std
::
chrono
::
duration
<
float
>
postprocess_diff
=
postprocess_end
-
postprocess_start
;
times
->
push_back
(
double
(
postprocess_diff
.
count
()
*
1000
)
);
times
[
2
]
=
double
(
postprocess_diff
.
count
()
*
1000
);
std
::
cout
<<
"result: "
<<
std
::
endl
;
std
::
cout
<<
"
\t
class id: "
<<
maxPosition
<<
std
::
endl
;
std
::
cout
<<
std
::
fixed
<<
std
::
setprecision
(
10
)
<<
"
\t
score: "
<<
double
(
out_data
[
maxPosition
])
<<
std
::
endl
;
return
inference_cost_time
;
/* std::cout << "result: " << std::endl; */
/* std::cout << "\tclass id: " << maxPosition << std::endl; */
/* std::cout << std::fixed << std::setprecision(10) */
/* << "\tscore: " << double(out_data[maxPosition]) << std::endl; */
}
}
// namespace PaddleClas
deploy/cpp/src/cls_config.cpp
浏览文件 @
504f32c5
...
...
@@ -13,6 +13,7 @@
// limitations under the License.
#include <include/cls_config.h>
#include <ostream>
namespace
PaddleClas
{
...
...
@@ -32,4 +33,20 @@ namespace PaddleClas {
exit
(
1
);
}
}
void
ClsConfig
::
ReadLabelMap
()
{
if
(
this
->
class_id_map_path
.
empty
())
{
std
::
cout
<<
"The Class Label file dose not input"
<<
std
::
endl
;
return
;
}
std
::
ifstream
in
(
this
->
class_id_map_path
);
std
::
string
line
;
if
(
in
)
{
while
(
getline
(
in
,
line
))
{
int
split_flag
=
line
.
find_first_of
(
" "
);
this
->
id_map
[
std
::
stoi
(
line
.
substr
(
0
,
split_flag
))]
=
line
.
substr
(
split_flag
+
1
,
line
.
size
());
}
}
}
};
// namespace PaddleClas
deploy/cpp/src/main.cpp
浏览文件 @
504f32c5
...
...
@@ -22,6 +22,7 @@
#include <ostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <fstream>
#include <numeric>
...
...
@@ -74,9 +75,13 @@ int main(int argc, char **argv) {
Classifier
classifier
(
config
);
double
elapsed_time
=
0.0
;
std
::
vector
<
double
>
cls_times
;
int
warmup_iter
=
img_files_list
.
size
()
>
5
?
5
:
0
;
std
::
vector
<
double
>
cls_times
=
{
0
,
0
,
0
};
std
::
vector
<
double
>
cls_times_total
=
{
0
,
0
,
0
};
double
infer_time
;
std
::
vector
<
float
>
out_data
;
std
::
vector
<
int
>
result_index
;
int
warmup_iter
=
5
;
bool
label_output_equal_flag
=
true
;
for
(
int
idx
=
0
;
idx
<
img_files_list
.
size
();
++
idx
)
{
std
::
string
img_path
=
img_files_list
[
idx
];
cv
::
Mat
srcimg
=
cv
::
imread
(
img_path
,
cv
::
IMREAD_COLOR
);
...
...
@@ -87,27 +92,44 @@ int main(int argc, char **argv) {
}
cv
::
cvtColor
(
srcimg
,
srcimg
,
cv
::
COLOR_BGR2RGB
);
classifier
.
Run
(
srcimg
,
out_data
,
result_index
,
cls_times
);
if
(
label_output_equal_flag
and
out_data
.
size
()
!=
config
.
id_map
.
size
())
{
std
::
cout
<<
"Warning: the label size is not equal to output size!"
<<
std
::
endl
;
label_output_equal_flag
=
false
;
}
double
run_time
=
classifier
.
Run
(
srcimg
,
&
cls_times
);
int
max_len
=
std
::
min
(
config
.
topk
,
int
(
out_data
.
size
()));
std
::
cout
<<
"Current image path: "
<<
img_path
<<
std
::
endl
;
infer_time
=
cls_times
[
0
]
+
cls_times
[
1
]
+
cls_times
[
2
];
std
::
cout
<<
"Current total inferen time cost: "
<<
infer_time
<<
" ms."
<<
std
::
endl
;
for
(
int
i
=
0
;
i
<
max_len
;
++
i
)
{
printf
(
"
\t
Top%d: class_id: %d, score: %.4f, "
,
i
+
1
,
result_index
[
i
],
out_data
[
result_index
[
i
]]);
if
(
label_output_equal_flag
)
printf
(
"label: %s
\n
"
,
config
.
id_map
[
result_index
[
i
]].
c_str
());
}
if
(
idx
>=
warmup_iter
)
{
elapsed_time
+=
run_time
;
std
::
cout
<<
"Current image path: "
<<
img_path
<<
std
::
endl
;
std
::
cout
<<
"Current time cost: "
<<
run_time
<<
" s, "
<<
"average time cost in all: "
<<
elapsed_time
/
(
idx
+
1
-
warmup_iter
)
<<
" s."
<<
std
::
endl
;
}
else
{
std
::
cout
<<
"Current time cost: "
<<
run_time
<<
" s."
<<
std
::
endl
;
for
(
int
i
=
0
;
i
<
cls_times
.
size
();
++
i
)
cls_times_total
[
i
]
+=
cls_times
[
i
];
}
}
if
(
img_files_list
.
size
()
>
warmup_iter
)
{
std
::
string
presion
=
"fp32"
;
infer_time
=
cls_times_total
[
0
]
+
cls_times_total
[
1
]
+
cls_times_total
[
2
];
std
::
cout
<<
"average time cost in all: "
<<
infer_time
/
(
img_files_list
.
size
()
-
warmup_iter
)
<<
" ms."
<<
std
::
endl
;
}
std
::
string
presion
=
"fp32"
;
if
(
config
.
use_fp16
)
presion
=
"fp16"
;
if
(
config
.
benchmark
)
{
AutoLogger
autolog
(
"Classification"
,
config
.
use_gpu
,
config
.
use_tensorrt
,
config
.
use_mkldnn
,
config
.
cpu_threads
,
1
,
"1, 3, 224, 224"
,
presion
,
cls_times
,
"1, 3, 224, 224"
,
presion
,
cls_times
_total
,
img_files_list
.
size
());
autolog
.
report
();
}
...
...
deploy/cpp/src/utility.cpp
浏览文件 @
504f32c5
...
...
@@ -20,20 +20,20 @@
namespace
PaddleClas
{
std
::
vector
<
std
::
string
>
Utility
::
ReadDict
(
const
std
::
string
&
path
)
{
std
::
ifstream
in
(
path
);
std
::
string
line
;
std
::
vector
<
std
::
string
>
m_vec
;
if
(
in
)
{
while
(
getline
(
in
,
line
))
{
m_vec
.
push_back
(
line
);
std
::
vector
<
std
::
string
>
Utility
::
ReadDict
(
const
std
::
string
&
path
)
{
std
::
ifstream
in
(
path
);
std
::
string
line
;
std
::
vector
<
std
::
string
>
m_vec
;
if
(
in
)
{
while
(
getline
(
in
,
line
))
{
m_vec
.
push_back
(
line
);
}
}
else
{
std
::
cout
<<
"no such label file: "
<<
path
<<
", exit the program..."
<<
std
::
endl
;
exit
(
1
);
}
return
m_vec
;
}
}
else
{
std
::
cout
<<
"no such label file: "
<<
path
<<
", exit the program..."
<<
std
::
endl
;
exit
(
1
);
}
return
m_vec
;
}
}
// namespace PaddleClas
\ No newline at end of file
deploy/cpp/tools/build.sh
浏览文件 @
504f32c5
OPENCV_DIR
=
/work/project/project/
cpp_infer
/opencv-3.4.7/opencv3
LIB_DIR
=
/work/project/project/
cpp_infer
/paddle_inference/
OPENCV_DIR
=
/work/project/project/
test
/opencv-3.4.7/opencv3
LIB_DIR
=
/work/project/project/
test
/paddle_inference/
CUDA_LIB_DIR
=
/usr/local/cuda/lib64
CUDNN_LIB_DIR
=
/usr/lib/x86_64-linux-gnu/
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录