Add a section of how to link IE with CMake project (#99)
[platform/upstream/dldt.git] / inference-engine / ie_bridges / python / sample / benchmark_app / utils / benchmark_utils.py
1 """
2  Copyright (c) 2018 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 logging
18 import argparse
19 import os
20 import cv2
21 import numpy as np
22 import sys
23
24 from glob import glob
25 from random import choice
26 from datetime import datetime
27 from fnmatch import fnmatch
28
29 from . constants import *
30
31 logging.basicConfig(format="[ %(levelname)s ] %(message)s", level=logging.INFO, stream=sys.stdout)
32 logger = logging.getLogger('BenchmarkApp')
33
34
35 def validate_args(args):
36     if args.number_iterations is not None and args.number_iterations < 0:
37         raise Exception("Number of iterations should be positive (invalid -niter option value)")
38     if args.number_infer_requests < 0:
39         raise Exception("Number of inference requests should be positive (invalid -nireq option value)")
40     if not fnmatch(args.path_to_model, XML_EXTENSION_PATTERN):
41         raise Exception('Path {} is not xml file.')
42
43
44 def parse_args():
45     parser = argparse.ArgumentParser()
46     parser.add_argument('-i', '--path_to_images', type=str, required=True, help=HELP_MESSAGES['IMAGE_MESSAGE'])
47     parser.add_argument('-m', '--path_to_model', type=str, required=True, help=HELP_MESSAGES['MODEL_MESSAGE'])
48     parser.add_argument('-c', '--path_to_cldnn_config', type=str, required=False,
49                         help=HELP_MESSAGES['CUSTOM_GPU_LIBRARY_MESSAGE'])
50     parser.add_argument('-l', '--path_to_extension', type=str, required=False, default=None,
51                         help=HELP_MESSAGES['CUSTOM_GPU_LIBRARY_MESSAGE'])
52     parser.add_argument('-api', '--api_type', type=str, required=False, default='async', choices=['sync', 'async'],
53                         help=HELP_MESSAGES['API_MESSAGE'])
54     parser.add_argument('-d', '--target_device', type=str, required=False, default="CPU",
55                         help=HELP_MESSAGES['TARGET_DEVICE_MESSAGE'])
56     parser.add_argument('-niter', '--number_iterations', type=int, required=False, default=None,
57                         help=HELP_MESSAGES['ITERATIONS_COUNT_MESSAGE'])
58     parser.add_argument('-nireq', '--number_infer_requests', type=int, required=False, default=2,
59                         help=HELP_MESSAGES['INFER_REQUESTS_COUNT_MESSAGE'])
60     parser.add_argument('-nthreads', '--number_threads', type=int, required=False, default=None,
61                         help=HELP_MESSAGES['INFER_NUM_THREADS_MESSAGE'])
62     parser.add_argument('-b', '--batch_size', type=int, required=False, default=None,
63                         help=HELP_MESSAGES['BATCH_SIZE_MESSAGE'])
64     parser.add_argument('-pin', '--infer_threads_pinning', type=str, required=False, default='YES',
65                         choices=['YES', 'NO'], help=HELP_MESSAGES['INFER_THREADS_PINNING_MESSAGE'])
66     return parser.parse_args()
67
68
69 def get_images(path_to_images, batch_size):
70     images = list()
71     if os.path.isfile(path_to_images):
72         while len(images) != batch_size:
73             images.append(path_to_images)
74     else:
75         path = os.path.join(path_to_images, '*')
76         files = glob(path, recursive=True)
77         for file in files:
78             file_extension = file.rsplit('.').pop().upper()
79             if file_extension in IMAGE_EXTENSIONS:
80                 images.append(file)
81         if len(images) == 0:
82             raise Exception("No images found in {}".format(path_to_images))
83         if len(images) < batch_size:
84             while len(images) != batch_size:
85                 images.append(choice(images))
86     return images
87
88
89 def get_duration_in_secs(target_device):
90     duration = 0
91     for device in DEVICE_DURATION_IN_SECS:
92         if device in target_device:
93             duration = max(duration, DEVICE_DURATION_IN_SECS[device])
94
95     if duration == 0:
96         duration = DEVICE_DURATION_IN_SECS[UNKNOWN_DEVICE_TYPE]
97         logger.warn("Default duration {} seconds for unknown device {} is used".format(duration, target_device))
98
99     return duration
100
101
102 def fill_blob_with_image(images_path, shape):
103     images = np.ndarray(shape)
104     for item in range(shape[0]):
105         image = cv2.imread(images_path[item])
106
107         new_im_size = tuple(shape[2:])
108         if image.shape[:-1] != new_im_size:
109             logger.warn("Image {} is resize from ({}) to ({})".format(images_path[item], image.shape[:-1], new_im_size))
110             image = cv2.resize(image, new_im_size)
111
112         image = image.transpose((2, 0, 1))
113         images[item] = image
114     return images
115
116
117 def sync_infer_request(exe_network, times, images):
118     iteration_start_time = datetime.now()
119     exe_network.infer(images)
120     current_time = datetime.now()
121     times.append((current_time - iteration_start_time).total_seconds())
122     return current_time