test_reconstruct_quantization.py 6.1 KB
Newer Older
G
gushiqiao 已提交
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) 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_post_static
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
25 26 27
from paddleslim.quant import quant_recon_static


G
gushiqiao 已提交
28 29 30 31 32
class TestRoundingOptimizer(StaticCase):
    def __init__(self, *args, **kwargs):
        super(TestRoundingOptimizer, self).__init__(*args, **kwargs)
        paddle.enable_static()
        self._gen_model()
33

G
gushiqiao 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    def _gen_model(self):
        image = paddle.static.data(
            name='image', shape=[None, 1, 28, 28], dtype='float32')
        label = paddle.static.data(name='label', shape=[None, 1], dtype='int64')
        model = MobileNet()
        out = model.net(input=image, class_dim=10)
        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(
            momentum=0.9,
            learning_rate=0.01,
            weight_decay=paddle.regularizer.L2Decay(4e-5))
        optimizer.minimize(avg_cost)
        main_prog = paddle.static.default_main_program()
        val_prog = main_prog.clone(for_test=True)
        place = paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda(
        ) else paddle.CPUPlace()
        exe = paddle.static.Executor(place)
        exe.run(paddle.static.default_startup_program())
55

G
gushiqiao 已提交
56 57
        def transform(x):
            return np.reshape(x, [1, 28, 28])
58

G
gushiqiao 已提交
59 60 61 62
        train_dataset = paddle.vision.datasets.MNIST(
            mode='train', backend='cv2', transform=transform)
        test_dataset = paddle.vision.datasets.MNIST(
            mode='test', backend='cv2', transform=transform)
63
        self.train_loader = paddle.io.DataLoader(
G
gushiqiao 已提交
64 65 66 67 68 69 70 71 72 73 74 75
            train_dataset,
            places=place,
            feed_list=[image, label],
            drop_last=True,
            batch_size=64,
            return_list=False)
        valid_loader = paddle.io.DataLoader(
            test_dataset,
            places=place,
            feed_list=[image, label],
            batch_size=64,
            return_list=False)
76

G
gushiqiao 已提交
77 78 79 80 81
        def sample_generator_creator():
            def __reader__():
                for data in test_dataset:
                    image, label = data
                    yield image, label
82

G
gushiqiao 已提交
83
            return __reader__
84

G
gushiqiao 已提交
85 86
        def train(program):
            iter = 0
87
            for data in self.train_loader():
G
gushiqiao 已提交
88 89 90 91 92 93 94 95 96
                cost, top1, top5 = exe.run(
                    program,
                    feed=data,
                    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))
97

G
gushiqiao 已提交
98 99 100 101 102 103 104 105 106
        train(main_prog)
        paddle.fluid.io.save_inference_model(
            dirname='./test_rounding_optimizer',
            feeded_var_names=[image.name, label.name],
            target_vars=[avg_cost, acc_top1, acc_top5],
            main_program=val_prog,
            executor=exe,
            model_filename='model',
            params_filename='params')
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

        self.data_loader = sample_generator_creator()

        self._regions = [['image', 'batch_norm_26.tmp_4']]
        self._region_weights_names = [[
            'conv1_weights', 'conv2_1_dw_weights', 'conv2_1_sep_weights',
            'conv2_2_dw_weights', 'conv2_2_sep_weights', 'conv3_1_dw_weights',
            'conv3_1_sep_weights', 'conv3_2_dw_weights', 'conv3_2_sep_weights',
            'conv4_1_dw_weights', 'conv4_1_sep_weights', 'conv4_2_dw_weights',
            'conv4_2_sep_weights', 'conv5_1_dw_weights', 'conv5_1_sep_weights',
            'conv5_2_dw_weights', 'conv5_2_sep_weights', 'conv5_3_dw_weights',
            'conv5_3_sep_weights', 'conv5_4_dw_weights', 'conv5_4_sep_weights',
            'conv5_5_dw_weights', 'conv5_5_sep_weights', 'conv5_6_dw_weights',
            'conv5_6_sep_weights', 'conv6_dw_weights', 'conv6_sep_weights'
        ]]

G
gushiqiao 已提交
123
    def test_qdrop(self):
124 125 126 127 128 129 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 156 157 158 159 160
        place = paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda(
        ) else paddle.CPUPlace()
        exe = paddle.static.Executor(place)
        quant_recon_static(
            exe,
            './test_rounding_optimizer',
            quantize_model_path='rsq_out',
            sample_generator=self.data_loader,
            model_filename='model',
            params_filename='params',
            batch_nums=10,
            algo='abs_max',
            regions=self._regions,
            region_weights_names=self._region_weights_names,
            recon_level='region-wise',
            simulate_activation_quant=True)

    def test_qdrop(self):
        place = paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda(
        ) else paddle.CPUPlace()
        exe = paddle.static.Executor(place)
        quant_recon_static(
            exe,
            './test_rounding_optimizer',
            quantize_model_path='rsq_out',
            sample_generator=self.data_loader,
            model_filename='model',
            params_filename='params',
            batch_nums=10,
            algo='KL',
            regions=self._regions,
            region_weights_names=self._region_weights_names,
            recon_level='layer-wise',
            simulate_activation_quant=True,
            bias_correction=True)


G
gushiqiao 已提交
161
if __name__ == '__main__':
162
    unittest.main()