Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / benchmark / command_line_reader.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 import os
18 import collections
19 import errno
20 import pathlib
21 from functools import partial
22 from argparse import ArgumentParser
23 from typing import Union
24
25 from ..accuracy_checker.accuracy_checker.config import ConfigReader
26 from ..accuracy_checker.accuracy_checker.utils import get_path
27 from ..network import Network
28
29 from .configuration import Configuration
30 from .logging import info
31
32
33 class CommandLineReader:
34     """
35     Class for parsing input config
36     """
37     @staticmethod
38     def read():
39         args, unknown_args = CommandLineReader.__build_arguments_parser().parse_known_args()
40         if unknown_args:
41             info("unknown command line arguments: {0}".format(unknown_args))
42
43         args.target_framework = "dlsdk"
44         args.aocl = None
45
46         merged_config = ConfigReader.merge(args)
47         launcher = merged_config['models'][0]['launchers'][0]
48
49         batch_size = args.batch_size if args.batch_size else (launcher['batch'] if 'batch' in launcher else None)
50         if not batch_size:
51             with Network(str(launcher['model']), str(launcher['weights'])) as network:
52                 batch_size = network.ie_network.batch_size
53
54         return Configuration(
55             config = merged_config,
56             model = str(launcher['model']),
57             weights = str(launcher['weights']),
58             cpu_extension = (str(launcher['cpu_extensions']) if 'cpu_extensions' in launcher else None),
59             gpu_extension = (str(launcher['gpu_extensions']) if 'gpu_extensions' in launcher else None),
60             device = launcher['device'],
61             benchmark_iterations_count = args.benchmark_iterations_count)
62
63     @staticmethod
64     def __build_arguments_parser():
65         parser = ArgumentParser(description='openvino.tools.benchmark')
66
67         parser.add_argument(
68             '-d', '--definitions',
69             help='Optional. Path to the YML file with definitions',
70             type=str,
71             required=False)
72
73         parser.add_argument(
74             '-c',
75             '--config',
76             help='Required. Path to the YML file with local configuration',
77             type=get_path,
78             required=True)
79
80         parser.add_argument(
81             '-m', '--models',
82             help='Optional. Prefix path to the models and weights',
83             type=partial(get_path, is_directory=True),
84             default=pathlib.Path.cwd(),
85             required=False)
86
87         parser.add_argument(
88             '-s', '--source',
89             help='Optional. prefix path to the data source',
90             type=partial(get_path, is_directory=True),
91             default=pathlib.Path.cwd(),
92             required=False)
93
94         parser.add_argument(
95             '-a', '--annotations',
96             help='Optional. prefix path to the converted annotations and datasets meta data',
97             type=partial(get_path, is_directory=True),
98             default=pathlib.Path.cwd(),
99             required=False)
100
101         parser.add_argument(
102             '-e', '--extensions',
103             help='Optional. Prefix path to extensions folder',
104             type=partial(get_path, is_directory=True),
105             default=pathlib.Path.cwd(),
106             required=False)
107
108         parser.add_argument(
109             '--cpu_extensions_mode',
110             help='Optional. specified preferable set of processor instruction for automatic searching cpu extension lib',
111             required=False,
112             choices=['avx2', 'sse4'])
113
114         parser.add_argument(
115             '-b', '--bitstreams',
116             help='Optional. prefix path to bitstreams folder',
117             type=partial(get_path, is_directory=True),
118             default=pathlib.Path.cwd(),
119             required=False)
120
121         parser.add_argument(
122             '-C', '--converted_models', '--converted-models',
123             help='Optional. directory to store Model Optimizer converted models. Used for DLSDK launcher only',
124             type=partial(get_path, is_directory=True),
125             default=pathlib.Path.cwd(),
126             required=False)
127
128         parser.add_argument(
129             '-td', '--target_devices', '--target-devices',
130             help='Optional. Space-separated list of devices for infer',
131             required=False,
132             nargs='+',
133             default=["CPU"])
134
135         parser.add_argument(
136             '-tt', '--target_tags', '--target-tags',
137             help='Optional. Space-separated list of launcher tags for infer',
138             required=False,
139             nargs='+')
140
141         parser.add_argument(
142             '--batch-size',
143             help='Optional. Batch size value. If not specified, the batch size value is determined from IR',
144             type=int,
145             required=False)
146
147         parser.add_argument(
148             '-ic',
149             '--benchmark_iterations_count',
150             help='Optional. Benchmark itertations count. (1000 is default)',
151             type=float,
152             required=False,
153             default=1000)
154
155         return parser