test_quant_aware_user_defined.py 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (c) 2019  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.quant import quant_aware, convert
19
from static_case import StaticCase
20 21 22 23 24 25
sys.path.append("../demo")
from models import MobileNet
from layers import conv_bn_layer
import numpy as np


B
Bai Yifan 已提交
26
def pact(x):
W
whs 已提交
27
    helper = paddle.fluid.layer_helper.LayerHelper("pact", **locals())
28 29
    dtype = 'float32'
    init_thres = 20
B
Bai Yifan 已提交
30
    u_param_attr = paddle.ParamAttr(
31
        name=x.name + '_pact',
B
Bai Yifan 已提交
32 33
        initializer=paddle.nn.initializer.Constant(value=init_thres),
        regularizer=paddle.regularizer.L2Decay(0.0001),
34
        learning_rate=1)
35
    u_param = helper.create_parameter(attr=u_param_attr, shape=[1], dtype=dtype)
36

B
Bai Yifan 已提交
37 38 39
    part_a = paddle.nn.functional.relu(x - u_param)
    part_b = paddle.nn.functional.relu(-u_param - x)
    x = x - part_a + part_b
40 41 42 43
    return x


def get_optimizer():
B
Bai Yifan 已提交
44
    return paddle.optimizer.Momentum(0.0001, 0.9)
45 46


47
class TestQuantAwareCase1(StaticCase):
48
    def get_model(self):
B
Bai Yifan 已提交
49 50 51
        image = paddle.static.data(
            name='image', shape=[None, 1, 28, 28], dtype='float32')
        label = paddle.static.data(name='label', shape=[None, 1], dtype='int64')
52 53
        model = MobileNet()
        out = model.net(input=image, class_dim=10)
B
Bai Yifan 已提交
54 55 56 57
        cost = paddle.nn.functional.loss.cross_entropy(input=out, label=label)
        avg_cost = paddle.mean(x=cost)
        startup_prog = paddle.static.default_startup_program()
        train_prog = paddle.static.default_main_program()
58 59 60
        return startup_prog, train_prog

    def test_accuracy(self):
B
Bai Yifan 已提交
61 62
        image = paddle.static.data(
            name='image', shape=[None, 1, 28, 28], dtype='float32')
63
        image.stop_gradient = False
B
Bai Yifan 已提交
64
        label = paddle.static.data(name='label', shape=[None, 1], dtype='int64')
65 66
        model = MobileNet()
        out = model.net(input=image, class_dim=10)
B
Bai Yifan 已提交
67 68 69 70 71
        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(
72 73
            momentum=0.9,
            learning_rate=0.01,
B
Bai Yifan 已提交
74
            weight_decay=paddle.regularizer.L2Decay(4e-5))
75
        optimizer.minimize(avg_cost)
B
Bai Yifan 已提交
76
        main_prog = paddle.static.default_main_program()
77 78
        val_prog = main_prog.clone(for_test=True)

B
Bai Yifan 已提交
79
        place = paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda(
B
Bai Yifan 已提交
80 81 82 83
        ) else paddle.CPUPlace()
        exe = paddle.static.Executor(place)
        exe.run(paddle.static.default_startup_program())

B
Bai Yifan 已提交
84 85
        def transform(x):
            return np.reshape(x, [1, 28, 28])
B
Bai Yifan 已提交
86

B
Bai Yifan 已提交
87 88 89 90 91 92 93 94 95
        train_dataset = paddle.vision.datasets.MNIST(
            mode='train', backend='cv2', transform=transform)
        test_dataset = paddle.vision.datasets.MNIST(
            mode='test', backend='cv2', transform=transform)
        train_loader = paddle.io.DataLoader(
            train_dataset,
            places=place,
            feed_list=[image, label],
            drop_last=True,
96
            return_list=False,
B
Bai Yifan 已提交
97 98
            batch_size=64)
        valid_loader = paddle.io.DataLoader(
99 100 101 102 103
            test_dataset,
            places=place,
            feed_list=[image, label],
            batch_size=64,
            return_list=False)
104 105 106

        def train(program):
            iter = 0
B
Bai Yifan 已提交
107
            for data in train_loader():
108 109
                cost, top1, top5 = exe.run(
                    program,
B
Bai Yifan 已提交
110
                    feed=data,
111 112 113 114 115 116 117 118 119 120
                    fetch_list=[avg_cost, acc_top1, acc_top5])
                iter += 1
                if iter % 100 == 0:
                    print(
                        'train iter={}, avg loss {}, acc_top1 {}, acc_top5 {}'.
                        format(iter, cost, top1, top5))

        def test(program):
            iter = 0
            result = [[], [], []]
B
Bai Yifan 已提交
121
            for data in valid_loader():
122 123
                cost, top1, top5 = exe.run(
                    program,
B
Bai Yifan 已提交
124
                    feed=data,
125 126 127
                    fetch_list=[avg_cost, acc_top1, acc_top5])
                iter += 1
                if iter % 100 == 0:
128 129
                    print('eval iter={}, avg loss {}, acc_top1 {}, acc_top5 {}'.
                          format(iter, cost, top1, top5))
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                result[0].append(cost)
                result[1].append(top1)
                result[2].append(top5)
            print(' avg loss {}, acc_top1 {}, acc_top5 {}'.format(
                np.mean(result[0]), np.mean(result[1]), np.mean(result[2])))
            return np.mean(result[1]), np.mean(result[2])

        train(main_prog)
        top1_1, top5_1 = test(main_prog)

        config = {
            'weight_quantize_type': 'channel_wise_abs_max',
            'activation_quantize_type': 'moving_average_abs_max',
            'quantize_op_types': ['depthwise_conv2d', 'mul', 'conv2d'],
        }
        quant_train_prog_pact = quant_aware(
            main_prog,
            place,
            config,
            for_test=False,
            act_preprocess_func=pact,
            optimizer_func=get_optimizer,
            executor=exe)

        quant_eval_prog = quant_aware(val_prog, place, config, for_test=True)
        train(quant_train_prog_pact)
156
        quant_eval_prog = convert(quant_eval_prog, place, config)
157 158 159 160 161 162 163 164
        top1_2, top5_2 = test(quant_eval_prog)
        # values before quantization and after quantization should be close
        print("before quantization: top1: {}, top5: {}".format(top1_1, top5_1))
        print("after quantization: top1: {}, top5: {}".format(top1_2, top5_2))


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