Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / calibration / __main__.py
1 """
2 Copyright (C) 2018-2019 Intel Corporation
3
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
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 """
16
17 from argparse import ArgumentParser
18
19 import openvino.tools.calibration as calibration
20 import openvino.tools.utils as utils
21
22
23 def calibrate():
24     config = calibration.CommandLineReader.read()
25     network = calibration.Calibrator(config).run()
26     network.serialize(config.output_model)
27
28
29 def check_accuracy():
30     config = calibration.CommandLineReader.read()
31     calibrator = calibration.CalibratorFactory.create(config.precision, calibration.CalibratorConfiguration(config))
32
33     print("Collecting accuracy for {}...".format(config.model))
34     result = calibrator.infer()
35     print("Accuracy: {0:.4f}%".format(100.0 * result.metrics.accuracy))
36
37
38 def collect_statistics():
39     import os
40     config = calibration.CommandLineReader.read()
41     calibrator = calibration.CalibratorFactory.create(config.precision, calibration.CalibratorConfiguration(config))
42
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))
46
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)
52
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]))
58
59
60 def __build_arguments_parser():
61     parser = ArgumentParser(description='Calibration Tool')
62     parser.add_argument(
63         'action',
64         help='Optional, possible values: calibrate, collect_statistics or check_accuracy',
65         nargs='?',
66         choices=('calibrate', 'collect_statistics', 'check_accuracy'))
67     return parser
68
69
70 if __name__ == '__main__':
71     parser, unknown_args = __build_arguments_parser().parse_known_args()
72     if parser.action == 'calibrate':
73         calibrate()
74     elif parser.action == 'collect_statistics':
75         collect_statistics()
76     elif parser.action == 'check_accuracy':
77         check_accuracy()
78     else:
79         calibrate()