Exemple #1
0
def create_core():
    if openvino_absent:
        raise ImportError('The OpenVINO package is not installed')

    log.info('OpenVINO Inference Engine')
    log.info('\tbuild: {}'.format(get_version()))
    return IECore()
Exemple #2
0
def make_plugin_or_core():
    version = get_version()
    use_core_openvino = False
    try:
        major, minor, reference = [int(x) for x in version.split('.')]
        if major >= 2 and minor >= 1:
            use_core_openvino = True
    except Exception:
        pass

    if use_core_openvino:
        ie = IECore()
        return ie

    if _IE_PLUGINS_PATH is None:
        raise OSError('Inference engine plugin path env not found in the system.')

    plugin = IEPlugin(device='CPU', plugin_dirs=[_IE_PLUGINS_PATH])
    if (_check_instruction('avx2')):
        plugin.add_cpu_extension(os.path.join(_IE_PLUGINS_PATH, 'libcpu_extension_avx2.so'))
    elif (_check_instruction('sse4')):
        plugin.add_cpu_extension(os.path.join(_IE_PLUGINS_PATH, 'libcpu_extension_sse4.so'))
    elif platform.system() == 'Darwin':
        plugin.add_cpu_extension(os.path.join(_IE_PLUGINS_PATH, 'libcpu_extension.dylib'))
    else:
        raise Exception('Inference engine requires a support of avx2 or sse4.')

    return plugin
Exemple #3
0
    def __init__(self, args):
        self.gpu_ext = args.gpu_lib
        self.allow_grow = args.allow_grow and not args.no_show

        log.info('OpenVINO Inference Engine')
        log.info('\tbuild: {}'.format(get_version()))
        ie = IECore()
        if args.cpu_lib and 'CPU' in {args.d_fd, args.d_lm, args.d_reid}:
            ie.add_extension(args.cpu_lib, 'CPU')

        self.face_detector = FaceDetector(ie,
                                          args.m_fd,
                                          args.fd_input_size,
                                          confidence_threshold=args.t_fd,
                                          roi_scale_factor=args.exp_r_fd)
        self.landmarks_detector = LandmarksDetector(ie, args.m_lm)
        self.face_identifier = FaceIdentifier(ie,
                                              args.m_reid,
                                              match_threshold=args.t_id,
                                              match_algo=args.match_algo)

        self.face_detector.deploy(args.d_fd, self.get_config(args.d_fd))
        self.landmarks_detector.deploy(args.d_lm, self.get_config(args.d_lm),
                                       self.QUEUE_SIZE)
        self.face_identifier.deploy(args.d_reid, self.get_config(args.d_reid),
                                    self.QUEUE_SIZE)

        log.debug('Building faces database using images from {}'.format(
            args.fg))
        self.faces_database = FacesDatabase(
            args.fg, self.face_identifier, self.landmarks_detector,
            self.face_detector if args.run_detector else None, args.no_show)
        self.face_identifier.set_faces_database(self.faces_database)
        log.info('Database is built, registered {} identities'.format(
            len(self.faces_database)))
def main():
    args = build_argparser().parse_args()

    start_time = perf_counter()
    with wave.open(args.input, 'rb') as wave_read:
        channel_num, sample_width, sampling_rate, pcm_length, compression_type, _ = wave_read.getparams(
        )
        assert sample_width == 2, "Only 16-bit WAV PCM supported"
        assert compression_type == 'NONE', "Only linear PCM WAV files supported"
        assert channel_num == 1, "Only mono WAV PCM supported"
        assert sampling_rate == 16000, "Only 16 KHz audio supported"
        audio = np.frombuffer(wave_read.readframes(pcm_length * channel_num),
                              dtype=np.int16).reshape(
                                  (pcm_length, channel_num))

    log_melspectrum = QuartzNet.audio_to_melspectrum(audio.flatten(),
                                                     sampling_rate)

    log.info('OpenVINO Inference Engine')
    log.info('\tbuild: {}'.format(get_version()))
    ie = IECore()

    quartz_net = QuartzNet(ie, args.model, log_melspectrum.shape, args.device)
    character_probs = quartz_net.infer(log_melspectrum)
    transcription = QuartzNet.ctc_greedy_decode(character_probs)
    total_latency = (perf_counter() - start_time) * 1e3
    log.info("Metrics report:")
    log.info("\tLatency: {:.1f} ms".format(total_latency))
    print(transcription)
Exemple #5
0
    def create_ie_plugin(self, log=True):
        if hasattr(self, 'plugin'):
            del self.plugin
        if log:
            print_info('IE version: {}'.format(ie.get_version()))
        if self._is_multi():
            self._create_multi_device_plugin(log)
        else:
            self.plugin = ie.IEPlugin(self._device)
            self.async_mode = self.get_value_from_config('async_mode')
            num_requests = get_or_parse_value(self.config.get('num_requests', 1), casting_type=int)
            if len(num_requests) != 1:
                raise ConfigError('Several values for _num_requests specified')
            self._num_requests = num_requests[0]
            if self._num_requests != 1 and not self.async_mode:
                warning('{} infer requests in sync mode is not supported. Only 1 infer request will be used.')
                self._num_requests = 1
            if log:
                print_info('Loaded {} plugin version: {}'.format(self.plugin.device, self.plugin.version))

        cpu_extensions = self.config.get('cpu_extensions')
        if cpu_extensions and 'CPU' in self._devices_list():
            selection_mode = self.config.get('_cpu_extensions_mode')
            cpu_extensions = DLSDKLauncher.get_cpu_extension(cpu_extensions, selection_mode)
            self.plugin.add_cpu_extension(str(cpu_extensions))
        gpu_extensions = self.config.get('gpu_extensions')
        if gpu_extensions and 'GPU' in self._devices_list():
            self.plugin.set_config('CONFIG_FILE', str(gpu_extensions))
        if self._is_vpu():
            log_level = self.config.get('_vpu_log_level')
            if log_level:
                self.plugin.set_config({'VPU_LOG_LEVEL': log_level})
Exemple #6
0
def main():
    args = build_argparser()

    log.info('OpenVINO Inference Engine')
    log.info('\tbuild: {}'.format(get_version()))
    ie = IECore()
    if args.device == "CPU" and args.cpu_extension:
        ie.add_extension(args.cpu_extension, 'CPU')

    log.info('Reading model {}'.format(args.model))
    net = ie.read_network(args.model, args.model[:-4] + ".bin")

    if len(net.input_info) != 1:
        log.error("Demo supports only models with 1 input layer")
        sys.exit(1)
    input_blob = next(iter(net.input_info))
    input_shape = net.input_info[input_blob].input_data.shape
    if len(net.outputs) != 1:
        log.error("Demo supports only models with 1 output layer")
        sys.exit(1)
    output_blob = next(iter(net.outputs))

    batch_size, channels, one, length = input_shape
    if one != 1:
        raise RuntimeError("Wrong third dimension size of model input shape - {} (expected 1)".format(one))

    hop = length - args.overlap if isinstance(args.overlap, int) else int(length * (1.0 - args.overlap))
    if hop < 0:
        log.error("Wrong value for '-ol/--overlap' argument - overlapping more than clip length")
        sys.exit(1)

    exec_net = ie.load_network(network=net, device_name=args.device)
    log.info('The model {} is loaded to {}'.format(args.model, args.device))

    labels = []
    if args.labels:
        with open(args.labels, "r") as file:
            labels = [line.rstrip() for line in file.readlines()]

    start_time = perf_counter()
    audio = AudioSource(args.input, channels=channels, samplerate=args.sample_rate)

    outputs = []
    clips = 0
    for idx, chunk in enumerate(audio.chunks(length, hop, num_chunks=batch_size)):
        chunk.shape = input_shape
        output = exec_net.infer(inputs={input_blob: chunk})
        clips += batch_size
        output = output[output_blob]
        for batch, data in enumerate(output):
            start_time = (idx*batch_size + batch)*hop / audio.samplerate
            end_time = ((idx*batch_size + batch)*hop + length) / audio.samplerate
            outputs.append(data)
            label = np.argmax(data)
            if start_time < audio.duration():
                log.info("[{:.2f}-{:.2f}] - {:6.2%} {:s}".format(start_time, end_time, data[label],
                                                                 labels[label] if labels else "Class {}".format(label)))
    total_latency = (perf_counter() - start_time) * 1e3
    log.info("Metrics report:")
    log.info("\tLatency: {:.1f} ms".format(total_latency))
Exemple #7
0
 def __init__(self, args, interactive_mode):
     self.args = args
     log.info('OpenVINO Inference Engine')
     log.info('\tbuild: {}'.format(get_version()))
     self.ie = IECore()
     self.encoder = read_net(self.args.m_encoder, self.ie,
                             'Formula Recognition Encoder')
     self.dec_step = read_net(self.args.m_decoder, self.ie,
                              'Formula Recognition Decoder')
     self.exec_net_encoder = self.ie.load_network(
         network=self.encoder, device_name=self.args.device)
     log.info(
         'The Formula Recognition Encoder model {} is loaded to {}'.format(
             args.m_encoder, args.device))
     self.exec_net_decoder = self.ie.load_network(
         network=self.dec_step, device_name=self.args.device)
     log.info(
         'The Formula Recognition Decoder model {} is loaded to {}'.format(
             args.m_decoder, args.device))
     self.images_list = []
     self.vocab = Vocab(self.args.vocab_path)
     self.model_status = Model.Status.READY
     self.is_async = interactive_mode
     self.num_infers_decoder = 0
     self.check_model_dimensions()
     if not interactive_mode:
         self.preprocess_inputs()
def load_ie_core(device, cpu_extension=None):
    """Loads IE Core"""
    log.info('OpenVINO Inference Engine')
    log.info('\tbuild: {}'.format(get_version()))
    ie = IECore()
    if device == "CPU" and cpu_extension:
        ie.add_extension(cpu_extension, "CPU")

    return ie
Exemple #9
0
 def get_version_info(self) -> str:
     logger.info('InferenceEngine:\n{: <9}{:.<24} {}'.format('', 'API version', get_version()))
     version_string = 'Device info\n'
     for device, version in self.ie.get_versions(self.device).items():
         version_string += '{: <9}{}\n'.format('', device)
         version_string += '{: <9}{:.<24}{} {}.{}\n'.format('', version.description, ' version', version.major,
                                                            version.minor)
         version_string += '{: <9}{:.<24} {}\n'.format('', 'Build', version.build_number)
     return version_string
 def _prepare_ie(self, log=True):
     if log:
         print_info('IE version: {}'.format(ie.get_version()))
     if self._is_multi():
         self._prepare_multi_device(log)
     else:
         self.async_mode = self.get_value_from_config('async_mode')
         self._set_nireq()
         if log:
             self._log_versions()
     self._device_specific_configuration()
def openvino_version_check():
    try:
        version = tuple(map(int, get_version().split(".")))[:2]
        if version != (2, 1):
            logger.warning(
                f"OpenVINO version: {version!r} not compatible with this library, "
                f"expected version: 2.1.xxx"
            )
    except NameError:
        version = '0.0.0'
    return version
Exemple #12
0
    def __init__(self, model_xml, model_bin, device, output_name):
        log.info('OpenVINO Inference Engine')
        log.info('\tbuild: {}'.format(get_version()))
        ie = IECore()

        log.info('Reading model {}'.format(model_xml))
        self.net = ie.read_network(model=model_xml, weights=model_bin)
        self.net_exec = ie.load_network(self.net, device)
        log.info('The model {} is loaded to {}'.format(model_xml, device))
        self.output_name = output_name
        assert self.output_name != "", "there is not output in model"
    def __init__(self, model_path, device, cpu_extension):
        log.info('OpenVINO Inference Engine')
        log.info('\tbuild: {}'.format(get_version()))
        ie = IECore()
        if cpu_extension and device == 'CPU':
            ie.add_extension(cpu_extension, 'CPU')

        path = '.'.join(model_path.split('.')[:-1])
        log.info('Reading model {}'.format(model_path))
        self.net = ie.read_network(path + '.xml', path + '.bin')
        self.output_name = list(self.net.outputs.keys())[0]
        self.exec_net = ie.load_network(network=self.net, device_name=device)
        log.info('The model {} is loaded to {}'.format(model_path, device))
    def __init__(self, model_path, device, cpu_extension):
        log.info('OpenVINO Inference Engine')
        log.info('\tbuild: {}'.format(get_version()))
        ie = IECore()
        if cpu_extension and device == 'CPU':
            ie.add_extension(cpu_extension, 'CPU')

        log.info('Reading model {}'.format(model_path))
        self.net = ie.read_network(model_path, model_path.with_suffix('.bin'))
        self.input_name = next(iter(self.net.input_info))
        self.output_name = next(iter(self.net.outputs))
        self.input_size = self.net.input_info[self.input_name].input_data.shape
        self.exec_net = ie.load_network(network=self.net, device_name=device)
        log.info('The model {} is loaded to {}'.format(model_path, device))
    def __init__(self, config_entry, model_name='', delayed_model_loading=False,
                 preprocessor=None, postpone_inputs_configuration=False):
        super().__init__(config_entry, model_name=model_name)
        if ie.get_version().split('-')[0] >= '2022.1.0':
            warnings.warn('dlsdk launcher is deprecated. Please use openvino instead', DeprecationWarning)

        self._set_variable = False
        self.ie_config = self.config.get('ie_config')
        self.ie_core = ie.IECore(xml_config_file=str(self.ie_config)) if self.ie_config is not None else ie.IECore()
        self._delayed_model_loading = delayed_model_loading
        dlsdk_launcher_config = DLSDKLauncherConfigValidator(
            'DLSDK_Launcher', fields=self.parameters(), delayed_model_loading=delayed_model_loading,
        )
        dlsdk_launcher_config.validate(self.config, ie_core=self.ie_core)
        device = self.config['device'].split('.')
        self._device = '.'.join((device[0].upper(), device[1])) if len(device) > 1 else device[0].upper()
        self.dynamic_shapes_policy = self.get_value_from_config('_undefined_shapes_resolving_policy')
        self._set_variable = False
        self._async_mode = False
        self._prepare_ie()
        self._delayed_model_loading = delayed_model_loading
        self._postpone_input_configuration = postpone_inputs_configuration
        self._preprocess_info = {}
        self._preprocess_steps = []
        self.disable_resize_to_input = False
        self._do_reshape = False
        self._use_set_blob = False
        self._output_layouts = {}
        self._output_precisions = {}
        self.dyn_input_layers = []
        self._partial_shapes = {}
        self.is_dynamic = False
        self.preprocessor = preprocessor

        if not delayed_model_loading:
            self._model, self._weights = automatic_model_search(
                self._model_name, self.get_value_from_config('model'),
                self.get_value_from_config('weights'),
                self.get_value_from_config('_model_type')
            )
            self.load_network(log=True, preprocessing=preprocessor)
            self.allow_reshape_input = self.get_value_from_config('allow_reshape_input') and self.network is not None
        else:
            self.allow_reshape_input = self.get_value_from_config('allow_reshape_input')
        self._target_layout_mapping = {}
        self._lstm_inputs = None
        if '_list_lstm_inputs' in self.config:
            self._configure_lstm_inputs()
        self.reset_memory_state = self.get_value_from_config('reset_memory_state')
Exemple #16
0
def main():
    model_bin, model_xml = get_ir_paths(args.model, args.bin)

    config = NNCFConfig.from_json(args.config)

    input_infos_list = create_input_infos(config)
    image_size = input_infos_list[0].shape[-1]

    size = int(image_size / 0.875)

    print('IE version: {}'.format(get_version()))

    # NOTE: importing torch after loading IE to plugin to avoid issue with built-in MKLDNN of PyTorch
    plugin = IEPlugin(device='CPU', plugin_dirs=args.cpu_plugin_dir)
    plugin.add_cpu_extension(
        os.path.join(args.cpu_plugin_dir, "libcpu_extension.so"))
    net = IENetwork(model=model_xml, weights=model_bin)
    exec_net = getExecNet(plugin, net)
    from torch.utils.data import DataLoader
    import torchvision.datasets as datasets
    import torchvision.transforms as transforms

    val_loader = DataLoader(datasets.ImageFolder(
        args.data,
        transforms.Compose([
            transforms.Resize(size),
            transforms.CenterCrop(image_size),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
        ])),
                            batch_size=1,
                            shuffle=False,
                            num_workers=4,
                            pin_memory=True)
    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)
    config['log_dir'] = args.output_dir

    infer_fn = partial(infer_ie_model, net=net)
    validate_general(val_loader, exec_net, infer_fn)

    validate_torch_model(os.path.join(args.output_dir, "PTH"),
                         config=config,
                         num_layers=args.num_layers,
                         dump=args.dump,
                         val_loader=val_loader,
                         cuda=args.cuda)
Exemple #17
0
def import_core_modules(silent: bool, path_to_module: str):
    try:
        from openvino.inference_engine import IECore, get_version  # pylint: disable=import-error
        from openvino.offline_transformations import ApplyMOCTransformations, CheckAPI  # pylint: disable=import-error

        import openvino  # pylint: disable=import-error

        ie_version = str(get_version())
        mo_version = str(version.get_version())  # pylint: disable=no-member

        if not silent:
            print("\t- {}: \t{}".format("Inference Engine found in",
                                        os.path.dirname(openvino.__file__)))
            print("{}: \t{}".format("Inference Engine version", ie_version))
            print("{}: \t    {}".format("Model Optimizer version", mo_version))

        # MO and IE version have a small difference in the beginning of version because
        # IE version also includes API version. For example:
        #   Inference Engine version: 2.1.custom_HEAD_4c8eae0ee2d403f8f5ae15b2c9ad19cfa5a9e1f9
        #   Model Optimizer version:      custom_HEAD_4c8eae0ee2d403f8f5ae15b2c9ad19cfa5a9e1f9
        # So to match this versions we skip IE API version.
        if not re.match(r"^([0-9]+).([0-9]+).{}$".format(mo_version),
                        ie_version):
            extracted_release_version = extract_release_version()
            is_custom_mo_version = extracted_release_version == (None, None)
            if not silent:
                print(
                    "[ WARNING ] Model Optimizer and Inference Engine versions do no match."
                )
                print(
                    "[ WARNING ] Consider building the Inference Engine Python API from sources or reinstall OpenVINO (TM) toolkit using \"pip install openvino{}\" {}"
                    .format(
                        "",
                        "(may be incompatible with the current Model Optimizer version)"
                        if is_custom_mo_version else "=={}.{}".format(
                            *extracted_release_version), ""))

        return True
    except Exception as e:
        # Do not print a warning if module wasn't found or silent mode is on
        if "No module named 'openvino'" not in str(e) and not silent:
            print(
                "[ WARNING ] Failed to import Inference Engine Python API in: {}"
                .format(path_to_module))
            print("[ WARNING ] {}".format(e))
        return False
Exemple #18
0
    def _create_ie_plugin(self, log=True):
        if hasattr(self, 'plugin'):
            del self.plugin
        self.plugin = ie.IEPlugin(self._device)
        if log:
            print_info('IE version: {}'.format(ie.get_version()))
            print_info('Loaded {} plugin version: {}'.format(
                self.plugin.device, self.plugin.version))

        cpu_extensions = self._config.get('cpu_extensions')
        if cpu_extensions and 'CPU' in self._device:
            selection_mode = self._config.get('_cpu_extensions_mode')
            cpu_extensions = DLSDKLauncher.get_cpu_extension(
                cpu_extensions, selection_mode)
            self.plugin.add_cpu_extension(str(cpu_extensions))
        if self._config.get('gpu_extensions') and 'GPU' in self._device:
            self.plugin.set_config('CONFIG_FILE',
                                   str(self._config.get('gpu_extensions')))
    def __init__(self, net_model_xml_path, device, stride):
        self.device = device
        self.stride = stride

        log.info('OpenVINO Inference Engine')
        log.info('\tbuild: {}'.format(get_version()))
        self.ie = IECore()

        log.info('Reading model {}'.format(net_model_xml_path))
        self.net = self.ie.read_network(net_model_xml_path,
                                        net_model_xml_path.with_suffix('.bin'))
        required_input_key = {'data'}
        assert required_input_key == set(self.net.input_info), \
            'Demo supports only topologies with the following input key: {}'.format(', '.join(required_input_key))
        required_output_keys = {'features', 'heatmaps', 'pafs'}
        assert required_output_keys.issubset(self.net.outputs.keys()), \
            'Demo supports only topologies with the following output keys: {}'.format(', '.join(required_output_keys))

        self.exec_net = self.ie.load_network(network=self.net,
                                             num_requests=1,
                                             device_name=device)
        log.info('The model {} is loaded to {}'.format(net_model_xml_path,
                                                       device))
Exemple #20
0
def main():
    args = build_arg_parser().parse_args()

    # Loading source image
    img = cv2.imread(args.input, cv2.IMREAD_COLOR)
    if img is None:
        log.error("Cannot load image " + args.input)
        return -1

    if args.auto_mask_color and args.auto_mask_random:
        log.error("-ar and -ac options cannot be used together")
        return -1

    log.info('OpenVINO Inference Engine')
    log.info('\tbuild: {}'.format(get_version()))
    ie = IECore()

    log.info('Reading model {}'.format(args.model))
    inpainting_processor = ImageInpainting(ie, args.model, args.device)
    log.info('The model {} is loaded to {}'.format(args.model, args.device))

    if args.auto_mask_color or args.auto_mask_random:
        # Command-line inpaining for just one image
        concat_image, result = inpaint_auto(img, inpainting_processor, args)
        if args.output != "":
            cv2.imwrite(args.output, result)
        if not args.no_show:
            cv2.imshow('Image Inpainting Demo', concat_image)
            cv2.waitKey(0)
    else:
        # Inpainting with GUI
        if args.no_show:
            log.error("--no_show argument cannot be used in GUI mode")
            return -1
        InpaintingGUI(img, inpainting_processor).run()
    return 0
Exemple #21
0
def import_core_modules(silent: bool, path_to_module: str):
    """
        This function checks that InferenceEngine Python API is available
        and necessary python modules exists. So the next list of imports
        must contain all IE/NG Python API imports that are used inside MO.

    :param silent: enables or disables logs printing to stdout
    :param path_to_module: path where python API modules were found
    :return: True if all imports were successful and False otherwise
    """
    try:
        from openvino.inference_engine import get_version, read_network  # pylint: disable=import-error,no-name-in-module
        from openvino.offline_transformations import ApplyMOCTransformations, ApplyLowLatencyTransformation, \
            ApplyMakeStatefulTransformation, GenerateMappingFile  # pylint: disable=import-error,no-name-in-module

        # TODO: it is temporary import to check that nGraph python API is available. But in future
        # we need to replace it with Frontend imports
        from ngraph.impl.op import Parameter  # pylint: disable=import-error,no-name-in-module
        from _pyngraph import PartialShape, Dimension  # pylint: disable=import-error,no-name-in-module

        import openvino  # pylint: disable=import-error,no-name-in-module
        import ngraph  # pylint: disable=import-error,no-name-in-module
        import ngraph.frontend  # pylint: disable=import-error,no-name-in-module

        if silent:
            return True

        ie_version = str(get_version())
        mo_version = str(v.get_version())  # pylint: disable=no-member,no-name-in-module

        print("\t- {}: \t{}".format("Inference Engine found in",
                                    os.path.dirname(openvino.__file__)))
        # TODO: when nGraph version will be available we need to start compare it to IE and MO versions. Ticket: 58091
        print("\t- {}: \t{}".format("nGraph found in",
                                    os.path.dirname(ngraph.__file__)))
        print("{}: \t{}".format("Inference Engine version", ie_version))
        print("{}: \t{}".format("Model Optimizer version", mo_version))

        versions_mismatch = False
        if mo_version != ie_version:
            versions_mismatch = True
            extracted_mo_release_version = v.extract_release_version(
                mo_version)
            mo_is_custom = extracted_mo_release_version == (None, None)

            print(
                "[ WARNING ] Model Optimizer and Inference Engine versions do no match."
            )
            print(
                "[ WARNING ] Consider building the Inference Engine Python API from sources or reinstall OpenVINO "
                "(TM) toolkit using",
                end=" ")
            if mo_is_custom:
                print(
                    "\"pip install openvino\" (may be incompatible with the current Model Optimizer version)"
                )
            else:
                print("\"pip install openvino=={}.{}\"".format(
                    *extracted_mo_release_version))

        simplified_mo_version = v.get_simplified_mo_version()
        message = str(
            dict({
                "platform": platform.system(),
                "mo_version": simplified_mo_version,
                "ie_version": v.get_simplified_ie_version(version=ie_version),
                "versions_mismatch": versions_mismatch,
            }))
        send_telemetry(simplified_mo_version, message, 'ie_version_check')

        return True
    except Exception as e:
        # Do not print a warning if module wasn't found or silent mode is on
        if "No module named 'openvino'" not in str(e):
            print(
                "[ WARNING ] Failed to import Inference Engine Python API in: {}"
                .format(path_to_module))
            print("[ WARNING ] {}".format(e))

            # Send telemetry message about warning
            simplified_mo_version = v.get_simplified_mo_version()
            message = str(
                dict({
                    "platform": platform.system(),
                    "mo_version": simplified_mo_version,
                    "ie_version": v.get_simplified_ie_version(env=os.environ),
                    "python_version": sys.version,
                    "error_type": classify_error_type(e),
                }))
            send_telemetry(simplified_mo_version, message, 'ie_import_failed')

        return False
Exemple #22
0
def main(args=None):
    try:
        # ------------------------------ 1. Parsing and validating input arguments -------------------------------------
        next_step()

        if not args:
            args = parse_args()

        # ------------------------------ 2. Loading Inference Engine ---------------------------------------------------
        next_step()

        device_name = args.target_device.upper()

        ie = IECore()

        if CPU_DEVICE_NAME in device_name:
            if args.path_to_extension:
                ie.add_cpu_extension(extension_path=args.path_to_extension,
                                     device_name=CPU_DEVICE_NAME)
        if GPU_DEVICE_NAME in device_name:
            if args.path_to_cldnn_config:
                ie.set_config({'CONFIG_FILE': args.path_to_cldnn_config},
                              GPU_DEVICE_NAME)
                logger.info("GPU extensions is loaded {}".format(
                    args.path_to_cldnn_config))

        logger.info("InferenceEngine:\n{: <9}{}".format("", get_version()))
        version_string = "Device is {}\n".format(device_name)
        for device, version in ie.get_versions(device_name).items():
            version_string += "{: <9}{}\n".format("", device)
            version_string += "{: <9}{:.<24}{} {}.{}\n".format(
                "", version.description, " version", version.major,
                version.minor)
            version_string += "{: <9}{:.<24} {}\n".format(
                "", "Build", version.build_number)
        logger.info(version_string)

        # --------------------- 3. Read the Intermediate Representation of the network ---------------------------------
        next_step()

        xml_filename = os.path.abspath(args.path_to_model)
        head, tail = os.path.splitext(xml_filename)
        bin_filename = os.path.abspath(head + BIN_EXTENSION)

        ie_network = IENetwork(xml_filename, bin_filename)

        input_info = ie_network.inputs

        if len(input_info) == 0:
            raise AttributeError('No inputs info is provided')

        # --------------------- 4. Resizing network to match image sizes and given batch -------------------------------
        next_step()

        batch_size = ie_network.batch_size
        precision = ie_network.precision

        if args.batch_size and args.batch_size != ie_network.batch_size:
            new_shapes = {}
            for key in input_info.keys():
                shape = input_info[key].shape
                layout = input_info[key].layout

                batchIndex = -1
                if ((layout == 'NCHW') or (layout == 'NCDHW')
                        or (layout == 'NHWC') or (layout == 'NDHWC')
                        or (layout == 'NC')):
                    batchIndex = 0
                elif (layout == 'CN'):
                    batchIndex = 1

                if ((batchIndex != -1)
                        and (shape[batchIndex] != args.batch_size)):
                    shape[batchIndex] = args.batch_size
                    new_shapes[key] = shape

            if (len(new_shapes) > 0):
                logger.info("Resizing network to batch = {}".format(
                    args.batch_size))
                ie_network.reshape(new_shapes)

            batch_size = args.batch_size

        logger.info("Network batch size: {}, precision {}".format(
            batch_size, precision))

        # --------------------- 5. Configuring input of the model ------------------------------------------------------
        next_step()

        for key in input_info.keys():
            if (isImage(input_info[key])):
                # Set the precision of input data provided by the user
                # Should be called before load of the network to the plugin
                input_info[key].precision = 'U8'

        # --------------------- 6. Setting device configuration --------------------------------------------------------
        next_step()

        devices = parseDevices(device_name)
        device_nstreams = parseValuePerDevice(devices, args.number_streams)
        for device in devices:
            if device == CPU_DEVICE_NAME:  ## CPU supports few special performance-oriented keys
                ## limit threading for CPU portion of inference
                if args.number_threads:
                    ie.set_config(
                        {'CPU_THREADS_NUM': str(args.number_threads)}, device)

                if MULTI_DEVICE_NAME in device_name and GPU_DEVICE_NAME in device_name:
                    ie.set_config({'CPU_BIND_THREAD': 'NO'}, CPU_DEVICE_NAME)
                else:
                    # pin threads for CPU portion of inference
                    ie.set_config(
                        {'CPU_BIND_THREAD': args.infer_threads_pinning},
                        device)

                ## for CPU execution, more throughput-oriented execution via streams
                # for pure CPU execution, more throughput-oriented execution via streams
                if args.api_type == 'async':
                    ie.set_config(
                        {
                            'CPU_THROUGHPUT_STREAMS':
                            str(device_nstreams.get(device))
                            if device in device_nstreams.keys() else
                            'CPU_THROUGHPUT_AUTO'
                        }, device)
                device_nstreams[device] = int(
                    ie.get_config(device, 'CPU_THROUGHPUT_STREAMS'))

            elif device == GPU_DEVICE_NAME:
                if args.api_type == 'async':
                    ie.set_config(
                        {
                            'GPU_THROUGHPUT_STREAMS':
                            str(device_nstreams.get(device))
                            if device in device_nstreams.keys() else
                            'GPU_THROUGHPUT_AUTO'
                        }, device)
                device_nstreams[device] = int(
                    ie.get_config(device, 'GPU_THROUGHPUT_STREAMS'))

                if MULTI_DEVICE_NAME in device_name and CPU_DEVICE_NAME in device_name:
                    ## multi-device execution with the CPU+GPU performs best with GPU trottling hint,
                    ## which releases another CPU thread (that is otherwise used by the GPU driver for active polling)
                    ie.set_config({'CLDNN_PLUGIN_THROTTLE': str(1)}, device)

            elif device == MYRIAD_DEVICE_NAME:
                ie.set_config(
                    {
                        'LOG_LEVEL': 'LOG_INFO',
                        'VPU_LOG_LEVEL': 'LOG_WARNING'
                    }, MYRIAD_DEVICE_NAME)

        # --------------------- 7. Loading the model to the device -----------------------------------------------------
        next_step()

        config = {'PERF_COUNT': ('YES' if args.perf_counts else 'NO')}

        exe_network = ie.load_network(ie_network,
                                      device_name,
                                      config=config,
                                      num_requests=args.number_infer_requests
                                      if args.number_infer_requests else 0)

        # --------------------- 8. Setting optimal runtime parameters --------------------------------------------------
        next_step()

        ## Number of requests
        infer_requests = exe_network.requests
        nireq = len(infer_requests)

        ## Iteration limit
        niter = args.number_iterations
        if niter and args.api_type == 'async':
            niter = (int)((niter + nireq - 1) / nireq) * nireq
            if (args.number_iterations != niter):
                logger.warn(
                    "Number of iterations was aligned by request number "
                    "from {} to {} using number of requests {}".format(
                        args.number_iterations, niter, nireq))

        ## Time limit
        duration_seconds = 0
        if args.time:
            ## time limit
            duration_seconds = args.time
        elif not args.number_iterations:
            ## default time limit
            duration_seconds = get_duration_in_secs(device)

        # ------------------------------------ 8. Creating infer requests and filling input blobs ----------------------
        next_step()

        request_queue = InferRequestsQueue(infer_requests)

        path_to_input = os.path.abspath(
            args.path_to_input) if args.path_to_input else None
        requests_input_data = getInputs(path_to_input, batch_size,
                                        ie_network.inputs, infer_requests)

        # ------------------------------------ 9. Measuring performance ------------------------------------------------

        progress_count = 0
        progress_bar_total_count = 10000

        output_string = "Start inference {}ronously".format(args.api_type)
        if (args.api_type == "async"):
            if output_string != "":
                output_string += ", "

            output_string += str(nireq) + " inference requests"
            device_ss = ''
            for device, nstreams in device_nstreams.items():
                if device_ss != '':
                    device_ss += ', '
                device_ss += "{} streams for {}".format(str(nstreams), device)
            if device_ss != '':
                output_string += " using " + device_ss

        output_string += ", limits: "
        if niter:
            if not duration_seconds:
                progress_bar_total_count = niter
            output_string += str(niter) + " iterations"

        if duration_seconds:
            if niter:
                output_string += ", "
            output_string += str(
                getDurationInMilliseconds(duration_seconds)) + " ms duration"

        next_step(output_string)

        ## warming up - out of scope
        infer_request = request_queue.getIdleRequest()
        if not infer_request:
            raise Exception("No idle Infer Requests!")

        if (args.api_type == 'sync'):
            infer_request.infer(requests_input_data[infer_request.id])
        else:
            infer_request.startAsync(requests_input_data[infer_request.id])

        request_queue.waitAll()
        request_queue.resetTimes()

        start_time = datetime.now()
        exec_time = (datetime.now() - start_time).total_seconds()
        iteration = 0

        progress_bar = ProgressBar(progress_bar_total_count,
                                   args.stream_output, args.progress)

        ## Start inference & calculate performance
        ## to align number if iterations to guarantee that last infer requests are executed in the same conditions **/
        while ((niter and iteration < niter)
               or (duration_seconds and exec_time < duration_seconds)
               or (args.api_type == "async" and iteration % nireq != 0)):
            infer_request = request_queue.getIdleRequest()
            if not infer_request:
                raise Exception("No idle Infer Requests!")

            if (args.api_type == 'sync'):
                infer_request.infer(requests_input_data[infer_request.id])
            else:
                infer_request.startAsync(requests_input_data[infer_request.id])
            iteration += 1

            exec_time = (datetime.now() - start_time).total_seconds()

            if niter:
                progress_bar.add_progress(1)
            else:
                ## calculate how many progress intervals are covered by current iteration.
                ## depends on the current iteration time and time of each progress interval.
                ## Previously covered progress intervals must be skipped.
                progress_interval_time = duration_seconds / progress_bar_total_count
                new_progress = (int)(exec_time / progress_interval_time -
                                     progress_count)
                progress_bar.add_progress(new_progress)
                progress_count += new_progress

        ## wait the latest inference executions
        request_queue.waitAll()

        total_duration_sec = request_queue.getDurationInSeconds()
        times = request_queue.times
        times.sort()
        latency_ms = median(times)
        fps = batch_size * 1000 / latency_ms if args.api_type == 'sync' else batch_size * iteration / total_duration_sec

        progress_bar.finish()

        # ------------------------------------ 10. Dumping statistics report -------------------------------------------
        next_step()

        if args.exec_graph_path:
            try:
                exec_graph_info = exe_network.get_exec_graph_info()
                exec_graph_info.serialize(args.exec_graph_path)
                logger.info("Executable graph is stored to {}".format(
                    args.exec_graph_path))
                del exec_graph_info
            except Exception as e:
                logging.exception(e)

        if args.perf_counts:
            for ni in range(int(nireq)):
                perf_counts = exe_network.requests[ni].get_perf_counts()
                logger.info(
                    "Pefrormance counts for {}-th infer request".format(ni))
                for layer, stats in perf_counts.items():
                    max_layer_name = 30
                    print("{:<30}{:<15}{:<30}{:<20}{:<20}{:<20}".format(
                        layer[:max_layer_name - 4] + '...' if
                        (len(layer) >= max_layer_name) else layer,
                        stats['status'],
                        'layerType: ' + str(stats['layer_type']),
                        'realTime: ' + str(stats['real_time']),
                        'cpu: ' + str(stats['cpu_time']),
                        'execType: ' + str(stats['exec_type'])))

        print("Count:      {} iterations".format(iteration))
        print("Duration:   {:.2f} ms".format(
            getDurationInMilliseconds(total_duration_sec)))
        if not MULTI_DEVICE_NAME in device_name:
            print("Latency:    {:.4f} ms".format(latency_ms))
        print("Throughput: {:.2f} FPS".format(fps))

        del exe_network
        del ie
        next_step.step_id = 0
    except Exception as e:
        logging.exception(e)
Exemple #23
0
    if enable_detection:
        devices = [
            args.device, args.device, args.device_age_gender,
            args.device_emotions, args.device_head_pose,
            args.device_facial_landmarks
        ]
        models = [
            args.model_ssd, args.model_face, args.model_age_gender,
            args.model_emotions, args.model_head_pose,
            args.model_facial_landmarks
        ]
        # openvino.inference_engine version '2.1.37988' is openvino_2020.1.033 build
        # , which does not need cpu extension. 
        # https://software.intel.com/en-us/forums/intel-distribution-of-openvino-toolkit/topic/848825
        if "CPU" in devices and args.cpu_extension is None and (get_version() < '2.1.37988'):
            print(
                "\nPlease try to specify cpu extensions library path in demo's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)

        # Create detectors class instance
        detections = interactive_detection.Detections(
            devices, models, args.cpu_extension, args.plugin_dir,
            args.prob_threshold, args.prob_threshold_face, is_async_mode)
        models = detections.models  # Get models to display WebUI.

    # Create a UDP socket to send and receive message with tello
    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
        host = '0.0.0.0'
        port = 9000
Exemple #24
0
def main():
    args = build_argparser().parse_args()

    cap = open_images_capture(args.input, args.loop)

    # Plugin initialization for specified device and load extensions library if specified.
    log.info('OpenVINO Inference Engine')
    log.info('\tbuild: {}'.format(get_version()))
    ie = IECore()
    if args.cpu_extension and 'CPU' in args.device:
        ie.add_extension(args.cpu_extension, 'CPU')
    # Read IR
    log.info('Reading Mask-RCNN model {}'.format(args.mask_rcnn_model))
    mask_rcnn_net = ie.read_network(args.mask_rcnn_model)

    model_required_inputs = {'image'}
    if set(mask_rcnn_net.input_info) == model_required_inputs:
        required_output_keys = {'boxes', 'labels', 'masks', 'text_features.0'}
        n, c, h, w = mask_rcnn_net.input_info['image'].input_data.shape
        assert n == 1, 'Only batch 1 is supported by the demo application'
    else:
        raise RuntimeError(
            'Demo supports only topologies with the following input keys: '
            f'{model_required_inputs}.')
    assert required_output_keys.issubset(mask_rcnn_net.outputs.keys()), \
        f'Demo supports only topologies with the following output keys: {required_output_keys}' \
        f'Found: {mask_rcnn_net.outputs.keys()}.'

    log.info('Reading Text Recognition Encoder model {}'.format(
        args.text_enc_model))
    text_enc_net = ie.read_network(args.text_enc_model)

    log.info('Reading Text Recognition Decoder model {}'.format(
        args.text_dec_model))
    text_dec_net = ie.read_network(args.text_dec_model)

    mask_rcnn_exec_net = ie.load_network(network=mask_rcnn_net,
                                         device_name=args.device,
                                         num_requests=2)
    log.info('The Mask-RCNN model {} is loaded to {}'.format(
        args.mask_rcnn_model, args.device))

    text_enc_exec_net = ie.load_network(network=text_enc_net,
                                        device_name=args.device)
    log.info('The Text Recognition Encoder model {} is loaded to {}'.format(
        args.text_enc_model, args.device))

    text_dec_exec_net = ie.load_network(network=text_dec_net,
                                        device_name=args.device)
    log.info('The Text Recognition Decoder model {} is loaded to {}'.format(
        args.text_dec_model, args.device))

    hidden_shape = text_dec_net.input_info[
        args.trd_input_prev_hidden].input_data.shape

    del mask_rcnn_net
    del text_enc_net
    del text_dec_net

    if args.no_track:
        tracker = None
    else:
        tracker = StaticIOUTracker()

    if args.delay:
        delay = args.delay
    else:
        delay = int(cap.get_type() in ('VIDEO', 'CAMERA'))

    visualizer = Visualizer(['__background__', 'text'],
                            show_boxes=args.show_boxes,
                            show_scores=args.show_scores)

    frames_processed = 0

    metrics = PerformanceMetrics()
    video_writer = cv2.VideoWriter()

    start_time = perf_counter()
    frame = cap.read()
    if frame is None:
        raise RuntimeError("Can't read an image from the input")

    presenter = monitors.Presenter(args.utilization_monitors, 45,
                                   (frame.shape[1] // 4, frame.shape[0] // 8))
    if args.output and not video_writer.open(
            args.output, cv2.VideoWriter_fourcc(*'MJPG'), cap.fps(),
        (frame.shape[1], frame.shape[0])):
        raise RuntimeError("Can't open video writer")

    while frame is not None:
        if not args.keep_aspect_ratio:
            # Resize the image to a target size.
            scale_x = w / frame.shape[1]
            scale_y = h / frame.shape[0]
            input_image = cv2.resize(frame, (w, h))
        else:
            # Resize the image to keep the same aspect ratio and to fit it to a window of a target size.
            scale_x = scale_y = min(h / frame.shape[0], w / frame.shape[1])
            input_image = cv2.resize(frame, None, fx=scale_x, fy=scale_y)

        input_image_size = input_image.shape[:2]
        input_image = np.pad(input_image,
                             ((0, h - input_image_size[0]),
                              (0, w - input_image_size[1]), (0, 0)),
                             mode='constant',
                             constant_values=0)
        # Change data layout from HWC to CHW.
        input_image = input_image.transpose((2, 0, 1))
        input_image = input_image.reshape((n, c, h, w)).astype(np.float32)

        # Run the net.
        outputs = mask_rcnn_exec_net.infer({'image': input_image})

        # Parse detection results of the current request
        boxes = outputs['boxes'][:, :4]
        scores = outputs['boxes'][:, 4]
        classes = outputs['labels'].astype(np.uint32)
        raw_masks = outputs['masks']
        text_features = outputs['text_features.0']

        # Filter out detections with low confidence.
        detections_filter = scores > args.prob_threshold
        scores = scores[detections_filter]
        classes = classes[detections_filter]
        boxes = boxes[detections_filter]
        raw_masks = raw_masks[detections_filter]
        text_features = text_features[detections_filter]

        boxes[:, 0::2] /= scale_x
        boxes[:, 1::2] /= scale_y
        masks = []
        for box, cls, raw_mask in zip(boxes, classes, raw_masks):
            mask = segm_postprocess(box, raw_mask, frame.shape[0],
                                    frame.shape[1])
            masks.append(mask)

        texts = []
        for feature in text_features:
            feature = text_enc_exec_net.infer({'input': feature})['output']
            feature = np.reshape(feature,
                                 (feature.shape[0], feature.shape[1], -1))
            feature = np.transpose(feature, (0, 2, 1))

            hidden = np.zeros(hidden_shape)
            prev_symbol_index = np.ones((1, )) * SOS_INDEX

            text = ''
            text_confidence = 1.0
            for i in range(MAX_SEQ_LEN):
                decoder_output = text_dec_exec_net.infer({
                    args.trd_input_prev_symbol:
                    prev_symbol_index,
                    args.trd_input_prev_hidden:
                    hidden,
                    args.trd_input_encoder_outputs:
                    feature
                })
                symbols_distr = decoder_output[args.trd_output_symbols_distr]
                symbols_distr_softmaxed = softmax(symbols_distr, axis=1)[0]
                prev_symbol_index = int(np.argmax(symbols_distr, axis=1))
                text_confidence *= symbols_distr_softmaxed[prev_symbol_index]
                if prev_symbol_index == EOS_INDEX:
                    break
                text += args.alphabet[prev_symbol_index]
                hidden = decoder_output[args.trd_output_cur_hidden]

            texts.append(text if text_confidence >= args.tr_threshold else '')

        if len(boxes) and args.raw_output_message:
            log.debug(
                '  -------------------------- Frame # {} --------------------------  '
                .format(frames_processed))
            log.debug(
                '  Class ID | Confidence |     XMIN |     YMIN |     XMAX |     YMAX '
            )
            for box, cls, score, mask in zip(boxes, classes, scores, masks):
                log.debug(
                    '{:>10} | {:>10f} | {:>8.2f} | {:>8.2f} | {:>8.2f} | {:>8.2f} '
                    .format(cls, score, *box))

        # Get instance track IDs.
        masks_tracks_ids = None
        if tracker is not None:
            masks_tracks_ids = tracker(masks, classes)

        presenter.drawGraphs(frame)

        # Visualize masks.
        frame = visualizer(frame, boxes, classes, scores, masks, texts,
                           masks_tracks_ids)
        metrics.update(start_time, frame)

        frames_processed += 1
        if video_writer.isOpened() and (args.output_limit <= 0 or
                                        frames_processed <= args.output_limit):
            video_writer.write(frame)

        if not args.no_show:
            # Show resulting image.
            cv2.imshow('Results', frame)

        if not args.no_show:
            key = cv2.waitKey(delay)
            esc_code = 27
            if key == esc_code:
                break
            presenter.handleKey(key)

        start_time = perf_counter()
        frame = cap.read()

    metrics.log_total()
    for rep in presenter.reportMeans():
        log.info(rep)
    cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
# Copyright (C) 2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import openvino.inference_engine as ie

print('OpenVINO version:', ie.get_version())
print('Available devices: ', ie.IECore().available_devices)
Exemple #26
0
def main():
    args = build_argparser().parse_args()

    # load vocabulary file for model
    vocab = load_vocab_file(args.vocab)
    log.debug("Loaded vocab file from {}, get {} tokens".format(
        args.vocab, len(vocab)))

    # create tokenizer
    tokenizer = Tokenizer(BPE(str(args.vocab), str(args.merges)))
    tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=False)
    tokenizer.decoder = decoders.ByteLevel()

    log.info('OpenVINO Inference Engine')
    log.info('\tbuild: {}'.format(get_version()))
    ie = IECore()

    # read IR
    model_xml = args.model
    model_bin = model_xml.with_suffix(".bin")
    log.info('Reading model {}'.format(args.model))
    ie_net = ie.read_network(model=model_xml, weights=model_bin)

    # check input and output names
    if len(ie_net.input_info) != 1:
        raise RuntimeError(
            'The demo expects model with single input, while provided {}'.
            format(len(ie_net.input_info)))
    if len(ie_net.outputs) != 1:
        raise RuntimeError(
            'The demo expects model with single output, while provided {}'.
            format(len(ie_net.outputs)))
    input_names = next(iter(ie_net.input_info))
    output_names = next(iter(ie_net.outputs))

    # load model to the device
    ie_net_exec = ie.load_network(network=ie_net, device_name=args.device)
    log.info('The model {} is loaded to {}'.format(args.model, args.device))

    if args.input:

        def prompts():
            for prompt in args.input:
                log.info("Input prompt: {}".format(prompt))
                yield prompt
    else:

        def prompts():
            while True:
                yield input('Type input prompt (empty string to exit):')

    # loop on user's or prepared prompts
    for prompt in prompts():
        if not prompt.strip():
            break

        # encode input
        tokens = tokenizer.encode_batch([prompt])[0].ids
        input_ids = np.array([tokens], dtype=np.int32)

        # maximum number of tokens that can be processed by network at once
        max_length = ie_net.input_info[input_names].input_data.shape[1]

        eos_token_id = len(vocab) - 1

        cur_input_len = input_ids.shape[-1]

        # maximum number of tokens that will be generated
        max_sample_token_num = args.max_sample_token_num + cur_input_len

        t0 = time.perf_counter()
        t_count = 0

        while True:
            # pad the rest of the request
            pad_len = max_length - cur_input_len
            model_input = np.concatenate(
                (input_ids, [[eos_token_id] * pad_len]), axis=-1)

            # create numpy inputs for IE
            inputs = {
                input_names: model_input,
            }

            # infer by IE
            t_start = time.perf_counter()
            res = ie_net_exec.infer(inputs=inputs)
            t_end = time.perf_counter()
            t_count += 1
            log.info(
                "Sequence of length {} is processed with {:0.2f} requests/sec ({:0.2} sec per request)"
                .format(max_length, 1 / (t_end - t_start), t_end - t_start))

            outputs = res[output_names]
            next_token_logits = outputs[:, cur_input_len - 1, :]

            # pre-process distribution
            next_token_scores = process_logits(input_ids, next_token_logits,
                                               eos_token_id)
            if args.top_k > 0:
                next_token_scores = get_top_k_logits(next_token_scores,
                                                     args.top_k)

            if args.top_p < 1.0:
                next_token_scores = get_top_p_logits(next_token_scores,
                                                     args.top_p)

            # get next token id
            probs = softmax(next_token_scores)
            next_tokens = np.random.choice(probs.shape[-1],
                                           1,
                                           p=probs[0],
                                           replace=True)

            # update info for the next step
            input_ids = np.concatenate((input_ids, [next_tokens]), axis=-1)

            cur_input_len = input_ids.shape[-1]

            if stop_criteria(input_ids, min(max_length, max_sample_token_num),
                             eos_token_id):
                break

        t1 = time.perf_counter()

        text = tokenizer.decode_batch(input_ids)[0]

        log.info(
            "{} requests of {} length were processed in {:0.2f}sec ({:0.2}sec per request)"
            .format(t_count, max_length, t1 - t0, (t1 - t0) / t_count))

        # print result
        log.info("GENERATED SEQUENCE: {}".format(text))
def main():
    current_dir = os.path.dirname(os.path.abspath(__file__))
    """Prepares data for the object tracking demo"""
    parser = argparse.ArgumentParser(description='Multi camera multi object \
                                                  tracking live demo script')
    parser.add_argument(
        '-i',
        '--input',
        required=True,
        nargs='+',
        help=
        'Required. Input sources (indexes of cameras or paths to video files)')
    parser.add_argument('--loop',
                        default=False,
                        action='store_true',
                        help='Optional. Enable reading the input in a loop')
    parser.add_argument('--config',
                        type=str,
                        default=os.path.join(current_dir, 'configs/person.py'),
                        required=False,
                        help='Configuration file')

    parser.add_argument('--detections',
                        type=str,
                        help='JSON file with bounding boxes')

    parser.add_argument('-m',
                        '--m_detector',
                        type=str,
                        required=False,
                        help='Path to the object detection model')
    parser.add_argument('--t_detector',
                        type=float,
                        default=0.6,
                        help='Threshold for the object detection model')

    parser.add_argument('--m_segmentation',
                        type=str,
                        required=False,
                        help='Path to the object instance segmentation model')
    parser.add_argument(
        '--t_segmentation',
        type=float,
        default=0.6,
        help='Threshold for object instance segmentation model')

    parser.add_argument(
        '--m_reid',
        type=str,
        required=True,
        help='Required. Path to the object re-identification model')

    parser.add_argument('--output_video',
                        type=str,
                        default='',
                        required=False,
                        help='Optional. Path to output video')
    parser.add_argument(
        '--history_file',
        type=str,
        default='',
        required=False,
        help='Optional. Path to file in JSON format to save results of the demo'
    )
    parser.add_argument(
        '--save_detections',
        type=str,
        default='',
        required=False,
        help='Optional. Path to file in JSON format to save bounding boxes')
    parser.add_argument("--no_show",
                        help="Optional. Don't show output",
                        action='store_true')

    parser.add_argument('-d', '--device', type=str, default='CPU')
    parser.add_argument('-l',
                        '--cpu_extension',
                        help='MKLDNN (CPU)-targeted custom layers.Absolute \
                              path to a shared library with the kernels impl.',
                        type=str,
                        default=None)
    parser.add_argument('-u',
                        '--utilization_monitors',
                        default='',
                        type=str,
                        help='Optional. List of monitors to show initially.')

    args = parser.parse_args()
    if check_detectors(args) != 1:
        sys.exit(1)

    if len(args.config):
        log.debug('Reading config from {}'.format(args.config))
        config = read_py_config(args.config)
    else:
        log.error(
            'No configuration file specified. Please specify parameter \'--config\''
        )
        sys.exit(1)

    random.seed(config.random_seed)
    capture = MulticamCapture(args.input, args.loop)

    log.info('OpenVINO Inference Engine')
    log.info('\tbuild: {}'.format(get_version()))
    ie = IECore()

    if args.detections:
        object_detector = DetectionsFromFileReader(args.detections,
                                                   args.t_detector)
    elif args.m_segmentation:
        object_detector = MaskRCNN(ie, args.m_segmentation,
                                   config.obj_segm.trg_classes,
                                   args.t_segmentation,
                                   args.device, args.cpu_extension,
                                   capture.get_num_sources())
    else:
        object_detector = Detector(ie, args.m_detector,
                                   config.obj_det.trg_classes, args.t_detector,
                                   args.device, args.cpu_extension,
                                   capture.get_num_sources())

    if args.m_reid:
        object_recognizer = VectorCNN(ie, args.m_reid, args.device,
                                      args.cpu_extension)
    else:
        object_recognizer = None

    run(args, config, capture, object_detector, object_recognizer)
def main():
    args = build_argparser().parse_args()

    cap = open_images_capture(args.input, args.loop)

    with open(args.labels, 'rt') as labels_file:
        class_labels = labels_file.read().splitlines()
        assert len(class_labels), 'The file with class labels is empty'

    # Plugin initialization for specified device and load extensions library if specified.
    log.info('OpenVINO Inference Engine')
    log.info('\tbuild: {}'.format(get_version()))
    ie = IECore()
    if args.cpu_extension and 'CPU' in args.device:
        ie.add_extension(args.cpu_extension, 'CPU')

    # Read IR
    log.info('Reading model {}'.format(args.model))
    net = ie.read_network(args.model, args.model.with_suffix('.bin'))
    image_input, image_info_input, (
        n, c, h, w), model_type, postprocessor = check_model(net)
    args.no_keep_aspect_ratio = model_type == 'yolact' or args.no_keep_aspect_ratio

    exec_net = ie.load_network(network=net,
                               device_name=args.device,
                               num_requests=2)
    log.info('The model {} is loaded to {}'.format(args.model, args.device))

    if args.no_track:
        tracker = None
    else:
        tracker = StaticIOUTracker()

    if args.delay:
        delay = args.delay
    else:
        delay = int(cap.get_type() in ('VIDEO', 'CAMERA'))

    frames_processed = 0
    metrics = PerformanceMetrics()
    visualizer = Visualizer(class_labels,
                            show_boxes=args.show_boxes,
                            show_scores=args.show_scores)
    video_writer = cv2.VideoWriter()

    start_time = perf_counter()
    frame = cap.read()
    if frame is None:
        raise RuntimeError("Can't read an image from the input")

    out_frame_size = (frame.shape[1], frame.shape[0])
    presenter = monitors.Presenter(
        args.utilization_monitors, 45,
        (round(out_frame_size[0] / 4), round(out_frame_size[1] / 8)))
    if args.output and not video_writer.open(args.output,
                                             cv2.VideoWriter_fourcc(*'MJPG'),
                                             cap.fps(), out_frame_size):
        raise RuntimeError("Can't open video writer")

    while frame is not None:
        if args.no_keep_aspect_ratio:
            # Resize the image to a target size.
            scale_x = w / frame.shape[1]
            scale_y = h / frame.shape[0]
            input_image = cv2.resize(frame, (w, h))
        else:
            # Resize the image to keep the same aspect ratio and to fit it to a window of a target size.
            scale_x = scale_y = min(h / frame.shape[0], w / frame.shape[1])
            input_image = cv2.resize(frame, None, fx=scale_x, fy=scale_y)

        input_image_size = input_image.shape[:2]
        input_image = np.pad(input_image,
                             ((0, h - input_image_size[0]),
                              (0, w - input_image_size[1]), (0, 0)),
                             mode='constant',
                             constant_values=0)
        # Change data layout from HWC to CHW.
        input_image = input_image.transpose((2, 0, 1))
        input_image = input_image.reshape((n, c, h, w)).astype(np.float32)
        input_image_info = np.asarray(
            [[input_image_size[0], input_image_size[1], 1]], dtype=np.float32)

        # Run the net.
        feed_dict = {image_input: input_image}
        if image_info_input:
            feed_dict[image_info_input] = input_image_info
        outputs = exec_net.infer(feed_dict)

        # Parse detection results of the current request
        scores, classes, boxes, masks = postprocessor(outputs, scale_x,
                                                      scale_y,
                                                      *frame.shape[:2], h, w,
                                                      args.prob_threshold)

        if len(boxes) and args.raw_output_message:
            log.debug(
                '  -------------------------- Frame # {} --------------------------  '
                .format(frames_processed))
            log.debug(
                '  Class ID | Confidence |     XMIN |     YMIN |     XMAX |     YMAX '
            )
            for box, cls, score, mask in zip(boxes, classes, scores, masks):
                log.debug(
                    '{:>10} | {:>10f} | {:>8.2f} | {:>8.2f} | {:>8.2f} | {:>8.2f} '
                    .format(cls, score, *box))

        # Get instance track IDs.
        masks_tracks_ids = None
        if tracker is not None:
            masks_tracks_ids = tracker(masks, classes)

        # Visualize masks.
        frame = visualizer(frame, boxes, classes, scores, presenter, masks,
                           masks_tracks_ids)

        metrics.update(start_time, frame)

        frames_processed += 1
        if video_writer.isOpened() and (args.output_limit <= 0 or
                                        frames_processed <= args.output_limit):
            video_writer.write(frame)

        if not args.no_show:
            # Show resulting image.
            cv2.imshow('Results', frame)

        if not args.no_show:
            key = cv2.waitKey(delay)
            esc_code = 27
            if key == esc_code:
                break
            presenter.handleKey(key)
        start_time = perf_counter()
        frame = cap.read()

    metrics.log_total()
    for rep in presenter.reportMeans():
        log.info(rep)
    cv2.destroyAllWindows()
Exemple #29
0
def import_core_modules(silent: bool, path_to_module: str):
    try:
        from openvino.inference_engine import IECore, get_version  # pylint: disable=import-error
        from openvino.offline_transformations import ApplyMOCTransformations, CheckAPI  # pylint: disable=import-error

        import openvino  # pylint: disable=import-error

        if silent:
            return True

        ie_version = str(get_version())
        mo_version = str(v.get_version())  # pylint: disable=no-member

        print("\t- {}: \t{}".format("Inference Engine found in",
                                    os.path.dirname(openvino.__file__)))
        print("{}: \t{}".format("Inference Engine version", ie_version))
        print("{}: \t    {}".format("Model Optimizer version", mo_version))

        versions_mismatch = False
        # MO and IE version have a small difference in the beginning of version because
        # IE version also includes API version. For example:
        #   Inference Engine version: 2.1.custom_HEAD_4c8eae0ee2d403f8f5ae15b2c9ad19cfa5a9e1f9
        #   Model Optimizer version:      custom_HEAD_4c8eae0ee2d403f8f5ae15b2c9ad19cfa5a9e1f9
        # So to match this versions we skip IE API version.
        if not re.match(r"^([0-9]+).([0-9]+).{}$".format(mo_version),
                        ie_version):
            versions_mismatch = True
            extracted_mo_release_version = v.extract_release_version(
                mo_version)
            mo_is_custom = extracted_mo_release_version == (None, None)

            print(
                "[ WARNING ] Model Optimizer and Inference Engine versions do no match."
            )
            print(
                "[ WARNING ] Consider building the Inference Engine Python API from sources or reinstall OpenVINO (TM) toolkit using",
                end=" ")
            if mo_is_custom:
                print(
                    "\"pip install openvino\" (may be incompatible with the current Model Optimizer version)"
                )
            else:
                print("\"pip install openvino=={}.{}\"".format(
                    *extracted_mo_release_version))

        simplified_mo_version = v.get_simplified_mo_version()
        message = str(
            dict({
                "platform": platform.system(),
                "mo_version": simplified_mo_version,
                "ie_version": v.get_simplified_ie_version(version=ie_version),
                "versions_mismatch": versions_mismatch,
            }))
        send_telemetry(simplified_mo_version, message, 'ie_version_check')

        return True
    except Exception as e:
        # Do not print a warning if module wasn't found or silent mode is on
        if "No module named 'openvino'" not in str(e) and not silent:
            print(
                "[ WARNING ] Failed to import Inference Engine Python API in: {}"
                .format(path_to_module))
            print("[ WARNING ] {}".format(e))

            # Send telemetry message about warning
            simplified_mo_version = v.get_simplified_mo_version()
            message = str(
                dict({
                    "platform": platform.system(),
                    "mo_version": simplified_mo_version,
                    "ie_version": v.get_simplified_ie_version(env=os.environ),
                    "python_version": sys.version,
                    "error_type": classify_error_type(e),
                }))
            send_telemetry(simplified_mo_version, message, 'ie_import_failed')

        return False
def main():
    args = build_argparser().parse_args()

    cap = open_images_capture(args.input, args.loop)

    # Plugin initialization for specified device and load extensions library if specified
    log.info('OpenVINO Inference Engine')
    log.info('\tbuild: {}'.format(get_version()))
    ie = IECore()

    # Read IR
    log.info('Reading Proposal model {}'.format(args.model_pnet))
    p_net = ie.read_network(args.model_pnet)
    assert len(p_net.input_info.keys()) == 1, "Pnet supports only single input topologies"
    assert len(p_net.outputs) == 2, "Pnet supports two output topologies"

    log.info('Reading Refine model {}'.format(args.model_rnet))
    r_net = ie.read_network(args.model_rnet)
    assert len(r_net.input_info.keys()) == 1, "Rnet supports only single input topologies"
    assert len(r_net.outputs) == 2, "Rnet supports two output topologies"

    log.info('Reading Output model {}'.format(args.model_onet))
    o_net = ie.read_network(args.model_onet)
    assert len(o_net.input_info.keys()) == 1, "Onet supports only single input topologies"
    assert len(o_net.outputs) == 3, "Onet supports three output topologies"

    pnet_input_blob = next(iter(p_net.input_info))
    rnet_input_blob = next(iter(r_net.input_info))
    onet_input_blob = next(iter(o_net.input_info))

    for name, blob in p_net.outputs.items():
        if blob.shape[1] == 2:
            pnet_cls_name = name
        elif blob.shape[1] == 4:
            pnet_roi_name = name
        else:
            raise RuntimeError("Unsupported output layer for Pnet")

    for name, blob in r_net.outputs.items():
        if blob.shape[1] == 2:
            rnet_cls_name = name
        elif blob.shape[1] == 4:
            rnet_roi_name = name
        else:
            raise RuntimeError("Unsupported output layer for Rnet")

    for name, blob in o_net.outputs.items():
        if blob.shape[1] == 2:
            onet_cls_name = name
        elif blob.shape[1] == 4:
            onet_roi_name = name
        elif blob.shape[1] == 10:
            onet_pts_name = name
        else:
            raise RuntimeError("Unsupported output layer for Onet")

    next_frame_id = 0

    metrics = PerformanceMetrics()
    presenter = None
    video_writer = cv2.VideoWriter()
    is_loaded_before = False

    while True:
        start_time = perf_counter()
        origin_image = cap.read()
        if origin_image is None:
            if next_frame_id == 0:
                raise ValueError("Can't read an image from the input")
            break
        if next_frame_id == 0:
            presenter = monitors.Presenter(args.utilization_monitors, 55,
                                           (round(origin_image.shape[1] / 4), round(origin_image.shape[0] / 8)))
            if args.output and not video_writer.open(args.output, cv2.VideoWriter_fourcc(*'MJPG'),
                                                     cap.fps(), (origin_image.shape[1], origin_image.shape[0])):
                raise RuntimeError("Can't open video writer")
        next_frame_id += 1

        rgb_image = cv2.cvtColor(origin_image, cv2.COLOR_BGR2RGB)
        oh, ow, _ = rgb_image.shape

        scales = utils.calculate_scales(rgb_image)

        # *************************************
        # Pnet stage
        # *************************************

        pnet_res = []
        for i, scale in enumerate(scales):
            hs = int(oh*scale)
            ws = int(ow*scale)
            image = preprocess_image(rgb_image, ws, hs)

            p_net.reshape({pnet_input_blob: [1, 3, ws, hs]})  # Change weidth and height of input blob
            exec_pnet = ie.load_network(network=p_net, device_name=args.device)
            if i == 0 and not is_loaded_before:
                log.info("The Proposal model {} is loaded to {}".format(args.model_pnet, args.device))

            p_res = exec_pnet.infer(inputs={pnet_input_blob: image})
            pnet_res.append(p_res)

        image_num = len(scales)
        rectangles = []
        for i in range(image_num):
            roi = pnet_res[i][pnet_roi_name]
            cls = pnet_res[i][pnet_cls_name]
            _, _, out_h, out_w = cls.shape
            out_side = max(out_h, out_w)
            rectangle = utils.detect_face_12net(cls[0][1], roi[0], out_side, 1/scales[i], ow, oh,
                                                score_threshold[0], iou_threshold[0])
            rectangles.extend(rectangle)
        rectangles = utils.NMS(rectangles, iou_threshold[1], 'iou')

        # Rnet stage
        if len(rectangles) > 0:

            r_net.reshape({rnet_input_blob: [len(rectangles), 3, 24, 24]})  # Change batch size of input blob
            exec_rnet = ie.load_network(network=r_net, device_name=args.device)
            if not is_loaded_before:
                log.info("The Refine model {} is loaded to {}".format(args.model_rnet, args.device))

            rnet_input = []
            for rectangle in rectangles:
                crop_img = rgb_image[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])]
                crop_img = preprocess_image(crop_img, 24, 24)
                rnet_input.extend(crop_img)

            rnet_res = exec_rnet.infer(inputs={rnet_input_blob: rnet_input})

            roi = rnet_res[rnet_roi_name]
            cls = rnet_res[rnet_cls_name]
            rectangles = utils.filter_face_24net(cls, roi, rectangles, ow, oh, score_threshold[1], iou_threshold[2])

        # Onet stage
        if len(rectangles) > 0:

            o_net.reshape({onet_input_blob: [len(rectangles), 3, 48, 48]})  # Change batch size of input blob
            exec_onet = ie.load_network(network=o_net, device_name=args.device)
            if not is_loaded_before:
                log.info("The Output model {} is loaded to {}".format(args.model_onet, args.device))
                is_loaded_before = True

            onet_input = []
            for rectangle in rectangles:
                crop_img = rgb_image[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])]
                crop_img = preprocess_image(crop_img, 48, 48)
                onet_input.extend(crop_img)

            onet_res = exec_onet.infer(inputs={onet_input_blob: onet_input})

            roi = onet_res[onet_roi_name]
            cls = onet_res[onet_cls_name]
            pts = onet_res[onet_pts_name]
            rectangles = utils.filter_face_48net(cls, roi, pts, rectangles, ow, oh,
                                                 score_threshold[2], iou_threshold[3])

        # display results
        for rectangle in rectangles:
            # Draw detected boxes
            cv2.putText(origin_image, 'confidence: {:.2f}'.format(rectangle[4]),
                        (int(rectangle[0]), int(rectangle[1])), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0))
            cv2.rectangle(origin_image, (int(rectangle[0]), int(rectangle[1])), (int(rectangle[2]), int(rectangle[3])),
                          (255, 0, 0), 1)
            # Draw landmarks
            for i in range(5, 15, 2):
                cv2.circle(origin_image, (int(rectangle[i+0]), int(rectangle[i+1])), 2, (0, 255, 0))

        metrics.update(start_time, origin_image)

        if video_writer.isOpened() and (args.output_limit <= 0 or next_frame_id <= args.output_limit):
            video_writer.write(origin_image)

        if not args.no_show:
            cv2.imshow('MTCNN Results', origin_image)
            key = cv2.waitKey(1)
            if key in {ord('q'), ord('Q'), 27}:
                break
            presenter.handleKey(key)

    metrics.log_total()