Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
38141036
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
38141036
编写于
11月 18, 2021
作者:
L
LielinJiang
提交者:
GitHub
11月 18, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
polish unittest of test_pretrained_model (#37307)
* fix cache * Fix unittest
上级
a6e9ff85
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
59 addition
and
31 deletion
+59
-31
python/paddle/tests/test_model.py
python/paddle/tests/test_model.py
+48
-26
python/paddle/tests/test_pretrained_model.py
python/paddle/tests/test_pretrained_model.py
+11
-5
未找到文件。
python/paddle/tests/test_model.py
浏览文件 @
38141036
...
...
@@ -199,7 +199,9 @@ class TestModel(unittest.TestCase):
cls
.
inputs
=
[
InputSpec
([
-
1
,
1
,
28
,
28
],
'float32'
,
'image'
)]
cls
.
labels
=
[
InputSpec
([
None
,
1
],
'int64'
,
'label'
)]
cls
.
save_dir
=
tempfile
.
mkdtemp
()
cls
.
save_dir
=
os
.
path
.
join
(
tempfile
.
mkdtemp
(),
'.cache_test_model'
)
if
not
os
.
path
.
exists
(
cls
.
save_dir
):
os
.
makedirs
(
cls
.
save_dir
)
cls
.
weight_path
=
os
.
path
.
join
(
cls
.
save_dir
,
'lenet'
)
fluid
.
dygraph
.
save_dygraph
(
dy_lenet
.
state_dict
(),
cls
.
weight_path
)
...
...
@@ -505,7 +507,9 @@ class TestModelFunction(unittest.TestCase):
fluid
.
disable_dygraph
()
if
dynamic
else
None
def
test_save_load
(
self
):
path
=
tempfile
.
mkdtemp
()
path
=
os
.
path
.
join
(
tempfile
.
mkdtemp
(),
'.cache_test_save_load'
)
if
not
os
.
path
.
exists
(
path
):
os
.
makedirs
(
path
)
for
dynamic
in
[
True
,
False
]:
device
=
paddle
.
set_device
(
'cpu'
)
fluid
.
enable_dygraph
(
device
)
if
dynamic
else
None
...
...
@@ -517,15 +521,19 @@ class TestModelFunction(unittest.TestCase):
model
=
Model
(
net
,
inputs
,
labels
)
model
.
prepare
(
optimizer
=
optim
,
loss
=
CrossEntropyLoss
(
reduction
=
"sum"
))
model
.
save
(
path
+
'/test'
)
model
.
load
(
path
+
'/test'
)
shutil
.
rmtree
(
path
)
model
.
save
(
path
)
model
.
load
(
path
)
fluid
.
disable_dygraph
()
if
dynamic
else
None
shutil
.
rmtree
(
path
)
def
test_dynamic_load
(
self
):
mnist_data
=
MnistDataset
(
mode
=
'train'
)
path
=
os
.
path
.
join
(
tempfile
.
mkdtemp
(),
'.cache_dynamic_load'
)
if
not
os
.
path
.
exists
(
path
):
os
.
makedirs
(
path
)
for
new_optimizer
in
[
True
,
False
]:
path
=
tempfile
.
mkdtemp
()
paddle
.
disable_static
()
net
=
LeNet
()
inputs
=
[
InputSpec
([
None
,
1
,
28
,
28
],
'float32'
,
'x'
)]
...
...
@@ -540,13 +548,16 @@ class TestModelFunction(unittest.TestCase):
model
.
prepare
(
optimizer
=
optim
,
loss
=
CrossEntropyLoss
(
reduction
=
"sum"
))
model
.
fit
(
mnist_data
,
batch_size
=
64
,
verbose
=
0
)
model
.
save
(
path
+
'/test'
)
model
.
load
(
path
+
'/test'
)
shutil
.
rmtree
(
path
)
model
.
save
(
path
)
model
.
load
(
path
)
paddle
.
enable_static
()
shutil
.
rmtree
(
path
)
def
test_dynamic_save_static_load
(
self
):
path
=
tempfile
.
mkdtemp
()
path
=
os
.
path
.
join
(
tempfile
.
mkdtemp
(),
'.cache_dynamic_save_static_load'
)
if
not
os
.
path
.
exists
(
path
):
os
.
makedirs
(
path
)
# dynamic saving
device
=
paddle
.
set_device
(
'cpu'
)
fluid
.
enable_dygraph
(
device
)
...
...
@@ -554,7 +565,7 @@ class TestModelFunction(unittest.TestCase):
optim
=
fluid
.
optimizer
.
SGD
(
learning_rate
=
0.001
,
parameter_list
=
model
.
parameters
())
model
.
prepare
(
optimizer
=
optim
,
loss
=
CrossEntropyLoss
(
reduction
=
"sum"
))
model
.
save
(
path
+
'/test'
)
model
.
save
(
path
)
fluid
.
disable_dygraph
()
inputs
=
[
InputSpec
([
None
,
20
],
'float32'
,
'x'
)]
...
...
@@ -563,12 +574,14 @@ class TestModelFunction(unittest.TestCase):
optim
=
fluid
.
optimizer
.
SGD
(
learning_rate
=
0.001
,
parameter_list
=
model
.
parameters
())
model
.
prepare
(
optimizer
=
optim
,
loss
=
CrossEntropyLoss
(
reduction
=
"sum"
))
model
.
load
(
path
+
'/test'
)
model
.
load
(
path
)
shutil
.
rmtree
(
path
)
def
test_static_save_dynamic_load
(
self
):
path
=
tempfile
.
mkdtemp
()
path
=
os
.
path
.
join
(
tempfile
.
mkdtemp
(),
'.cache_test_static_save_dynamic_load'
)
if
not
os
.
path
.
exists
(
path
):
os
.
makedirs
(
path
)
net
=
MyModel
()
inputs
=
[
InputSpec
([
None
,
20
],
'float32'
,
'x'
)]
labels
=
[
InputSpec
([
None
,
1
],
'int64'
,
'label'
)]
...
...
@@ -576,7 +589,7 @@ class TestModelFunction(unittest.TestCase):
parameter_list
=
net
.
parameters
())
model
=
Model
(
net
,
inputs
,
labels
)
model
.
prepare
(
optimizer
=
optim
,
loss
=
CrossEntropyLoss
(
reduction
=
"sum"
))
model
.
save
(
path
+
'/test'
)
model
.
save
(
path
)
device
=
paddle
.
set_device
(
'cpu'
)
fluid
.
enable_dygraph
(
device
)
#if dynamic else None
...
...
@@ -588,7 +601,7 @@ class TestModelFunction(unittest.TestCase):
parameter_list
=
net
.
parameters
())
model
=
Model
(
net
,
inputs
,
labels
)
model
.
prepare
(
optimizer
=
optim
,
loss
=
CrossEntropyLoss
(
reduction
=
"sum"
))
model
.
load
(
path
+
'/test'
)
model
.
load
(
path
)
shutil
.
rmtree
(
path
)
fluid
.
disable_dygraph
()
...
...
@@ -722,6 +735,12 @@ class TestModelFunction(unittest.TestCase):
def
test_export_deploy_model
(
self
):
self
.
set_seed
()
np
.
random
.
seed
(
201
)
save_dir
=
os
.
path
.
join
(
tempfile
.
mkdtemp
(),
'.cache_test_export_deploy_model'
)
if
not
os
.
path
.
exists
(
save_dir
):
os
.
makedirs
(
save_dir
)
for
dynamic
in
[
True
,
False
]:
paddle
.
disable_static
()
if
dynamic
else
None
prog_translator
=
ProgramTranslator
()
...
...
@@ -730,9 +749,7 @@ class TestModelFunction(unittest.TestCase):
inputs
=
[
InputSpec
([
None
,
1
,
28
,
28
],
'float32'
,
'x'
)]
model
=
Model
(
net
,
inputs
)
model
.
prepare
()
save_dir
=
tempfile
.
mkdtemp
()
if
not
os
.
path
.
exists
(
save_dir
):
os
.
makedirs
(
save_dir
)
tensor_img
=
np
.
array
(
np
.
random
.
random
((
1
,
1
,
28
,
28
)),
dtype
=
np
.
float32
)
...
...
@@ -753,19 +770,22 @@ class TestModelFunction(unittest.TestCase):
fetch_list
=
fetch_targets
)
np
.
testing
.
assert_allclose
(
results
,
ori_results
,
rtol
=
1e-5
,
atol
=
1e-7
)
shutil
.
rmtree
(
save_dir
)
paddle
.
enable_static
()
shutil
.
rmtree
(
save_dir
)
def
test_dygraph_export_deploy_model_about_inputs
(
self
):
self
.
set_seed
()
np
.
random
.
seed
(
201
)
mnist_data
=
MnistDataset
(
mode
=
'train'
)
paddle
.
disable_static
()
# without inputs
save_dir
=
os
.
path
.
join
(
tempfile
.
mkdtemp
(),
'.cache_test_dygraph_export_deploy'
)
if
not
os
.
path
.
exists
(
save_dir
):
os
.
makedirs
(
save_dir
)
for
initial
in
[
"fit"
,
"train_batch"
,
"eval_batch"
,
"predict_batch"
]:
save_dir
=
tempfile
.
mkdtemp
()
if
not
os
.
path
.
exists
(
save_dir
):
os
.
makedirs
(
save_dir
)
net
=
LeNet
()
model
=
Model
(
net
)
optim
=
fluid
.
optimizer
.
Adam
(
...
...
@@ -786,9 +806,10 @@ class TestModelFunction(unittest.TestCase):
model
.
predict_batch
([
img
])
model
.
save
(
save_dir
,
training
=
False
)
shutil
.
rmtree
(
save_dir
)
shutil
.
rmtree
(
save_dir
)
# with inputs, and the type of inputs is InputSpec
save_dir
=
tempfile
.
mkdtemp
()
save_dir
=
os
.
path
.
join
(
tempfile
.
mkdtemp
(),
'.cache_test_dygraph_export_deploy_2'
)
if
not
os
.
path
.
exists
(
save_dir
):
os
.
makedirs
(
save_dir
)
net
=
LeNet
()
...
...
@@ -988,13 +1009,14 @@ class TestRaiseError(unittest.TestCase):
def
test_save_infer_model_without_inputs_and_run_in_dygraph
(
self
):
paddle
.
disable_static
()
net
=
MyModel
()
save_dir
=
tempfile
.
mkdtemp
(
)
save_dir
=
os
.
path
.
join
(
tempfile
.
mkdtemp
(),
'.cache_test_save_infer'
)
if
not
os
.
path
.
exists
(
save_dir
):
os
.
makedirs
(
save_dir
)
with
self
.
assertRaises
(
RuntimeError
):
model
=
Model
(
net
)
model
.
save
(
save_dir
,
training
=
False
)
paddle
.
enable_static
()
shutil
.
rmtree
(
save_dir
)
def
test_save_infer_model_without_file_prefix
(
self
):
paddle
.
enable_static
()
...
...
python/paddle/tests/test_pretrained_model.py
浏览文件 @
38141036
...
...
@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import
os
import
unittest
import
tempfile
import
shutil
...
...
@@ -26,7 +27,9 @@ import paddle.vision.models as models
# when used pretrained model
class
TestPretrainedModel
(
unittest
.
TestCase
):
def
infer
(
self
,
arch
):
path
=
tempfile
.
mkdtemp
()
path
=
os
.
path
.
join
(
tempfile
.
mkdtemp
(),
'.cache_test_pretrained_model'
)
if
not
os
.
path
.
exists
(
path
):
os
.
makedirs
(
path
)
x
=
np
.
array
(
np
.
random
.
random
((
2
,
3
,
224
,
224
)),
dtype
=
np
.
float32
)
res
=
{}
for
dygraph
in
[
True
,
False
]:
...
...
@@ -52,11 +55,14 @@ class TestPretrainedModel(unittest.TestCase):
np
.
testing
.
assert_allclose
(
res
[
'dygraph'
],
res
[
'static'
])
def
test_models
(
self
):
# TODO (LielinJiang): when model file cache is ok. add following test back
# 'resnet18', 'vgg16', 'alexnet', 'resnext50_32x4d', 'inception_v3',
# 'densenet121', 'googlenet', 'wide_resnet50_2', 'wide_resnet101_2'
arches
=
[
'mobilenet_v1'
,
'mobilenet_v2'
,
'resnet18'
,
'vgg16'
,
'alexnet'
,
'
resnext50_32x4d'
,
'inception_v3'
,
'densenet121'
,
'squeezenet1_0
'
,
'squeezenet1_
1'
,
'googlenet'
,
'shufflenet_v2_x0_25
'
,
'shufflenet_v2_
swish'
,
'wide_resnet50_2'
,
'wide_resnet101_2'
'mobilenet_v1'
,
'
mobilenet_v2
'
,
'squeezenet1_
0
'
,
'shufflenet_v2_
x0_25'
,
]
for
arch
in
arches
:
self
.
infer
(
arch
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录