Refine vision models (#27476)

* refine vision models
revert-27356-init_low_level_gloo
LielinJiang 5 years ago committed by GitHub
parent 0b4bb023a7
commit b38e4f2840
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -301,10 +301,11 @@ class ProgBarLogger(Callback):
train_dataset = paddle.vision.datasets.MNIST(mode='train')
model = paddle.Model(paddle.vision.LeNet(classifier_activation=None),
lenet = paddle.vision.LeNet()
model = paddle.Model(lenet,
inputs, labels)
optim = paddle.optimizer.Adam(0.001)
optim = paddle.optimizer.Adam(0.001, parameters=lenet.parameters())
model.prepare(optimizer=optim,
loss=paddle.nn.CrossEntropyLoss(),
metrics=paddle.metric.Accuracy())
@ -436,10 +437,11 @@ class ModelCheckpoint(Callback):
train_dataset = paddle.vision.datasets.MNIST(mode='train')
model = paddle.Model(paddle.vision.LeNet(classifier_activation=None),
lenet = paddle.vision.LeNet()
model = paddle.Model(lenet,
inputs, labels)
optim = paddle.optimizer.Adam(0.001)
optim = paddle.optimizer.Adam(0.001, parameters=lenet.parameters())
model.prepare(optimizer=optim,
loss=paddle.nn.CrossEntropyLoss(),
metrics=paddle.metric.Accuracy())

@ -814,10 +814,9 @@ class Model(object):
from paddle.static import InputSpec
device = paddle.set_device('cpu') # or 'gpu'
# if use static graph, do not set
paddle.disable_static(device)
net = nn.Sequential(
nn.Flatten(1),
nn.Linear(784, 200),
nn.Tanh(),
nn.Linear(200, 10))
@ -833,7 +832,7 @@ class Model(object):
paddle.nn.CrossEntropyLoss(),
paddle.metric.Accuracy())
data = paddle.vision.datasets.MNIST(mode='train', chw_format=False)
data = paddle.vision.datasets.MNIST(mode='train')
model.fit(data, epochs=2, batch_size=32, verbose=1)
"""
@ -850,7 +849,8 @@ class Model(object):
if not isinstance(inputs, (list, dict, Input)):
raise TypeError(
"'inputs' must be list or dict, and couldn't be None.")
"'inputs' must be list or dict in static graph mode")
self._inputs = self._verify_spec(inputs, True)
self._labels = self._verify_spec(labels)
@ -885,7 +885,6 @@ class Model(object):
from paddle.static import InputSpec
device = paddle.set_device('cpu') # or 'gpu'
paddle.disable_static(device)
net = nn.Sequential(
nn.Linear(784, 200),
@ -930,7 +929,6 @@ class Model(object):
from paddle.static import InputSpec
device = paddle.set_device('cpu') # or 'gpu'
paddle.disable_static(device)
net = nn.Sequential(
nn.Linear(784, 200),
@ -970,9 +968,12 @@ class Model(object):
import numpy as np
import paddle
import paddle.nn as nn
from paddle.static import InputSpec
device = paddle.set_device('cpu') # or 'gpu'
paddle.disable_static(device)
input = InputSpec([None, 784], 'float32', 'x')
label = InputSpec([None, 1], 'int64', 'label')
net = nn.Sequential(
nn.Linear(784, 200),
@ -980,7 +981,7 @@ class Model(object):
nn.Linear(200, 10),
nn.Softmax())
model = paddle.Model(net)
model = paddle.Model(net, input, label)
model.prepare()
data = np.random.random(size=(4,784)).astype(np.float32)
out = model.test_batch([data])
@ -1026,6 +1027,7 @@ class Model(object):
def __init__(self):
super(Mnist, self).__init__()
self.net = nn.Sequential(
nn.Flatten(1),
nn.Linear(784, 200),
nn.Tanh(),
nn.Linear(200, 10),
@ -1045,7 +1047,7 @@ class Model(object):
optim = paddle.optimizer.SGD(learning_rate=1e-3,
parameters=model.parameters())
model.prepare(optim, paddle.nn.CrossEntropyLoss())
data = paddle.vision.datasets.MNIST(mode='train', chw_format=False)
data = paddle.vision.datasets.MNIST(mode='train')
model.fit(data, epochs=1, batch_size=32, verbose=0)
model.save('checkpoint/test') # save for training
model.save('inference_model', False) # save for inference
@ -1092,15 +1094,18 @@ class Model(object):
import paddle
import paddle.nn as nn
from paddle.static import InputSpec
device = paddle.set_device('cpu')
paddle.disable_static(device)
input = InputSpec([None, 784], 'float32', 'x')
model = paddle.Model(nn.Sequential(
nn.Linear(784, 200),
nn.Tanh(),
nn.Linear(200, 10),
nn.Softmax()))
nn.Softmax()), input)
model.save('checkpoint/test')
model.load('checkpoint/test')
"""
@ -1165,13 +1170,15 @@ class Model(object):
import paddle
import paddle.nn as nn
from paddle.static import InputSpec
paddle.disable_static()
input = InputSpec([None, 784], 'float32', 'x')
model = paddle.Model(nn.Sequential(
nn.Linear(784, 200),
nn.Tanh(),
nn.Linear(200, 10)))
nn.Linear(200, 10)), input)
params = model.parameters()
"""
return self._adapter.parameters()
@ -1313,7 +1320,7 @@ class Model(object):
label = InputSpec([None, 1], 'int64', 'label')
model = paddle.Model(
paddle.vision.models.LeNet(classifier_activation=None),
paddle.vision.models.LeNet(),
input, label)
optim = paddle.optimizer.Adam(
learning_rate=0.001, parameters=model.parameters())
@ -1350,7 +1357,7 @@ class Model(object):
label = InputSpec([None, 1], 'int64', 'label')
model = paddle.Model(
paddle.vision.models.LeNet(classifier_activation=None), input, label)
paddle.vision.models.LeNet(), input, label)
optim = paddle.optimizer.Adam(
learning_rate=0.001, parameters=model.parameters())
model.prepare(
@ -1483,7 +1490,7 @@ class Model(object):
# imperative mode
paddle.disable_static()
model = paddle.Model(paddle.vision.models.LeNet())
model = paddle.Model(paddle.vision.models.LeNet(), input, label)
model.prepare(metrics=paddle.metric.Accuracy())
result = model.evaluate(val_dataset, batch_size=64)
print(result)
@ -1580,19 +1587,20 @@ class Model(object):
test_dataset = MnistDataset(mode='test', return_label=False)
# declarative mode
# imperative mode
input = InputSpec([-1, 1, 28, 28], 'float32', 'image')
model = paddle.Model(paddle.vision.models.LeNet(), input)
model.prepare()
result = model.predict(test_dataset, batch_size=64)
print(len(result[0]), result[0][0].shape)
# imperative mode
# declarative mode
device = paddle.set_device('cpu')
paddle.disable_static(device)
model = paddle.Model(paddle.vision.models.LeNet())
paddle.enable_static()
input = InputSpec([-1, 1, 28, 28], 'float32', 'image')
model = paddle.Model(paddle.vision.models.LeNet(), input)
model.prepare()
result = model.predict(test_dataset, batch_size=64)
print(len(result[0]), result[0][0].shape)
"""
@ -1832,15 +1840,11 @@ class Model(object):
import paddle
from paddle.static import InputSpec
dynamic = True
device = paddle.set_device('cpu')
paddle.disable_static(device) if dynamic else None
input = InputSpec([None, 1, 28, 28], 'float32', 'image')
label = InputSpec([None, 1], 'int64', 'label')
model = paddle.Model(paddle.vision.LeNet(classifier_activation=None),
model = paddle.Model(paddle.vision.LeNet(),
input, label)
optim = paddle.optimizer.Adam(
learning_rate=0.001, parameters=model.parameters())

@ -182,7 +182,6 @@ class Accuracy(Metric):
import numpy as np
import paddle
paddle.disable_static()
x = paddle.to_tensor(np.array([
[0.1, 0.2, 0.3, 0.4],
[0.1, 0.4, 0.3, 0.2],
@ -202,11 +201,13 @@ class Accuracy(Metric):
.. code-block:: python
import paddle
paddle.disable_static()
from paddle.static import InputSpec
input = InputSpec([None, 1, 28, 28], 'float32', 'image')
label = InputSpec([None, 1], 'int64', 'label')
train_dataset = paddle.vision.datasets.MNIST(mode='train')
model = paddle.Model(paddle.vision.LeNet(classifier_activation=None))
model = paddle.Model(paddle.vision.LeNet(), input, label)
optim = paddle.optimizer.Adam(
learning_rate=0.001, parameters=model.parameters())
model.prepare(

@ -8,10 +8,6 @@ foreach(TEST_OP ${DIST_TEST_OPS})
list(REMOVE_ITEM TEST_OPS ${TEST_OP})
endforeach()
# disable test_pretrained_model and test_vision_models
list(REMOVE_ITEM TEST_OPS test_pretrained_model)
list(REMOVE_ITEM TEST_OPS test_vision_models)
foreach(src ${TEST_OPS})
py_test(${src} SRCS ${src}.py)
endforeach()

@ -68,7 +68,7 @@ class TestDistTraning(unittest.TestCase):
inputs = [Input(im_shape, 'float32', 'image')]
labels = [Input([None, 1], 'int64', 'label')]
model = Model(LeNet(classifier_activation=None), inputs, labels)
model = Model(LeNet(), inputs, labels)
optim = fluid.optimizer.Momentum(
learning_rate=0.001, momentum=.9, parameter_list=model.parameters())
model.prepare(optim, CrossEntropyLoss(), Accuracy())

@ -67,7 +67,7 @@ class TestDistTraning(unittest.TestCase):
inputs = [Input(im_shape, 'float32', 'image')]
labels = [Input([None, 1], 'int64', 'label')]
model = Model(LeNet(classifier_activation=None), inputs, labels)
model = Model(LeNet(), inputs, labels)
optim = fluid.optimizer.Momentum(
learning_rate=0.001, momentum=.9, parameter_list=model.parameters())
model.prepare(optim, CrossEntropyLoss(), Accuracy())

@ -40,7 +40,7 @@ from paddle.fluid.dygraph.dygraph_to_static.program_translator import ProgramTra
class LeNetDygraph(paddle.nn.Layer):
def __init__(self, num_classes=10, classifier_activation=None):
def __init__(self, num_classes=10):
super(LeNetDygraph, self).__init__()
self.num_classes = num_classes
self.features = Sequential(
@ -55,8 +55,7 @@ class LeNetDygraph(paddle.nn.Layer):
if num_classes > 0:
self.fc = Sequential(
Linear(400, 120), Linear(120, 84), Linear(84, 10),
Softmax()) #Todo: accept any activation
Linear(400, 120), Linear(120, 84), Linear(84, 10))
def forward(self, inputs):
x = self.features(inputs)
@ -67,6 +66,34 @@ class LeNetDygraph(paddle.nn.Layer):
return x
class LeNetDeclarative(fluid.dygraph.Layer):
def __init__(self, num_classes=10):
super(LeNetDeclarative, self).__init__()
self.num_classes = num_classes
self.features = Sequential(
Conv2d(
1, 6, 3, stride=1, padding=1),
ReLU(),
Pool2D(2, 'max', 2),
Conv2d(
6, 16, 5, stride=1, padding=0),
ReLU(),
Pool2D(2, 'max', 2))
if num_classes > 0:
self.fc = Sequential(
Linear(400, 120), Linear(120, 84), Linear(84, 10))
@declarative
def forward(self, inputs):
x = self.features(inputs)
if self.num_classes > 0:
x = fluid.layers.flatten(x, 1)
x = self.fc(x)
return x
class MnistDataset(MNIST):
def __init__(self, mode, return_label=True, sample_num=None):
super(MnistDataset, self).__init__(mode=mode)
@ -198,7 +225,7 @@ class TestModel(unittest.TestCase):
paddle.manual_seed(seed)
paddle.framework.random._manual_program_seed(seed)
net = LeNet(classifier_activation=None)
net = LeNet()
optim_new = fluid.optimizer.Adam(
learning_rate=0.001, parameter_list=net.parameters())
model = Model(net, inputs=self.inputs, labels=self.labels)
@ -287,14 +314,12 @@ class TestModel(unittest.TestCase):
class MyModel(paddle.nn.Layer):
def __init__(self, classifier_activation='softmax'):
def __init__(self):
super(MyModel, self).__init__()
self._fc = Linear(20, 10)
self._act = Softmax() #Todo: accept any activation
def forward(self, x):
y = self._fc(x)
y = self._act(y)
return y
@ -311,7 +336,7 @@ class TestModelFunction(unittest.TestCase):
def get_expect():
fluid.enable_dygraph(fluid.CPUPlace())
self.set_seed()
m = MyModel(classifier_activation=None)
m = MyModel()
optim = fluid.optimizer.SGD(learning_rate=0.001,
parameter_list=m.parameters())
m.train()
@ -330,7 +355,7 @@ class TestModelFunction(unittest.TestCase):
fluid.enable_dygraph(device) if dynamic else None
self.set_seed()
net = MyModel(classifier_activation=None)
net = MyModel()
optim2 = fluid.optimizer.SGD(learning_rate=0.001,
parameter_list=net.parameters())
@ -374,7 +399,7 @@ class TestModelFunction(unittest.TestCase):
for dynamic in [True, False]:
device = paddle.set_device('cpu')
fluid.enable_dygraph(device) if dynamic else None
net = MyModel(classifier_activation=None)
net = MyModel()
inputs = [InputSpec([None, 20], 'float32', 'x')]
labels = [InputSpec([None, 1], 'int64', 'label')]
optim = fluid.optimizer.SGD(learning_rate=0.001,
@ -417,7 +442,7 @@ class TestModelFunction(unittest.TestCase):
fluid.enable_dygraph(device)
inputs = [InputSpec([None, 20], 'float32', 'x')]
labels = [InputSpec([None, 1], 'int64', 'label')]
model = Model(MyModel(classifier_activation=None), inputs, labels)
model = Model(MyModel(), inputs, labels)
optim = fluid.optimizer.SGD(learning_rate=0.001,
parameter_list=model.parameters())
model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))
@ -426,7 +451,7 @@ class TestModelFunction(unittest.TestCase):
inputs = [InputSpec([None, 20], 'float32', 'x')]
labels = [InputSpec([None, 1], 'int64', 'label')]
model = Model(MyModel(classifier_activation=None), inputs, labels)
model = Model(MyModel(), inputs, labels)
optim = fluid.optimizer.SGD(learning_rate=0.001,
parameter_list=model.parameters())
model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))
@ -436,7 +461,7 @@ class TestModelFunction(unittest.TestCase):
def test_static_save_dynamic_load(self):
path = tempfile.mkdtemp()
net = MyModel(classifier_activation=None)
net = MyModel()
inputs = [InputSpec([None, 20], 'float32', 'x')]
labels = [InputSpec([None, 1], 'int64', 'label')]
optim = fluid.optimizer.SGD(learning_rate=0.001,
@ -448,7 +473,7 @@ class TestModelFunction(unittest.TestCase):
device = paddle.set_device('cpu')
fluid.enable_dygraph(device) #if dynamic else None
net = MyModel(classifier_activation=None)
net = MyModel()
inputs = [InputSpec([None, 20], 'float32', 'x')]
labels = [InputSpec([None, 1], 'int64', 'label')]
optim = fluid.optimizer.SGD(learning_rate=0.001,
@ -557,7 +582,7 @@ class TestModelFunction(unittest.TestCase):
class TestRaiseError(unittest.TestCase):
def test_input_without_name(self):
net = MyModel(classifier_activation=None)
net = MyModel()
inputs = [InputSpec([None, 10], 'float32')]
labels = [InputSpec([None, 1], 'int64', 'label')]
@ -567,7 +592,7 @@ class TestRaiseError(unittest.TestCase):
def test_input_without_input_spec(self):
for dynamic in [True, False]:
paddle.disable_static() if dynamic else None
net = MyModel(classifier_activation=None)
net = MyModel()
with self.assertRaises(TypeError):
model = Model(net)
paddle.enable_static()

@ -13,6 +13,8 @@
# limitations under the License.
import unittest
import tempfile
import shutil
import numpy as np
import paddle
@ -23,27 +25,36 @@ import paddle.vision.models as models
# 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:
paddle.disable_static()
net = models.__dict__[arch](pretrained=True, classifier_activation=None)
inputs = [InputSpec([None, 3, 224, 224], 'float32', 'image')]
model = paddle.Model(network=net, inputs=inputs)
model.prepare()
res = model.test_batch(x)
if dygraph:
paddle.enable_static()
return res
def infer(self, arch):
path = tempfile.mkdtemp()
x = np.array(np.random.random((2, 3, 224, 224)), dtype=np.float32)
res = {}
for dygraph in [True, False]:
if not dygraph:
paddle.enable_static()
net = models.__dict__[arch]()
inputs = [InputSpec([None, 3, 224, 224], 'float32', 'image')]
model = paddle.Model(network=net, inputs=inputs)
model.prepare()
if dygraph:
model.save(path)
res['dygraph'] = model.test_batch(x)
else:
model.load(path)
res['static'] = model.test_batch(x)
if not dygraph:
paddle.disable_static()
shutil.rmtree(path)
np.testing.assert_allclose(res['dygraph'], res['static'])
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)
self.infer(arch)
if __name__ == '__main__':

@ -36,7 +36,7 @@ class TestVisonModels(unittest.TestCase):
model.test_batch(x)
def test_mobilenetv2_pretrained(self):
self.models_infer('mobilenet_v2', pretrained=True)
self.models_infer('mobilenet_v2', pretrained=False)
def test_mobilenetv1(self):
self.models_infer('mobilenet_v1')

@ -12,20 +12,19 @@
#See the License for the specific language governing permissions and
#limitations under the License.
import paddle.fluid as fluid
from paddle.nn import Conv2d, Pool2D, Linear, ReLU, Sequential, Softmax
import paddle
import paddle.nn as nn
__all__ = ['LeNet']
class LeNet(fluid.dygraph.Layer):
class LeNet(nn.Layer):
"""LeNet model from
`"LeCun Y, Bottou L, Bengio Y, et al. Gradient-based learning applied to document recognition[J]. Proceedings of the IEEE, 1998, 86(11): 2278-2324.`_
Args:
num_classes (int): output dim of last fc layer. If num_classes <=0, last fc layer
will not be defined. Default: 10.
classifier_activation (str): activation for the last fc layer. Default: 'softmax'.
Examples:
.. code-block:: python
@ -35,28 +34,27 @@ class LeNet(fluid.dygraph.Layer):
model = LeNet()
"""
def __init__(self, num_classes=10, classifier_activation='softmax'):
def __init__(self, num_classes=10):
super(LeNet, self).__init__()
self.num_classes = num_classes
self.features = Sequential(
Conv2d(
self.features = nn.Sequential(
nn.Conv2d(
1, 6, 3, stride=1, padding=1),
ReLU(),
Pool2D(2, 'max', 2),
Conv2d(
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(
6, 16, 5, stride=1, padding=0),
ReLU(),
Pool2D(2, 'max', 2))
nn.ReLU(),
nn.MaxPool2d(2, 2))
if num_classes > 0:
self.fc = Sequential(
Linear(400, 120), Linear(120, 84), Linear(84, 10),
Softmax()) #Todo: accept any activation
self.fc = nn.Sequential(
nn.Linear(400, 120), nn.Linear(120, 84), nn.Linear(84, 10))
def forward(self, inputs):
x = self.features(inputs)
if self.num_classes > 0:
x = fluid.layers.flatten(x, 1)
x = paddle.flatten(x, 1)
x = self.fc(x)
return x

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -12,9 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import paddle.fluid as fluid
from paddle.nn import Conv2d, Pool2D, BatchNorm, Linear, ReLU, Softmax
from paddle.fluid.dygraph.container import Sequential
import paddle
import paddle.nn as nn
from paddle.utils.download import get_weights_path_from_url
@ -28,39 +27,18 @@ __all__ = [
model_urls = {
'vgg16': ('https://paddle-hapi.bj.bcebos.com/models/vgg16.pdparams',
'c788f453a3b999063e8da043456281ee')
'89bbffc0f87d260be9b8cdc169c991c4')
}
class Classifier(fluid.dygraph.Layer):
def __init__(self, num_classes, classifier_activation='softmax'):
super(Classifier, self).__init__()
self.linear1 = Linear(512 * 7 * 7, 4096)
self.linear2 = Linear(4096, 4096)
self.linear3 = Linear(4096, num_classes)
self.act = Softmax() #Todo: accept any activation
def forward(self, x):
x = self.linear1(x)
x = fluid.layers.relu(x)
x = fluid.layers.dropout(x, 0.5)
x = self.linear2(x)
x = fluid.layers.relu(x)
x = fluid.layers.dropout(x, 0.5)
x = self.linear3(x)
out = self.act(x)
return out
class VGG(fluid.dygraph.Layer):
class VGG(nn.Layer):
"""VGG model from
`"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`_
Args:
features (fluid.dygraph.Layer): vgg features create by function make_layers.
features (nn.Layer): vgg features create by function make_layers.
num_classes (int): output dim of last fc layer. If num_classes <=0, last fc layer
will not be defined. Default: 1000.
classifier_activation (str): activation for the last fc layer. Default: 'softmax'.
Examples:
.. code-block:: python
@ -76,44 +54,41 @@ class VGG(fluid.dygraph.Layer):
"""
def __init__(self,
features,
num_classes=1000,
classifier_activation='softmax'):
def __init__(self, features, num_classes=1000):
super(VGG, self).__init__()
self.features = features
self.num_classes = num_classes
if num_classes > 0:
classifier = Classifier(num_classes, classifier_activation)
self.classifier = self.add_sublayer("classifier",
Sequential(classifier))
self.avgpool = nn.AdaptiveAvgPool2d((7, 7))
self.classifier = nn.Sequential(
nn.Linear(512 * 7 * 7, 4096),
nn.ReLU(),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(),
nn.Dropout(),
nn.Linear(4096, num_classes), )
def forward(self, x):
x = self.features(x)
if self.num_classes > 0:
x = fluid.layers.flatten(x, 1)
x = self.classifier(x)
x = self.avgpool(x)
x = paddle.flatten(x, 1)
x = self.classifier(x)
return x
def make_layers(cfg, batch_norm=False):
layers = []
in_channels = 3
for v in cfg:
if v == 'M':
layers += [Pool2D(pool_size=2, pool_stride=2)]
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
if batch_norm:
conv2d = Conv2d(in_channels, v, kernel_size=3, padding=1)
layers += [conv2d, BatchNorm(v), ReLU()]
layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU()]
else:
conv2d = Conv2d(in_channels, v, kernel_size=3, padding=1)
layers += [conv2d, ReLU()]
layers += [conv2d, nn.ReLU()]
in_channels = v
return Sequential(*layers)
return nn.Sequential(*layers)
cfgs = {
@ -144,7 +119,7 @@ def _vgg(arch, cfg, batch_norm, pretrained, **kwargs):
model_urls[arch][1])
assert weight_path.endswith(
'.pdparams'), "suffix of weight must be .pdparams"
param, _ = fluid.load_dygraph(weight_path)
param, _ = paddle.load(weight_path)
model.load_dict(param)
return model

Loading…
Cancel
Save