Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
hapi
提交
28ee9ac1
H
hapi
项目概览
PaddlePaddle
/
hapi
通知
11
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
7
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hapi
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
7
合并请求
7
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
28ee9ac1
编写于
4月 28, 2020
作者:
L
LielinJiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix test model
上级
46158530
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
29 addition
and
46 deletion
+29
-46
hapi/tests/test_model.py
hapi/tests/test_model.py
+29
-46
未找到文件。
hapi/tests/test_model.py
浏览文件 @
28ee9ac1
...
...
@@ -20,6 +20,8 @@ import unittest
import
os
import
cv2
import
numpy
as
np
import
tempfile
import
shutil
import
paddle
from
paddle
import
fluid
...
...
@@ -36,14 +38,6 @@ from hapi.download import get_weights_path_from_url
class
LeNetDygraph
(
fluid
.
dygraph
.
Layer
):
"""LeNet model from
`"LeCun Y, Bottou L, Bengio Y, et al. Gradient-based learning applied to document recognition[J]. Proceedings of the IEEE, 1998, 86(11): 2278-2324.`_
Args:
num_classes (int): output dim of last fc layer. If num_classes <=0, last fc layer
will not be defined. Default: 10.
classifier_activation (str): activation for the last fc layer. Default: 'softmax'.
"""
def
__init__
(
self
,
num_classes
=
10
,
classifier_activation
=
'softmax'
):
super
(
LeNetDygraph
,
self
).
__init__
()
...
...
@@ -137,6 +131,15 @@ class TestEvaluatePredict(unittest.TestCase):
low_level_lenet_dygraph_train
(
self
.
lenet_dygraph
,
train_dataloader
)
self
.
acc1
=
low_level_dynamic_evaluate
(
self
.
lenet_dygraph
,
val_dataloader
)
self
.
save_dir
=
tempfile
.
mkdtemp
()
self
.
weight_path
=
os
.
path
.
join
(
self
.
save_dir
,
'lenet'
)
fluid
.
dygraph
.
save_dygraph
(
self
.
lenet_dygraph
.
state_dict
(),
self
.
weight_path
)
fluid
.
disable_dygraph
()
def
tearDown
(
self
):
shutil
.
rmtree
(
self
.
save_dir
)
def
evaluate
(
self
,
dynamic
):
fluid
.
enable_dygraph
(
self
.
device
)
if
dynamic
else
None
...
...
@@ -144,67 +147,44 @@ class TestEvaluatePredict(unittest.TestCase):
inputs
=
[
Input
([
-
1
,
1
,
28
,
28
],
'float32'
,
name
=
'image'
)]
labels
=
[
Input
([
None
,
1
],
'int64'
,
name
=
'label'
)]
if
fluid
.
in_dygraph_mode
():
feed_list
=
None
else
:
feed_list
=
[
x
.
forward
()
for
x
in
inputs
+
labels
]
self
.
train_dataloader
=
fluid
.
io
.
DataLoader
(
self
.
train_dataset
,
places
=
self
.
device
,
batch_size
=
64
,
feed_list
=
feed_list
)
self
.
val_dataloader
=
fluid
.
io
.
DataLoader
(
val_dataloader
=
fluid
.
io
.
DataLoader
(
self
.
val_dataset
,
places
=
self
.
device
,
batch_size
=
64
,
feed_list
=
feed_list
)
self
.
test_dataloader
=
fluid
.
io
.
DataLoader
(
self
.
test_dataset
,
places
=
self
.
device
,
batch_size
=
64
,
feed_list
=
feed_list
)
return_list
=
True
)
model
=
LeNet
()
model
.
load_dict
(
self
.
lenet_dygraph
.
state_dict
())
model
.
load
(
self
.
weight_path
)
model
.
prepare
(
metrics
=
Accuracy
(),
inputs
=
inputs
,
labels
=
labels
)
result
=
model
.
evaluate
(
self
.
val_dataloader
)
result
=
model
.
evaluate
(
val_dataloader
)
np
.
testing
.
assert_allclose
(
result
[
'acc'
],
self
.
acc1
)
if
fluid
.
in_dygraph_mode
():
fluid
.
disable_dygraph
()
def
predict
(
self
,
dynamic
):
fluid
.
enable_dygraph
(
self
.
device
)
if
dynamic
else
None
inputs
=
[
Input
([
-
1
,
1
,
28
,
28
],
'float32'
,
name
=
'image'
)]
labels
=
[
Input
([
None
,
1
],
'int64'
,
name
=
'label'
)]
if
fluid
.
in_dygraph_mode
():
feed_list
=
None
else
:
feed_list
=
[
x
.
forward
()
for
x
in
inputs
+
labels
]
self
.
train_dataloader
=
fluid
.
io
.
DataLoader
(
self
.
train_dataset
,
places
=
self
.
device
,
batch_size
=
64
,
feed_list
=
feed_list
)
self
.
val_dataloader
=
fluid
.
io
.
DataLoader
(
self
.
val_dataset
,
places
=
self
.
device
,
batch_size
=
64
,
feed_list
=
feed_list
)
self
.
test_dataloader
=
fluid
.
io
.
DataLoader
(
test_dataloader
=
fluid
.
io
.
DataLoader
(
self
.
test_dataset
,
places
=
self
.
device
,
batch_size
=
64
,
feed_list
=
feed_list
)
return_list
=
True
)
model
=
LeNet
()
model
.
load_dict
(
self
.
lenet_dygraph
.
state_dict
())
model
.
load
(
self
.
weight_path
)
model
.
prepare
(
metrics
=
Accuracy
(),
inputs
=
inputs
,
labels
=
labels
)
output
=
model
.
predict
(
self
.
test_dataloader
,
stack_outputs
=
True
)
output
=
model
.
predict
(
test_dataloader
,
stack_outputs
=
True
)
np
.
testing
.
assert_equal
(
output
[
0
].
shape
[
0
],
len
(
self
.
test_dataset
))
...
...
@@ -212,6 +192,9 @@ class TestEvaluatePredict(unittest.TestCase):
np
.
testing
.
assert_allclose
(
acc
,
self
.
acc1
)
if
fluid
.
in_dygraph_mode
():
fluid
.
disable_dygraph
()
def
test_evaluate_dygraph
(
self
):
self
.
evaluate
(
True
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录