test_model.py 24.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 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.

from __future__ import division
from __future__ import print_function

import unittest

import os
import numpy as np
import shutil
import tempfile

L
Leo Chen 已提交
25
import paddle
26
from paddle import fluid
27
from paddle import to_tensor
C
cnn 已提交
28
from paddle.nn import Conv2D, Linear, ReLU, Sequential, Softmax
29

30 31
from paddle import Model
from paddle.static import InputSpec
32
from paddle.nn.layer.loss import CrossEntropyLoss
33
from paddle.metric import Accuracy
34 35
from paddle.vision.datasets import MNIST
from paddle.vision.models import LeNet
36
from paddle.io import DistributedBatchSampler, Dataset
37
from paddle.hapi.model import prepare_distributed_context
38 39
from paddle.fluid.dygraph.jit import declarative
from paddle.fluid.dygraph.dygraph_to_static.program_translator import ProgramTranslator
40 41


42
class LeNetDygraph(paddle.nn.Layer):
L
LielinJiang 已提交
43
    def __init__(self, num_classes=10):
44 45 46
        super(LeNetDygraph, self).__init__()
        self.num_classes = num_classes
        self.features = Sequential(
C
cnn 已提交
47
            Conv2D(
48
                1, 6, 3, stride=1, padding=1),
L
LielinJiang 已提交
49
            ReLU(),
50
            paddle.fluid.dygraph.Pool2D(2, 'max', 2),
C
cnn 已提交
51
            Conv2D(
52
                6, 16, 5, stride=1, padding=0),
L
LielinJiang 已提交
53
            ReLU(),
54
            paddle.fluid.dygraph.Pool2D(2, 'max', 2))
55 56 57

        if num_classes > 0:
            self.fc = Sequential(
L
LielinJiang 已提交
58
                Linear(400, 120), Linear(120, 84), Linear(84, 10))
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

    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)
        self.return_label = return_label
        if sample_num:
            self.images = self.images[:sample_num]
            self.labels = self.labels[:sample_num]

    def __getitem__(self, idx):
        img, label = self.images[idx], self.labels[idx]
        img = np.reshape(img, [1, 28, 28])
        if self.return_label:
            return img, np.array(self.labels[idx]).astype('int64')
        return img,

    def __len__(self):
        return len(self.images)


def compute_acc(pred, label):
    pred = np.argmax(pred, -1)
    label = np.array(label)
    correct = pred[:, np.newaxis] == label
    return np.sum(correct) / correct.shape[0]


def dynamic_train(model, dataloader):
    optim = fluid.optimizer.Adam(
        learning_rate=0.001, parameter_list=model.parameters())
    model.train()
    for inputs, labels in dataloader:
        outputs = model(inputs)
101
        loss = CrossEntropyLoss(reduction="sum")(outputs, labels)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        avg_loss = fluid.layers.reduce_sum(loss)
        avg_loss.backward()
        optim.minimize(avg_loss)
        model.clear_gradients()


def dynamic_evaluate(model, dataloader):
    with fluid.dygraph.no_grad():
        model.eval()
        cnt = 0
        for inputs, labels in dataloader:
            outputs = model(inputs)

            cnt += (np.argmax(outputs.numpy(), -1)[:, np.newaxis] ==
                    labels.numpy()).astype('int').sum()

    return cnt / len(dataloader.dataset)


@unittest.skipIf(not fluid.is_compiled_with_cuda(),
                 'CPU testing is not supported')
class TestModel(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        if not fluid.is_compiled_with_cuda():
            self.skipTest('module not tested when ONLY_CPU compling')
128
        cls.device = paddle.set_device('gpu')
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        fluid.enable_dygraph(cls.device)

        sp_num = 1280
        cls.train_dataset = MnistDataset(mode='train', sample_num=sp_num)
        cls.val_dataset = MnistDataset(mode='test', sample_num=sp_num)
        cls.test_dataset = MnistDataset(
            mode='test', return_label=False, sample_num=sp_num)

        cls.train_loader = fluid.io.DataLoader(
            cls.train_dataset, places=cls.device, batch_size=64)
        cls.val_loader = fluid.io.DataLoader(
            cls.val_dataset, places=cls.device, batch_size=64)
        cls.test_loader = fluid.io.DataLoader(
            cls.test_dataset, places=cls.device, batch_size=64)

        seed = 333
C
cnn 已提交
145
        paddle.seed(seed)
L
Leo Chen 已提交
146
        paddle.framework.random._manual_program_seed(seed)
147 148 149 150 151 152 153

        dy_lenet = LeNetDygraph()
        cls.init_param = dy_lenet.state_dict()
        dynamic_train(dy_lenet, cls.train_loader)

        cls.acc1 = dynamic_evaluate(dy_lenet, cls.val_loader)

154 155
        cls.inputs = [InputSpec([-1, 1, 28, 28], 'float32', 'image')]
        cls.labels = [InputSpec([None, 1], 'int64', 'label')]
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

        cls.save_dir = tempfile.mkdtemp()
        cls.weight_path = os.path.join(cls.save_dir, 'lenet')
        fluid.dygraph.save_dygraph(dy_lenet.state_dict(), cls.weight_path)

        fluid.disable_dygraph()

    @classmethod
    def tearDownClass(cls):
        shutil.rmtree(cls.save_dir)

    def test_fit_dygraph(self):
        self.fit(True)

    def test_fit_static(self):
        self.fit(False)

173 174 175 176 177 178
    def test_fit_dynamic_with_rank(self):
        self.fit(True, 2, 0)

    def test_fit_static_with_rank(self):
        self.fit(False, 2, 0)

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    def test_evaluate_dygraph(self):
        self.evaluate(True)

    def test_evaluate_static(self):
        self.evaluate(False)

    def test_predict_dygraph(self):
        self.predict(True)

    def test_predict_static(self):
        self.predict(False)

    def test_prepare_context(self):
        prepare_distributed_context()

194
    def fit(self, dynamic, num_replicas=None, rank=None):
195 196
        fluid.enable_dygraph(self.device) if dynamic else None
        seed = 333
C
cnn 已提交
197
        paddle.seed(seed)
L
Leo Chen 已提交
198
        paddle.framework.random._manual_program_seed(seed)
199

L
LielinJiang 已提交
200
        net = LeNet()
201
        optim_new = fluid.optimizer.Adam(
202 203
            learning_rate=0.001, parameter_list=net.parameters())
        model = Model(net, inputs=self.inputs, labels=self.labels)
204 205
        model.prepare(
            optim_new,
206
            loss=CrossEntropyLoss(reduction="sum"),
207
            metrics=Accuracy())
208 209 210 211 212 213
        model.fit(self.train_dataset, batch_size=64, shuffle=False)

        result = model.evaluate(self.val_dataset, batch_size=64)
        np.testing.assert_allclose(result['acc'], self.acc1)

        train_sampler = DistributedBatchSampler(
214 215 216 217 218
            self.train_dataset,
            batch_size=64,
            shuffle=False,
            num_replicas=num_replicas,
            rank=rank)
219
        val_sampler = DistributedBatchSampler(
220 221 222 223 224
            self.val_dataset,
            batch_size=64,
            shuffle=False,
            num_replicas=num_replicas,
            rank=rank)
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

        train_loader = fluid.io.DataLoader(
            self.train_dataset,
            batch_sampler=train_sampler,
            places=self.device,
            return_list=True)

        val_loader = fluid.io.DataLoader(
            self.val_dataset,
            batch_sampler=val_sampler,
            places=self.device,
            return_list=True)

        model.fit(train_loader, val_loader)
        fluid.disable_dygraph() if dynamic else None

    def evaluate(self, dynamic):
        fluid.enable_dygraph(self.device) if dynamic else None
243 244
        model = Model(LeNet(), self.inputs, self.labels)
        model.prepare(metrics=Accuracy())
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
        model.load(self.weight_path)
        result = model.evaluate(self.val_dataset, batch_size=64)
        np.testing.assert_allclose(result['acc'], self.acc1)

        sampler = DistributedBatchSampler(
            self.val_dataset, batch_size=64, shuffle=False)

        val_loader = fluid.io.DataLoader(
            self.val_dataset,
            batch_sampler=sampler,
            places=self.device,
            return_list=True)

        model.evaluate(val_loader)

        fluid.disable_dygraph() if dynamic else None

    def predict(self, dynamic):
        fluid.enable_dygraph(self.device) if dynamic else None
264 265
        model = Model(LeNet(), self.inputs)
        model.prepare()
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
        model.load(self.weight_path)
        output = model.predict(
            self.test_dataset, batch_size=64, stack_outputs=True)
        np.testing.assert_equal(output[0].shape[0], len(self.test_dataset))

        acc = compute_acc(output[0], self.val_dataset.labels)
        np.testing.assert_allclose(acc, self.acc1)

        sampler = DistributedBatchSampler(
            self.test_dataset, batch_size=64, shuffle=False)

        test_loader = fluid.io.DataLoader(
            self.test_dataset,
            batch_sampler=sampler,
            places=self.device,
            return_list=True)

        model.evaluate(test_loader)

        fluid.disable_dygraph() if dynamic else None


288
class MyModel(paddle.nn.Layer):
L
LielinJiang 已提交
289
    def __init__(self):
290
        super(MyModel, self).__init__()
291
        self._fc = Linear(20, 10)
292 293 294 295 296 297

    def forward(self, x):
        y = self._fc(x)
        return y


298 299 300 301 302 303 304 305 306
class MyDataset(Dataset):
    def __getitem__(self, idx):
        return np.random.random(size=(20,)).astype(np.float32), \
               np.random.randint(0, 10, size=(1,)).astype(np.int64)

    def __len__(self):
        return 40


307 308
class TestModelFunction(unittest.TestCase):
    def set_seed(self, seed=1024):
C
cnn 已提交
309
        paddle.seed(seed)
L
Leo Chen 已提交
310
        paddle.framework.random._manual_program_seed(seed)
311 312 313 314 315 316 317 318 319

    def test_train_batch(self, dynamic=True):
        dim = 20
        data = np.random.random(size=(4, dim)).astype(np.float32)
        label = np.random.randint(0, 10, size=(4, 1)).astype(np.int64)

        def get_expect():
            fluid.enable_dygraph(fluid.CPUPlace())
            self.set_seed()
L
LielinJiang 已提交
320
            m = MyModel()
321 322 323
            optim = fluid.optimizer.SGD(learning_rate=0.001,
                                        parameter_list=m.parameters())
            m.train()
324 325
            output = m(to_tensor(data))
            loss = CrossEntropyLoss(reduction='sum')(output, to_tensor(label))
326 327 328 329 330 331 332 333 334
            avg_loss = fluid.layers.reduce_sum(loss)
            avg_loss.backward()
            optim.minimize(avg_loss)
            m.clear_gradients()
            fluid.disable_dygraph()
            return avg_loss.numpy()

        ref = get_expect()
        for dynamic in [True, False]:
335
            device = paddle.set_device('cpu')
336 337 338
            fluid.enable_dygraph(device) if dynamic else None
            self.set_seed()

L
LielinJiang 已提交
339
            net = MyModel()
340
            optim2 = fluid.optimizer.SGD(learning_rate=0.001,
341
                                         parameter_list=net.parameters())
342

343 344
            inputs = [InputSpec([None, dim], 'float32', 'x')]
            labels = [InputSpec([None, 1], 'int64', 'label')]
345
            model = Model(net, inputs, labels)
346
            model.prepare(optim2, loss=CrossEntropyLoss(reduction="sum"))
347 348 349 350
            loss, = model.train_batch([data], [label])
            np.testing.assert_allclose(loss.flatten(), ref.flatten())
            fluid.disable_dygraph() if dynamic else None

351
    def test_test_batch(self):
352 353 354 355 356 357 358 359
        dim = 20
        data = np.random.random(size=(4, dim)).astype(np.float32)

        def get_expect():
            fluid.enable_dygraph(fluid.CPUPlace())
            self.set_seed()
            m = MyModel()
            m.eval()
360
            output = m(to_tensor(data))
361 362 363 364 365
            fluid.disable_dygraph()
            return output.numpy()

        ref = get_expect()
        for dynamic in [True, False]:
366
            device = paddle.set_device('cpu')
367 368
            fluid.enable_dygraph(device) if dynamic else None
            self.set_seed()
369
            net = MyModel()
370
            inputs = [InputSpec([None, dim], 'float32', 'x')]
371 372
            model = Model(net, inputs)
            model.prepare()
373 374
            out, = model.test_batch([data])

375
            np.testing.assert_allclose(out, ref, rtol=1e-6)
376 377 378 379 380
            fluid.disable_dygraph() if dynamic else None

    def test_save_load(self):
        path = tempfile.mkdtemp()
        for dynamic in [True, False]:
381
            device = paddle.set_device('cpu')
382
            fluid.enable_dygraph(device) if dynamic else None
L
LielinJiang 已提交
383
            net = MyModel()
384 385
            inputs = [InputSpec([None, 20], 'float32', 'x')]
            labels = [InputSpec([None, 1], 'int64', 'label')]
386
            optim = fluid.optimizer.SGD(learning_rate=0.001,
387 388
                                        parameter_list=net.parameters())
            model = Model(net, inputs, labels)
389
            model.prepare(
390
                optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))
391 392 393 394 395
            model.save(path + '/test')
            model.load(path + '/test')
            shutil.rmtree(path)
            fluid.disable_dygraph() if dynamic else None

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    def test_dynamic_load(self):
        mnist_data = MnistDataset(mode='train')
        for new_optimizer in [True, False]:
            path = tempfile.mkdtemp()
            paddle.disable_static()
            net = LeNet()
            inputs = [InputSpec([None, 1, 28, 28], 'float32', 'x')]
            labels = [InputSpec([None, 1], 'int64', 'label')]
            if new_optimizer:
                optim = paddle.optimizer.Adam(
                    learning_rate=0.001, parameters=net.parameters())
            else:
                optim = fluid.optimizer.Adam(
                    learning_rate=0.001, parameter_list=net.parameters())
            model = Model(net, inputs, labels)
            model.prepare(
                optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))
            model.fit(mnist_data, batch_size=64, verbose=0)
            model.save(path + '/test')
            model.load(path + '/test')
            shutil.rmtree(path)
            paddle.enable_static()

419 420
    def test_dynamic_save_static_load(self):
        path = tempfile.mkdtemp()
421
        # dynamic saving
422
        device = paddle.set_device('cpu')
423
        fluid.enable_dygraph(device)
424
        model = Model(MyModel())
425 426
        optim = fluid.optimizer.SGD(learning_rate=0.001,
                                    parameter_list=model.parameters())
427
        model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))
428 429
        model.save(path + '/test')
        fluid.disable_dygraph()
430

431 432
        inputs = [InputSpec([None, 20], 'float32', 'x')]
        labels = [InputSpec([None, 1], 'int64', 'label')]
L
LielinJiang 已提交
433
        model = Model(MyModel(), inputs, labels)
434 435
        optim = fluid.optimizer.SGD(learning_rate=0.001,
                                    parameter_list=model.parameters())
436
        model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))
437 438 439 440 441 442
        model.load(path + '/test')
        shutil.rmtree(path)

    def test_static_save_dynamic_load(self):
        path = tempfile.mkdtemp()

L
LielinJiang 已提交
443
        net = MyModel()
444 445
        inputs = [InputSpec([None, 20], 'float32', 'x')]
        labels = [InputSpec([None, 1], 'int64', 'label')]
446
        optim = fluid.optimizer.SGD(learning_rate=0.001,
447 448
                                    parameter_list=net.parameters())
        model = Model(net, inputs, labels)
449
        model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))
450 451
        model.save(path + '/test')

452
        device = paddle.set_device('cpu')
453 454
        fluid.enable_dygraph(device)  #if dynamic else None

L
LielinJiang 已提交
455
        net = MyModel()
456 457
        inputs = [InputSpec([None, 20], 'float32', 'x')]
        labels = [InputSpec([None, 1], 'int64', 'label')]
458
        optim = fluid.optimizer.SGD(learning_rate=0.001,
459 460
                                    parameter_list=net.parameters())
        model = Model(net, inputs, labels)
461
        model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))
462 463 464 465 466 467
        model.load(path + '/test')
        shutil.rmtree(path)
        fluid.disable_dygraph()

    def test_parameters(self):
        for dynamic in [True, False]:
468
            device = paddle.set_device('cpu')
469
            fluid.enable_dygraph(device) if dynamic else None
470
            net = MyModel()
471
            inputs = [InputSpec([None, 20], 'float32', 'x')]
472 473
            model = Model(net, inputs)
            model.prepare()
474 475 476 477 478
            params = model.parameters()
            self.assertTrue(params[0].shape[0] == 20)
            self.assertTrue(params[0].shape[1] == 10)
            fluid.disable_dygraph() if dynamic else None

L
LielinJiang 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    def test_summary(self):
        def _get_param_from_state_dict(state_dict):
            params = 0
            for k, v in state_dict.items():
                params += np.prod(v.numpy().shape)
            return params

        for dynamic in [True, False]:
            device = paddle.set_device('cpu')
            fluid.enable_dygraph(device) if dynamic else None
            net = MyModel()
            inputs = [InputSpec([None, 20], 'float32', 'x')]
            model = Model(net, inputs)
            model.prepare()
            params_info = model.summary()
            gt_params = _get_param_from_state_dict(net.state_dict())

            np.testing.assert_allclose(params_info['total_params'], gt_params)
            print(params_info)

499 500
            model.summary(input_size=(20))
            model.summary(input_size=[(20)])
L
LielinJiang 已提交
501
            model.summary(input_size=(20), dtype='float32')
502

L
LielinJiang 已提交
503 504
    def test_summary_nlp(self):
        paddle.enable_static()
L
LielinJiang 已提交
505 506 507 508 509 510 511
        nlp_net = paddle.nn.GRU(input_size=2,
                                hidden_size=3,
                                num_layers=3,
                                direction="bidirectional")
        paddle.summary(nlp_net, (1, 1, 2))
        rnn = paddle.nn.LSTM(16, 32, 2)
        paddle.summary(rnn, [(-1, 23, 16), ((2, None, 32), (2, -1, 32))])
L
LielinJiang 已提交
512

L
LielinJiang 已提交
513 514 515 516 517
    def test_summary_dtype(self):
        input_shape = (3, 1)
        net = paddle.nn.Embedding(10, 3, sparse=True)
        paddle.summary(net, input_shape, dtypes='int64')

L
LielinJiang 已提交
518 519 520
    def test_summary_error(self):
        with self.assertRaises(TypeError):
            nlp_net = paddle.nn.GRU(input_size=2, hidden_size=3, num_layers=3)
L
LielinJiang 已提交
521
            paddle.summary(nlp_net, (1, 1, '2'))
L
LielinJiang 已提交
522 523 524 525 526 527 528

        with self.assertRaises(ValueError):
            nlp_net = paddle.nn.GRU(input_size=2, hidden_size=3, num_layers=3)
            paddle.summary(nlp_net, (-1, -1))

        paddle.disable_static()
        nlp_net = paddle.nn.GRU(input_size=2, hidden_size=3, num_layers=3)
L
LielinJiang 已提交
529
        paddle.summary(nlp_net, (1, 1, 2))
L
LielinJiang 已提交
530

531
    def test_export_deploy_model(self):
532 533
        self.set_seed()
        np.random.seed(2020)
534
        for dynamic in [True, False]:
535
            paddle.disable_static() if dynamic else None
536 537
            prog_translator = ProgramTranslator()
            prog_translator.enable(False) if not dynamic else None
538
            net = LeNet()
539
            inputs = [InputSpec([None, 1, 28, 28], 'float32', 'x')]
540 541 542 543 544 545 546
            model = Model(net, inputs)
            model.prepare()
            save_dir = tempfile.mkdtemp()
            if not os.path.exists(save_dir):
                os.makedirs(save_dir)
            tensor_img = np.array(
                np.random.random((1, 1, 28, 28)), dtype=np.float32)
547

548
            model.save(save_dir, training=False)
549
            ori_results = model.test_batch(tensor_img)
550
            fluid.disable_dygraph() if dynamic else None
551

552 553 554 555 556 557 558 559 560 561 562 563 564 565
            place = fluid.CPUPlace() if not fluid.is_compiled_with_cuda(
            ) else fluid.CUDAPlace(0)
            new_scope = fluid.Scope()
            with fluid.scope_guard(new_scope):
                exe = fluid.Executor(place)
                [inference_program, feed_target_names, fetch_targets] = (
                    fluid.io.load_inference_model(
                        dirname=save_dir, executor=exe))
                results = exe.run(inference_program,
                                  feed={feed_target_names[0]: tensor_img},
                                  fetch_list=fetch_targets)
                np.testing.assert_allclose(
                    results, ori_results, rtol=1e-5, atol=1e-7)
                shutil.rmtree(save_dir)
566
            paddle.enable_static()
567

L
LiuChiachi 已提交
568
    def test_dygraph_export_deploy_model_about_inputs(self):
569 570
        mnist_data = MnistDataset(mode='train')
        paddle.disable_static()
L
LiuChiachi 已提交
571
        # without inputs
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
        for initial in ["fit", "train_batch", "eval_batch", "test_batch"]:
            save_dir = tempfile.mkdtemp()
            if not os.path.exists(save_dir):
                os.makedirs(save_dir)
            net = LeNet()
            model = Model(net)
            optim = fluid.optimizer.Adam(
                learning_rate=0.001, parameter_list=model.parameters())
            model.prepare(
                optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))
            if initial == "fit":
                model.fit(mnist_data, batch_size=64, verbose=0)
            else:
                img = np.array(
                    np.random.random((1, 1, 28, 28)), dtype=np.float32)
                label = np.array(np.random.rand(1, 1), dtype=np.int64)
                if initial == "train_batch":
                    model.train_batch([img], [label])
                elif initial == "eval_batch":
                    model.eval_batch([img], [label])
                else:
                    model.test_batch([img])

            model.save(save_dir, training=False)
            shutil.rmtree(save_dir)
L
LiuChiachi 已提交
597 598 599 600 601 602 603 604 605 606 607 608
        # with inputs, and the type of inputs is InputSpec
        save_dir = tempfile.mkdtemp()
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        net = LeNet()
        inputs = InputSpec([None, 1, 28, 28], 'float32', 'x')
        model = Model(net, inputs)
        optim = fluid.optimizer.Adam(
            learning_rate=0.001, parameter_list=model.parameters())
        model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))
        model.save(save_dir, training=False)
        shutil.rmtree(save_dir)
609

610

611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
class TestModelWithLRScheduler(unittest.TestCase):
    def test_fit(self):
        def make_optimizer(parameters=None):
            base_lr = 1e-3
            momentum = 0.9
            weight_decay = 5e-4
            boundaries = [5, 8]
            values = [base_lr * (0.1**i) for i in range(len(boundaries) + 1)]
            learning_rate = paddle.optimizer.lr.PiecewiseDecay(
                boundaries=boundaries, values=values)
            learning_rate = paddle.optimizer.lr.LinearWarmup(
                learning_rate=learning_rate,
                warmup_steps=4,
                start_lr=base_lr / 5.,
                end_lr=base_lr,
                verbose=True)
            optimizer = paddle.optimizer.Momentum(
                learning_rate=learning_rate,
                weight_decay=weight_decay,
                momentum=momentum,
                parameters=parameters)
            return optimizer

634
        # dynamic test
635 636 637 638 639 640 641 642 643 644 645 646
        device = paddle.set_device('cpu')
        fluid.enable_dygraph(device)
        net = MyModel()
        inputs = [InputSpec([None, 20], 'float32', 'x')]
        labels = [InputSpec([None, 1], 'int64', 'label')]
        optim = make_optimizer(net.parameters())
        model = Model(net, inputs, labels)
        model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))

        dataset = MyDataset()
        model.fit(dataset, dataset, batch_size=4, epochs=10, num_workers=0)

647
        # static test
648 649
        paddle.enable_static()

650 651 652 653 654 655 656 657 658 659
        net = MyModel()
        inputs = [InputSpec([None, 20], 'float32', 'x')]
        labels = [InputSpec([None, 1], 'int64', 'label')]
        optim = make_optimizer(net.parameters())
        model = Model(net, inputs, labels)
        model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum"))

        dataset = MyDataset()
        model.fit(dataset, dataset, batch_size=4, epochs=10, num_workers=0)

660

661 662
class TestRaiseError(unittest.TestCase):
    def test_input_without_name(self):
L
LielinJiang 已提交
663
        net = MyModel()
664

665 666
        inputs = [InputSpec([None, 10], 'float32')]
        labels = [InputSpec([None, 1], 'int64', 'label')]
667 668 669
        with self.assertRaises(ValueError):
            model = Model(net, inputs, labels)

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
    def test_static_without_inputs(self):
        paddle.enable_static()
        net = MyModel()
        with self.assertRaises(TypeError):
            model = Model(net)

    def test_save_infer_model_without_inputs_and_run_in_dygraph(self):
        paddle.disable_static()
        net = MyModel()
        save_dir = tempfile.mkdtemp()
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        with self.assertRaises(RuntimeError):
            model = Model(net)
            model.save(save_dir, training=False)
        paddle.enable_static()
686

687

688 689
if __name__ == '__main__':
    unittest.main()