未验证 提交 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)
......
......@@ -39,36 +39,40 @@ from .loss import Loss
from .distributed import DistributedBatchSampler, _all_gather, prepare_distributed_context, _parallel_context_initialized
from .metrics import Metric
from .callbacks import config_callbacks
from .utils import to_list, to_numpy, flatten_list, restore_flatten_list
from .utils import to_list, to_numpy, flatten_list, restore_flatten_list, extract_args
from .device import _get_device
__all__ = [
'Model',
'Input',
'set_device',
]
def set_device(device):
"""
Args:
device (str): specify device type, 'cpu' or 'gpu'.
Returns:
fluid.CUDAPlace or fluid.CPUPlace: Created GPU or CPU place.
class Input(fluid.dygraph.Layer):
"""
Define inputs the model.
assert isinstance(device, six.string_types) and device.lower() in ['cpu', 'gpu'], \
"Expected device in ['cpu', 'gpu'], but got {}".format(device)
Args:
name (str): The name/alias of the variable, see :ref:`api_guide_Name`
for more details.
shape (tuple(integers)|list[integers]): List|Tuple of integers
declaring the shape. You can set "None" or -1 at a dimension
to indicate the dimension can be of any size. For example,
it is useful to set changeable batch size as "None" or -1.
dtype (np.dtype|VarType|str, optional): The type of the data. Supported
dtype: bool, float16, float32, float64, int8, int16, int32, int64,
uint8. Default: float32.
place = fluid.CUDAPlace(ParallelEnv().dev_id) \
if device.lower() == 'gpu' and fluid.is_compiled_with_cuda() \
else fluid.CPUPlace()
Examples:
.. code-block:: python
return place
import paddle.incubate.hapi as hapi
input = hapi.Input('x', [None, 784], 'float32')
label = hapi.Input('label', [None, 1], 'int64')
"""
class Input(fluid.dygraph.Layer):
def __init__(self, shape=None, dtype=None, name=None):
def __init__(self, name, shape=None, dtype='float32'):
super(Input, self).__init__()
self.shape = shape
self.dtype = dtype
......@@ -132,7 +136,7 @@ class StaticGraphAdapter(object):
return self._run(inputs, None)
def parameters(self, *args, **kwargs):
return super(Model, self.model).parameters(*args, **kwargs)
return self.model.network.parameters(*args, **kwargs)
def save(self, path):
def _save(state, path):
......@@ -151,7 +155,7 @@ class StaticGraphAdapter(object):
if dir_name and not os.path.exists(dir_name):
os.makedirs(dir_name)
param_path = path + ".pdparams"
_save(self.model.state_dict(), param_path)
_save(self.model.network.state_dict(), param_path)
prog = self._progs.get('train', None)
if prog is None or self.model._optimizer is None:
return
......@@ -384,7 +388,7 @@ class StaticGraphAdapter(object):
inputs = [k.forward() for k in to_list(ins)]
labels = [k.forward() for k in to_list(lbls)]
self._label_vars[mode] = labels
outputs = to_list(self.model.forward(*inputs))
outputs = to_list(self.model.network.forward(*inputs))
if mode != 'test' and self.model._loss_function:
losses = self.model._loss_function(outputs, labels)
......@@ -479,8 +483,8 @@ class DynamicGraphAdapter(object):
stradegy.local_rank = ParallelEnv().local_rank
stradegy.trainer_endpoints = ParallelEnv().trainer_endpoints
stradegy.current_endpoint = ParallelEnv().current_endpoint
self.ddp_model = fluid.dygraph.parallel.DataParallel(self.model,
stradegy)
self.ddp_model = fluid.dygraph.parallel.DataParallel(
self.model.network, stradegy)
@property
def mode(self):
......@@ -494,7 +498,7 @@ class DynamicGraphAdapter(object):
def train_batch(self, inputs, labels=None):
assert self.model._optimizer, \
"model not ready, please call `model.prepare()` first"
super(Model, self.model).train()
self.model.network.train()
self.mode = 'train'
inputs = to_list(inputs)
if labels is not None:
......@@ -507,13 +511,14 @@ class DynamicGraphAdapter(object):
final_loss.backward()
self.ddp_model.apply_collective_grads()
else:
outputs = self.model.forward(* [to_variable(x) for x in inputs])
outputs = self.model.network.forward(
* [to_variable(x) for x in inputs])
losses = self.model._loss_function(outputs, labels)
final_loss = fluid.layers.sum(losses)
final_loss.backward()
self.model._optimizer.minimize(final_loss)
self.model.clear_gradients()
self.model.network.clear_gradients()
metrics = []
for metric in self.model._metrics:
metric_outs = metric.add_metric_op(*(to_list(outputs) + to_list(
......@@ -525,12 +530,12 @@ class DynamicGraphAdapter(object):
if len(metrics) > 0 else [to_numpy(l) for l in losses]
def eval_batch(self, inputs, labels=None):
super(Model, self.model).eval()
self.model.network.eval()
self.mode = 'eval'
inputs = to_list(inputs)
if labels is not None:
labels = [to_variable(l) for l in to_list(labels)]
outputs = self.model.forward(* [to_variable(x) for x in inputs])
outputs = self.model.network.forward(* [to_variable(x) for x in inputs])
if self.model._loss_function:
losses = self.model._loss_function(outputs, labels)
else:
......@@ -571,20 +576,20 @@ class DynamicGraphAdapter(object):
if len(metrics) > 0 else [to_numpy(l) for l in losses]
def test_batch(self, inputs):
super(Model, self.model).eval()
self.model.network.eval()
self.mode = 'test'
inputs = [to_variable(x) for x in to_list(inputs)]
outputs = self.model.forward(*inputs)
outputs = self.model.network.forward(*inputs)
if self._nranks > 1 and isinstance(self.model._place, fluid.CUDAPlace):
outputs = [_all_gather(o, self._nranks) for o in to_list(outputs)]
return [to_numpy(o) for o in to_list(outputs)]
def parameters(self, *args, **kwargs):
return super(Model, self.model).parameters(*args, **kwargs)
return self.model.network.parameters(*args, **kwargs)
def save(self, path):
params = self.model.state_dict()
params = self.model.network.state_dict()
fluid.save_dygraph(params, path)
if self.model._optimizer is None:
return
......@@ -614,7 +619,7 @@ class DynamicGraphAdapter(object):
opt_cls_name = self.model._optimizer.__class__.__name__
opt_name = opt_unq_name[:opt_unq_name.rfind("_")] # remove suffix idx
param_names = [param.name for param in self.model.parameters()]
param_names = [param.name for param in self.model.network.parameters()]
for var_name, state_var in sorted(
optim_state.items(), key=lambda x: len(x[0]), reverse=True):
if var_name in ["@LR_DECAY_COUNTER@", "global_step"]:
......@@ -649,7 +654,7 @@ class DynamicGraphAdapter(object):
self.model._optimizer.set_dict(converted_state)
class Model(fluid.dygraph.Layer):
class Model(object):
"""
An Model object is network with training and inference features.
Dynamic graph and static graph are supported at the same time,
......@@ -658,56 +663,79 @@ class Model(fluid.dygraph.Layer):
instantiating a Model. The input description, i.e, hapi.Input,
must be required for static graph.
Args:
network (fluid.dygraph.Layer): The network is an instance of
fluid.dygraph.Layer.
inputs (Input|list|dict|None): `inputs`, entry points of network,
could be a Input layer, or lits of Input layers,
or dict (name: Input), or None. For static graph,
inputs must be set. For dynamic graph, it could be None.
labels (Input|list|None): `labels`, entry points of network,
could be a Input layer or lits of Input layers, or None.
For static graph, if labels is required in loss_function,
labels must be set. Otherwise, it could be None.
Usage:
.. code-block:: python
import numpy as np
import paddle
import paddle.fluid as fluid
#import paddle.incubate.hapi as hapi
from paddle.incubate.hapi import Model, Input, set_device
from paddle.incubate.hapi.loss import CrossEntropy
from paddle.incubate.hapi.dataset import MNIST
import paddle.incubate.hapi as hapi
class MyModel(Model):
class MyNet(fluid.dygraph.Layer):
def __init__(self):
super(MyModel, self).__init__()
self._fc = fluid.dygraph.Linear(784, 10, act='softmax')
super(MyNet, self).__init__()
self._fc1 = fluid.dygraph.Linear(784, 200, act='softmax')
def forward(self, x):
y = self._fc(x)
y = self._fc1(x)
return y
device = set_device('gpu')
device = hapi.set_device('gpu')
# if use static graph, do not set
fluid.enable_dygraph(device)
model = MyModel()
optim = fluid.optimizer.SGD(learning_rate=1e-3,
parameter_list=model.parameters())
inputs = [Input([None, 784], 'float32', name='x')]
labels = [Input([None, 1], 'int64', name='label')]
# inputs and labels are not required for dynamic graph.
input = hapi.Input('x', [None, 784], 'float32')
label = hapi.Input('label', [None, 1], 'int64')
mnist_data = MNIST(mode='train')
model = hapi.Model(MyNet(), input, label)
optim = fluid.optimizer.SGD(learning_rate=1e-3,
parameter_list=model.parameters())
model.prepare(optim,
CrossEntropy(average=True),
hapi.metrics.Accuracy(),
inputs,
labels,
device=device)
hapi.loss.CrossEntropy(average=True),
hapi.metrics.Accuracy())
mnist_data = hapi.datasets.MNIST(mode='train', chw_format=False)
model.fit(mnist_data, epochs=2, batch_size=32, verbose=1)
"""
def __init__(self):
super(Model, self).__init__(self.__class__.__name__)
def __init__(self, network, inputs=None, labels=None):
self.mode = 'train'
self.network = network
self._inputs = None
self._labels = None
self._loss_function = None
self._loss_weights = None
self._optimizer = None
self._device = None
self._optimizer = None
self._test_dataloader = None
if not in_dygraph_mode():
if not isinstance(inputs, (list, dict, Input)):
raise TypeError(
"'inputs' must be list or dict in static graph mode")
if inputs is None:
self._inputs = [Input(name=n) \
for n in extract_args(self.network.forward) if n != 'self']
elif isinstance(input, dict):
self._inputs = [inputs[n] \
for n in extract_args(self.network.forward) if n != 'self']
else:
self._inputs = to_list(inputs)
self._labels = to_list(labels)
# init backend
if fluid.in_dygraph_mode():
self._adapter = DynamicGraphAdapter(self)
......@@ -735,12 +763,12 @@ class Model(fluid.dygraph.Layer):
import numpy as np
import paddle.fluid as fluid
from paddle.incubate.hapi import Model, Input, set_device
import paddle.incubate.hapi as hapi
class MyModel(Model):
class MyNet(fluid.dygraph.Layer):
def __init__(self):
super(MyModel, self).__init__()
self._fc = Linear(784, 1, act='softmax')
super(MyNet, self).__init__()
self._fc = fluid.dygraph.Linear(784, 10, act='softmax')
def forward(self, x):
y = self._fc(x)
return y
......@@ -748,17 +776,12 @@ class Model(fluid.dygraph.Layer):
device = hapi.set_device('gpu')
fluid.enable_dygraph(device)
model = MyModel()
input = hapi.Input('x', [None, 784], 'float32')
label = hapi.Input('label', [None, 1], 'int64')
model = hapi.Model(MyNet(), input, label)
optim = fluid.optimizer.SGD(learning_rate=1e-3,
parameter_list=model.parameters())
inputs = [Input([None, 784], 'float32', name='x')]
labels = [Input([None, 1], 'int64', name='label')]
model.prepare(optim,
CrossEntropy(average=True),
inputs=inputs,
labels=labels,
device=device)
model.prepare(optim, hapi.loss.CrossEntropy(average=True))
data = np.random.random(size=(4,784)).astype(np.float32)
label = np.random.randint(0, 10, size=(4, 1)).astype(np.int64)
loss = model.train_batch([data], [label])
......@@ -787,30 +810,26 @@ class Model(fluid.dygraph.Layer):
import numpy as np
import paddle.fluid as fluid
from paddle.incubate.hapi import Model, Input, set_device
import paddle.incubate.hapi as hapi
class MyModel(Model):
class MyNet(fluid.dygraph.Layer):
def __init__(self):
super(MyModel, self).__init__()
self._fc = fluid.dygraph.Linear(784, 1, act='softmax')
super(MyNet, self).__init__()
self._fc = fluid.dygraph.Linear(784, 10, act='softmax')
def forward(self, x):
y = self._fc(x)
return y
device = set_device('gpu')
device = hapi.set_device('gpu')
fluid.enable_dygraph(device)
model = MyModel()
input = hapi.Input('x', [None, 784], 'float32')
label = hapi.Input('label', [None, 1], 'int64')
model = hapi.Model(MyNet(), input, label)
optim = fluid.optimizer.SGD(learning_rate=1e-3,
parameter_list=model.parameters())
inputs = [Input([None, 784], 'float32', name='x')]
labels = [Input([None, 1], 'int64', name='label')]
model.prepare(optim,
CrossEntropy(average=True),
inputs=inputs,
labels=labels,
device=device)
hapi.loss.CrossEntropy(average=True))
data = np.random.random(size=(4,784)).astype(np.float32)
label = np.random.randint(0, 10, size=(4, 1)).astype(np.int64)
loss = model.eval_batch([data], [label])
......@@ -836,23 +855,21 @@ class Model(fluid.dygraph.Layer):
import numpy as np
import paddle.fluid as fluid
from paddle.incubate.hapi import Model, Input, set_device
import paddle.incubate.hapi as hapi
class MyModel(Model):
class MyNet(fluid.dygraph.Layer):
def __init__(self):
super(MyModel, self).__init__()
super(MyNet, self).__init__()
self._fc = fluid.dygraph.Linear(784, 1, act='softmax')
def forward(self, x):
y = self._fc(x)
return y
device = set_device('gpu')
device = hapi.set_device('gpu')
fluid.enable_dygraph(device)
model = MyModel()
inputs = [Input([None, 784], 'float32', name='x')]
model.prepare(inputs=inputs,
device=device)
model = hapi.Model(MyNet())
model.prepare()
data = np.random.random(size=(4,784)).astype(np.float32)
out = model.eval_batch([data])
print(out)
......@@ -886,19 +903,19 @@ class Model(fluid.dygraph.Layer):
.. code-block:: python
import paddle.fluid as fluid
from paddle.incubate.hapi import Model, set_device
import paddle.incubate.hapi as hapi
class MyModel(Model):
class MyNet(fluid.dygraph.Layer):
def __init__(self):
super(MyModel, self).__init__()
super(MyNet, self).__init__()
self._fc = fluid.dygraph.Linear(784, 1, act='softmax')
def forward(self, x):
y = self._fc(x)
return y
device = set_device('cpu')
device = hapi.set_device('cpu')
fluid.enable_dygraph(device)
model = MyModel()
model = hapi.Model(MyNet())
model.save('checkpoint/test')
"""
if ParallelEnv().local_rank == 0:
......@@ -938,19 +955,19 @@ class Model(fluid.dygraph.Layer):
.. code-block:: python
import paddle.fluid as fluid
from paddle.incubate.hapi import Model, set_device
import paddle.incubate.hapi as hapi
class MyModel(Model):
class MyNet(fluid.dygraph.Layer):
def __init__(self):
super(MyModel, self).__init__()
super(MyNet, self).__init__()
self._fc = fluid.dygraph.Linear(784, 1, act='softmax')
def forward(self, x):
y = self._fc(x)
return y
device = set_device('cpu')
device = hapi.set_device('cpu')
fluid.enable_dygraph(device)
model = MyModel()
model = hapi.Model(MyNet())
model.load('checkpoint/test')
"""
......@@ -983,7 +1000,7 @@ class Model(fluid.dygraph.Layer):
assert param_state, "Failed to load parameters, please check path."
matched_param_state = []
for key, param in self.state_dict().items():
for key, param in self.network.state_dict().items():
try:
match_res = _check_match(key, param)
except ValueError as err:
......@@ -1012,28 +1029,24 @@ class Model(fluid.dygraph.Layer):
.. code-block:: python
from paddle.incubate.hapi.model import Model, Input, set_device
class MyModel(Model):
import paddle.fluid as fluid
from paddle.incubate.hapi import Model
class MyNet(fluid.dygraph.Layer):
def __init__(self):
super(MyModel, self).__init__()
super(MyNet, self).__init__()
self._fc = fluid.dygraph.Linear(20, 10, act='softmax')
def forward(self, x):
y = self._fc(x)
return y
fluid.enable_dygraph()
model = MyModel()
model = Model(MyNet())
params = model.parameters()
"""
return self._adapter.parameters()
def prepare(self,
optimizer=None,
loss_function=None,
metrics=None,
inputs=None,
labels=None,
device=None):
def prepare(self, optimizer=None, loss_function=None, metrics=None):
"""
Configures the model before runing.
......@@ -1046,32 +1059,13 @@ class Model(fluid.dygraph.Layer):
no loss.
metrics (Metric|list of Metric|None): If metrics is set, all
metrics will be calculated and output in train/eval mode.
inputs (Input|list|dict|None): `inputs`, entry points of network,
could be a Input layer, or lits of Input layers,
or dict (name: Input), or None. For static graph,
inputs must be set. For dynamic graph, it could be None.
labels (Input|list|None): `labels`, entry points of network,
could be a Input layer or lits of Input layers, or None.
For static graph, if labels is required in loss_function,
labels must be set. Otherwise, it could be None.
device (str|fluid.CUDAPlace|fluid.CPUPlace|None): Specify device
type, 'CPU', 'GPU', fluid.CUDAPlace or fluid.CPUPlace.
If None, automatically select device according to
installation package version.
Returns:
None
"""
if isinstance(device, fluid.CUDAPlace) or \
(isinstance(device, six.string_types) and device.lower() == 'gpu') \
or (device is None and fluid.is_compiled_with_cuda()):
if isinstance(device, fluid.CUDAPlace):
self._place = device
else:
self._place = fluid.CUDAPlace(ParallelEnv().dev_id) \
if ParallelEnv().nranks > 1 else fluid.CUDAPlace(0)
self._place = _get_device()
if isinstance(self._place, fluid.CUDAPlace):
global _parallel_context_initialized
if ParallelEnv().nranks > 1 and not _parallel_context_initialized:
if fluid.in_dygraph_mode():
......@@ -1088,27 +1082,13 @@ class Model(fluid.dygraph.Layer):
fluid.dygraph.parallel.prepare_context()
else:
prepare_distributed_context(self._place)
_parallel_context_initialized = True
elif isinstance(device, fluid.CPUPlace):
self._place = device
elif (isinstance(device, six.string_types) and device.lower() == 'cpu') \
or (device is None):
self._place = fluid.CPUPlace()
else:
raise ValueError(
"Expected device in ('gpu', 'cpu', fluid.CUDAPlace, fluid.CPUPlace, None), \
but got {}".format(device))
self._optimizer = optimizer
if loss_function:
if not isinstance(loss_function, Loss):
raise TypeError("'loss_function' must be sub classes of 'Loss'")
self._loss_function = loss_function
if not in_dygraph_mode():
if not isinstance(inputs, (list, dict, Input)):
raise TypeError(
"'inputs' must be list or dict in static graph mode")
metrics = metrics or []
for metric in to_list(metrics):
......@@ -1117,11 +1097,6 @@ class Model(fluid.dygraph.Layer):
metric.__class__.__name__)
self._metrics = to_list(metrics)
self._inputs = to_list(inputs) if not isinstance(inputs, dict) else [
inputs[n] for n in extract_args(self.forward) if n != 'self'
]
self._labels = to_list(labels)
if not in_dygraph_mode():
self._adapter.prepare()
......@@ -1192,32 +1167,26 @@ class Model(fluid.dygraph.Layer):
.. code-block:: python
from paddle.incubate.hapi.model import Model, Input, set_device
from paddle.incubate.hapi.loss import CrossEntropy
from paddle.incubate.hapi.metrics import Accuracy
from paddle.incubate.hapi.datasets import MNIST
from paddle.incubate.hapi.vision.models import LeNet
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
dynamic = True
device = set_device(FLAGS.device)
device = hapi.set_device('gpu')
fluid.enable_dygraph(device) if dynamic else None
train_dataset = MNIST(mode='train')
val_dataset = MNIST(mode='test')
train_dataset = hapi.datasets.MNIST(mode='train')
val_dataset = hapi.datasets.MNIST(mode='test')
inputs = [Input([None, 1, 28, 28], 'float32', name='image')]
labels = [Input([None, 1], 'int64', name='label')]
input = hapi.Input('image', [None, 1, 28, 28], 'float32')
label = hapi.Input('label', [None, 1], 'int64')
model = LeNet()
model = hapi.Model(hapi.vision.LeNet(), input, label)
optim = fluid.optimizer.Adam(
learning_rate=0.001, parameter_list=model.parameters())
model.prepare(
optim,
CrossEntropy(),
Accuracy(topk=(1, 2)),
inputs=inputs,
labels=labels,
device=device)
hapi.loss.CrossEntropy(),
hapi.metrics.Accuracy(topk=(1, 2)))
model.fit(train_dataset,
val_dataset,
epochs=2,
......@@ -1229,36 +1198,30 @@ class Model(fluid.dygraph.Layer):
.. code-block:: python
from paddle.incubate.hapi.model import Model, Input, set_device
from paddle.incubate.hapi.loss import CrossEntropy
from paddle.incubate.hapi.metrics import Accuracy
from paddle.incubate.hapi.datasets import MNIST
from paddle.incubate.hapi.vision.models import LeNet
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
dynamic = True
device = set_device(FLAGS.device)
device = hapi.set_device('gpu')
fluid.enable_dygraph(device) if dynamic else None
train_dataset = MNIST(mode='train')
train_dataset = hapi.datasets.MNIST(mode='train')
train_loader = fluid.io.DataLoader(train_dataset,
places=device, batch_size=64)
val_dataset = MNIST(mode='test')
val_dataset = hapi.datasets.MNIST(mode='test')
val_loader = fluid.io.DataLoader(val_dataset,
places=device, batch_size=64)
inputs = [Input([None, 1, 28, 28], 'float32', name='image')]
labels = [Input([None, 1], 'int64', name='label')]
input = hapi.Input('image', [None, 1, 28, 28], 'float32')
label = hapi.Input('label', [None, 1], 'int64')
model = LeNet()
model = hapi.Model(hapi.vision.LeNet(), input, label)
optim = fluid.optimizer.Adam(
learning_rate=0.001, parameter_list=model.parameters())
model.prepare(
optim,
CrossEntropy(),
Accuracy(topk=(1, 2)),
inputs=inputs,
labels=labels,
device=device)
hapi.loss.CrossEntropy(),
hapi.metrics.Accuracy(topk=(1, 2)))
model.fit(train_loader,
val_loader,
epochs=2,
......@@ -1370,33 +1333,24 @@ class Model(fluid.dygraph.Layer):
Examples:
.. code-block:: python
# declarative mode
import numpy as np
from paddle.incubate.hapi.metrics import Accuracy
from paddle.incubate.hapi.datasets import MNIST
from paddle.incubate.hapi.vision.transforms import Compose,Resize
from paddle.incubate.hapi.vision.models import LeNet
from paddle.incubate.hapi.model import Input, set_device
inputs = [Input([-1, 1, 28, 28], 'float32', name='image')]
labels = [Input([None, 1], 'int64', name='label')]
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
val_dataset = MNIST(mode='test')
# declarative mode
val_dataset = hapi.datasets.MNIST(mode='test')
model = LeNet()
model.prepare(metrics=Accuracy(), inputs=inputs, labels=labels)
input = hapi.Input('image', [-1, 1, 28, 28], 'float32')
label = hapi.Input('label', [None, 1], 'int64')
model = hapi.Model(hapi.vision.LeNet(), input, label)
model.prepare(metrics=hapi.metrics.Accuracy())
result = model.evaluate(val_dataset, batch_size=64)
print(result)
# imperative mode
import paddle.fluid.dygraph as dg
place = set_device('cpu')
with dg.guard(place) as g:
model = LeNet()
model.prepare(metrics=Accuracy(), inputs=inputs, labels=labels)
fluid.enable_dygraph()
model = hapi.Model(hapi.vision.LeNet())
model.prepare(metrics=hapi.metrics.Accuracy())
result = model.evaluate(val_dataset, batch_size=64)
print(result)
......@@ -1471,15 +1425,11 @@ class Model(fluid.dygraph.Layer):
Examples:
.. code-block:: python
# declarative mode
import numpy as np
from paddle.incubate.hapi.metrics import Accuracy
from paddle.incubate.hapi.datasets import MNIST
from paddle.incubate.hapi.vision.transforms import Compose,Resize
from paddle.incubate.hapi.vision.models import LeNet
from paddle.incubate.hapi.model import Input, set_device
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
class MnistDataset(MNIST):
class MnistDataset(hapi.datasets.MNIST):
def __init__(self, mode, return_label=True):
super(MnistDataset, self).__init__(mode=mode)
self.return_label = return_label
......@@ -1493,25 +1443,23 @@ class Model(fluid.dygraph.Layer):
def __len__(self):
return len(self.images)
inputs = [Input([-1, 1, 28, 28], 'float32', name='image')]
test_dataset = MnistDataset(mode='test', return_label=False)
model = LeNet()
model.prepare(inputs=inputs)
# declarative mode
input = hapi.Input('image', [-1, 1, 28, 28], 'float32')
model = hapi.Model(hapi.vision.LeNet(), input)
model.prepare()
result = model.predict(test_dataset, batch_size=64)
print(result)
print(len(result[0]), result[0][0].shape)
# imperative mode
import paddle.fluid.dygraph as dg
place = set_device('cpu')
with dg.guard(place) as g:
model = LeNet()
model.prepare(inputs=inputs)
device = hapi.set_device('cpu')
fluid.enable_dygraph(device)
model = hapi.Model(hapi.vision.LeNet())
model.prepare()
result = model.predict(test_dataset, batch_size=64)
print(result)
print(len(result[0]), result[0][0].shape)
"""
if test_data is not None and isinstance(test_data, Dataset):
......@@ -1572,6 +1520,19 @@ class Model(fluid.dygraph.Layer):
Returns:
list: The fetch variables' name list
Examples:
.. code-block:: python
import paddle.fluid as fluid
import paddle.incubate.hapi as hapi
input = hapi.Input('image', [-1, 1, 28, 28], 'float32')
model = hapi.Model(hapi.vision.LeNet(), input)
model.prepare()
model.save_inference_model('inference_model')
"""
assert not fluid.in_dygraph_mode(
), 'Save inference model must in static mode!'
......
......@@ -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.
先完成此消息的编辑!
想要评论请 注册