test_post_training_quantization_resnet50.py 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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.

Z
zhouzj 已提交
15 16
import sys
import time
17
import unittest
18

Z
zhouzj 已提交
19
import numpy as np
20 21
from test_post_training_quantization_mobilenetv1 import (
    TestPostTrainingQuantization,
Z
zhouzj 已提交
22
    val,
23
)
24

P
pangyoki 已提交
25 26 27
import paddle

paddle.enable_static()
28 29 30 31 32


class TestPostTrainingForResnet50(TestPostTrainingQuantization):
    def test_post_training_resnet50(self):
        model = "ResNet-50"
33
        algo = "min_max"
34
        round_type = "round"
35
        data_urls = [
36
            'http://paddle-inference-dist.bj.bcebos.com/int8/resnet50_int8_model_combined.tar.gz'
37
        ]
38
        data_md5s = ['db212fd4e9edc83381aef4533107e60c']
39
        quantizable_op_type = ["conv2d", "mul"]
40
        is_full_quantize = False
41
        is_use_cache_file = False
42
        is_optimize_model = False
43
        diff_threshold = 0.025
44 45
        self.run_test(
            model,
46 47
            'model.pdmodel',
            'model.pdiparams',
48 49 50 51
            algo,
            round_type,
            data_urls,
            data_md5s,
Z
zhouzj 已提交
52
            "model",
53 54 55 56 57 58
            quantizable_op_type,
            is_full_quantize,
            is_use_cache_file,
            is_optimize_model,
            diff_threshold,
        )
59

Z
zhouzj 已提交
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    def run_program(
        self,
        model_path,
        model_filename,
        params_filename,
        batch_size,
        infer_iterations,
    ):
        image_shape = [3, 224, 224]
        place = paddle.CPUPlace()
        exe = paddle.static.Executor(place)
        [
            infer_program,
            feed_dict,
            fetch_targets,
        ] = paddle.static.load_inference_model(
            model_path,
            exe,
            model_filename=model_filename,
            params_filename=params_filename,
        )
        val_reader = paddle.batch(val(), batch_size)
        iterations = infer_iterations

        test_info = []
        cnt = 0
        periods = []
        for batch_id, data in enumerate(val_reader()):
            image = np.array([x[0].reshape(image_shape) for x in data]).astype(
                "float32"
            )
            label = np.array([x[1] for x in data]).astype("int64")
            label = label.reshape([-1, 1])

            t1 = time.time()
            _, acc1, _ = exe.run(
                infer_program,
                feed={feed_dict[0]: image, feed_dict[1]: label},
                fetch_list=fetch_targets,
            )
            t2 = time.time()
            period = t2 - t1
            periods.append(period)

            test_info.append(np.mean(acc1) * len(data))
            cnt += len(data)

            if (batch_id + 1) % 100 == 0:
                print(f"{batch_id + 1} images,")
                sys.stdout.flush()
            if (batch_id + 1) == iterations:
                break

        throughput = cnt / np.sum(periods)
        latency = np.average(periods)
        acc1 = np.sum(test_info) / cnt
        return (throughput, latency, acc1)

118

Z
zhouzj 已提交
119
class TestPostTrainingForResnet50ONNXFormat(TestPostTrainingForResnet50):
120 121 122
    def test_post_training_resnet50(self):
        model = "ResNet-50"
        algo = "min_max"
123
        round_type = "round"
124
        data_urls = [
125
            'http://paddle-inference-dist.bj.bcebos.com/int8/resnet50_int8_model_combined.tar.gz'
126
        ]
127
        data_md5s = ['db212fd4e9edc83381aef4533107e60c']
128 129 130 131 132 133
        quantizable_op_type = ["conv2d", "mul"]
        is_full_quantize = False
        is_use_cache_file = False
        is_optimize_model = False
        diff_threshold = 0.025
        onnx_format = True
134 135
        self.run_test(
            model,
136 137
            'model.pdmodel',
            'model.pdiparams',
138 139 140 141
            algo,
            round_type,
            data_urls,
            data_md5s,
Z
zhouzj 已提交
142
            "model",
143 144 145 146 147 148 149
            quantizable_op_type,
            is_full_quantize,
            is_use_cache_file,
            is_optimize_model,
            diff_threshold,
            onnx_format=onnx_format,
        )
150 151


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