Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / main.py
1 """
2 Copyright (c) 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 pathlib import Path
18 from argparse import ArgumentParser
19 from functools import partial
20
21 from .config import ConfigReader
22 from .logging import print_info, add_file_handler
23 from .model_evaluator import ModelEvaluator
24 from .progress_reporters import ProgressReporter
25 from .utils import get_path
26
27
28 def build_arguments_parser():
29     parser = ArgumentParser(description='NN Validation on Caffe and IE', allow_abbrev=False)
30     parser.add_argument(
31         '-d', '--definitions',
32         help='path to the yml file with definitions',
33         type=get_path,
34         required=False
35     )
36     parser.add_argument(
37         '-c', '--config',
38         help='path to the yml file with local configuration',
39         type=get_path,
40         required=True
41     )
42     parser.add_argument(
43         '-m', '--models',
44         help='prefix path to the models and weights',
45         type=partial(get_path, is_directory=True),
46         default=Path.cwd(),
47         required=False
48     )
49     parser.add_argument(
50         '-s', '--source',
51         help='prefix path to the data source',
52         type=partial(get_path, is_directory=True),
53         default=Path.cwd(),
54         required=False
55     )
56     parser.add_argument(
57         '-a', '--annotations',
58         help='prefix path to the converted annotations and datasets meta data',
59         type=partial(get_path, is_directory=True),
60         default=Path.cwd(),
61         required=False
62     )
63     parser.add_argument(
64         '-e', '--extensions',
65         help='prefix path to extensions folder',
66         type=partial(get_path, is_directory=True),
67         default=Path.cwd(),
68         required=False
69     )
70     parser.add_argument(
71         '--cpu_extensions_mode',
72         help='specified preferable set of processor instruction for automatic searching cpu extension lib',
73         required=False,
74         choices=['avx2', 'sse4']
75     )
76     parser.add_argument(
77         '-b', '--bitstreams',
78         help='prefix path to bitstreams folder',
79         type=partial(get_path, is_directory=True),
80         default=Path.cwd(),
81         required=False
82     )
83     parser.add_argument(
84         '--stored_predictions',
85         help='path to file with saved predictions. Used for development',
86         # since at the first time file does not exist and then created we can not always check existence
87         required=False
88     )
89     parser.add_argument(
90         '-C', '--converted_models',
91         help='directory to store Model Optimizer converted models. Used for DLSDK launcher only',
92         type=partial(get_path, is_directory=True),
93         default=Path.cwd(),
94         required=False
95     )
96     parser.add_argument(
97         '-M', '--model_optimizer',
98         help='path to model optimizer caffe directory',
99         type=partial(get_path, is_directory=True),
100         # there is no default value because if user did not specify it we use specific locations
101         # defined in model_conversion.py
102         required=False
103     )
104     parser.add_argument(
105         '--tf_custom_op_config_dir',
106         help='path to directory with tensorflow custom operation configuration files for model optimizer',
107         type=partial(get_path, is_directory=True),
108         # there is no default value because if user did not specify it we use specific location
109         # defined in model_conversion.py
110         required=False
111     )
112     parser.add_argument(
113         '--tf_obj_detection_api_pipeline_config_path',
114         help='path to directory with tensorflow object detection api pipeline configuration files for model optimizer',
115         type=partial(get_path, is_directory=True),
116         # there is no default value because if user did not specify it we use specific location
117         # defined in model_conversion.py
118         required=False
119     )
120     parser.add_argument(
121         '--progress',
122         help='progress reporter',
123         required=False,
124         default='bar'
125     )
126     parser.add_argument(
127         '-tf', '--target_framework',
128         help='framework for infer',
129         required=False
130     )
131     parser.add_argument(
132         '-td', '--target_devices',
133         help='Space separated list of devices for infer',
134         required=False,
135         nargs='+'
136     )
137
138     parser.add_argument(
139         '-tt', '--target_tags',
140         help='Space separated list of launcher tags for infer',
141         required=False,
142         nargs='+'
143     )
144
145     parser.add_argument(
146         '-l', '--log_file',
147         help='file for additional logging results',
148         required=False
149     )
150
151     parser.add_argument(
152         '--ignore_result_formatting',
153         help='allow to get raw metrics results without data formatting',
154         required=False,
155         default=False
156     )
157
158     parser.add_argument(
159         '-am', '--affinity_map',
160         help='prefix path to the affinity maps',
161         type=partial(get_path, is_directory=True),
162         default=Path.cwd(),
163         required=False
164     )
165
166     parser.add_argument(
167         '--aocl',
168         help='aocl executable path for FPGA bitstream programming',
169         type=get_path,
170         required=False
171     )
172
173     return parser
174
175
176 def main():
177     args = build_arguments_parser().parse_args()
178     progress_reporter = ProgressReporter.provide((
179         args.progress if ':' not in args.progress
180         else args.progress.split(':')[0]
181     ))
182     if args.log_file:
183         add_file_handler(args.log_file)
184
185     config = ConfigReader.merge(args)
186
187     for model in config['models']:
188         for launcher_config in model['launchers']:
189             for dataset_config in model['datasets']:
190                 print_processing_info(
191                     model['name'],
192                     launcher_config['framework'],
193                     launcher_config['device'],
194                     launcher_config.get('tags'),
195                     dataset_config['name']
196                 )
197                 model_evaluator = ModelEvaluator.from_configs(launcher_config, dataset_config)
198                 progress_reporter.reset(len(model_evaluator.dataset))
199                 model_evaluator.process_dataset(args.stored_predictions, progress_reporter=progress_reporter)
200                 model_evaluator.compute_metrics(ignore_results_formatting=args.ignore_result_formatting)
201
202                 model_evaluator.release()
203
204
205 def print_processing_info(model, launcher, device, tags, dataset):
206     print_info('Processing info:')
207     print_info('model: {}'.format(model))
208     print_info('launcher: {}'.format(launcher))
209     if tags:
210         print_info('launcher tags: {}'.format(' '.join(tags)))
211     print_info('device: {}'.format(device))
212     print_info('dataset: {}'.format(dataset))
213
214
215 if __name__ == '__main__':
216     main()