Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
165450ff
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
165450ff
编写于
2月 06, 2018
作者:
Y
Yiqun Liu
提交者:
kexinzhao
2月 05, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refine the inference unittest recognize_digits. (#8147)
上级
b0ecb365
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
49 addition
and
18 deletion
+49
-18
paddle/inference/tests/book/test_inference_recognize_digits.cc
...e/inference/tests/book/test_inference_recognize_digits.cc
+46
-17
python/paddle/v2/fluid/tests/book/test_recognize_digits.py
python/paddle/v2/fluid/tests/book/test_recognize_digits.py
+3
-1
未找到文件。
paddle/inference/tests/book/test_inference_recognize_digits.cc
浏览文件 @
165450ff
...
...
@@ -58,6 +58,47 @@ void TestInference(const std::string& dirname,
delete
scope
;
}
template
<
typename
T
>
void
SetupTensor
(
paddle
::
framework
::
LoDTensor
&
input
,
paddle
::
framework
::
DDim
dims
,
T
lower
,
T
upper
)
{
srand
(
time
(
0
));
float
*
input_ptr
=
input
.
mutable_data
<
T
>
(
dims
,
paddle
::
platform
::
CPUPlace
());
for
(
int
i
=
0
;
i
<
input
.
numel
();
++
i
)
{
input_ptr
[
i
]
=
(
static_cast
<
T
>
(
rand
())
/
static_cast
<
T
>
(
RAND_MAX
))
*
(
upper
-
lower
)
+
lower
;
}
}
template
<
typename
T
>
void
CheckError
(
paddle
::
framework
::
LoDTensor
&
output1
,
paddle
::
framework
::
LoDTensor
&
output2
)
{
// Check lod information
EXPECT_EQ
(
output1
.
lod
(),
output2
.
lod
());
EXPECT_EQ
(
output1
.
dims
(),
output2
.
dims
());
EXPECT_EQ
(
output1
.
numel
(),
output2
.
numel
());
T
err
=
static_cast
<
T
>
(
0
);
if
(
typeid
(
T
)
==
typeid
(
float
))
{
err
=
1E-3
;
}
else
if
(
typeid
(
T
)
==
typeid
(
double
))
{
err
=
1E-6
;
}
else
{
err
=
0
;
}
size_t
count
=
0
;
for
(
int64_t
i
=
0
;
i
<
output1
.
numel
();
++
i
)
{
if
(
fabs
(
output1
.
data
<
T
>
()[
i
]
-
output2
.
data
<
T
>
()[
i
])
>
err
)
{
count
++
;
}
}
EXPECT_EQ
(
count
,
0
)
<<
"There are "
<<
count
<<
" different elements."
;
}
TEST
(
inference
,
recognize_digits
)
{
if
(
FLAGS_dirname
.
empty
())
{
LOG
(
FATAL
)
<<
"Usage: ./example --dirname=path/to/your/model"
;
...
...
@@ -70,12 +111,10 @@ TEST(inference, recognize_digits) {
// In unittests, this is done in paddle/testing/paddle_gtest_main.cc
paddle
::
framework
::
LoDTensor
input
;
srand
(
time
(
0
));
float
*
input_ptr
=
input
.
mutable_data
<
float
>
({
1
,
28
,
28
},
paddle
::
platform
::
CPUPlace
());
for
(
int
i
=
0
;
i
<
784
;
++
i
)
{
input_ptr
[
i
]
=
rand
()
/
(
static_cast
<
float
>
(
RAND_MAX
));
}
// Use normilized image pixels as input data,
// which should be in the range [-1.0, 1.0].
SetupTensor
<
float
>
(
input
,
{
1
,
28
,
28
},
static_cast
<
float
>
(
-
1
),
static_cast
<
float
>
(
1
));
std
::
vector
<
paddle
::
framework
::
LoDTensor
*>
cpu_feeds
;
cpu_feeds
.
push_back
(
&
input
);
...
...
@@ -98,16 +137,6 @@ TEST(inference, recognize_digits) {
dirname
,
cpu_feeds
,
cpu_fetchs2
);
LOG
(
INFO
)
<<
output2
.
dims
();
EXPECT_EQ
(
output1
.
dims
(),
output2
.
dims
());
EXPECT_EQ
(
output1
.
numel
(),
output2
.
numel
());
float
err
=
1E-3
;
int
count
=
0
;
for
(
int64_t
i
=
0
;
i
<
output1
.
numel
();
++
i
)
{
if
(
fabs
(
output1
.
data
<
float
>
()[
i
]
-
output2
.
data
<
float
>
()[
i
])
>
err
)
{
count
++
;
}
}
EXPECT_EQ
(
count
,
0
)
<<
"There are "
<<
count
<<
" different elements."
;
CheckError
<
float
>
(
output1
,
output2
);
#endif
}
python/paddle/v2/fluid/tests/book/test_recognize_digits.py
浏览文件 @
165450ff
...
...
@@ -166,7 +166,9 @@ def infer(use_cuda, save_dirname=None):
fetch_targets
]
=
fluid
.
io
.
load_inference_model
(
save_dirname
,
exe
)
# The input's dimension of conv should be 4-D or 5-D.
tensor_img
=
numpy
.
random
.
rand
(
1
,
1
,
28
,
28
).
astype
(
"float32"
)
# Use normilized image pixels as input data, which should be in the range [-1.0, 1.0].
tensor_img
=
numpy
.
random
.
uniform
(
-
1.0
,
1.0
,
[
1
,
1
,
28
,
28
]).
astype
(
"float32"
)
# Construct feed as a dictionary of {feed_target_name: feed_target_data}
# and results will contain a list of data corresponding to fetch_targets.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录