test_analysis_helper.py 3.0 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 25 26 27 28
# 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 sys
sys.path.append("../")
import unittest
import paddle
from paddleslim.common import VarCollector
from static_case import StaticCase
sys.path.append("../demo")
from models import MobileNet
from layers import conv_bn_layer
import paddle.dataset.mnist as reader
import numpy as np


class TestAnalysisHelper(StaticCase):
    def test_analysis_helper(self):
B
Bai Yifan 已提交
29 30 31
        image = paddle.static.data(
            name='image', shape=[None, 1, 28, 28], dtype='float32')
        label = paddle.static.data(name='label', shape=[None, 1], dtype='int64')
32 33
        model = MobileNet()
        out = model.net(input=image, class_dim=10)
B
Bai Yifan 已提交
34 35 36 37 38
        cost = paddle.nn.functional.loss.cross_entropy(input=out, label=label)
        avg_cost = paddle.mean(x=cost)
        acc_top1 = paddle.metric.accuracy(input=out, label=label, k=1)
        acc_top5 = paddle.metric.accuracy(input=out, label=label, k=5)
        optimizer = paddle.optimizer.Momentum(
39 40
            momentum=0.9,
            learning_rate=0.01,
B
Bai Yifan 已提交
41
            weight_decay=paddle.regularizer.L2Decay(4e-5))
42
        optimizer.minimize(avg_cost)
B
Bai Yifan 已提交
43
        main_prog = paddle.static.default_main_program()
44

B
Bai Yifan 已提交
45 46 47 48 49 50 51 52 53 54 55 56
        places = paddle.static.cuda_places() if paddle.is_compiled_with_cuda(
        ) else paddle.static.cpu_places()
        exe = paddle.static.Executor(places[0])

        def transform(x):
            return np.reshape(x, [1, 28, 28])

        train_dataset = paddle.vision.datasets.MNIST(
            mode='train', backend='cv2', transform=transform)
        train_loader = paddle.io.DataLoader(
            train_dataset,
            places=places,
57
            feed_list=[image, label],
B
Bai Yifan 已提交
58
            drop_last=True,
59
            return_list=False,
B
Bai Yifan 已提交
60 61
            batch_size=64)
        exe.run(paddle.static.default_startup_program())
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

        vars = ['conv2d_0.tmp_0', 'fc_0.tmp_0', 'fc_0.tmp_1', 'fc_0.tmp_2']
        var_collector1 = VarCollector(main_prog, vars, use_ema=True)
        values = var_collector1.abs_max_run(
            train_loader, exe, step=None, loss_name=avg_cost.name)
        vars = [v.name for v in main_prog.list_vars() if v.persistable]
        var_collector2 = VarCollector(main_prog, vars, use_ema=False)
        values = var_collector2.run(train_loader,
                                    exe,
                                    step=None,
                                    loss_name=avg_cost.name)
        var_collector2.pdf(values)


if __name__ == '__main__':
W
whs 已提交
77
    unittest.main()