|
1 | 1 | # |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 | # |
4 | | -# Copyright (c) 2018 Intel Corporation |
| 4 | +# Copyright (c) 2022 Intel Corporation |
5 | 5 | # |
6 | 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
7 | 7 | # you may not use this file except in compliance with the License. |
|
16 | 16 | # limitations under the License. |
17 | 17 | # |
18 | 18 | import time |
19 | | -import shutil |
20 | 19 | import numpy as np |
21 | | -from argparse import ArgumentParser |
22 | | -from neural_compressor import data |
23 | 20 | import tensorflow as tf |
| 21 | +from neural_compressor.utils import logger |
24 | 22 | tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR) |
25 | 23 |
|
26 | 24 | flags = tf.compat.v1.flags |
|
42 | 40 | flags.DEFINE_bool( |
43 | 41 | 'benchmark', False, 'whether to benchmark the model') |
44 | 42 |
|
45 | | -flags.DEFINE_string( |
46 | | - 'config', 'bert.yaml', 'yaml configuration of the model') |
47 | | - |
48 | 43 | flags.DEFINE_string( |
49 | 44 | 'calib_data', None, 'location of calibration dataset') |
50 | 45 |
|
51 | 46 | flags.DEFINE_string( |
52 | 47 | 'eval_data', None, 'location of evaluate dataset') |
53 | 48 |
|
54 | | -from neural_compressor.experimental.metric.metric import TensorflowTopK |
55 | | -from neural_compressor.experimental.data.transforms.transform import ComposeTransform |
56 | | -from neural_compressor.experimental.data.datasets.dataset import TensorflowImageRecord |
57 | | -from neural_compressor.experimental.data.transforms.imagenet_transform import LabelShift |
58 | | -from neural_compressor.experimental.data.dataloaders.default_dataloader import DefaultDataLoader |
| 49 | +flags.DEFINE_integer('batch_size', 32, 'batch_size') |
| 50 | + |
| 51 | +flags.DEFINE_integer( |
| 52 | + 'iters', 100, 'maximum iteration when evaluating performance') |
| 53 | + |
| 54 | +from neural_compressor.metric.metric import TensorflowTopK |
| 55 | +from neural_compressor.data.transforms.transform import ComposeTransform |
| 56 | +from neural_compressor.data.datasets.dataset import TensorflowImageRecord |
| 57 | +from neural_compressor.data.transforms.imagenet_transform import LabelShift |
| 58 | +from neural_compressor.data.dataloaders.default_dataloader import DefaultDataLoader |
59 | 59 | from neural_compressor.data.transforms.imagenet_transform import BilinearImagenetTransform |
60 | 60 |
|
61 | 61 | eval_dataset = TensorflowImageRecord(root=FLAGS.eval_data, transform=ComposeTransform(transform_list= \ |
62 | | - [BilinearImagenetTransform(height=299, width=299)])) |
63 | | -if FLAGS.benchmark and FLAGS.mode == 'performance': |
64 | | - eval_dataloader = DefaultDataLoader(dataset=eval_dataset, batch_size=1) |
65 | | -else: |
66 | | - eval_dataloader = DefaultDataLoader(dataset=eval_dataset, batch_size=32) |
| 62 | + [BilinearImagenetTransform(height=299, width=299)])) |
| 63 | + |
| 64 | +eval_dataloader = DefaultDataLoader(dataset=eval_dataset, batch_size=FLAGS.batch_size) |
| 65 | + |
67 | 66 | if FLAGS.calib_data: |
68 | | - calib_dataset = TensorflowImageRecord(root=FLAGS.calib_data, transform=ComposeTransform(transform_list= \ |
69 | | - [BilinearImagenetTransform(height=299, width=299)])) |
70 | | - calib_dataloader = DefaultDataLoader(dataset=calib_dataset, batch_size=10) |
71 | | - |
72 | | -def evaluate(model, measurer=None): |
73 | | - """ |
74 | | - Custom evaluate function to inference the model for specified metric on validation dataset. |
75 | | - |
76 | | - Args: |
77 | | - model (tf.saved_model.load): The input model will be the class of tf.saved_model.load(quantized_model_path). |
78 | | - measurer (object, optional): for benchmark measurement of duration. |
79 | | -
|
80 | | - Returns: |
81 | | - accuracy (float): evaluation result, the larger is better. |
82 | | - """ |
83 | | - infer = model.signatures["serving_default"] |
84 | | - output_dict_keys = infer.structured_outputs.keys() |
85 | | - output_name = list(output_dict_keys )[0] |
86 | | - postprocess = LabelShift(label_shift=1) |
87 | | - metric = TensorflowTopK(k=1) |
88 | | - |
89 | | - def eval_func(dataloader, metric): |
90 | | - results = [] |
91 | | - for idx, (inputs, labels) in enumerate(dataloader): |
92 | | - inputs = np.array(inputs) |
93 | | - input_tensor = tf.constant(inputs) |
94 | | - if measurer: |
95 | | - measurer.start() |
96 | | - predictions = infer(input_tensor)[output_name] |
97 | | - if measurer: |
98 | | - measurer.end() |
99 | | - predictions = predictions.numpy() |
100 | | - predictions, labels = postprocess((predictions, labels)) |
101 | | - metric.update(predictions, labels) |
102 | | - return results |
103 | | - |
104 | | - results = eval_func(eval_dataloader, metric) |
105 | | - acc = metric.result() |
106 | | - return acc |
| 67 | + calib_dataset = TensorflowImageRecord(root=FLAGS.calib_data, transform= \ |
| 68 | + ComposeTransform(transform_list= [BilinearImagenetTransform(height=299, width=299)])) |
| 69 | + calib_dataloader = DefaultDataLoader(dataset=calib_dataset, batch_size=10) |
| 70 | + |
| 71 | +def evaluate(model): |
| 72 | + """ |
| 73 | + Custom evaluate function to inference the model for specified metric on validation dataset. |
| 74 | +
|
| 75 | + Args: |
| 76 | + model (tf.saved_model.load): The input model will be the class of tf.saved_model.load(quantized_model_path). |
| 77 | + measurer (object, optional): for benchmark measurement of duration. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + accuracy (float): evaluation result, the larger is better. |
| 81 | + """ |
| 82 | + infer = model.signatures["serving_default"] |
| 83 | + output_dict_keys = infer.structured_outputs.keys() |
| 84 | + output_name = list(output_dict_keys )[0] |
| 85 | + postprocess = LabelShift(label_shift=1) |
| 86 | + metric = TensorflowTopK(k=1) |
| 87 | + latency_list = [] |
| 88 | + |
| 89 | + def eval_func(dataloader, metric): |
| 90 | + warmup = 5 |
| 91 | + iteration = None |
| 92 | + |
| 93 | + if FLAGS.benchmark and FLAGS.mode == 'performance': |
| 94 | + iteration = FLAGS.iters |
| 95 | + for idx, (inputs, labels) in enumerate(dataloader): |
| 96 | + inputs = np.array(inputs) |
| 97 | + input_tensor = tf.constant(inputs) |
| 98 | + start = time.time() |
| 99 | + predictions = infer(input_tensor)[output_name] |
| 100 | + end = time.time() |
| 101 | + latency_list.append(end - start) |
| 102 | + predictions = predictions.numpy() |
| 103 | + predictions, labels = postprocess((predictions, labels)) |
| 104 | + metric.update(predictions, labels) |
| 105 | + if iteration and idx >= iteration: |
| 106 | + break |
| 107 | + latency = np.array(latency_list[warmup:]).mean() / eval_dataloader.batch_size |
| 108 | + return latency |
| 109 | + |
| 110 | + latency = eval_func(eval_dataloader, metric) |
| 111 | + if FLAGS.benchmark: |
| 112 | + logger.info("\n{} mode benchmark result:".format(FLAGS.mode)) |
| 113 | + for i, res in enumerate(latency_list): |
| 114 | + logger.debug("Iteration {} result {}:".format(i, res)) |
| 115 | + if FLAGS.benchmark and FLAGS.mode == 'performance': |
| 116 | + logger.info("Batch size = {}".format(eval_dataloader.batch_size)) |
| 117 | + logger.info("Latency: {:.3f} ms".format(latency * 1000)) |
| 118 | + logger.info("Throughput: {:.3f} images/sec".format(1. / latency)) |
| 119 | + acc = metric.result() |
| 120 | + return acc |
107 | 121 |
|
108 | 122 | def main(_): |
109 | | - if FLAGS.tune: |
110 | | - from neural_compressor.experimental import Quantization, common |
111 | | - quantizer = Quantization(FLAGS.config) |
112 | | - quantizer.model = common.Model(FLAGS.input_model) |
113 | | - quantizer.eval_func = evaluate |
114 | | - quantizer.calib_dataloader = calib_dataloader |
115 | | - q_model = quantizer.fit() |
116 | | - q_model.save(FLAGS.output_model) |
117 | | - |
118 | | - |
119 | | - if FLAGS.benchmark: |
120 | | - from neural_compressor.experimental import Benchmark, common |
121 | | - evaluator = Benchmark(FLAGS.config) |
122 | | - evaluator.model = common.Model(FLAGS.input_model) |
123 | | - evaluator.b_func = evaluate |
124 | | - evaluator.b_dataloader = eval_dataloader |
125 | | - evaluator(FLAGS.mode) |
| 123 | + if FLAGS.tune: |
| 124 | + from neural_compressor.quantization import fit |
| 125 | + from neural_compressor.config import PostTrainingQuantConfig |
| 126 | + from neural_compressor.utils.utility import set_random_seed |
| 127 | + set_random_seed(9527) |
| 128 | + config = PostTrainingQuantConfig(calibration_sampling_size=[50, 100]) |
| 129 | + q_model = fit( |
| 130 | + model=FLAGS.input_model, |
| 131 | + conf=config, |
| 132 | + calib_dataloader=calib_dataloader, |
| 133 | + eval_dataloader=eval_dataloader, |
| 134 | + eval_func=evaluate) |
| 135 | + q_model.save(FLAGS.output_model) |
| 136 | + |
| 137 | + if FLAGS.benchmark: |
| 138 | + from neural_compressor.benchmark import fit |
| 139 | + from neural_compressor.config import BenchmarkConfig |
| 140 | + if FLAGS.mode == 'performance': |
| 141 | + conf = BenchmarkConfig(iteration=100, cores_per_instance=4, num_of_instance=7) |
| 142 | + fit(FLAGS.input_model, conf, b_func=evaluate) |
| 143 | + else: |
| 144 | + from neural_compressor.model.model import Model |
| 145 | + accuracy = evaluate(Model(FLAGS.input_model).model) |
| 146 | + logger.info('Batch size = %d' % FLAGS.batch_size) |
| 147 | + logger.info("Accuracy: %.5f" % accuracy) |
126 | 148 |
|
127 | 149 | if __name__ == "__main__": |
128 | 150 | tf.compat.v1.app.run() |
0 commit comments