Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
944ffc57
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
接近 2 年 前同步成功
通知
116
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看板
提交
944ffc57
编写于
11月 29, 2021
作者:
D
dongshuilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
code format for cls cpp infer
上级
4dbd85b8
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
380 addition
and
397 deletion
+380
-397
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
+43
-43
deploy/cpp/include/cls_config.h
deploy/cpp/include/cls_config.h
+76
-74
deploy/cpp/include/preprocess_op.h
deploy/cpp/include/preprocess_op.h
+19
-19
deploy/cpp/include/utility.h
deploy/cpp/include/utility.h
+10
-10
deploy/cpp/readme.md
deploy/cpp/readme.md
+18
-22
deploy/cpp/src/cls.cpp
deploy/cpp/src/cls.cpp
+90
-90
deploy/cpp/src/cls_config.cpp
deploy/cpp/src/cls_config.cpp
+14
-14
deploy/cpp/src/main.cpp
deploy/cpp/src/main.cpp
+64
-62
deploy/cpp/src/preprocess_op.cpp
deploy/cpp/src/preprocess_op.cpp
+45
-45
deploy/cpp/tools/build.sh
deploy/cpp/tools/build.sh
+1
-0
deploy/cpp/tools/config.txt
deploy/cpp/tools/config.txt
+0
-17
deploy/cpp/tools/run.sh
deploy/cpp/tools/run.sh
+0
-1
未找到文件。
deploy/cpp/docs/imgs/cpp_infer_result.png
查看替换文件 @
4dbd85b8
浏览文件 @
944ffc57
41.8 KB
|
W:
|
H:
85.1 KB
|
W:
|
H:
2-up
Swipe
Onion skin
deploy/cpp/include/cls.h
浏览文件 @
944ffc57
...
@@ -35,55 +35,55 @@ using namespace paddle_infer;
...
@@ -35,55 +35,55 @@ using namespace paddle_infer;
namespace
PaddleClas
{
namespace
PaddleClas
{
class
Classifier
{
class
Classifier
{
public:
public:
explicit
Classifier
(
const
ClsConfig
&
config
)
{
explicit
Classifier
(
const
ClsConfig
&
config
)
{
this
->
use_gpu_
=
config
.
use_gpu
;
this
->
use_gpu_
=
config
.
use_gpu
;
this
->
gpu_id_
=
config
.
gpu_id
;
this
->
gpu_id_
=
config
.
gpu_id
;
this
->
gpu_mem_
=
config
.
gpu_mem
;
this
->
gpu_mem_
=
config
.
gpu_mem
;
this
->
cpu_math_library_num_threads_
=
config
.
cpu_threads
;
this
->
cpu_math_library_num_threads_
=
config
.
cpu_threads
;
this
->
use_fp16_
=
config
.
use_fp16
;
this
->
use_fp16_
=
config
.
use_fp16
;
this
->
use_mkldnn_
=
config
.
use_mkldnn
;
this
->
use_mkldnn_
=
config
.
use_mkldnn
;
this
->
use_tensorrt_
=
config
.
use_tensorrt
;
this
->
use_tensorrt_
=
config
.
use_tensorrt
;
this
->
mean_
=
config
.
mean
;
this
->
mean_
=
config
.
mean
;
this
->
std_
=
config
.
std
;
this
->
std_
=
config
.
std
;
this
->
resize_short_size_
=
config
.
resize_short_size
;
this
->
resize_short_size_
=
config
.
resize_short_size
;
this
->
scale_
=
config
.
scale
;
this
->
scale_
=
config
.
scale
;
this
->
crop_size_
=
config
.
crop_size
;
this
->
crop_size_
=
config
.
crop_size
;
this
->
ir_optim_
=
config
.
ir_optim
;
this
->
ir_optim_
=
config
.
ir_optim
;
LoadModel
(
config
.
cls_model_path
,
config
.
cls_params_path
);
LoadModel
(
config
.
cls_model_path
,
config
.
cls_params_path
);
}
}
// Load Paddle inference model
// Load Paddle inference model
void
LoadModel
(
const
std
::
string
&
model_path
,
const
std
::
string
&
params_path
);
void
LoadModel
(
const
std
::
string
&
model_path
,
const
std
::
string
&
params_path
);
// Run predictor
// Run predictor
double
Run
(
cv
::
Mat
&
img
,
std
::
vector
<
double
>
*
times
);
double
Run
(
cv
::
Mat
&
img
,
std
::
vector
<
double
>
*
times
);
private:
private:
std
::
shared_ptr
<
Predictor
>
predictor_
;
std
::
shared_ptr
<
Predictor
>
predictor_
;
bool
use_gpu_
=
false
;
bool
use_gpu_
=
false
;
int
gpu_id_
=
0
;
int
gpu_id_
=
0
;
int
gpu_mem_
=
4000
;
int
gpu_mem_
=
4000
;
int
cpu_math_library_num_threads_
=
4
;
int
cpu_math_library_num_threads_
=
4
;
bool
use_mkldnn_
=
false
;
bool
use_mkldnn_
=
false
;
bool
use_tensorrt_
=
false
;
bool
use_tensorrt_
=
false
;
bool
use_fp16_
=
false
;
bool
use_fp16_
=
false
;
bool
ir_optim_
=
true
;
bool
ir_optim_
=
true
;
std
::
vector
<
float
>
mean_
=
{
0.485
f
,
0.456
f
,
0.406
f
};
std
::
vector
<
float
>
mean_
=
{
0.485
f
,
0.456
f
,
0.406
f
};
std
::
vector
<
float
>
std_
=
{
0.229
f
,
0.224
f
,
0.225
f
};
std
::
vector
<
float
>
std_
=
{
0.229
f
,
0.224
f
,
0.225
f
};
float
scale_
=
0.00392157
;
float
scale_
=
0.00392157
;
int
resize_short_size_
=
256
;
int
resize_short_size_
=
256
;
int
crop_size_
=
224
;
int
crop_size_
=
224
;
// pre-process
// pre-process
ResizeImg
resize_op_
;
ResizeImg
resize_op_
;
Normalize
normalize_op_
;
Normalize
normalize_op_
;
Permute
permute_op_
;
Permute
permute_op_
;
CenterCropImg
crop_op_
;
CenterCropImg
crop_op_
;
};
};
}
// namespace PaddleClas
}
// namespace PaddleClas
deploy/cpp/include/cls_config.h
浏览文件 @
944ffc57
...
@@ -31,81 +31,83 @@
...
@@ -31,81 +31,83 @@
namespace
PaddleClas
{
namespace
PaddleClas
{
class
ClsConfig
{
class
ClsConfig
{
public:
public:
explicit
ClsConfig
(
const
std
::
string
&
path
)
{
explicit
ClsConfig
(
const
std
::
string
&
path
)
{
ReadYamlConfig
(
path
);
ReadYamlConfig
(
path
);
this
->
infer_imgs
=
this
->
infer_imgs
=
this
->
config_file
[
"Global"
][
"infer_imgs"
].
as
<
std
::
string
>
();
this
->
config_file
[
"Global"
][
"infer_imgs"
].
as
<
std
::
string
>
();
this
->
batch_size
=
this
->
config_file
[
"Global"
][
"batch_size"
].
as
<
int
>
();
this
->
batch_size
=
this
->
config_file
[
"Global"
][
"batch_size"
].
as
<
int
>
();
this
->
use_gpu
=
this
->
config_file
[
"Global"
][
"use_gpu"
].
as
<
bool
>
();
this
->
use_gpu
=
this
->
config_file
[
"Global"
][
"use_gpu"
].
as
<
bool
>
();
if
(
this
->
config_file
[
"Global"
][
"gpu_id"
].
IsDefined
())
if
(
this
->
config_file
[
"Global"
][
"gpu_id"
].
IsDefined
())
this
->
gpu_id
=
this
->
config_file
[
"Global"
][
"gpu_id"
].
as
<
int
>
();
this
->
gpu_id
=
this
->
config_file
[
"Global"
][
"gpu_id"
].
as
<
int
>
();
else
else
this
->
gpu_id
=
0
;
this
->
gpu_id
=
0
;
this
->
gpu_mem
=
this
->
config_file
[
"Global"
][
"gpu_mem"
].
as
<
int
>
();
this
->
gpu_mem
=
this
->
config_file
[
"Global"
][
"gpu_mem"
].
as
<
int
>
();
this
->
cpu_threads
=
this
->
cpu_threads
=
this
->
config_file
[
"Global"
][
"cpu_num_threads"
].
as
<
int
>
();
this
->
config_file
[
"Global"
][
"cpu_num_threads"
].
as
<
int
>
();
this
->
use_mkldnn
=
this
->
config_file
[
"Global"
][
"enable_mkldnn"
].
as
<
bool
>
();
this
->
use_mkldnn
=
this
->
config_file
[
"Global"
][
"enable_mkldnn"
].
as
<
bool
>
();
this
->
use_tensorrt
=
this
->
config_file
[
"Global"
][
"use_tensorrt"
].
as
<
bool
>
();
this
->
use_tensorrt
=
this
->
config_file
[
"Global"
][
"use_tensorrt"
].
as
<
bool
>
();
this
->
use_fp16
=
this
->
config_file
[
"Global"
][
"use_fp16"
].
as
<
bool
>
();
this
->
use_fp16
=
this
->
config_file
[
"Global"
][
"use_fp16"
].
as
<
bool
>
();
this
->
enable_benchmark
=
this
->
enable_benchmark
=
this
->
config_file
[
"Global"
][
"enable_benchmark"
].
as
<
bool
>
();
this
->
config_file
[
"Global"
][
"enable_benchmark"
].
as
<
bool
>
();
this
->
ir_optim
=
this
->
config_file
[
"Global"
][
"ir_optim"
].
as
<
bool
>
();
this
->
ir_optim
=
this
->
config_file
[
"Global"
][
"ir_optim"
].
as
<
bool
>
();
this
->
enable_profile
=
this
->
enable_profile
=
this
->
config_file
[
"Global"
][
"enable_profile"
].
as
<
bool
>
();
this
->
config_file
[
"Global"
][
"enable_profile"
].
as
<
bool
>
();
this
->
cls_model_path
=
this
->
cls_model_path
=
this
->
config_file
[
"Global"
][
"inference_model_dir"
].
as
<
std
::
string
>
()
+
this
->
config_file
[
"Global"
][
"inference_model_dir"
].
as
<
std
::
string
>
()
+
OS_PATH_SEP
+
"inference.pdmodel"
;
OS_PATH_SEP
+
"inference.pdmodel"
;
this
->
cls_params_path
=
this
->
cls_params_path
=
this
->
config_file
[
"Global"
][
"inference_model_dir"
].
as
<
std
::
string
>
()
+
this
->
config_file
[
"Global"
][
"inference_model_dir"
].
as
<
std
::
string
>
()
+
OS_PATH_SEP
+
"inference.pdiparams"
;
OS_PATH_SEP
+
"inference.pdiparams"
;
this
->
resize_short_size
=
this
->
resize_short_size
=
this
->
config_file
[
"PreProcess"
][
"transform_ops"
][
0
][
"ResizeImage"
]
this
->
config_file
[
"PreProcess"
][
"transform_ops"
][
0
][
"ResizeImage"
]
[
"resize_short"
]
[
"resize_short"
]
.
as
<
int
>
();
.
as
<
int
>
();
this
->
crop_size
=
this
->
crop_size
=
this
->
config_file
[
"PreProcess"
][
"transform_ops"
][
1
][
"CropImage"
][
"size"
]
this
->
config_file
[
"PreProcess"
][
"transform_ops"
][
1
][
"CropImage"
][
"size"
]
.
as
<
int
>
();
.
as
<
int
>
();
this
->
scale
=
this
->
config_file
[
"PreProcess"
][
"transform_ops"
][
2
]
this
->
scale
=
this
->
config_file
[
"PreProcess"
][
"transform_ops"
][
2
]
[
"NormalizeImage"
][
"scale"
]
[
"NormalizeImage"
][
"scale"
]
.
as
<
float
>
();
.
as
<
float
>
();
this
->
mean
=
this
->
config_file
[
"PreProcess"
][
"transform_ops"
][
2
]
this
->
mean
=
this
->
config_file
[
"PreProcess"
][
"transform_ops"
][
2
]
[
"NormalizeImage"
][
"mean"
]
[
"NormalizeImage"
][
"mean"
]
.
as
<
std
::
vector
<
float
>>
();
.
as
<
std
::
vector
<
float
>>
();
this
->
std
=
this
->
config_file
[
"PreProcess"
][
"transform_ops"
][
2
]
this
->
std
=
this
->
config_file
[
"PreProcess"
][
"transform_ops"
][
2
]
[
"NormalizeImage"
][
"std"
]
[
"NormalizeImage"
][
"std"
]
.
as
<
std
::
vector
<
float
>>
();
.
as
<
std
::
vector
<
float
>>
();
if
(
this
->
config_file
[
"Global"
][
"benchmark"
].
IsDefined
())
if
(
this
->
config_file
[
"Global"
][
"benchmark"
].
IsDefined
())
this
->
benchmark
=
this
->
config_file
[
"Global"
][
"benchmark"
].
as
<
bool
>
();
this
->
benchmark
=
this
->
config_file
[
"Global"
][
"benchmark"
].
as
<
bool
>
();
else
else
this
->
benchmark
=
false
;
this
->
benchmark
=
false
;
}
}
YAML
::
Node
config_file
;
YAML
::
Node
config_file
;
bool
use_gpu
=
false
;
bool
use_gpu
=
false
;
int
gpu_id
=
0
;
int
gpu_id
=
0
;
int
gpu_mem
=
4000
;
int
gpu_mem
=
4000
;
int
cpu_threads
=
1
;
int
cpu_threads
=
1
;
bool
use_mkldnn
=
false
;
bool
use_mkldnn
=
false
;
bool
use_tensorrt
=
false
;
bool
use_tensorrt
=
false
;
bool
use_fp16
=
false
;
bool
use_fp16
=
false
;
bool
benchmark
=
false
;
bool
benchmark
=
false
;
int
batch_size
=
1
;
int
batch_size
=
1
;
bool
enable_benchmark
=
false
;
bool
enable_benchmark
=
false
;
bool
ir_optim
=
true
;
bool
ir_optim
=
true
;
bool
enable_profile
=
false
;
bool
enable_profile
=
false
;
std
::
string
cls_model_path
;
std
::
string
cls_model_path
;
std
::
string
cls_params_path
;
std
::
string
cls_params_path
;
std
::
string
infer_imgs
;
std
::
string
infer_imgs
;
int
resize_short_size
=
256
;
int
resize_short_size
=
256
;
int
crop_size
=
224
;
int
crop_size
=
224
;
float
scale
=
0.00392157
;
float
scale
=
0.00392157
;
std
::
vector
<
float
>
mean
=
{
0.485
,
0.456
,
0.406
};
std
::
vector
<
float
>
mean
=
{
0.485
,
0.456
,
0.406
};
std
::
vector
<
float
>
std
=
{
0.229
,
0.224
,
0.225
};
std
::
vector
<
float
>
std
=
{
0.229
,
0.224
,
0.225
};
void
PrintConfigInfo
();
void
ReadYamlConfig
(
const
std
::
string
&
path
);
void
PrintConfigInfo
();
};
void
ReadYamlConfig
(
const
std
::
string
&
path
);
};
}
// namespace PaddleClas
}
// namespace PaddleClas
deploy/cpp/include/preprocess_op.h
浏览文件 @
944ffc57
...
@@ -31,26 +31,26 @@ using namespace std;
...
@@ -31,26 +31,26 @@ using namespace std;
namespace
PaddleClas
{
namespace
PaddleClas
{
class
Normalize
{
class
Normalize
{
public:
public:
virtual
void
Run
(
cv
::
Mat
*
im
,
const
std
::
vector
<
float
>
&
mean
,
virtual
void
Run
(
cv
::
Mat
*
im
,
const
std
::
vector
<
float
>
&
mean
,
const
std
::
vector
<
float
>
&
std
,
float
&
scale
);
const
std
::
vector
<
float
>
&
std
,
float
&
scale
);
};
};
// RGB -> CHW
// RGB -> CHW
class
Permute
{
class
Permute
{
public:
public:
virtual
void
Run
(
const
cv
::
Mat
*
im
,
float
*
data
);
virtual
void
Run
(
const
cv
::
Mat
*
im
,
float
*
data
);
};
};
class
CenterCropImg
{
class
CenterCropImg
{
public:
public:
virtual
void
Run
(
cv
::
Mat
&
im
,
const
int
crop_size
=
224
);
virtual
void
Run
(
cv
::
Mat
&
im
,
const
int
crop_size
=
224
);
};
};
class
ResizeImg
{
class
ResizeImg
{
public:
public:
virtual
void
Run
(
const
cv
::
Mat
&
img
,
cv
::
Mat
&
resize_img
,
int
max_size_len
);
virtual
void
Run
(
const
cv
::
Mat
&
img
,
cv
::
Mat
&
resize_img
,
int
max_size_len
);
};
};
}
// namespace PaddleClas
}
// namespace PaddleClas
deploy/cpp/include/utility.h
浏览文件 @
944ffc57
...
@@ -32,15 +32,15 @@
...
@@ -32,15 +32,15 @@
namespace
PaddleClas
{
namespace
PaddleClas
{
class
Utility
{
class
Utility
{
public:
public:
static
std
::
vector
<
std
::
string
>
ReadDict
(
const
std
::
string
&
path
);
static
std
::
vector
<
std
::
string
>
ReadDict
(
const
std
::
string
&
path
);
// template <class ForwardIterator>
// template <class ForwardIterator>
// inline static size_t argmax(ForwardIterator first, ForwardIterator last)
// inline static size_t argmax(ForwardIterator first, ForwardIterator last)
// {
// {
// return std::distance(first, std::max_element(first, last));
// return std::distance(first, std::max_element(first, last));
// }
// }
};
};
}
// namespace PaddleClas
}
// namespace PaddleClas
\ No newline at end of file
deploy/cpp/readme.md
浏览文件 @
944ffc57
...
@@ -143,10 +143,10 @@ tar -xvf paddle_inference.tgz
...
@@ -143,10 +143,10 @@ tar -xvf paddle_inference.tgz
```
```
inference/
inference/
|--
cls_infer
.pdmodel
|--
inference
.pdmodel
|--
cls_infer
.pdiparams
|--
inference
.pdiparams
```
```
**注意**
:上述文件中,
`
cls_infer.pdmodel`
文件存储了模型结构信息,
`cls_infer.pdiparams`
文件存储了模型参数信息。注意两个文件的路径需要与配置文件
`tools/config.txt`
中的
`cls_model_path`
和
`cls_params_path`
参数对应一致
。
**注意**
:上述文件中,
`
inference.pdmodel`
文件存储了模型结构信息,
`inference.pdiparams`
文件存储了模型参数信息。模型目录可以随意设置,但是模型名字不能修改
。
### 2.2 编译PaddleClas C++预测demo
### 2.2 编译PaddleClas C++预测demo
...
@@ -183,6 +183,7 @@ cmake .. \
...
@@ -183,6 +183,7 @@ cmake .. \
-DCUDA_LIB
=
${
CUDA_LIB_DIR
}
\
-DCUDA_LIB
=
${
CUDA_LIB_DIR
}
\
make
-j
make
-j
cd
..
```
```
上述命令中,
上述命令中,
...
@@ -200,31 +201,26 @@ make -j
...
@@ -200,31 +201,26 @@ make -j
在执行上述命令,编译完成之后,会在当前路径下生成
`build`
文件夹,其中生成一个名为
`clas_system`
的可执行文件。
在执行上述命令,编译完成之后,会在当前路径下生成
`build`
文件夹,其中生成一个名为
`clas_system`
的可执行文件。
### 运行demo
### 2.3 运行demo
*
首先修改
`tools/config.txt`
中对应字段:
#### 2.3.1 设置配置文件
*
use_gpu:是否使用GPU;
*
gpu_id:使用的GPU卡号;
*
gpu_mem:显存;
*
cpu_math_library_num_threads:底层科学计算库所用线程的数量;
*
use_mkldnn:是否使用MKLDNN加速;
*
use_tensorrt: 是否使用tensorRT进行加速;
*
use_fp16:是否使用半精度浮点数进行计算,该选项仅在use_tensorrt为true时有效;
*
cls_model_path:预测模型结构文件路径;
*
cls_params_path:预测模型参数文件路径;
*
resize_short_size:预处理时图像缩放大小;
*
crop_size:预处理时图像裁剪后的大小。
*
然后修改
`tools/run.sh`
:
```
shell
*
`./build/clas_system ./tools/config.txt ./docs/imgs/ILSVRC2012_val_00000666.JPEG`
cp
../configs/inference_cls.yaml tools/
*
上述命令中分别为:编译得到的可执行文件
`clas_system`
;运行时的配置文件
`config.txt`
;待预测的图像。
```
根据
[
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`
等参数。
*
最后执行以下命令,完成对一幅图像的分类。
#### 2.3.2 执行
```
shell
```
shell
sh tools/run.sh
./build/clas_system
-c
inference_cls.yaml
# or
./build/clas_system
-config
inference_cls.yaml
```
```
*
最终屏幕上会输出结果,如下图所示。
最终屏幕上会输出结果,如下图所示。
<div
align=
"center"
>
<div
align=
"center"
>
<img
src=
"./docs/imgs/cpp_infer_result.png"
width=
"600"
>
<img
src=
"./docs/imgs/cpp_infer_result.png"
width=
"600"
>
...
...
deploy/cpp/src/cls.cpp
浏览文件 @
944ffc57
...
@@ -16,97 +16,97 @@
...
@@ -16,97 +16,97 @@
namespace
PaddleClas
{
namespace
PaddleClas
{
void
Classifier
::
LoadModel
(
const
std
::
string
&
model_path
,
void
Classifier
::
LoadModel
(
const
std
::
string
&
model_path
,
const
std
::
string
&
params_path
)
{
const
std
::
string
&
params_path
)
{
paddle_infer
::
Config
config
;
paddle_infer
::
Config
config
;
config
.
SetModel
(
model_path
,
params_path
);
config
.
SetModel
(
model_path
,
params_path
);
if
(
this
->
use_gpu_
)
{
if
(
this
->
use_gpu_
)
{
config
.
EnableUseGpu
(
this
->
gpu_mem_
,
this
->
gpu_id_
);
config
.
EnableUseGpu
(
this
->
gpu_mem_
,
this
->
gpu_id_
);
if
(
this
->
use_tensorrt_
)
{
if
(
this
->
use_tensorrt_
)
{
config
.
EnableTensorRtEngine
(
config
.
EnableTensorRtEngine
(
1
<<
20
,
1
,
3
,
1
<<
20
,
1
,
3
,
this
->
use_fp16_
?
paddle_infer
::
Config
::
Precision
::
kHalf
this
->
use_fp16_
?
paddle_infer
::
Config
::
Precision
::
kHalf
:
paddle_infer
::
Config
::
Precision
::
kFloat32
,
:
paddle_infer
::
Config
::
Precision
::
kFloat32
,
false
,
false
);
false
,
false
);
}
}
else
{
config
.
DisableGpu
();
if
(
this
->
use_mkldnn_
)
{
config
.
EnableMKLDNN
();
// cache 10 different shapes for mkldnn to avoid memory leak
config
.
SetMkldnnCacheCapacity
(
10
);
}
config
.
SetCpuMathLibraryNumThreads
(
this
->
cpu_math_library_num_threads_
);
}
config
.
SwitchUseFeedFetchOps
(
false
);
// true for multiple input
config
.
SwitchSpecifyInputNames
(
true
);
config
.
SwitchIrOptim
(
this
->
ir_optim_
);
config
.
EnableMemoryOptim
();
config
.
DisableGlogInfo
();
this
->
predictor_
=
CreatePredictor
(
config
);
}
}
}
else
{
config
.
DisableGpu
();
double
Classifier
::
Run
(
cv
::
Mat
&
img
,
std
::
vector
<
double
>
*
times
)
{
if
(
this
->
use_mkldnn_
)
{
cv
::
Mat
srcimg
;
config
.
EnableMKLDNN
();
cv
::
Mat
resize_img
;
// cache 10 different shapes for mkldnn to avoid memory leak
img
.
copyTo
(
srcimg
);
config
.
SetMkldnnCacheCapacity
(
10
);
auto
preprocess_start
=
std
::
chrono
::
system_clock
::
now
();
this
->
resize_op_
.
Run
(
img
,
resize_img
,
this
->
resize_short_size_
);
this
->
crop_op_
.
Run
(
resize_img
,
this
->
crop_size_
);
this
->
normalize_op_
.
Run
(
&
resize_img
,
this
->
mean_
,
this
->
std_
,
this
->
scale_
);
std
::
vector
<
float
>
input
(
1
*
3
*
resize_img
.
rows
*
resize_img
.
cols
,
0.0
f
);
this
->
permute_op_
.
Run
(
&
resize_img
,
input
.
data
());
auto
input_names
=
this
->
predictor_
->
GetInputNames
();
auto
input_t
=
this
->
predictor_
->
GetInputHandle
(
input_names
[
0
]);
input_t
->
Reshape
({
1
,
3
,
resize_img
.
rows
,
resize_img
.
cols
});
auto
preprocess_end
=
std
::
chrono
::
system_clock
::
now
();
auto
infer_start
=
std
::
chrono
::
system_clock
::
now
();
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
();
int
out_num
=
std
::
accumulate
(
output_shape
.
begin
(),
output_shape
.
end
(),
1
,
std
::
multiplies
<
int
>
());
out_data
.
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
();
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
));
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
);
std
::
chrono
::
duration
<
float
>
postprocess_diff
=
postprocess_end
-
postprocess_start
;
times
->
push_back
(
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
;
}
}
config
.
SetCpuMathLibraryNumThreads
(
this
->
cpu_math_library_num_threads_
);
}
config
.
SwitchUseFeedFetchOps
(
false
);
// true for multiple input
config
.
SwitchSpecifyInputNames
(
true
);
config
.
SwitchIrOptim
(
this
->
ir_optim_
);
config
.
EnableMemoryOptim
();
config
.
DisableGlogInfo
();
this
->
predictor_
=
CreatePredictor
(
config
);
}
double
Classifier
::
Run
(
cv
::
Mat
&
img
,
std
::
vector
<
double
>
*
times
)
{
cv
::
Mat
srcimg
;
cv
::
Mat
resize_img
;
img
.
copyTo
(
srcimg
);
auto
preprocess_start
=
std
::
chrono
::
system_clock
::
now
();
this
->
resize_op_
.
Run
(
img
,
resize_img
,
this
->
resize_short_size_
);
this
->
crop_op_
.
Run
(
resize_img
,
this
->
crop_size_
);
this
->
normalize_op_
.
Run
(
&
resize_img
,
this
->
mean_
,
this
->
std_
,
this
->
scale_
);
std
::
vector
<
float
>
input
(
1
*
3
*
resize_img
.
rows
*
resize_img
.
cols
,
0.0
f
);
this
->
permute_op_
.
Run
(
&
resize_img
,
input
.
data
());
auto
input_names
=
this
->
predictor_
->
GetInputNames
();
auto
input_t
=
this
->
predictor_
->
GetInputHandle
(
input_names
[
0
]);
input_t
->
Reshape
({
1
,
3
,
resize_img
.
rows
,
resize_img
.
cols
});
auto
preprocess_end
=
std
::
chrono
::
system_clock
::
now
();
auto
infer_start
=
std
::
chrono
::
system_clock
::
now
();
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
();
int
out_num
=
std
::
accumulate
(
output_shape
.
begin
(),
output_shape
.
end
(),
1
,
std
::
multiplies
<
int
>
());
out_data
.
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
();
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
));
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
);
std
::
chrono
::
duration
<
float
>
postprocess_diff
=
postprocess_end
-
postprocess_start
;
times
->
push_back
(
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
;
}
}
// namespace PaddleClas
}
// namespace PaddleClas
deploy/cpp/src/cls_config.cpp
浏览文件 @
944ffc57
...
@@ -16,20 +16,20 @@
...
@@ -16,20 +16,20 @@
namespace
PaddleClas
{
namespace
PaddleClas
{
void
ClsConfig
::
PrintConfigInfo
()
{
void
ClsConfig
::
PrintConfigInfo
()
{
std
::
cout
<<
"=======Paddle Class inference config======"
<<
std
::
endl
;
std
::
cout
<<
"=======Paddle Class inference config======"
<<
std
::
endl
;
std
::
cout
<<
this
->
config_file
<<
std
::
endl
;
std
::
cout
<<
this
->
config_file
<<
std
::
endl
;
std
::
cout
<<
"=======End of Paddle Class inference config======"
<<
std
::
endl
;
std
::
cout
<<
"=======End of Paddle Class inference config======"
<<
std
::
endl
;
}
}
void
ClsConfig
::
ReadYamlConfig
(
const
std
::
string
&
path
)
{
void
ClsConfig
::
ReadYamlConfig
(
const
std
::
string
&
path
)
{
try
{
try
{
this
->
config_file
=
YAML
::
LoadFile
(
path
);
this
->
config_file
=
YAML
::
LoadFile
(
path
);
}
catch
(
YAML
::
BadFile
&
e
)
{
}
catch
(
YAML
::
BadFile
&
e
)
{
std
::
cout
<<
"Something wrong in yaml file, please check yaml file"
std
::
cout
<<
"Something wrong in yaml file, please check yaml file"
<<
std
::
endl
;
<<
std
::
endl
;
exit
(
1
);
exit
(
1
);
}
}
}
}
};
// namespace PaddleClas
};
// namespace PaddleClas
deploy/cpp/src/main.cpp
浏览文件 @
944ffc57
...
@@ -35,79 +35,81 @@ using namespace std;
...
@@ -35,79 +35,81 @@ using namespace std;
using
namespace
cv
;
using
namespace
cv
;
using
namespace
PaddleClas
;
using
namespace
PaddleClas
;
DEFINE_string
(
config
,
""
,
"Path of yaml file"
);
DEFINE_string
(
config
,
DEFINE_string
(
c
,
""
,
"Path of yaml file"
);
""
,
"Path of yaml file"
);
DEFINE_string
(
c
,
""
,
"Path of yaml file"
);
int
main
(
int
argc
,
char
**
argv
)
{
int
main
(
int
argc
,
char
**
argv
)
{
google
::
ParseCommandLineFlags
(
&
argc
,
&
argv
,
true
);
google
::
ParseCommandLineFlags
(
&
argc
,
&
argv
,
true
);
std
::
string
yaml_path
=
""
;
std
::
string
yaml_path
=
""
;
if
(
FLAGS_config
==
""
&&
FLAGS_c
==
""
)
{
if
(
FLAGS_config
==
""
&&
FLAGS_c
==
""
)
{
std
::
cerr
<<
"[ERROR] usage: "
<<
std
::
endl
std
::
cerr
<<
"[ERROR] usage: "
<<
std
::
endl
<<
argv
[
0
]
<<
" -c $yaml_path"
<<
std
::
endl
<<
argv
[
0
]
<<
" -c $yaml_path"
<<
std
::
endl
<<
"or:"
<<
std
::
endl
<<
"or:"
<<
std
::
endl
<<
argv
[
0
]
<<
" -config $yaml_path"
<<
std
::
endl
;
<<
argv
[
0
]
<<
" -config $yaml_path"
<<
std
::
endl
;
exit
(
1
);
exit
(
1
);
}
else
if
(
FLAGS_config
!=
""
)
{
}
else
if
(
FLAGS_config
!=
""
)
{
yaml_path
=
FLAGS_config
;
yaml_path
=
FLAGS_config
;
}
else
{
}
else
{
yaml_path
=
FLAGS_c
;
yaml_path
=
FLAGS_c
;
}
}
ClsConfig
config
(
yaml_path
);
ClsConfig
config
(
yaml_path
);
config
.
PrintConfigInfo
();
config
.
PrintConfigInfo
();
std
::
string
path
(
config
.
infer_imgs
);
std
::
string
path
(
config
.
infer_imgs
);
std
::
vector
<
std
::
string
>
img_files_list
;
std
::
vector
<
std
::
string
>
img_files_list
;
if
(
cv
::
utils
::
fs
::
isDirectory
(
path
))
{
if
(
cv
::
utils
::
fs
::
isDirectory
(
path
))
{
std
::
vector
<
cv
::
String
>
filenames
;
std
::
vector
<
cv
::
String
>
filenames
;
cv
::
glob
(
path
,
filenames
);
cv
::
glob
(
path
,
filenames
);
for
(
auto
f
:
filenames
)
{
for
(
auto
f
:
filenames
)
{
img_files_list
.
push_back
(
f
);
img_files_list
.
push_back
(
f
);
}
}
else
{
img_files_list
.
push_back
(
path
);
}
}
}
else
{
img_files_list
.
push_back
(
path
);
}
std
::
cout
<<
"img_file_list length: "
<<
img_files_list
.
size
()
<<
std
::
endl
;
std
::
cout
<<
"img_file_list length: "
<<
img_files_list
.
size
()
<<
std
::
endl
;
Classifier
classifier
(
config
);
Classifier
classifier
(
config
);
double
elapsed_time
=
0.0
;
double
elapsed_time
=
0.0
;
std
::
vector
<
double
>
cls_times
;
std
::
vector
<
double
>
cls_times
;
int
warmup_iter
=
img_files_list
.
size
()
>
5
?
5
:
0
;
int
warmup_iter
=
img_files_list
.
size
()
>
5
?
5
:
0
;
for
(
int
idx
=
0
;
idx
<
img_files_list
.
size
();
++
idx
)
{
for
(
int
idx
=
0
;
idx
<
img_files_list
.
size
();
++
idx
)
{
std
::
string
img_path
=
img_files_list
[
idx
];
std
::
string
img_path
=
img_files_list
[
idx
];
cv
::
Mat
srcimg
=
cv
::
imread
(
img_path
,
cv
::
IMREAD_COLOR
);
cv
::
Mat
srcimg
=
cv
::
imread
(
img_path
,
cv
::
IMREAD_COLOR
);
if
(
!
srcimg
.
data
)
{
if
(
!
srcimg
.
data
)
{
std
::
cerr
<<
"[ERROR] image read failed! image path: "
<<
img_path
std
::
cerr
<<
"[ERROR] image read failed! image path: "
<<
img_path
<<
"
\n
"
;
<<
"
\n
"
;
exit
(
-
1
);
exit
(
-
1
);
}
}
cv
::
cvtColor
(
srcimg
,
srcimg
,
cv
::
COLOR_BGR2RGB
);
cv
::
cvtColor
(
srcimg
,
srcimg
,
cv
::
COLOR_BGR2RGB
);
double
run_time
=
classifier
.
Run
(
srcimg
,
&
cls_times
);
double
run_time
=
classifier
.
Run
(
srcimg
,
&
cls_times
);
if
(
idx
>=
warmup_iter
)
{
if
(
idx
>=
warmup_iter
)
{
elapsed_time
+=
run_time
;
elapsed_time
+=
run_time
;
std
::
cout
<<
"Current image path: "
<<
img_path
<<
std
::
endl
;
std
::
cout
<<
"Current image path: "
<<
img_path
<<
std
::
endl
;
std
::
cout
<<
"Current time cost: "
<<
run_time
<<
" s, "
std
::
cout
<<
"Current time cost: "
<<
run_time
<<
" s, "
<<
"average time cost in all: "
<<
"average time cost in all: "
<<
elapsed_time
/
(
idx
+
1
-
warmup_iter
)
<<
" s."
<<
std
::
endl
;
<<
elapsed_time
/
(
idx
+
1
-
warmup_iter
)
<<
" s."
<<
std
::
endl
;
}
else
{
}
else
{
std
::
cout
<<
"Current time cost: "
<<
run_time
<<
" s."
<<
std
::
endl
;
std
::
cout
<<
"Current time cost: "
<<
run_time
<<
" s."
<<
std
::
endl
;
}
}
}
}
std
::
string
presion
=
"fp32"
;
std
::
string
presion
=
"fp32"
;
if
(
config
.
use_fp16
)
if
(
config
.
use_fp16
)
presion
=
"fp16"
;
presion
=
"fp16"
;
if
(
config
.
benchmark
)
{
if
(
config
.
benchmark
)
{
AutoLogger
autolog
(
"Classification"
,
config
.
use_gpu
,
config
.
use_tensorrt
,
AutoLogger
autolog
(
"Classification"
,
config
.
use_gpu
,
config
.
use_tensorrt
,
config
.
use_mkldnn
,
config
.
cpu_threads
,
1
,
config
.
use_mkldnn
,
config
.
cpu_threads
,
1
,
"1, 3, 224, 224"
,
presion
,
cls_times
,
"1, 3, 224, 224"
,
presion
,
cls_times
,
img_files_list
.
size
());
img_files_list
.
size
());
autolog
.
report
();
autolog
.
report
();
}
}
return
0
;
return
0
;
}
}
deploy/cpp/src/preprocess_op.cpp
浏览文件 @
944ffc57
...
@@ -32,57 +32,57 @@
...
@@ -32,57 +32,57 @@
namespace
PaddleClas
{
namespace
PaddleClas
{
void
Permute
::
Run
(
const
cv
::
Mat
*
im
,
float
*
data
)
{
void
Permute
::
Run
(
const
cv
::
Mat
*
im
,
float
*
data
)
{
int
rh
=
im
->
rows
;
int
rh
=
im
->
rows
;
int
rw
=
im
->
cols
;
int
rw
=
im
->
cols
;
int
rc
=
im
->
channels
();
int
rc
=
im
->
channels
();
for
(
int
i
=
0
;
i
<
rc
;
++
i
)
{
for
(
int
i
=
0
;
i
<
rc
;
++
i
)
{
cv
::
extractChannel
(
*
im
,
cv
::
Mat
(
rh
,
rw
,
CV_32FC1
,
data
+
i
*
rh
*
rw
),
i
);
cv
::
extractChannel
(
*
im
,
cv
::
Mat
(
rh
,
rw
,
CV_32FC1
,
data
+
i
*
rh
*
rw
),
i
);
}
}
}
}
void
Normalize
::
Run
(
cv
::
Mat
*
im
,
const
std
::
vector
<
float
>
&
mean
,
void
Normalize
::
Run
(
cv
::
Mat
*
im
,
const
std
::
vector
<
float
>
&
mean
,
const
std
::
vector
<
float
>
&
std
,
float
&
scale
)
{
const
std
::
vector
<
float
>
&
std
,
float
&
scale
)
{
if
(
scale
)
{
if
(
scale
)
{
(
*
im
).
convertTo
(
*
im
,
CV_32FC3
,
scale
);
(
*
im
).
convertTo
(
*
im
,
CV_32FC3
,
scale
);
}
}
for
(
int
h
=
0
;
h
<
im
->
rows
;
h
++
)
{
for
(
int
h
=
0
;
h
<
im
->
rows
;
h
++
)
{
for
(
int
w
=
0
;
w
<
im
->
cols
;
w
++
)
{
for
(
int
w
=
0
;
w
<
im
->
cols
;
w
++
)
{
im
->
at
<
cv
::
Vec3f
>
(
h
,
w
)[
0
]
=
im
->
at
<
cv
::
Vec3f
>
(
h
,
w
)[
0
]
=
(
im
->
at
<
cv
::
Vec3f
>
(
h
,
w
)[
0
]
-
mean
[
0
])
/
std
[
0
];
(
im
->
at
<
cv
::
Vec3f
>
(
h
,
w
)[
0
]
-
mean
[
0
])
/
std
[
0
];
im
->
at
<
cv
::
Vec3f
>
(
h
,
w
)[
1
]
=
im
->
at
<
cv
::
Vec3f
>
(
h
,
w
)[
1
]
=
(
im
->
at
<
cv
::
Vec3f
>
(
h
,
w
)[
1
]
-
mean
[
1
])
/
std
[
1
];
(
im
->
at
<
cv
::
Vec3f
>
(
h
,
w
)[
1
]
-
mean
[
1
])
/
std
[
1
];
im
->
at
<
cv
::
Vec3f
>
(
h
,
w
)[
2
]
=
im
->
at
<
cv
::
Vec3f
>
(
h
,
w
)[
2
]
=
(
im
->
at
<
cv
::
Vec3f
>
(
h
,
w
)[
2
]
-
mean
[
2
])
/
std
[
2
];
(
im
->
at
<
cv
::
Vec3f
>
(
h
,
w
)[
2
]
-
mean
[
2
])
/
std
[
2
];
}
}
}
}
}
}
void
CenterCropImg
::
Run
(
cv
::
Mat
&
img
,
const
int
crop_size
)
{
void
CenterCropImg
::
Run
(
cv
::
Mat
&
img
,
const
int
crop_size
)
{
int
resize_w
=
img
.
cols
;
int
resize_w
=
img
.
cols
;
int
resize_h
=
img
.
rows
;
int
resize_h
=
img
.
rows
;
int
w_start
=
int
((
resize_w
-
crop_size
)
/
2
);
int
w_start
=
int
((
resize_w
-
crop_size
)
/
2
);
int
h_start
=
int
((
resize_h
-
crop_size
)
/
2
);
int
h_start
=
int
((
resize_h
-
crop_size
)
/
2
);
cv
::
Rect
rect
(
w_start
,
h_start
,
crop_size
,
crop_size
);
cv
::
Rect
rect
(
w_start
,
h_start
,
crop_size
,
crop_size
);
img
=
img
(
rect
);
img
=
img
(
rect
);
}
}
void
ResizeImg
::
Run
(
const
cv
::
Mat
&
img
,
cv
::
Mat
&
resize_img
,
void
ResizeImg
::
Run
(
const
cv
::
Mat
&
img
,
cv
::
Mat
&
resize_img
,
int
resize_short_size
)
{
int
resize_short_size
)
{
int
w
=
img
.
cols
;
int
w
=
img
.
cols
;
int
h
=
img
.
rows
;
int
h
=
img
.
rows
;
float
ratio
=
1.
f
;
float
ratio
=
1.
f
;
if
(
h
<
w
)
{
if
(
h
<
w
)
{
ratio
=
float
(
resize_short_size
)
/
float
(
h
);
ratio
=
float
(
resize_short_size
)
/
float
(
h
);
}
else
{
}
else
{
ratio
=
float
(
resize_short_size
)
/
float
(
w
);
ratio
=
float
(
resize_short_size
)
/
float
(
w
);
}
}
int
resize_h
=
round
(
float
(
h
)
*
ratio
);
int
resize_h
=
round
(
float
(
h
)
*
ratio
);
int
resize_w
=
round
(
float
(
w
)
*
ratio
);
int
resize_w
=
round
(
float
(
w
)
*
ratio
);
cv
::
resize
(
img
,
resize_img
,
cv
::
Size
(
resize_w
,
resize_h
));
cv
::
resize
(
img
,
resize_img
,
cv
::
Size
(
resize_w
,
resize_h
));
}
}
}
// namespace PaddleClas
}
// namespace PaddleClas
deploy/cpp/tools/build.sh
浏览文件 @
944ffc57
...
@@ -18,3 +18,4 @@ cmake .. \
...
@@ -18,3 +18,4 @@ cmake .. \
-DCUDA_LIB
=
${
CUDA_LIB_DIR
}
\
-DCUDA_LIB
=
${
CUDA_LIB_DIR
}
\
make
-j
make
-j
cd
..
deploy/cpp/tools/config.txt
已删除
100755 → 0
浏览文件 @
4dbd85b8
# model load config
use_gpu 0
gpu_id 0
gpu_mem 4000
cpu_threads 10
use_mkldnn 1
use_tensorrt 0
use_fp16 0
# cls config
cls_model_path /PaddleClas/inference/cls_infer.pdmodel
cls_params_path /PaddleClas/inference/cls_infer.pdiparams
resize_short_size 256
crop_size 224
# for log env info
benchmark 0
deploy/cpp/tools/run.sh
已删除
100755 → 0
浏览文件 @
4dbd85b8
./build/clas_system ./tools/config.txt ./docs/imgs/ILSVRC2012_val_00000666.JPEG
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录