Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
51b7bbee
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
51b7bbee
编写于
10月 22, 2020
作者:
L
LielinJiang
提交者:
GitHub
10月 22, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[cherry-pick] Update hapi predict interface (#28186)
* update hapi predict interface
上级
f27d1bee
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
52 addition
and
29 deletion
+52
-29
python/paddle/fluid/tests/unittests/test_rnn_decode_api.py
python/paddle/fluid/tests/unittests/test_rnn_decode_api.py
+1
-1
python/paddle/hapi/model.py
python/paddle/hapi/model.py
+25
-19
python/paddle/tests/test_model.py
python/paddle/tests/test_model.py
+15
-4
python/paddle/tests/test_pretrained_model.py
python/paddle/tests/test_pretrained_model.py
+2
-2
python/paddle/tests/test_transforms.py
python/paddle/tests/test_transforms.py
+6
-0
python/paddle/tests/test_vision_models.py
python/paddle/tests/test_vision_models.py
+2
-2
python/paddle/vision/transforms/transforms.py
python/paddle/vision/transforms/transforms.py
+1
-1
未找到文件。
python/paddle/fluid/tests/unittests/test_rnn_decode_api.py
浏览文件 @
51b7bbee
...
@@ -628,7 +628,7 @@ class ModuleApiTest(unittest.TestCase):
...
@@ -628,7 +628,7 @@ class ModuleApiTest(unittest.TestCase):
model
.
prepare
()
model
.
prepare
()
if
self
.
param_states
:
if
self
.
param_states
:
model
.
load
(
self
.
param_states
,
optim_state
=
None
)
model
.
load
(
self
.
param_states
,
optim_state
=
None
)
return
model
.
tes
t_batch
(
self
.
inputs
)
return
model
.
predic
t_batch
(
self
.
inputs
)
def
check_output_with_place
(
self
,
place
,
mode
=
"test"
):
def
check_output_with_place
(
self
,
place
,
mode
=
"test"
):
dygraph_output
=
self
.
_calc_output
(
place
,
mode
,
dygraph
=
True
)
dygraph_output
=
self
.
_calc_output
(
place
,
mode
,
dygraph
=
True
)
...
...
python/paddle/hapi/model.py
浏览文件 @
51b7bbee
...
@@ -261,7 +261,7 @@ class StaticGraphAdapter(object):
...
@@ -261,7 +261,7 @@ class StaticGraphAdapter(object):
self
.
mode
=
'eval'
self
.
mode
=
'eval'
return
self
.
_run
(
inputs
,
labels
)
return
self
.
_run
(
inputs
,
labels
)
def
tes
t_batch
(
self
,
inputs
):
def
predic
t_batch
(
self
,
inputs
):
self
.
mode
=
'test'
self
.
mode
=
'test'
return
self
.
_run
(
inputs
,
None
)
return
self
.
_run
(
inputs
,
None
)
...
@@ -723,7 +723,7 @@ class DynamicGraphAdapter(object):
...
@@ -723,7 +723,7 @@ class DynamicGraphAdapter(object):
else
:
else
:
return
metrics
return
metrics
def
tes
t_batch
(
self
,
inputs
):
def
predic
t_batch
(
self
,
inputs
):
self
.
model
.
network
.
eval
()
self
.
model
.
network
.
eval
()
self
.
mode
=
'test'
self
.
mode
=
'test'
inputs
=
[
to_variable
(
x
)
for
x
in
to_list
(
inputs
)]
inputs
=
[
to_variable
(
x
)
for
x
in
to_list
(
inputs
)]
...
@@ -894,10 +894,13 @@ class Model(object):
...
@@ -894,10 +894,13 @@ class Model(object):
Run one training step on a batch of data.
Run one training step on a batch of data.
Args:
Args:
inputs (list): A list of numpy.ndarray, each is a batch of
inputs (numpy.ndarray|Tensor|list): Batch of input data. It could
input data.
be a numpy array or paddle.Tensor, or a list of arrays or
labels (list): A list of numpy.ndarray, each is a batch of
tensors (in case the model has multiple inputs).
input label. If has no labels, set None. Default is None.
labels (numpy.ndarray|Tensor|list): Batch of labels. It could be
a numpy array or paddle.Tensor, or a list of arrays or tensors
(in case the model has multiple labels). If has no labels,
set None. Default is None.
Returns:
Returns:
A list of scalar training loss if the model has no metrics,
A list of scalar training loss if the model has no metrics,
...
@@ -941,10 +944,13 @@ class Model(object):
...
@@ -941,10 +944,13 @@ class Model(object):
Run one evaluating step on a batch of data.
Run one evaluating step on a batch of data.
Args:
Args:
inputs (list): A list of numpy.ndarray, each is a batch of
inputs (numpy.ndarray|Tensor|list): Batch of input data. It could
input data.
be a numpy array or paddle.Tensor, or a list of arrays or
labels (list): A list of numpy.ndarray, each is a batch of
tensors (in case the model has multiple inputs).
input label. If has no labels, set None. Default is None.
labels (numpy.ndarray|Tensor|list): Batch of labels. It could be
a numpy array or paddle.Tensor, or a list of arrays or tensors
(in case the model has multiple labels). If has no labels,
set None. Default is None.
Returns:
Returns:
A list of scalar testing loss if the model has no metrics,
A list of scalar testing loss if the model has no metrics,
...
@@ -984,13 +990,14 @@ class Model(object):
...
@@ -984,13 +990,14 @@ class Model(object):
self
.
_update_inputs
()
self
.
_update_inputs
()
return
loss
return
loss
def
tes
t_batch
(
self
,
inputs
):
def
predic
t_batch
(
self
,
inputs
):
"""
"""
Run one
tes
ting step on a batch of data.
Run one
predic
ting step on a batch of data.
Args:
Args:
inputs (list): A list of numpy.ndarray, each is a batch of
inputs (numpy.ndarray|Tensor|list): Batch of input data. It could
input data.
be a numpy array or paddle.Tensor, or a list of arrays or
tensors (in case the model has multiple inputs).
Returns:
Returns:
A list of numpy.ndarray of predictions, that is the outputs
A list of numpy.ndarray of predictions, that is the outputs
...
@@ -1019,10 +1026,10 @@ class Model(object):
...
@@ -1019,10 +1026,10 @@ class Model(object):
model = paddle.Model(net, input, label)
model = paddle.Model(net, input, label)
model.prepare()
model.prepare()
data = np.random.random(size=(4,784)).astype(np.float32)
data = np.random.random(size=(4,784)).astype(np.float32)
out = model.
tes
t_batch([data])
out = model.
predic
t_batch([data])
print(out)
print(out)
"""
"""
loss
=
self
.
_adapter
.
tes
t_batch
(
inputs
)
loss
=
self
.
_adapter
.
predic
t_batch
(
inputs
)
if
fluid
.
in_dygraph_mode
()
and
self
.
_input_shapes
is
None
:
if
fluid
.
in_dygraph_mode
()
and
self
.
_input_shapes
is
None
:
self
.
_update_inputs
()
self
.
_update_inputs
()
return
loss
return
loss
...
@@ -1847,10 +1854,9 @@ class Model(object):
...
@@ -1847,10 +1854,9 @@ class Model(object):
logs
[
k
]
=
v
logs
[
k
]
=
v
else
:
else
:
if
self
.
_inputs
is
not
None
:
if
self
.
_inputs
is
not
None
:
outs
=
getattr
(
self
,
outs
=
self
.
predict_batch
(
data
[:
len
(
self
.
_inputs
)])
mode
+
'_batch'
)(
data
[:
len
(
self
.
_inputs
)])
else
:
else
:
outs
=
getattr
(
self
,
mode
+
'_batch'
)
(
data
)
outs
=
self
.
predict_batch
(
data
)
outputs
.
append
(
outs
)
outputs
.
append
(
outs
)
...
...
python/paddle/tests/test_model.py
浏览文件 @
51b7bbee
...
@@ -284,6 +284,17 @@ class TestModel(unittest.TestCase):
...
@@ -284,6 +284,17 @@ class TestModel(unittest.TestCase):
fluid
.
disable_dygraph
()
if
dynamic
else
None
fluid
.
disable_dygraph
()
if
dynamic
else
None
def
test_predict_without_inputs
(
self
):
fluid
.
enable_dygraph
(
self
.
device
)
model
=
Model
(
LeNet
())
model
.
prepare
()
model
.
load
(
self
.
weight_path
)
model
.
_inputs
=
None
output
=
model
.
predict
(
self
.
test_dataset
,
batch_size
=
64
,
stack_outputs
=
True
)
np
.
testing
.
assert_equal
(
output
[
0
].
shape
[
0
],
len
(
self
.
test_dataset
))
fluid
.
disable_dygraph
()
class
MyModel
(
paddle
.
nn
.
Layer
):
class
MyModel
(
paddle
.
nn
.
Layer
):
def
__init__
(
self
):
def
__init__
(
self
):
...
@@ -370,7 +381,7 @@ class TestModelFunction(unittest.TestCase):
...
@@ -370,7 +381,7 @@ class TestModelFunction(unittest.TestCase):
inputs
=
[
InputSpec
([
None
,
dim
],
'float32'
,
'x'
)]
inputs
=
[
InputSpec
([
None
,
dim
],
'float32'
,
'x'
)]
model
=
Model
(
net
,
inputs
)
model
=
Model
(
net
,
inputs
)
model
.
prepare
()
model
.
prepare
()
out
,
=
model
.
tes
t_batch
([
data
])
out
,
=
model
.
predic
t_batch
([
data
])
np
.
testing
.
assert_allclose
(
out
,
ref
,
rtol
=
1e-6
)
np
.
testing
.
assert_allclose
(
out
,
ref
,
rtol
=
1e-6
)
fluid
.
disable_dygraph
()
if
dynamic
else
None
fluid
.
disable_dygraph
()
if
dynamic
else
None
...
@@ -546,7 +557,7 @@ class TestModelFunction(unittest.TestCase):
...
@@ -546,7 +557,7 @@ class TestModelFunction(unittest.TestCase):
np
.
random
.
random
((
1
,
1
,
28
,
28
)),
dtype
=
np
.
float32
)
np
.
random
.
random
((
1
,
1
,
28
,
28
)),
dtype
=
np
.
float32
)
model
.
save
(
save_dir
,
training
=
False
)
model
.
save
(
save_dir
,
training
=
False
)
ori_results
=
model
.
tes
t_batch
(
tensor_img
)
ori_results
=
model
.
predic
t_batch
(
tensor_img
)
fluid
.
disable_dygraph
()
if
dynamic
else
None
fluid
.
disable_dygraph
()
if
dynamic
else
None
place
=
fluid
.
CPUPlace
()
if
not
fluid
.
is_compiled_with_cuda
(
place
=
fluid
.
CPUPlace
()
if
not
fluid
.
is_compiled_with_cuda
(
...
@@ -569,7 +580,7 @@ class TestModelFunction(unittest.TestCase):
...
@@ -569,7 +580,7 @@ class TestModelFunction(unittest.TestCase):
mnist_data
=
MnistDataset
(
mode
=
'train'
)
mnist_data
=
MnistDataset
(
mode
=
'train'
)
paddle
.
disable_static
()
paddle
.
disable_static
()
# without inputs
# without inputs
for
initial
in
[
"fit"
,
"train_batch"
,
"eval_batch"
,
"
tes
t_batch"
]:
for
initial
in
[
"fit"
,
"train_batch"
,
"eval_batch"
,
"
predic
t_batch"
]:
save_dir
=
tempfile
.
mkdtemp
()
save_dir
=
tempfile
.
mkdtemp
()
if
not
os
.
path
.
exists
(
save_dir
):
if
not
os
.
path
.
exists
(
save_dir
):
os
.
makedirs
(
save_dir
)
os
.
makedirs
(
save_dir
)
...
@@ -590,7 +601,7 @@ class TestModelFunction(unittest.TestCase):
...
@@ -590,7 +601,7 @@ class TestModelFunction(unittest.TestCase):
elif
initial
==
"eval_batch"
:
elif
initial
==
"eval_batch"
:
model
.
eval_batch
([
img
],
[
label
])
model
.
eval_batch
([
img
],
[
label
])
else
:
else
:
model
.
tes
t_batch
([
img
])
model
.
predic
t_batch
([
img
])
model
.
save
(
save_dir
,
training
=
False
)
model
.
save
(
save_dir
,
training
=
False
)
shutil
.
rmtree
(
save_dir
)
shutil
.
rmtree
(
save_dir
)
...
...
python/paddle/tests/test_pretrained_model.py
浏览文件 @
51b7bbee
...
@@ -40,10 +40,10 @@ class TestPretrainedModel(unittest.TestCase):
...
@@ -40,10 +40,10 @@ class TestPretrainedModel(unittest.TestCase):
if
dygraph
:
if
dygraph
:
model
.
save
(
path
)
model
.
save
(
path
)
res
[
'dygraph'
]
=
model
.
tes
t_batch
(
x
)
res
[
'dygraph'
]
=
model
.
predic
t_batch
(
x
)
else
:
else
:
model
.
load
(
path
)
model
.
load
(
path
)
res
[
'static'
]
=
model
.
tes
t_batch
(
x
)
res
[
'static'
]
=
model
.
predic
t_batch
(
x
)
if
not
dygraph
:
if
not
dygraph
:
paddle
.
disable_static
()
paddle
.
disable_static
()
...
...
python/paddle/tests/test_transforms.py
浏览文件 @
51b7bbee
...
@@ -205,6 +205,12 @@ class TestTransformsCV2(unittest.TestCase):
...
@@ -205,6 +205,12 @@ class TestTransformsCV2(unittest.TestCase):
assert
isinstance
(
tensor
,
paddle
.
Tensor
)
assert
isinstance
(
tensor
,
paddle
.
Tensor
)
np
.
testing
.
assert_equal
(
tensor
.
shape
,
(
3
,
50
,
100
))
np
.
testing
.
assert_equal
(
tensor
.
shape
,
(
3
,
50
,
100
))
def
test_keys
(
self
):
fake_img1
=
self
.
create_image
((
200
,
150
,
3
))
fake_img2
=
self
.
create_image
((
200
,
150
,
3
))
trans_pad
=
transforms
.
Pad
(
10
,
keys
=
(
"image"
,
))
fake_img_padded
=
trans_pad
((
fake_img1
,
fake_img2
))
def
test_exception
(
self
):
def
test_exception
(
self
):
trans
=
transforms
.
Compose
([
transforms
.
Resize
(
-
1
)])
trans
=
transforms
.
Compose
([
transforms
.
Resize
(
-
1
)])
...
...
python/paddle/tests/test_vision_models.py
浏览文件 @
51b7bbee
...
@@ -33,7 +33,7 @@ class TestVisonModels(unittest.TestCase):
...
@@ -33,7 +33,7 @@ class TestVisonModels(unittest.TestCase):
model
=
paddle
.
Model
(
net
,
input
)
model
=
paddle
.
Model
(
net
,
input
)
model
.
prepare
()
model
.
prepare
()
model
.
tes
t_batch
(
x
)
model
.
predic
t_batch
(
x
)
def
test_mobilenetv2_pretrained
(
self
):
def
test_mobilenetv2_pretrained
(
self
):
self
.
models_infer
(
'mobilenet_v2'
,
pretrained
=
False
)
self
.
models_infer
(
'mobilenet_v2'
,
pretrained
=
False
)
...
@@ -77,7 +77,7 @@ class TestVisonModels(unittest.TestCase):
...
@@ -77,7 +77,7 @@ class TestVisonModels(unittest.TestCase):
lenet
.
prepare
()
lenet
.
prepare
()
x
=
np
.
array
(
np
.
random
.
random
((
2
,
1
,
28
,
28
)),
dtype
=
np
.
float32
)
x
=
np
.
array
(
np
.
random
.
random
((
2
,
1
,
28
,
28
)),
dtype
=
np
.
float32
)
lenet
.
tes
t_batch
(
x
)
lenet
.
predic
t_batch
(
x
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
python/paddle/vision/transforms/transforms.py
浏览文件 @
51b7bbee
...
@@ -272,7 +272,7 @@ class BaseTransform(object):
...
@@ -272,7 +272,7 @@ class BaseTransform(object):
else
:
else
:
outputs
.
append
(
apply_func
(
inputs
[
i
]))
outputs
.
append
(
apply_func
(
inputs
[
i
]))
if
len
(
inputs
)
>
len
(
self
.
keys
):
if
len
(
inputs
)
>
len
(
self
.
keys
):
outputs
.
extend
(
input
[
len
(
self
.
keys
):])
outputs
.
extend
(
input
s
[
len
(
self
.
keys
):])
if
len
(
outputs
)
==
1
:
if
len
(
outputs
)
==
1
:
outputs
=
outputs
[
0
]
outputs
=
outputs
[
0
]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录