2 Copyright (C) 2018-2019 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 from argparse import ArgumentParser
19 import openvino.tools.calibration as calibration
20 import openvino.tools.utils as utils
24 config = calibration.CommandLineReader.read()
25 network = calibration.Calibrator(config).run()
26 network.serialize(config.output_model)
30 config = calibration.CommandLineReader.read()
31 calibrator = calibration.CalibratorFactory.create(config.precision, calibration.CalibratorConfiguration(config))
33 print("Collecting accuracy for {}...".format(config.model))
34 result = calibrator.infer()
35 print("Accuracy: {0:.4f}%".format(100.0 * result.metrics.accuracy))
38 def collect_statistics():
40 config = calibration.CommandLineReader.read()
41 calibrator = calibration.CalibratorFactory.create(config.precision, calibration.CalibratorConfiguration(config))
43 print("Collecting FP32 statistics for {}...".format(config.model))
44 fp32_result = calibrator.infer(add_outputs=True, collect_aggregated_statistics=True)
45 print("FP32 accuracy: {0:.4f}%".format(100.0 * fp32_result.metrics.accuracy))
47 output_model_file_path = \
48 os.path.splitext(config.model)[0] + ("_{}_statistics_without_ignored.xml".format(config.precision.lower()) if
49 config.ignore_layer_names else
50 "_{}_statistics.xml".format(config.precision.lower()))
51 output_weights_file_path = utils.Path.get_weights(output_model_file_path)
53 quantization_levels = \
54 calibrator.get_quantization_levels(calibration.CalibrationConfigurationHelper.read_ignore_layer_names(config))
55 statistics = fp32_result.aggregated_statistics.get_node_statistics()
56 calibrator.save(output_model_file_path, output_weights_file_path, quantization_levels, statistics)
57 print("Network with statistics was written to {}.(xml|bin) IR file".format(os.path.splitext(output_model_file_path)[0]))
60 def __build_arguments_parser():
61 parser = ArgumentParser(description='Calibration Tool')
64 help='Optional, possible values: calibrate, collect_statistics or check_accuracy',
66 choices=('calibrate', 'collect_statistics', 'check_accuracy'))
70 if __name__ == '__main__':
71 parser, unknown_args = __build_arguments_parser().parse_known_args()
72 if parser.action == 'calibrate':
74 elif parser.action == 'collect_statistics':
76 elif parser.action == 'check_accuracy':