Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
565c89de
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
338
Star
4
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
271
列表
看板
标记
里程碑
合并请求
78
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle-Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
271
Issue
271
列表
看板
标记
里程碑
合并请求
78
合并请求
78
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
565c89de
编写于
9月 12, 2019
作者:
C
Chunwei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update cpp_demo.md
上级
da118bee
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
87 addition
and
83 deletion
+87
-83
cpp_demo.md
cpp_demo.md
+87
-83
未找到文件。
cpp_demo.md
浏览文件 @
565c89de
<!--ts-->
<!--ts-->
*
[
C++ Demo
](
#c-demo
)
*
[
编译
](
#编译-1
)
-
[
C++ Demo
](
#c-demo
)
*
[
准备执行环境
](
#准备执行环境
)
-
[
编译
](
#编译-1
)
*
[
使用安卓手机
](
#使用安卓手机
)
-
[
准备执行环境
](
#准备执行环境
)
*
[
使用安卓模拟器
](
#使用安卓模拟器
)
-
[
使用安卓手机
](
#使用安卓手机
)
*
[
下载模型并运行示例
](
#下载模型并运行示例
)
-
[
使用安卓模拟器
](
#使用安卓模拟器
)
*
[
Demo 程序运行结果
](
#demo-程序运行结果
)
-
[
下载模型并运行示例
](
#下载模型并运行示例
)
*
[
如何在代码中使用 API
](
#如何在代码中使用-api
)
-
[
Demo 程序运行结果
](
#demo-程序运行结果
)
-
[
如何在代码中使用 API
](
#如何在代码中使用-api
)
<!-- Added by: yanchunwei, at: Mon Aug 26 22:23:07 CST 2019 -->
<!-- Added by: yanchunwei, at: Mon Aug 26 22:23:07 CST 2019 -->
...
@@ -84,6 +85,7 @@ adb -s emulator-5554 push mobilenetv1_full_api /data/local/tmp/
...
@@ -84,6 +85,7 @@ adb -s emulator-5554 push mobilenetv1_full_api /data/local/tmp/
adb
-s
emulator-5554 shell
chmod
+x /data/local/tmp/mobilenetv1_full_api
adb
-s
emulator-5554 shell
chmod
+x /data/local/tmp/mobilenetv1_full_api
adb
-s
emulator-5554 shell
"/data/local/tmp/mobilenetv1_full_api --model_dir=/data/local/tmp/mobilenet_v1 --optimized_model_dir=/data/local/tmp/mobilenet_v1.opt"
adb
-s
emulator-5554 shell
"/data/local/tmp/mobilenetv1_full_api --model_dir=/data/local/tmp/mobilenet_v1 --optimized_model_dir=/data/local/tmp/mobilenet_v1.opt"
```
```
注:我们也提供了轻量级 API 的 demo,可以执行以下代码运行轻量级 API 示例。
注:我们也提供了轻量级 API 的 demo,可以执行以下代码运行轻量级 API 示例。
```
bash
```
bash
...
@@ -93,7 +95,9 @@ adb -s emulator-5554 push mobilenetv1_light_api /data/local/tmp/
...
@@ -93,7 +95,9 @@ adb -s emulator-5554 push mobilenetv1_light_api /data/local/tmp/
adb
-s
emulator-5554 shell
chmod
+x /data/local/tmp/mobilenetv1_light_api
adb
-s
emulator-5554 shell
chmod
+x /data/local/tmp/mobilenetv1_light_api
adb
-s
emulator-5554 shell
"/data/local/tmp/mobilenetv1_light_api --model_dir=/data/local/tmp/mobilenet_v1.opt --threads=1 "
adb
-s
emulator-5554 shell
"/data/local/tmp/mobilenetv1_light_api --model_dir=/data/local/tmp/mobilenet_v1.opt --threads=1 "
```
```
## Demo 程序运行结果
## Demo 程序运行结果
Demo 运行成功后 ,将在控制台输出预测结果的前10个类别的预测概率:
Demo 运行成功后 ,将在控制台输出预测结果的前10个类别的预测概率:
```
bash
```
bash
...
@@ -128,50 +132,50 @@ Output[900]: 0.000586
...
@@ -128,50 +132,50 @@ Output[900]: 0.000586
-
通过MobileConfig设置:模型文件位置(model_dir)、线程数(thread)和能耗模式( power mode )。输入数据(input),从 MobileConfig 创建 PaddlePredictor 并执行预测。 (注:Lite还支持从memory直接加载模型,可以通过MobileConfig::set_model_buffer方法实现)
-
通过MobileConfig设置:模型文件位置(model_dir)、线程数(thread)和能耗模式( power mode )。输入数据(input),从 MobileConfig 创建 PaddlePredictor 并执行预测。 (注:Lite还支持从memory直接加载模型,可以通过MobileConfig::set_model_buffer方法实现)
代码示例:
代码示例:
```
cpp
```
cpp
// 1. Create MobileConfig
// 1. Create MobileConfig
MobileConfig
config
;
MobileConfig
config
;
// 2. Load model
// 2. Load model
config
.
set_model_dir
(
"path to your model directory"
);
//
model dir
config
.
set_model_dir
(
"path to your model directory"
);
//
model dir
/*
load model: Lite supports loading model from file or from memory (naive buffer from optimized model)
//
load model: Lite supports loading model from file or from memory (naive buffer from optimized model)
//Method One: Load model from memory:
//Method One: Load model from memory:
void set_model_buffer(const char* model_buffer,
void
set_model_buffer
(
const
char
*
model_buffer
,
size_t
model_buffer_size
,
size_t
model_buffer_size
,
const
char
*
param_buffer
,
const
char
*
param_buffer
,
size_t
param_buffer_size
)
size_t
param_buffer_size
)
//Method Two: Load model from file:
//Method Two: Load model from file:
void set_model_dir(const std::string& model_dir) */
void
set_model_dir
(
const
std
::
string
&
model_dir
)
*/
// 3. Set MobileConfig (or you can skip this step to use default value):
// 3. Set MobileConfig (or you can skip this step to use default value):
config
.
set_power_mode
(
LITE_POWER_HIGH
);
//
power mode
config
.
set_power_mode
(
LITE_POWER_HIGH
);
//
power mode
/*power modes: Lite supports the following power modes
/*power modes: Lite supports the following power modes
LITE_POWER_HIGH
LITE_POWER_HIGH
LITE_POWER_LOW
LITE_POWER_LOW
LITE_POWER_FULL
LITE_POWER_FULL
LITE_POWER_NO_BIND
LITE_POWER_NO_BIND
LITE_POWER_RAND_HIGH
LITE_POWER_RAND_HIGH
LITE_POWER_RAND_LOW */
LITE_POWER_RAND_LOW */
config
.
set_threads
(
"num of threads"
);
//
threads
config
.
set_threads
(
"num of threads"
);
//
threads
// 4. Create PaddlePredictor by MobileConfig
// 4. Create PaddlePredictor by MobileConfig
std
::
shared_ptr
<
PaddlePredictor
>
predictor
=
std
::
shared_ptr
<
PaddlePredictor
>
predictor
=
CreatePaddlePredictor
<
MobileConfig
>
(
config
);
CreatePaddlePredictor
<
MobileConfig
>
(
config
);
// 5. Prepare input data
// 5. Prepare input data
std
::
unique_ptr
<
Tensor
>
input_tensor
(
std
::
move
(
predictor
->
GetInput
(
0
)));
std
::
unique_ptr
<
Tensor
>
input_tensor
(
std
::
move
(
predictor
->
GetInput
(
0
)));
input_tensor
->
Resize
({
1
,
3
,
224
,
224
});
input_tensor
->
Resize
({
1
,
3
,
224
,
224
});
auto
*
data
=
input_tensor
->
mutable_data
<
float
>
();
auto
*
data
=
input_tensor
->
mutable_data
<
float
>
();
for
(
int
i
=
0
;
i
<
ShapeProduction
(
input_tensor
->
shape
());
++
i
)
{
for
(
int
i
=
0
;
i
<
ShapeProduction
(
input_tensor
->
shape
());
++
i
)
{
data
[
i
]
=
1
;
data
[
i
]
=
1
;
}
}
// 6. Run predictor
// 6. Run predictor
predictor
->
Run
();
predictor
->
Run
();
// 7. Get output
// 7. Get output
std
::
unique_ptr
<
const
Tensor
>
output_tensor
(
std
::
unique_ptr
<
const
Tensor
>
output_tensor
(
std
::
move
(
predictor
->
GetOutput
(
0
)));
std
::
move
(
predictor
->
GetOutput
(
0
)));
```
```
## CxxConfig案例: OCR_model的运行
## CxxConfig案例: OCR_model的运行
...
@@ -179,25 +183,25 @@ Output[900]: 0.000586
...
@@ -179,25 +183,25 @@ Output[900]: 0.000586
1.
OCR 模型文件:
1.
OCR 模型文件:
-
我们提供Pb格式的
[
ocr_attention_mode
](
https://paddle-inference-dist.cdn.bcebos.com/ocr_attention.tar.gz
)
l下载
-
我们提供Pb格式的
[
ocr_attention_mode
](
https://paddle-inference-dist.cdn.bcebos.com/ocr_attention.tar.gz
)
l下载
-
也可以从
[
Paddle/model项目
](
https://github.com/PaddlePaddle/models/tree/develop/PaddleCV/ocr_recognition
)
中训练出模型
-
也可以从
[
Paddle/model项目
](
https://github.com/PaddlePaddle/models/tree/develop/PaddleCV/ocr_recognition
)
中训练出模型
2.
示例代码:
2.
示例代码:
```
c++
```
c++
#include <gflags/gflags.h>
#include <stdio.h>
#include <vector>
#include "paddle_api.h" // NOLINT
#include "paddle_api.h" // NOLINT
#include "paddle_use_kernels.h" // NOLINT
#include "paddle_use_kernels.h" // NOLINT
#include "paddle_use_ops.h" // NOLINT
#include "paddle_use_ops.h" // NOLINT
#include "paddle_use_passes.h" // NOLINT
#include "paddle_use_passes.h" // NOLINT
#include <gflags/gflags.h>
#include <stdio.h>
#include <vector>
using
namespace
paddle
::
lite_api
;
// NOLINT
using
namespace
paddle
::
lite_api
;
// NOLINT
DEFINE_string
(
model_dir
,
""
,
"Model dir path."
);
DEFINE_string
(
model_dir
,
""
,
"Model dir path."
);
DEFINE_bool
(
prefer_int8_kernel
,
false
,
"Prefer to run model with int8 kernels"
);
DEFINE_bool
(
prefer_int8_kernel
,
false
,
"Prefer to run model with int8 kernels"
);
int64_t
ShapeProduction
(
const
shape_t
&
shape
)
{
int64_t
ShapeProduction
(
const
shape_t
&
shape
)
{
int64_t
res
=
1
;
int64_t
res
=
1
;
for
(
auto
i
:
shape
)
res
*=
i
;
for
(
auto
i
:
shape
)
res
*=
i
;
return
res
;
return
res
;
}
}
...
@@ -205,7 +209,8 @@ void RunModel() {
...
@@ -205,7 +209,8 @@ void RunModel() {
// 1. Set CxxConfig
// 1. Set CxxConfig
CxxConfig
config
;
CxxConfig
config
;
config
.
set_model_dir
(
FLAGS_model_dir
);
config
.
set_model_dir
(
FLAGS_model_dir
);
std
::
vector
<
Place
>
valid_places
{
Place
{
TARGET
(
kARM
),
PRECISION
(
kFloat
)},
Place
{
TARGET
(
kHost
),
PRECISION
(
kFloat
)}};
std
::
vector
<
Place
>
valid_places
{
Place
{
TARGET
(
kARM
),
PRECISION
(
kFloat
)},
Place
{
TARGET
(
kHost
),
PRECISION
(
kFloat
)}};
config
.
set_preferred_place
(
Place
{
TARGET
(
kARM
),
PRECISION
(
kFloat
)});
config
.
set_preferred_place
(
Place
{
TARGET
(
kARM
),
PRECISION
(
kFloat
)});
config
.
set_valid_places
(
valid_places
);
config
.
set_valid_places
(
valid_places
);
...
@@ -214,45 +219,44 @@ void RunModel() {
...
@@ -214,45 +219,44 @@ void RunModel() {
CreatePaddlePredictor
<
CxxConfig
>
(
config
);
CreatePaddlePredictor
<
CxxConfig
>
(
config
);
// 3. Prepare input data
// 3. Prepare input data
//input 0
//
input 0
std
::
unique_ptr
<
Tensor
>
input_tensor
(
std
::
move
(
predictor
->
GetInput
(
0
)));
std
::
unique_ptr
<
Tensor
>
input_tensor
(
std
::
move
(
predictor
->
GetInput
(
0
)));
input_tensor
->
Resize
(
shape_t
({
1
,
1
,
48
,
512
}));
input_tensor
->
Resize
(
shape_t
({
1
,
1
,
48
,
512
}));
auto
*
data
=
input_tensor
->
mutable_data
<
float
>
();
auto
*
data
=
input_tensor
->
mutable_data
<
float
>
();
for
(
int
i
=
0
;
i
<
ShapeProduction
(
input_tensor
->
shape
());
++
i
)
{
for
(
int
i
=
0
;
i
<
ShapeProduction
(
input_tensor
->
shape
());
++
i
)
{
data
[
i
]
=
1
;
data
[
i
]
=
1
;
}
}
//input1
//
input1
std
::
unique_ptr
<
Tensor
>
init_ids
(
std
::
move
(
predictor
->
GetInput
(
1
)));
std
::
unique_ptr
<
Tensor
>
init_ids
(
std
::
move
(
predictor
->
GetInput
(
1
)));
init_ids
->
Resize
(
shape_t
({
1
,
1
}));
init_ids
->
Resize
(
shape_t
({
1
,
1
}));
auto
*
data_ids
=
init_ids
->
mutable_data
<
float
>
();
auto
*
data_ids
=
init_ids
->
mutable_data
<
float
>
();
for
(
int
i
=
0
;
i
<
ShapeProduction
(
init_ids
->
shape
());
++
i
)
{
for
(
int
i
=
0
;
i
<
ShapeProduction
(
init_ids
->
shape
());
++
i
)
{
data_ids
[
i
]
=
0
;
data_ids
[
i
]
=
0
;
}
}
lod_t
lod_i
{{
0
,
1
},{
0
,
1
}}
;
lod_t
lod_i
({{
0
,
1
},
{
0
,
1
}})
;
init_ids
->
SetLoD
(
lod_i
);
init_ids
->
SetLoD
(
lod_i
);
//input2
//
input2
std
::
unique_ptr
<
Tensor
>
init_scores
(
std
::
move
(
predictor
->
GetInput
(
2
)));
std
::
unique_ptr
<
Tensor
>
init_scores
(
std
::
move
(
predictor
->
GetInput
(
2
)));
init_scores
->
Resize
(
shape_t
({
1
,
1
}));
init_scores
->
Resize
(
shape_t
({
1
,
1
}));
auto
*
data_scores
=
init_scores
->
mutable_data
<
float
>
();
auto
*
data_scores
=
init_scores
->
mutable_data
<
float
>
();
for
(
int
i
=
0
;
i
<
ShapeProduction
(
init_scores
->
shape
());
++
i
)
{
for
(
int
i
=
0
;
i
<
ShapeProduction
(
init_scores
->
shape
());
++
i
)
{
data_scores
[
i
]
=
0
;
data_scores
[
i
]
=
0
;
}
}
lod_t
lod_s
{{
0
,
1
},{
0
,
1
}};
lod_t
lod_s
{{
0
,
1
},
{
0
,
1
}};
init_scores
->
SetLoD
(
lod_s
);
init_scores
->
SetLoD
(
lod_s
);
// 4. Run predictor
// 4. Run predictor
predictor
->
Run
();
predictor
->
Run
();
// 5. Get output
// 5. Get output
std
::
unique_ptr
<
const
Tensor
>
output_tensor
(
std
::
unique_ptr
<
const
Tensor
>
output_tensor
(
std
::
move
(
predictor
->
GetOutput
(
0
)));
std
::
move
(
predictor
->
GetOutput
(
0
)));
for
(
int
i
=
0
;
i
<
ShapeProduction
(
output_tensor
->
shape
());
i
++
)
{
for
(
int
i
=
0
;
i
<
ShapeProduction
(
output_tensor
->
shape
());
i
++
)
{
printf
(
"Output[%d]: %f
\n
"
,
i
,
output_tensor
->
data
<
float
>
()[
i
]);
printf
(
"Output[%d]: %f
\n
"
,
i
,
output_tensor
->
data
<
float
>
()[
i
]);
}
}
}
}
int
main
(
int
argc
,
char
**
argv
)
{
int
main
(
int
argc
,
char
**
argv
)
{
google
::
ParseCommandLineFlags
(
&
argc
,
&
argv
,
true
);
google
::
ParseCommandLineFlags
(
&
argc
,
&
argv
,
true
);
RunModel
();
RunModel
();
return
0
;
return
0
;
...
@@ -260,8 +264,8 @@ int main(int argc, char** argv) {
...
@@ -260,8 +264,8 @@ int main(int argc, char** argv) {
```
```
3.
运行方法:
3.
运行方法:
参考以上代码编译出可执行文件
`OCR_DEMO`
,模型文件夹为
`ocr_attention`
。手机以USB调试、文件传输模式连接电脑
参考以上代码编译出可执行文件
`OCR_DEMO`
,模型文件夹为
`ocr_attention`
。手机以USB调试、文件传输模式连接电脑
在终端中输入以下命令执行OCR model测试:
在终端中输入以下命令执行OCR model测试:
```
```
#OCR_DEMO为编译出的可执行文件名称,ocr_attention为ocr_attention模型的文件夹名称
#OCR_DEMO为编译出的可执行文件名称,ocr_attention为ocr_attention模型的文件夹名称
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录