Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tools / collect_statistics_tool / collect_statistics.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
18 import os
19 from openvino.tools.calibration import CalibratorConfiguration, CalibrationConfigurationHelper, CalibratorFactory, CommandLineProcessor
20 from openvino.tools.utils import Path
21
22 def collect_statistics():
23     with CommandLineProcessor.process() as configuration:
24         calibrator = CalibratorFactory.create(configuration.precision, CalibratorConfiguration(configuration))
25
26         print("Collecting FP32 statistics for {}...".format(configuration.model))
27         fp32_result = calibrator.infer(add_outputs=True, collect_aggregated_statistics=True)
28         print("FP32 accuracy: {0:.4f}%".format(100.0 * fp32_result.metrics.accuracy))
29
30         output_model_file_path = Path.get_model(configuration.output_model, "_statistics")
31         output_weights_file_path = Path.get_weights(configuration.output_weights, "_statistics")
32
33         quantization_levels = calibrator.get_quantization_levels(CalibrationConfigurationHelper.read_ignore_layer_names(configuration))
34         statistics = fp32_result.aggregated_statistics.get_node_statistics()
35         calibrator.save(output_model_file_path, output_weights_file_path, quantization_levels, statistics)
36         print("Network with statistics was written to {}.(xml|bin) IR file".format(os.path.splitext(output_model_file_path)[0]))
37
38 if __name__ == '__main__':
39     collect_statistics()