未验证 提交 b5f8784c 编写于 作者: Q qingqing01 提交者: GitHub

Refine Model of high level API (#25559)

* Refine Model

1. Take the network (instance of Layer) as the input of Model.
2. Refine set_dict/load_dict of Layer.
3. Refine Input interface, so update code sample about Input
上级 4152d399
......@@ -94,7 +94,9 @@ def save_dygraph(state_dict, model_path):
pickle.dump(model_dict, f, protocol=2)
@dygraph_only
# TODO(qingqing01): remove dygraph_only to support loading static model.
# maybe need to unify the loading interface after 2.0 API is ready.
#@dygraph_only
def load_dygraph(model_path, keep_name_table=False):
'''
:api_attr: imperative
......
......@@ -16,9 +16,12 @@ import collections
import contextlib
import sys
import numpy as np
import collections
import six
import re
import copy
import weakref
import warnings
from . import parallel_helper
from .. import unique_name
from paddle.fluid import core
......@@ -26,9 +29,6 @@ from .layer_object_helper import LayerObjectHelper
from .base import program_desc_tracing_guard, param_guard
from paddle.fluid import framework
from ..param_attr import ParamAttr
import copy
import weakref
import warnings
__all__ = ['Layer']
......
......@@ -66,7 +66,6 @@ CONTROL_DEP_VAR_PREFIX = core.kControlDepVarName()
_dygraph_tracer_ = None
_dygraph_current_expected_place_ = None
_current_device = None
global_prog_seed = 0
......
......@@ -16,7 +16,10 @@ from . import logger
from . import progressbar
from . import callbacks
from . import download
from . import model
from .model import *
from . import metrics
from . import loss
from . import datasets
......@@ -24,6 +27,11 @@ from . import distributed
from . import vision
from . import text
from . import device
from .device import *
from .dygraph_layer_patch import monkey_patch_layer
logger.setup_logger()
__all__ = [
......@@ -35,6 +43,6 @@ __all__ = [
'loss',
'vision',
'text',
]
] + model.__all__ + device.__all__
__all__ += model.__all__
monkey_patch_layer()
......@@ -291,30 +291,22 @@ class ProgBarLogger(Callback):
Examples:
.. code-block:: python
import numpy as np
from paddle import fluid
from paddle.incubate.hapi.metrics import Accuracy
from paddle.incubate.hapi.loss import CrossEntropy
from paddle.incubate.hapi.datasets import MNIST
from paddle.incubate.hapi.vision.models import LeNet
from paddle.incubate.hapi.callbacks import ProgBarLogger
from paddle.incubate.hapi.model import Input, set_device
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
inputs = [Input([-1, 1, 28, 28], 'float32', name='image')]
labels = [Input([None, 1], 'int64', name='label')]
inputs = [hapi.Input('image', [-1, 1, 28, 28], 'float32')]
labels = [hapi.Input('label', [None, 1], 'int64')]
train_dataset = MNIST(mode='train')
train_dataset = hapi.datasets.MNIST(mode='train')
model = LeNet()
model = hapi.Model(hapi.vision.LeNet(), inputs, labels)
optim = fluid.optimizer.Adam(0.001)
model.prepare(optimizer=optim,
loss_function=CrossEntropy(),
metrics=Accuracy(),
inputs=inputs,
labels=labels)
loss_function=hapi.loss.CrossEntropy(),
metrics=hapi.metrics.Accuracy())
callback = ProgBarLogger(log_freq=10)
callback = hapi.callbacks.ProgBarLogger(log_freq=10)
model.fit(train_dataset, batch_size=64, callbacks=callback)
"""
......@@ -433,31 +425,22 @@ class ModelCheckpoint(Callback):
Examples:
.. code-block:: python
import numpy as np
from paddle import fluid
from paddle.incubate.hapi.metrics import Accuracy
from paddle.incubate.hapi.loss import CrossEntropy
from paddle.incubate.hapi.datasets import MNIST
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
from paddle.incubate.hapi.vision.models import LeNet
from paddle.incubate.hapi.callbacks import ModelCheckpoint
from paddle.incubate.hapi.model import Input, set_device
inputs = [hapi.Input('image', [-1, 1, 28, 28], 'float32')]
labels = [hapi.Input('label', [None, 1], 'int64')]
inputs = [Input([-1, 1, 28, 28], 'float32', name='image')]
labels = [Input([None, 1], 'int64', name='label')]
train_dataset = hapi.datasets.MNIST(mode='train')
train_dataset = MNIST(mode='train')
model = LeNet()
model = hapi.Model(hapi.vision.LeNet(), inputs, labels)
optim = fluid.optimizer.Adam(0.001)
model.prepare(optimizer=optim,
loss_function=CrossEntropy(),
metrics=Accuracy(),
inputs=inputs,
labels=labels)
loss_function=hapi.loss.CrossEntropy(),
metrics=hapi.metrics.Accuracy())
callback = ModelCheckpoint(save_dir='./temp')
callback = hapi.callbacks.ModelCheckpoint(save_dir='./temp')
model.fit(train_dataset, batch_size=64, callbacks=callback)
"""
......
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import six
import paddle.fluid as fluid
from paddle.fluid.dygraph.parallel import ParallelEnv
__all__ = ['set_device', ]
# TODO(qingqing01): remove or refine _global_device, set_device and get_device
# after core framework supporting these function.
_global_device = None
def set_device(device):
"""
Args:
device (str): specify device type, 'cpu' or 'gpu'.
Returns:
fluid.CUDAPlace or fluid.CPUPlace: Created GPU or CPU place.
Examples:
.. code-block:: python
import paddle.incubate.hapi as hapi
input = hapi.set_device('gpu')
"""
assert isinstance(device, six.string_types) and device.lower() in ['cpu', 'gpu'], \
"Expected device in ['cpu', 'gpu'], but got {}".format(device)
device = fluid.CUDAPlace(ParallelEnv().dev_id) \
if device.lower() == 'gpu' and fluid.is_compiled_with_cuda() \
else fluid.CPUPlace()
global _global_device
_global_device = device
return device
def _get_device():
"""
Return global device.
"""
if _global_device is not None:
device = _global_device
else:
if fluid.is_compiled_with_cuda():
device = fluid.CUDAPlace(ParallelEnv().dev_id)
else:
device = fluid.CPUPlace()
return device
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import warnings
import paddle.fluid as fluid
from paddle.fluid.framework import in_dygraph_mode
from .device import _get_device
def monkey_patch_layer():
def load_dict(self,
stat_dict,
include_sublayers=True,
use_structured_name=True):
'''
Set parameters from stat_dict. All the parameters will be reset by the
tensor in the stat_dict
This api will be Deprecated. Please use set_dict
Parameters:
state_dict(dict) : Dict contains all the parameters
include_sublayers(bool, optional) : If true, also include the
parameters from sublayers. Default: True
use_structured_name(bool, optional) : If true, use structured name
as key, otherwise, use parameter name as key. Default: True
Returns:
None
Examples:
.. code-block:: python
import paddle.fluid as fluid
with fluid.dygraph.guard():
emb = fluid.dygraph.Embedding([10, 10])
state_dict = emb.state_dict()
fluid.save_dygraph( state_dict, "paddle_dy")
para_state_dict, _ = fluid.load_dygraph( "paddle_dy")
emb.load_dict( para_state_dict )
'''
def _check_match(key, param):
state = stat_dict.get(key, None)
if state is None:
raise ValueError(
"{} is not found in the providing file.".format(key))
if list(state.shape) != list(param.shape):
raise ValueError(
"{} receives a shape {}, but the expected shape is {}.".
format(key, list(state.shape), list(param.shape)))
return param, state
matched_param_state = []
for key, param in self.state_dict().items():
key_name = key if use_structured_name else param.name
try:
match_res = _check_match(key_name, param)
except ValueError as err:
warnings.warn(("Skip loading for {}. ".format(key) + str(err)))
matched_param_state.append(match_res)
if in_dygraph_mode():
for param, state in matched_param_state:
param.set_value(state)
else:
def _set_var(var, ndarray):
t = fluid.global_scope().find_var(var.name).get_tensor()
p = t._place()
if p.is_cpu_place():
place = fluid.CPUPlace()
elif p.is_cuda_pinned_place():
place = fluid.CUDAPinnedPlace()
else:
p = fluid.core.Place()
p.set_place(t._place())
place = fluid.CUDAPlace(p.gpu_device_id())
t.set(ndarray, place)
executor = fluid.Executor(_get_device())._default_executor
# restore parameter states
fluid.core._create_loaded_parameter(
[param for param, state in matched_param_state],
fluid.global_scope(), executor)
for param, state in matched_param_state:
_set_var(param, state)
setattr(fluid.dygraph.Layer, 'load_dict', load_dict)
......@@ -86,16 +86,13 @@ class CrossEntropy(Loss):
Examples:
.. code-block:: python
from paddle.incubate.hapi.model import Input
from paddle.incubate.hapi.vision.models import LeNet
from paddle.incubate.hapi.loss import CrossEntropy
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
inputs = [Input([-1, 1, 28, 28], 'float32', name='image')]
labels = [Input([None, 1], 'int64', name='label')]
fluid.enable_dygraph()
model = LeNet()
loss = CrossEntropy()
model.prepare(loss_function=loss, inputs=inputs, labels=labels)
model = hapi.Model(hapi.vision.LeNet())
model.prepare(loss_function=hapi.loss.CrossEntropy())
"""
......@@ -123,16 +120,14 @@ class SoftmaxWithCrossEntropy(Loss):
Examples:
.. code-block:: python
from paddle.incubate.hapi.model import Input
from paddle.incubate.hapi.vision.models import LeNet
from paddle.incubate.hapi.loss import SoftmaxWithCrossEntropy
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
inputs = [Input([-1, 1, 28, 28], 'float32', name='image')]
labels = [Input([None, 1], 'int64', name='label')]
fluid.enable_dygraph()
model = LeNet(classifier_activation=None)
loss = SoftmaxWithCrossEntropy()
model.prepare(loss_function=loss, inputs=inputs, labels=labels)
model = hapi.Model(hapi.vision.LeNet(classifier_activation=None))
loss = hapi.loss.SoftmaxWithCrossEntropy()
model.prepare(loss_function=loss)
"""
def __init__(self, average=True):
......
......@@ -170,30 +170,20 @@ class Accuracy(Metric):
.. code-block:: python
from paddle import fluid
from paddle.incubate.hapi.metrics import Accuracy
from paddle.incubate.hapi.loss import CrossEntropy
from paddle.incubate.hapi.datasets import MNIST
from paddle.incubate.hapi.model import Input
from paddle.incubate.hapi.vision.models import LeNet
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
fluid.enable_dygraph()
train_dataset = MNIST(mode='train')
train_dataset = hapi.datasets.MNIST(mode='train')
model = LeNet()
model = hapi.Model(hapi.vision.LeNet())
optim = fluid.optimizer.Adam(
learning_rate=0.001, parameter_list=model.parameters())
inputs = [Input([-1, 1, 28, 28], 'float32', name='image')]
labels = [Input([None, 1], 'int64', name='label')]
model.prepare(
optim,
loss_function=CrossEntropy(average=False),
metrics=Accuracy(),
inputs=inputs,
labels=labels)
loss_function=hapi.loss.CrossEntropy(average=False),
metrics=hapi.metrics.Accuracy())
model.fit(train_dataset, batch_size=64)
......
......@@ -22,7 +22,7 @@ import contextlib
from paddle import fluid
from paddle.incubate.hapi.model import Model, Input, set_device
from paddle.incubate.hapi import Model, Input, set_device
from paddle.incubate.hapi.loss import CrossEntropy
from paddle.incubate.hapi.vision.models import LeNet
from paddle.incubate.hapi.metrics import Accuracy
......@@ -64,20 +64,19 @@ class TestDistTraning(unittest.TestCase):
im_shape = (-1, 1, 28, 28)
batch_size = 128
inputs = [Input(im_shape, 'float32', name='image')]
labels = [Input([None, 1], 'int64', name='label')]
inputs = [Input('image', im_shape, 'float32')]
labels = [Input('label', [None, 1], 'int64')]
model = Model(LeNet(), inputs, labels)
optim = fluid.optimizer.Momentum(
learning_rate=0.001, momentum=.9, parameter_list=model.parameters())
model.prepare(optim, CrossEntropy(), Accuracy())
train_dataset = MnistDataset(mode='train')
val_dataset = MnistDataset(mode='test')
test_dataset = MnistDataset(mode='test', return_label=False)
model = LeNet()
optim = fluid.optimizer.Momentum(
learning_rate=0.001, momentum=.9, parameter_list=model.parameters())
loss = CrossEntropy()
model.prepare(optim, loss, Accuracy(), inputs, labels, device=device)
cbk = ProgBarLogger(50)
model.fit(train_dataset,
val_dataset,
epochs=2,
......
......@@ -22,7 +22,7 @@ import contextlib
from paddle import fluid
from paddle.incubate.hapi.model import Model, Input, set_device
from paddle.incubate.hapi import Model, Input, set_device
from paddle.incubate.hapi.loss import CrossEntropy
from paddle.incubate.hapi.vision.models import LeNet
from paddle.incubate.hapi.metrics import Accuracy
......@@ -63,20 +63,19 @@ class TestDistTraning(unittest.TestCase):
im_shape = (-1, 1, 28, 28)
batch_size = 128
inputs = [Input(im_shape, 'float32', name='image')]
labels = [Input([None, 1], 'int64', name='label')]
inputs = [Input('image', im_shape, 'float32')]
labels = [Input('label', [None, 1], 'int64')]
model = Model(LeNet(), inputs, labels)
optim = fluid.optimizer.Momentum(
learning_rate=0.001, momentum=.9, parameter_list=model.parameters())
model.prepare(optim, CrossEntropy(), Accuracy())
train_dataset = MnistDataset(mode='train')
val_dataset = MnistDataset(mode='test')
test_dataset = MnistDataset(mode='test', return_label=False)
model = LeNet()
optim = fluid.optimizer.Momentum(
learning_rate=0.001, momentum=.9, parameter_list=model.parameters())
loss = CrossEntropy()
model.prepare(optim, loss, Accuracy(), inputs, labels, device=device)
cbk = ProgBarLogger(50)
model.fit(train_dataset,
val_dataset,
epochs=2,
......
......@@ -18,7 +18,7 @@ import random
import tempfile
import shutil
from paddle.incubate.hapi.model import Input
from paddle.incubate.hapi.model import Model, Input
from paddle.incubate.hapi.vision.models import LeNet
from paddle.incubate.hapi.callbacks import config_callbacks
......@@ -36,9 +36,9 @@ class TestCallbacks(unittest.TestCase):
freq = 2
eval_steps = 20
lenet = LeNet()
inputs = [Input([None, 1, 28, 28], 'float32', name='image')]
lenet.prepare(inputs=inputs)
inputs = [Input('image', [None, 1, 28, 28], 'float32')]
lenet = Model(LeNet(), inputs)
lenet.prepare()
cbks = config_callbacks(
model=lenet,
......
......@@ -26,7 +26,8 @@ from paddle import fluid
from paddle.nn import Conv2D, Pool2D, Linear, ReLU, Sequential
from paddle.fluid.dygraph.base import to_variable
from paddle.incubate.hapi.model import Model, Input, set_device
import paddle.incubate.hapi as hapi
from paddle.incubate.hapi import Model, Input
from paddle.incubate.hapi.loss import CrossEntropy
from paddle.incubate.hapi.metrics import Accuracy
from paddle.incubate.hapi.datasets import MNIST
......@@ -123,7 +124,7 @@ class TestModel(unittest.TestCase):
def setUpClass(cls):
if not fluid.is_compiled_with_cuda():
self.skipTest('module not tested when ONLY_CPU compling')
cls.device = set_device('gpu')
cls.device = hapi.set_device('gpu')
fluid.enable_dygraph(cls.device)
sp_num = 1280
......@@ -149,8 +150,8 @@ class TestModel(unittest.TestCase):
cls.acc1 = dynamic_evaluate(dy_lenet, cls.val_loader)
cls.inputs = [Input([-1, 1, 28, 28], 'float32', name='image')]
cls.labels = [Input([None, 1], 'int64', name='label')]
cls.inputs = [Input('image', [-1, 1, 28, 28], 'float32')]
cls.labels = [Input('label', [None, 1], 'int64')]
cls.save_dir = tempfile.mkdtemp()
cls.weight_path = os.path.join(cls.save_dir, 'lenet')
......@@ -189,15 +190,14 @@ class TestModel(unittest.TestCase):
fluid.default_startup_program().random_seed = seed
fluid.default_main_program().random_seed = seed
model = LeNet()
net = LeNet()
optim_new = fluid.optimizer.Adam(
learning_rate=0.001, parameter_list=model.parameters())
learning_rate=0.001, parameter_list=net.parameters())
model = Model(net, inputs=self.inputs, labels=self.labels)
model.prepare(
optim_new,
loss_function=CrossEntropy(average=False),
metrics=Accuracy(),
inputs=self.inputs,
labels=self.labels)
metrics=Accuracy())
model.fit(self.train_dataset, batch_size=64, shuffle=False)
result = model.evaluate(self.val_dataset, batch_size=64)
......@@ -225,9 +225,8 @@ class TestModel(unittest.TestCase):
def evaluate(self, dynamic):
fluid.enable_dygraph(self.device) if dynamic else None
model = LeNet()
model.prepare(
metrics=Accuracy(), inputs=self.inputs, labels=self.labels)
model = Model(LeNet(), self.inputs, self.labels)
model.prepare(metrics=Accuracy())
model.load(self.weight_path)
result = model.evaluate(self.val_dataset, batch_size=64)
np.testing.assert_allclose(result['acc'], self.acc1)
......@@ -247,8 +246,8 @@ class TestModel(unittest.TestCase):
def predict(self, dynamic):
fluid.enable_dygraph(self.device) if dynamic else None
model = LeNet()
model.prepare(inputs=self.inputs)
model = Model(LeNet(), self.inputs)
model.prepare()
model.load(self.weight_path)
output = model.predict(
self.test_dataset, batch_size=64, stack_outputs=True)
......@@ -271,7 +270,7 @@ class TestModel(unittest.TestCase):
fluid.disable_dygraph() if dynamic else None
class MyModel(Model):
class MyModel(fluid.dygraph.Layer):
def __init__(self):
super(MyModel, self).__init__()
self._fc = Linear(20, 10, act='softmax')
......@@ -310,28 +309,24 @@ class TestModelFunction(unittest.TestCase):
ref = get_expect()
for dynamic in [True, False]:
device = set_device('cpu')
device = hapi.set_device('cpu')
fluid.enable_dygraph(device) if dynamic else None
self.set_seed()
model = MyModel()
net = MyModel()
optim2 = fluid.optimizer.SGD(learning_rate=0.001,
parameter_list=model.parameters())
parameter_list=net.parameters())
inputs = [Input([None, dim], 'float32', name='x')]
labels = [Input([None, 1], 'int64', name='label')]
model.prepare(
optim2,
loss_function=CrossEntropy(average=False),
inputs=inputs,
labels=labels,
device=device)
inputs = [Input('x', [None, dim], 'float32')]
labels = [Input('label', [None, 1], 'int64')]
model = Model(net, inputs, labels)
model.prepare(optim2, loss_function=CrossEntropy(average=False))
loss, = model.train_batch([data], [label])
np.testing.assert_allclose(loss.flatten(), ref.flatten())
fluid.disable_dygraph() if dynamic else None
def test_test_batch(self, dynamic=True):
def test_test_batch(self):
dim = 20
data = np.random.random(size=(4, dim)).astype(np.float32)
......@@ -346,32 +341,31 @@ class TestModelFunction(unittest.TestCase):
ref = get_expect()
for dynamic in [True, False]:
device = set_device('cpu')
device = hapi.set_device('cpu')
fluid.enable_dygraph(device) if dynamic else None
self.set_seed()
model = MyModel()
inputs = [Input([None, dim], 'float32', name='x')]
model.prepare(inputs=inputs, device=device)
net = MyModel()
inputs = [Input('x', [None, dim], 'float32')]
model = Model(net, inputs)
model.prepare()
out, = model.test_batch([data])
np.testing.assert_allclose(out, ref)
np.testing.assert_allclose(out, ref, rtol=1e-6)
fluid.disable_dygraph() if dynamic else None
def test_save_load(self):
path = tempfile.mkdtemp()
for dynamic in [True, False]:
device = set_device('cpu')
device = hapi.set_device('cpu')
fluid.enable_dygraph(device) if dynamic else None
model = MyModel()
inputs = [Input([None, 20], 'float32', name='x')]
labels = [Input([None, 1], 'int64', name='label')]
net = MyModel()
inputs = [Input('x', [None, 20], 'float32')]
labels = [Input('label', [None, 1], 'int64')]
optim = fluid.optimizer.SGD(learning_rate=0.001,
parameter_list=model.parameters())
parameter_list=net.parameters())
model = Model(net, inputs, labels)
model.prepare(
inputs=inputs,
optimizer=optim,
loss_function=CrossEntropy(average=False),
labels=labels)
optimizer=optim, loss_function=CrossEntropy(average=False))
model.save(path + '/test')
model.load(path + '/test')
shutil.rmtree(path)
......@@ -379,82 +373,73 @@ class TestModelFunction(unittest.TestCase):
def test_dynamic_save_static_load(self):
path = tempfile.mkdtemp()
# for dynamic in [True, False]:
device = set_device('cpu')
fluid.enable_dygraph(device) #if dynamic else None
model = MyModel()
inputs = [Input([None, 20], 'float32', name='x')]
labels = [Input([None, 1], 'int64', name='label')]
# dynamic saving
device = hapi.set_device('cpu')
fluid.enable_dygraph(device)
model = Model(MyModel())
optim = fluid.optimizer.SGD(learning_rate=0.001,
parameter_list=model.parameters())
model.prepare(
inputs=inputs,
optimizer=optim,
loss_function=CrossEntropy(average=False),
labels=labels)
optimizer=optim, loss_function=CrossEntropy(average=False))
model.save(path + '/test')
fluid.disable_dygraph()
model = MyModel()
inputs = [Input([None, 20], 'float32', name='x')]
labels = [Input([None, 1], 'int64', name='label')]
inputs = [Input('x', [None, 20], 'float32')]
labels = [Input('label', [None, 1], 'int64')]
model = Model(MyModel(), inputs, labels)
optim = fluid.optimizer.SGD(learning_rate=0.001,
parameter_list=model.parameters())
model.prepare(
inputs=inputs,
optimizer=optim,
loss_function=CrossEntropy(average=False),
labels=labels)
optimizer=optim, loss_function=CrossEntropy(average=False))
model.load(path + '/test')
shutil.rmtree(path)
def test_static_save_dynamic_load(self):
path = tempfile.mkdtemp()
model = MyModel()
inputs = [Input([None, 20], 'float32', name='x')]
labels = [Input([None, 1], 'int64', name='label')]
net = MyModel()
inputs = [Input('x', [None, 20], 'float32')]
labels = [Input('label', [None, 1], 'int64')]
optim = fluid.optimizer.SGD(learning_rate=0.001,
parameter_list=model.parameters())
parameter_list=net.parameters())
model = Model(net, inputs, labels)
model.prepare(
inputs=inputs,
optimizer=optim,
loss_function=CrossEntropy(average=False),
labels=labels)
optimizer=optim, loss_function=CrossEntropy(average=False))
model.save(path + '/test')
device = set_device('cpu')
device = hapi.set_device('cpu')
fluid.enable_dygraph(device) #if dynamic else None
model = MyModel()
inputs = [Input([None, 20], 'float32', name='x')]
labels = [Input([None, 1], 'int64', name='label')]
net = MyModel()
inputs = [Input('x', [None, 20], 'float32')]
labels = [Input('label', [None, 1], 'int64')]
optim = fluid.optimizer.SGD(learning_rate=0.001,
parameter_list=model.parameters())
parameter_list=net.parameters())
model = Model(net, inputs, labels)
model.prepare(
inputs=inputs,
optimizer=optim,
loss_function=CrossEntropy(average=False),
labels=labels)
optimizer=optim, loss_function=CrossEntropy(average=False))
model.load(path + '/test')
shutil.rmtree(path)
fluid.disable_dygraph()
def test_parameters(self):
for dynamic in [True, False]:
device = set_device('cpu')
device = hapi.set_device('cpu')
fluid.enable_dygraph(device) if dynamic else None
model = MyModel()
inputs = [Input([None, 20], 'float32', name='x')]
model.prepare(inputs=inputs)
net = MyModel()
inputs = [Input('x', [None, 20], 'float32')]
model = Model(net, inputs)
model.prepare()
params = model.parameters()
self.assertTrue(params[0].shape[0] == 20)
self.assertTrue(params[0].shape[1] == 10)
fluid.disable_dygraph() if dynamic else None
def test_export_deploy_model(self):
model = LeNet()
inputs = [Input([-1, 1, 28, 28], 'float32', name='image')]
model.prepare(inputs=inputs)
net = LeNet()
inputs = [Input('image', [-1, 1, 28, 28], 'float32')]
model = Model(net, inputs)
model.prepare()
save_dir = tempfile.mkdtemp()
if not os.path.exists(save_dir):
os.makedirs(save_dir)
......@@ -476,7 +461,7 @@ class TestModelFunction(unittest.TestCase):
feed={feed_target_names[0]: tensor_img},
fetch_list=fetch_targets)
np.testing.assert_allclose(results, ori_results)
np.testing.assert_allclose(results, ori_results, rtol=1e-6)
shutil.rmtree(save_dir)
......
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import numpy as np
import paddle.fluid as fluid
import paddle.incubate.hapi.vision.models as models
from paddle.incubate.hapi import Model, Input
# test the predicted resutls of static graph and dynamic graph are equal
# when used pretrained model
class TestPretrainedModel(unittest.TestCase):
def infer(self, x, arch, dygraph=True):
if dygraph:
fluid.enable_dygraph()
net = models.__dict__[arch](pretrained=True, classifier_activation=None)
inputs = [Input('image', [None, 3, 224, 224], 'float32')]
model = Model(network=net, inputs=inputs)
model.prepare()
res = model.test_batch(x)
if dygraph:
fluid.disable_dygraph()
return res
def test_models(self):
arches = ['mobilenet_v1', 'mobilenet_v2', 'resnet18']
for arch in arches:
x = np.array(np.random.random((2, 3, 224, 224)), dtype=np.float32)
y_dygraph = self.infer(x, arch)
y_static = self.infer(x, arch, dygraph=False)
np.testing.assert_allclose(y_dygraph, y_static)
if __name__ == '__main__':
unittest.main()
......@@ -23,7 +23,7 @@ import numpy as np
import paddle.fluid as fluid
from paddle.fluid.dygraph import Embedding, Linear, Layer
from paddle.fluid.layers import BeamSearchDecoder
from paddle.incubate.hapi.model import Model, Input, set_device
from paddle.incubate.hapi import Model, Input, set_device
from paddle.incubate.hapi.text import *
......@@ -36,7 +36,7 @@ class ModuleApiTest(unittest.TestCase):
np.random.seed(cls._random_seed)
random.seed(cls._random_seed)
cls.model_cls = type(cls.__name__ + "Model", (Model, ), {
cls.model_cls = type(cls.__name__ + "Model", (Layer, ), {
"__init__": cls.model_init_wrapper(cls.model_init),
"forward": cls.model_forward
})
......@@ -49,7 +49,7 @@ class ModuleApiTest(unittest.TestCase):
@staticmethod
def model_init_wrapper(func):
def __impl__(self, *args, **kwargs):
Model.__init__(self)
Layer.__init__(self)
func(self, *args, **kwargs)
return __impl__
......@@ -89,9 +89,10 @@ class ModuleApiTest(unittest.TestCase):
fluid.disable_dygraph()
fluid.default_main_program().random_seed = self._random_seed
fluid.default_startup_program().random_seed = self._random_seed
model = self.model_cls(**self.attrs) if isinstance(
layer = self.model_cls(**self.attrs) if isinstance(
self.attrs, dict) else self.model_cls(*self.attrs)
model.prepare(inputs=self.make_inputs(), device=place)
model = Model(layer, inputs=self.make_inputs())
model.prepare()
if self.param_states:
model.load(self.param_states, optim_state=None)
return model.test_batch(self.inputs)
......@@ -141,10 +142,7 @@ class TestBasicLSTM(ModuleApiTest):
def make_inputs(self):
inputs = [
Input(
[None, None, self.inputs[-1].shape[-1]],
"float32",
name="input"),
Input("input", [None, None, self.inputs[-1].shape[-1]], "float32"),
]
return inputs
......@@ -170,10 +168,7 @@ class TestBasicGRU(ModuleApiTest):
def make_inputs(self):
inputs = [
Input(
[None, None, self.inputs[-1].shape[-1]],
"float32",
name="input"),
Input("input", [None, None, self.inputs[-1].shape[-1]], "float32"),
]
return inputs
......@@ -224,11 +219,8 @@ class TestBeamSearch(ModuleApiTest):
def make_inputs(self):
inputs = [
Input(
[None, self.inputs[0].shape[-1]], "float32",
name="init_hidden"),
Input(
[None, self.inputs[1].shape[-1]], "float32", name="init_cell"),
Input("init_hidden", [None, self.inputs[0].shape[-1]], "float32"),
Input("init_cell", [None, self.inputs[1].shape[-1]], "float32"),
]
return inputs
......@@ -280,14 +272,10 @@ class TestTransformerEncoder(ModuleApiTest):
def make_inputs(self):
inputs = [
Input(
[None, None, self.inputs[0].shape[-1]],
"float32",
name="enc_input"),
Input(
[None, self.inputs[1].shape[1], None, None],
"float32",
name="attn_bias"),
Input("enc_input", [None, None, self.inputs[0].shape[-1]],
"float32"),
Input("attn_bias", [None, self.inputs[1].shape[1], None, None],
"float32"),
]
return inputs
......@@ -348,22 +336,14 @@ class TestTransformerDecoder(TestTransformerEncoder):
def make_inputs(self):
inputs = [
Input(
[None, None, self.inputs[0].shape[-1]],
"float32",
name="dec_input"),
Input(
[None, None, self.inputs[0].shape[-1]],
"float32",
name="enc_output"),
Input(
[None, self.inputs[-1].shape[1], None, None],
"float32",
name="self_attn_bias"),
Input(
[None, self.inputs[-1].shape[1], None, None],
"float32",
name="cross_attn_bias"),
Input("dec_input", [None, None, self.inputs[0].shape[-1]],
"float32"),
Input("enc_output", [None, None, self.inputs[0].shape[-1]],
"float32"),
Input("self_attn_bias",
[None, self.inputs[-1].shape[1], None, None], "float32"),
Input("cross_attn_bias",
[None, self.inputs[-1].shape[1], None, None], "float32"),
]
return inputs
......@@ -451,14 +431,10 @@ class TestTransformerBeamSearchDecoder(ModuleApiTest):
def make_inputs(self):
inputs = [
Input(
[None, None, self.inputs[0].shape[-1]],
"float32",
name="enc_output"),
Input(
[None, self.inputs[1].shape[1], None, None],
"float32",
name="trg_src_attn_bias"),
Input("enc_output", [None, None, self.inputs[0].shape[-1]],
"float32"),
Input("trg_src_attn_bias",
[None, self.inputs[1].shape[1], None, None], "float32"),
]
return inputs
......@@ -497,12 +473,9 @@ class TestSequenceTagging(ModuleApiTest):
def make_inputs(self):
inputs = [
Input(
[None, None], "int64", name="word"),
Input(
[None], "int64", name="lengths"),
Input(
[None, None], "int64", name="target"),
Input("word", [None, None], "int64"),
Input("lengths", [None], "int64"),
Input("target", [None, None], "int64"),
]
return inputs
......@@ -544,10 +517,7 @@ class TestStackedRNN(ModuleApiTest):
def make_inputs(self):
inputs = [
Input(
[None, None, self.inputs[-1].shape[-1]],
"float32",
name="input"),
Input("input", [None, None, self.inputs[-1].shape[-1]], "float32"),
]
return inputs
......@@ -573,10 +543,7 @@ class TestLSTM(ModuleApiTest):
def make_inputs(self):
inputs = [
Input(
[None, None, self.inputs[-1].shape[-1]],
"float32",
name="input"),
Input("input", [None, None, self.inputs[-1].shape[-1]], "float32"),
]
return inputs
......@@ -612,10 +579,7 @@ class TestBiLSTM(ModuleApiTest):
def make_inputs(self):
inputs = [
Input(
[None, None, self.inputs[-1].shape[-1]],
"float32",
name="input"),
Input("input", [None, None, self.inputs[-1].shape[-1]], "float32"),
]
return inputs
......@@ -645,10 +609,7 @@ class TestGRU(ModuleApiTest):
def make_inputs(self):
inputs = [
Input(
[None, None, self.inputs[-1].shape[-1]],
"float32",
name="input"),
Input("input", [None, None, self.inputs[-1].shape[-1]], "float32"),
]
return inputs
......@@ -684,10 +645,7 @@ class TestBiGRU(ModuleApiTest):
def make_inputs(self):
inputs = [
Input(
[None, None, self.inputs[-1].shape[-1]],
"float32",
name="input"),
Input("input", [None, None, self.inputs[-1].shape[-1]], "float32"),
]
return inputs
......@@ -722,9 +680,7 @@ class TestCNNEncoder(ModuleApiTest):
def make_inputs(self):
inputs = [
Input(
[None, self.inputs[-1].shape[1], None], "float32",
name="input"),
Input("input", [None, self.inputs[-1].shape[1], None], "float32"),
]
return inputs
......
......@@ -16,7 +16,7 @@ import unittest
import numpy as np
import paddle.incubate.hapi.vision.models as models
from paddle.incubate.hapi.model import Input
import paddle.incubate.hapi as hapi
class TestVisonModels(unittest.TestCase):
......@@ -24,13 +24,13 @@ class TestVisonModels(unittest.TestCase):
x = np.array(np.random.random((2, 3, 224, 224)), dtype=np.float32)
if batch_norm:
model = models.__dict__[arch](pretrained=pretrained,
batch_norm=True)
net = models.__dict__[arch](pretrained=pretrained, batch_norm=True)
else:
model = models.__dict__[arch](pretrained=pretrained)
inputs = [Input([None, 3, 224, 224], 'float32', name='image')]
net = models.__dict__[arch](pretrained=pretrained)
model.prepare(inputs=inputs)
input = hapi.Input('image', [None, 3, 224, 224], 'float32')
model = hapi.Model(net, input)
model.prepare()
model.test_batch(x)
......@@ -71,10 +71,9 @@ class TestVisonModels(unittest.TestCase):
self.models_infer('resnet152')
def test_lenet(self):
lenet = models.__dict__['LeNet']()
inputs = [Input([None, 1, 28, 28], 'float32', name='x')]
lenet.prepare(inputs=inputs)
input = hapi.Input('x', [None, 1, 28, 28], 'float32')
lenet = hapi.Model(models.__dict__['LeNet'](), input)
lenet.prepare()
x = np.array(np.random.random((2, 1, 28, 28)), dtype=np.float32)
lenet.test_batch(x)
......
......@@ -15,12 +15,10 @@
import paddle.fluid as fluid
from paddle.nn import Conv2D, Pool2D, Linear, ReLU, Sequential
from ...model import Model
__all__ = ['LeNet']
class LeNet(Model):
class LeNet(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.`_
......
......@@ -17,7 +17,6 @@ from paddle.fluid.initializer import MSRA
from paddle.fluid.param_attr import ParamAttr
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear
from ...model import Model
from ...download import get_weights_path_from_url
__all__ = ['MobileNetV1', 'mobilenet_v1']
......@@ -103,7 +102,7 @@ class DepthwiseSeparable(fluid.dygraph.Layer):
return y
class MobileNetV1(Model):
class MobileNetV1(fluid.dygraph.Layer):
"""MobileNetV1 model from
`"MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications" <https://arxiv.org/abs/1704.04861>`_.
......@@ -276,7 +275,8 @@ def _mobilenet(arch, pretrained=False, **kwargs):
model_urls[arch][1])
assert weight_path.endswith(
'.pdparams'), "suffix of weight must be .pdparams"
model.load(weight_path)
param, _ = fluid.load_dygraph(weight_path)
model.load_dict(param)
return model
......
......@@ -18,7 +18,6 @@ import paddle.fluid as fluid
from paddle.fluid.param_attr import ParamAttr
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear
from ...model import Model
from ...download import get_weights_path_from_url
__all__ = ['MobileNetV2', 'mobilenet_v2']
......@@ -150,7 +149,7 @@ class InvresiBlocks(fluid.dygraph.Layer):
return y
class MobileNetV2(Model):
class MobileNetV2(fluid.dygraph.Layer):
"""MobileNetV2 model from
`"MobileNetV2: Inverted Residuals and Linear Bottlenecks" <https://arxiv.org/abs/1801.04381>`_.
......@@ -252,7 +251,8 @@ def _mobilenet(arch, pretrained=False, **kwargs):
model_urls[arch][1])
assert weight_path.endswith(
'.pdparams'), "suffix of weight must be .pdparams"
model.load(weight_path)
param, _ = fluid.load_dygraph(weight_path)
model.load_dict(param)
return model
......
......@@ -21,7 +21,6 @@ import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear
from paddle.fluid.dygraph.container import Sequential
from ...model import Model
from ...download import get_weights_path_from_url
__all__ = [
......@@ -166,7 +165,7 @@ class BottleneckBlock(fluid.dygraph.Layer):
return fluid.layers.relu(x)
class ResNet(Model):
class ResNet(fluid.dygraph.Layer):
"""ResNet model from
`"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_
......@@ -278,7 +277,9 @@ def _resnet(arch, Block, depth, pretrained, **kwargs):
model_urls[arch][1])
assert weight_path.endswith(
'.pdparams'), "suffix of weight must be .pdparams"
model.load(weight_path)
param, _ = fluid.load_dygraph(weight_path)
model.set_dict(param)
return model
......
......@@ -16,7 +16,6 @@ import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear
from paddle.fluid.dygraph.container import Sequential
from ...model import Model
from ...download import get_weights_path_from_url
__all__ = [
......@@ -51,7 +50,7 @@ class Classifier(fluid.dygraph.Layer):
return out
class VGG(Model):
class VGG(fluid.dygraph.Layer):
"""VGG model from
`"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`_
......@@ -144,7 +143,8 @@ def _vgg(arch, cfg, batch_norm, pretrained, **kwargs):
model_urls[arch][1])
assert weight_path.endswith(
'.pdparams'), "suffix of weight must be .pdparams"
model.load(weight_path)
param, _ = fluid.load_dygraph(weight_path)
model.load_dict(param)
return model
......
......@@ -130,7 +130,7 @@ class BatchCompose(object):
import numpy as np
from paddle.io import DataLoader
from paddle.incubate.hapi.model import set_device
from paddle.incubate.hapi import set_device
from paddle.incubate.hapi.datasets import Flowers
from paddle.incubate.hapi.vision.transforms import Compose, BatchCompose, Resize
......
......@@ -108,6 +108,11 @@
"Metric.accumulate",
"Metric.name",
"Metric.add_metric_op",
"Accuracy.reset",
"Accuracy.update",
"Accuracy.accumulate",
"Accuracy.name",
"Accuracy.add_metric_op",
"Callback.set_params",
"Callback.on_train_begin",
"Callback.on_train_end",
......@@ -122,7 +127,8 @@
"Callback.on_eval_batch_begin",
"Callback.on_eval_batch_end",
"Callback.on_test_batch_begin",
"Callback.on_test_batch_end"
"Callback.on_test_batch_end",
"Model.prepare"
],
"wlist_no_op_pass":[
"gelu",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册