def init_model(xml, bins):
    model_xml = xml
    model_bin = bins
    # Plugin initialization for specified device and load extensions library if specified
    plugin = IEPlugin(device='CPU')
    plugin.add_cpu_extension(
        'utils/libcpu_extension_sse4.so')
    log.info("Reading IR...")
    net = IENetwork(model=model_xml, weights=model_bin)

    if plugin.device == "CPU":
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers]
        if len(not_supported_layers) != 0:
            log.error("Following layers are not supported by the plugin for specified device {}:\n {}".
                      format(plugin.device, ', '.join(not_supported_layers)))
            log.error("Please try to specify cpu extensions library path in demo's command line parameters using -l "
                      "or --cpu_extension command line argument")
            sys.exit(1)
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    log.info("Loading IR to the plugin...")
    exec_nets = plugin.load(network=net, num_requests=2)
    n, c, h, w = net.inputs[input_blob].shape
    del net
    return exec_nets, n, c, w, h, input_blob, out_blob, plugin
コード例 #2
0
def init_openvino():
    CNN_xml = CNN_ov_file_name
    CNN_bin = os.path.splitext(CNN_xml)[0] + ".bin"
    plugin = IEPlugin(device=device)
    if plugin.device == "CPU":
        for ext in cpu_extensions:
            plugin.add_cpu_extension(ext)
    CNN_net = IENetwork(model=CNN_xml, weights=CNN_bin)

    if plugin.device == "CPU":
        supported_layers = plugin.get_supported_layers(CNN_net)
        layers = CNN_net.layers.keys()
        not_supported_layers = [l for l in layers if l not in supported_layers]
        if len(not_supported_layers) != 0:
            print(
                "Following layers are not supported by the plugin for specified device {}:\n {}"
                .format(plugin.device, ', '.join(not_supported_layers)),
                file=sys.stderr)
            print(
                "Please try to specify cpu extensions library path in demo's command line parameters using -l "
                "or --cpu_extension command line argument",
                file=sys.stderr)
            sys.exit(1)
    input_CNN = next(iter(CNN_net.inputs))
    output_CNN = next(iter(CNN_net.outputs))
    exec_net = plugin.load(network=CNN_net, num_requests=1)
    del CNN_net
    return exec_net, input_CNN, output_CNN
コード例 #3
0
    def __init__(self, model, weights):
        self._model = model
        self._weights = weights

        IE_PLUGINS_PATH = os.getenv("IE_PLUGINS_PATH")
        if not IE_PLUGINS_PATH:
            raise OSError("Inference engine plugin path env not found in the system.")

        plugin = IEPlugin(device="CPU", plugin_dirs=[IE_PLUGINS_PATH])
        if (self._check_instruction("avx2")):
            plugin.add_cpu_extension(os.path.join(IE_PLUGINS_PATH, "libcpu_extension_avx2.so"))
        elif (self._check_instruction("sse4")):
            plugin.add_cpu_extension(os.path.join(IE_PLUGINS_PATH, "libcpu_extension_sse4.so"))
        else:
            raise Exception("Inference engine requires a support of avx2 or sse4.")

        network = IENetwork.from_ir(model=self._model, weights=self._weights)
        supported_layers = plugin.get_supported_layers(network)
        not_supported_layers = [l for l in network.layers.keys() if l not in supported_layers]
        if len(not_supported_layers) != 0:
            raise Exception("Following layers are not supported by the plugin for specified device {}:\n {}".
                      format(plugin.device, ", ".join(not_supported_layers)))

        self._input_blob_name = next(iter(network.inputs))
        self._output_blob_name = next(iter(network.outputs))

        self._net = plugin.load(network=network, num_requests=2)
        input_type = network.inputs[self._input_blob_name]
        self._input_layout = input_type if isinstance(input_type, list) else input_type.shape
コード例 #4
0
def load_ie_model(model_xml, device, plugin_dir, cpu_extension=''):
    """Loads a model in the Inference Engine format"""
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    # Plugin initialization for specified device and load extensions library if specified
    plugin = IEPlugin(device=device, plugin_dirs=plugin_dir)
    if cpu_extension and 'CPU' in device:
        plugin.add_cpu_extension(cpu_extension)
    # Read IR
    log.info("Loading network files:\n\t%s\n\t%s", model_xml, model_bin)
    net = IENetwork(model=model_xml, weights=model_bin)

    if "CPU" in plugin.device:
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers]
        if not_supported_layers:
            log.error("Following layers are not supported by the plugin for specified device %s:\n %s",
                      plugin.device, ', '.join(not_supported_layers))
            log.error("Please try to specify cpu extensions library path in sample's command line parameters using -l "
                      "or --cpu_extension command line argument")
            sys.exit(1)

    assert len(net.inputs.keys()) == 1, "Checker supports only single input topologies"
    assert len(net.outputs) == 1, "Checker supports only single output topologies"

    log.info("Preparing input blobs")
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    net.batch_size = 1

    # Loading model to the plugin
    log.info("Loading model to the plugin")
    exec_net = plugin.load(network=net)
    model = IEModel(exec_net, net.inputs, input_blob, out_blob)
    del net
    return model
コード例 #5
0
def load_ir_model(model_xml, device, plugin_dir, cpu_extension):
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    # initialize plugin
    log.info("Initializing plugin for %s device...", device)
    plugin = IEPlugin(device=device, plugin_dirs=plugin_dir)
    if cpu_extension and 'CPU' in device:
        plugin.add_cpu_extension(cpu_extension)

    # read IR
    net = IENetwork(model=model_xml, weights=model_bin)

    if "CPU" in device:
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [
            l for l in net.layers.keys() if l not in supported_layers
        ]
        if not_supported_layers:
            log.error(
                "Following layers are not supported by the plugin for specified device %s:\n %s",
                plugin.device, ', '.join(not_supported_layers))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using "
                "--cpu_extension command line argument")
            sys.exit(1)

    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    exec_net = plugin.load(network=net, num_requests=2)
    shape = net.inputs[input_blob].shape  # pylint: disable=E1136
    del net

    return exec_net, plugin, input_blob, out_blob, shape
コード例 #6
0
def load_model(feature,model_xml,device,plugin_dirs,input_key_length,output_key_length,cpu_extension):

    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    log.info("Initializing plugin for {} device...".format(device))
    plugin = IEPlugin(device, plugin_dirs)

    log.info("Loading network files for {}".format(feature))
    if cpu_extension and 'CPU' in device:
        plugin.add_cpu_extension(cpu_extension)
    else:
        plugin.set_config({"PERF_COUNT":"YES"})

    net = IENetwork(model=model_xml, weights=model_bin)

    if plugin.device == "CPU":
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers]
        if len(not_supported_layers) != 0:
            log.error("Following layers are not supported by the plugin for specified device {}:\n {}".
                      format(plugin.device, ', '.join(not_supported_layers)))
            log.error("Please try to specify cpu extensions library path in demo's command line parameters using -l "
                      "or --cpu_extension command line argument")
            sys.exit(1)
    
    log.info("Checking {} network inputs".format(feature))
    assert len(net.inputs.keys()) == input_key_length, "Demo supports only single input topologies"
    log.info("Checking {} network outputs".format(feature))
    assert len(net.outputs) == output_key_length, "Demo supports only single output topologies"
    return plugin,net
コード例 #7
0
ファイル: openvinotest.py プロジェクト: VladoDemcak/ovdebug
def main():
    log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    # Plugin initialization for specified device and load extensions library if specified
    plugin = IEPlugin(device=args.device, plugin_dirs=args.plugin_dir)
    if args.cpu_extension and 'CPU' in args.device:
        plugin.add_cpu_extension(args.cpu_extension)
    # Read IR
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)

    if plugin.device == "CPU":
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers]
        if len(not_supported_layers) != 0:
            log.error("Following layers are not supported by the plugin for specified device {}:\n {}".
                      format(plugin.device, ', '.join(not_supported_layers)))
            log.error("Please try to specify cpu extensions library path in sample's command line parameters using -l "
                      "or --cpu_extension command line argument")
            sys.exit(1)

    assert len(net.inputs.keys()) == 1, "Sample supports only single input topologies"
    assert len(net.outputs) == 1, "Sample supports only single output topologies"

    log.info("Preparing input blobs")
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))

    cap = cv2.VideoCapture(0) if args.input is None else cv2.VideoCapture(args.input)
    
    # Loading model to the plugin
    log.info("Loading model to the plugin")
    exec_net = plugin.load(network=net)

    n, c, h, w = net.inputs[input_blob].shape  

    i = 0
    while cap.isOpened():
        ret, img = cap.read()
        if img is None:
            break
        frame = cv2.resize(img, (w, h)).transpose((2, 0, 1)) if img.shape[:-1] != (h, w) else img
        log.info("#{} exec_net.infer starting...".format(i))
        res = exec_net.infer(inputs={input_blob: frame})[out_blob]
        log.info("#{} exec_net.infer finished, res: {}".format(i, res[0][0][0]))
        i += 1

        cv2.imshow("debug", img)
        cv2.waitKey(1)

    cv2.destroyAllWindows()
    cap.release()
コード例 #8
0
def test_get_supported_layers(device):
    with warnings.catch_warnings(record=True) as w:
        plugin = IEPlugin(device)
        net = IENetwork(model=test_net_xml, weights=test_net_bin)
        supported = plugin.get_supported_layers(net)
        layers = ['19/Fused_Add_', '21', '22', '23', '24/Fused_Add_', '26', '27', '29', 'data', 'fc_out']
        if device == "GPU":
            layers.remove("data")
        assert sorted(supported) == layers
    assert len(w) == 2
    assert "IEPlugin class is deprecated. " \
               "Please use IECore class instead." in str(w[0].message)
    assert "Reading network using constructor is deprecated. " \
            "Please, use IECore.read_network() method instead"  in str(w[1].message)
コード例 #9
0
def load_openvino_model(device, model_xml, model_bin):
  plugin = IEPlugin(device=device, plugin_dirs=None)
  net = IENetwork(model=model_xml, weights=model_bin)
  supported_layers = plugin.get_supported_layers(net)
  not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers]
  if len(not_supported_layers) != 0:
    print("Following layers are not supported by the plugin for specified device {}:\n {}".format(
        plugin.device, ', '.join(not_supported_layers)))
  else:
    print("All layers supported")
  exec_net = plugin.load(network=net)
  input_blob = next(iter(net.inputs))
  output_blob = next(iter(net.outputs))
  return plugin, exec_net, input_blob, output_blob
コード例 #10
0
def main():
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    # Plugin initialization for specified device and load extensions library if specified
    plugin = IEPlugin(device=args.device, plugin_dirs=args.plugin_dir)
    if args.cpu_extension and 'CPU' in args.device:
        plugin.add_cpu_extension(args.cpu_extension)
    # Read IR
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)

    if plugin.device == "CPU":
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [
            l for l in net.layers.keys() if l not in supported_layers
        ]
        if len(not_supported_layers) != 0:
            log.error(
                "Following layers are not supported by the plugin for specified device {}:\n {}"
                .format(plugin.device, ', '.join(not_supported_layers)))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)

    assert len(
        net.inputs.keys()) == 1, "Sample supports only single input topologies"
    assert len(
        net.outputs) == 1, "Sample supports only single output topologies"

    log.info("Preparing input blobs")
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    net.batch_size = 1

    print("input_shape is", net.inputs[input_blob].shape)

    # Loading model to the plugin
    log.info("Loading model to the plugin")
    exec_net = plugin.load(network=net)

    print("The test stop here")

    return
コード例 #11
0
    def load_model(self,
                   model,
                   device,
                   cpu_extension=DEFAULT_CPU_EXTENSION_LINUX):
        '''
        Load the model given IR files.
        Defaults to CPU as device for use in the workspace.
        Synchronous requests made within.
        '''
        ### Load the model ###
        model_xml = model
        model_bin = os.path.splitext(model_xml)[0] + ".bin"

        # Initialize the plugin
        if device:
            plugin = IEPlugin(device)
        else:
            plugin = IEPlugin(device="CPU")

        # Read the IR as a IENetwork
        self.network = IENetwork(model=model_xml, weights=model_bin)

        ### Check for supported layers ###
        supported_layers = plugin.get_supported_layers(self.network)
        needed_layers = self.network.layers.keys()
        unsupported_layers = set(needed_layers) - set(supported_layers)

        self.core = IECore()

        ### Add any necessary extensions ###
        if len(unsupported_layers) > 0:
            if cpu_extension and device and "CPU" in device:
                self.core.add_extension(cpu_extension, device)
            else:
                print(
                    "Error : there's unsupported layers in this model. Please use the -d argument and specify a supporting device"
                )
                print('Unsupported_layers: ', unsupported_layers)
                sys.exit(1)

        # Load the IENetwork into the plugin
        self.exec_network = self.core.load_network(self.network, device)

        # Get the input layer
        self.input_blob = next(iter(self.network.inputs))
        self.output_blob = next(iter(self.network.outputs))

        ### Return the loaded inference plugin ###
        return self.core
コード例 #12
0
    def ArrangeNetwork(self):
        # Read IR
        log.info("Reading IR...")
        model_xml = self.args.model
        model_bin = os.path.splitext(model_xml)[0] + ".bin"
        net = IENetwork(model=model_xml, weights=model_bin)

        # Plugin initialization for specified device and load extensions library if specified
        log.info("Initializing plugin for {} device...".format(self.args.device))
        plugin = IEPlugin(device=self.args.device,
                          plugin_dirs=self.args.plugin_dir)

        if self.args.cpu_extension and 'CPU' in self.args.device:
            plugin.add_cpu_extension(self.args.cpu_extension)

        if plugin.device == "CPU":
            supported_layers = plugin.get_supported_layers(net)
            not_supported_layers = [
                l for l in net.layers.keys() if l not in supported_layers]
            if len(not_supported_layers) != 0:
                log.error("Following layers are not supported by the plugin for specified device {}:\n {}".format(
                    plugin.device, ', '.join(not_supported_layers)))
                log.error(
                    "Please try to specify cpu extensions library path in sample's command line parameters using -l or --cpu_extension command line argument")
                sys.exit(1)

        self.execNet = plugin.load(network=net)

        # Documentation for FasterRCNN
        # net.inputs:
        # {'image_info': <openvino.inference_engine.ie_api.InputInfo object at 0x7528af80>, 'image_tensor': <openvino.inference_engine.ie_api.InputInfo object at 0x72fb9020>}

        # net.inputs['image_tensor'].shape
        # NCHW [1, 3, 600, 600]

        # net.inputs['image_info'].shape
        # creates input to store input image height, width and scales (usually 1.0s)
        # [1, 3] ~ [h, w, 1]

        # net.outputs:
        # {'detection_output': <openvino.inference_engine.ie_api.OutputInfo object at 0x7522b7f0>}

        # net.outputs['detection_output'].shape
        # [1, 1, 500, 7]

        # NCHW [1, 3, 600, 600]
        self.inputShape = net.inputs['image_tensor'].shape
        self.inputImageInfo = np.asarray(
            [[self.inputShape[2], self.inputShape[3], 1]], dtype=np.float32)  # [1, 3] ~ [h, w, 1]
コード例 #13
0
    def initialise_inference(self):

        model_xml = self.model
        model_bin = os.path.splitext(model_xml)[0] + ".bin"

        # Plugin initialization for specified device and load extensions library if specified
        log.info("Initializing plugin for {} device...".format(self.device))
        plugin = IEPlugin(device=self.device, plugin_dirs=self.plugin_dir)
        if self.cpu_extension and 'CPU' in self.device:
            log.info("Loading plugins for {} device...".format(self.device))
            plugin.add_cpu_extension(self.cpu_extension)

        # Read IR
        log.info("Reading IR...")
        net = IENetwork(model=model_xml, weights=model_bin)

        if plugin.device == "CPU":
            supported_layers = plugin.get_supported_layers(net)
            not_supported_layers = [
                l for l in net.layers.keys() if l not in supported_layers
            ]
            if len(not_supported_layers) != 0:
                log.error(
                    "Following layers are not supported by the plugin for specified device {}:\n {}"
                    .format(plugin.device, ', '.join(not_supported_layers)))
                log.error(
                    "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                    "or --cpu_extension command line argument")
                sys.exit(1)

        assert len(net.inputs.keys()
                   ) == 1, "Sample supports only single input topologies"
        assert len(
            net.outputs) == 1, "Sample supports only single output topologies"

        input_blob = next(iter(net.inputs))
        out_blob = next(iter(net.outputs))
        log.info("Loading IR to the plugin...")
        exec_net = plugin.load(network=net, num_requests=2)

        if isinstance(net.inputs[input_blob], list):
            n, c, h, w = net.inputs[input_blob]
        else:
            n, c, h, w = net.inputs[input_blob].shape
        del net
        processor = Processor(exec_net, input_blob, out_blob, n, c, h, w)
        return processor
コード例 #14
0
def inference(args, model_xml, model_bin, inputs, outputs):
    from openvino.inference_engine import IENetwork
    from openvino.inference_engine import IEPlugin
    plugin = IEPlugin(device=args.device, plugin_dirs=args.plugin_dir)
    if args.cpu_extension and 'CPU' in args.device:
        plugin.add_cpu_extension(args.cpu_extension)
    log.info('Loading network files:\n\t{}\n\t{}'.format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)

    if plugin.device == 'CPU':
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [
            l for l in net.layers.keys() if l not in supported_layers
        ]
        if not_supported_layers:
            log.error('Folowing layers are not supported by the plugin for '
                      'specified device {}:\n {}'.format(
                          plugin.device, ', '.join(not_supported_layers)))
            log.error('Please try to specify cpu extensions library path in '
                      'sample\'s command line parameters using '
                      '--cpu-extension command line argument')
            sys.exis(1)

    assert len(net.inputs) == len(inputs)
    ie_inputs = {}
    for item in inputs:
        assert item[0] in set(net.inputs.keys())
        ie_inputs[item[0]] = item[1]

    log.info('Loading model to the plugin')
    exec_net = plugin.load(network=net)

    res = exec_net.infer(inputs=ie_inputs)

    assert len(res) == len(outputs)
    for name, output in outputs:
        assert name in res
        actual_output = res[name]
        np.testing.assert_allclose(output, actual_output, rtol=1e-3, atol=1e-4)
        log.info('{}: OK'.format(name))
    log.info('ALL OK')

    def compute():
        exec_net.infer(inputs=ie_inputs)

    return run_onnx_util.run_benchmark(compute, args.iterations)
コード例 #15
0
def prepare_model(log, model, weights, cpu_extension, device_list, plugin_dir,
                  thread_num, stream_num):
    model_xml = model
    model_bin = weights
    if len(device_list) == 1:
        device = device_list[0]
    elif len(device_list) == 2:
        device = 'HETERO:{},{}'.format(device_list[0], device_list[1])
    else:
        log.error('Wrong count devices')
        sys.exit(1)
    log.info('Plugin initialization.');
    plugin = IEPlugin(device = device, plugin_dirs = plugin_dir)
    if cpu_extension and 'CPU' in device:
        plugin.add_cpu_extension(cpu_extension)
    log.info('Loading network files:\n\t {0}\n\t {1}'.format(
        model_xml, model_bin))
    net = IENetwork(model = model_xml, weights = model_bin)
    if plugin.device == 'CPU':
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [ l for l in net.layers.keys() \
            if l not in supported_layers ]
        if len(not_supported_layers) != 0:
            log.error('Following layers are not supported by the plugin \
                for specified device {0}:\n {1}'.format(plugin.device,
                ', '.join(not_supported_layers)))
            log.error('Please try to specify cpu extensions library path in \
                sample\'s command line parameters using -l or --cpu_extension \
                command line argument')
            sys.exit(1)
    if thread_num is not None:
        if 'CPU' in device_list:
            plugin.set_config({'CPU_THREADS_NUM': str(thread_num)})
        else:
            log.error('Parameter : Number of threads is used only for CPU')
            sys.exit(1)
    if stream_num is not None:
        if 'CPU' in device_list:
            plugin.set_config({'CPU_THROUGHPUT_STREAMS': str(stream_num)})
        else:
            log.error('Parameter : Number of streams is used only for CPU')
            sys.exit(1)
    if len(device_list) == 2:
        plugin.set_config({'TARGET_FALLBACK': device})
        plugin.set_initial_affinity(net)
    return net, plugin
コード例 #16
0
def init_model(model, device, plugin_dir, cpu_extension):
    model_xml = model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    # ------------- 1. Plugin initialization for specified device and load extensions library if specified -------------
    plugin = IEPlugin(device=device, plugin_dirs=plugin_dir)
    if cpu_extension and 'CPU' in device:
        plugin.add_cpu_extension(cpu_extension)

    # -------------------- 2. Reading the IR generated by the Model Optimizer (.xml and .bin files) --------------------
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)

    # ---------------------------------- 3. Load CPU extension for support specific layer ------------------------------
    if plugin.device == "CPU":
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [
            l for l in net.layers.keys() if l not in supported_layers
        ]
        if len(not_supported_layers) != 0:
            log.error(
                "Following layers are not supported by the plugin for specified device {}:\n {}"
                .format(plugin.device, ', '.join(not_supported_layers)))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)

    assert len(net.inputs.keys(
    )) == 1, "Sample supports only YOLO V2 based single input topologies"
    assert len(
        net.outputs
    ) == 1, "Sample supports only YOLO V2 based single output topologies"

    # ---------------------------------------------- 4. Preparing inputs -----------------------------------------------
    log.info("Preparing inputs")

    #  Defaulf batch_size is 1
    net.batch_size = 1

    # ----------------------------------------- 5. Loading model to the plugin -----------------------------------------
    log.info("Loading model to the plugin")
    exec_net = plugin.load(network=net, num_requests=2)

    return net, exec_net
コード例 #17
0
def load_model(
    device,
    labels,
    model_xml,
    model_bin,
    plugin_dir,
    cpu_extension='/opt/intel/openvino/deployment_tools/inference_engine/lib/intel64/libcpu_extension_sse4.so'
):
    # ------------- 1. Plugin initialization for specified device and load extensions library if specified ----------
    plugin = IEPlugin(device=device, plugin_dirs=plugin_dir)
    if 'CPU' in device:
        plugin.add_cpu_extension(cpu_extension)

    # -------------------- 2. Reading the IR generated by the Model Optimizer (.xml and .bin files) ----------------
    net = IENetwork(model=model_xml, weights=model_bin)

    # ---------------------------------- 3. Load CPU extension for support specific layer --------------------------
    if plugin.device == "CPU":
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [
            l for l in net.layers.keys() if l not in supported_layers
        ]
        if len(not_supported_layers) != 0:
            print(
                "Following layers are not supported by the plugin for specified device {}:\n {}"
                .format(plugin.device, ', '.join(not_supported_layers)))
            print(
                "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)

    assert len(net.inputs.keys(
    )) == 1, "Sample supports only YOLO V3 based single input topologies"
    assert len(
        net.outputs
    ) == 3, "Sample supports only YOLO V3 based triple output topologies"

    # ---------------------------------------------- 4. Preparing inputs -------------------------------------------

    #  Defaulf batch_size is 1
    net.batch_size = 1

    # Read and pre-process input images

    return net, plugin.load(network=net, num_requests=2)
コード例 #18
0
 def __init__(self):
     self.symbols = "0123456789abcdefghijklmnopqrstuvwxyz#"
     plugin = IEPlugin(device='CPU')
     net = IENetwork(model=config.PATH_TEXT_RECOGNITION_MODEL_XML, weights=config.PATH_TEXT_RECOGNITION_MODEL_BIN)
     plugin.add_cpu_extension(config.PATH_TO_INFERENCE_ENGINE)
     
     supported_layers = plugin.get_supported_layers(net)
     not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers]
     if len(not_supported_layers) != 0:
         print('thing')
     
     print("Preparing input blobs")
     self.input_blob = next(iter(net.inputs))
     self.out_blob = next(iter(net.outputs))
     
     print("Loading model to the plugin")
     self.exec_net = plugin.load(network=net)
     del net
コード例 #19
0
ファイル: infer.py プロジェクト: Ujjwal-9/ihack
def main(args=None):
    args = parse_args(args)

    model_xml = args.xml
    model_bin = args.bin
    img_fn = args.img

    plugin = IEPlugin(device="CPU", plugin_dirs=args.plugin)
    plugin.add_cpu_extension(args.cpu_ext)
    net = IENetwork(model=model_xml, weights=model_bin)

    supported_layers = plugin.get_supported_layers(net)
    not_supported_layers = [
        l for l in net.layers.keys() if l not in supported_layers
    ]
    print('not supported layers:', not_supported_layers)

    input_blob = 'input_1'
    cl_out_blob = 'classification/concat'
    rg_out_blob = 'regression/concat'

    print('out:', len(net.outputs))
    print('outputs', net.outputs)

    net.batch_size = 1
    n, c, h, w = net.inputs[input_blob].shape
    print(n, c, h, w)

    print("Loading model to the plugin")
    exec_net = plugin.load(network=net)
    del net

    # load images
    image = cv2.imread(img_fn)
    if image.shape[:-1] != (h, w):
        print("Image {} is resized from {} to {}".format(
            img_fn, image.shape[:-1], (h, w)))
        image = cv2.resize(image, (w, h))

    image = image.transpose((2, 0, 1))  # Change data layout from HWC to CHW
    image = np.expand_dims(image, axis=0)

    res = exec_net.infer(inputs={input_blob: image})
    print(res)
コード例 #20
0
    def __init__(self):
        net = cv2.dnn.readNet(config.PATH_TEXT_DETECTION_MODEL_XML, config.PATH_TEXT_DETECTION_MODEL_BIN)

        plugin = IEPlugin(device='CPU')
        net = IENetwork(model=config.PATH_TEXT_DETECTION_MODEL_XML, weights=config.PATH_TEXT_DETECTION_MODEL_BIN)
        plugin.add_cpu_extension(config.PATH_TO_INFERENCE_ENGINE)
        
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers]
        if len(not_supported_layers) != 0:
            print('thing')
        
        print("Preparing input blobs")
        self.input_blob = next(iter(net.inputs))
        self.out_blob = next(iter(net.outputs))
        
        print("Loading model to the plugin")
        self.td = plugin.load(network=net)
        del net
コード例 #21
0
def load_ir_model(model_xml, device, plugin_dir, cpu_extension):
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    # initialize plugin
    log.info("Initializing plugin for %s device...", device)
    plugin = IEPlugin(device=device, plugin_dirs=plugin_dir)
    if cpu_extension and 'CPU' in device:
        plugin.add_cpu_extension(cpu_extension)

    # read IR
    print("model_bin: ", model_bin)
    log.info("Reading IR...")
    net = IENetwork(model=model_xml, weights=model_bin)

    if "CPU" in device:
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [
            l for l in net.layers.keys() if l not in supported_layers
        ]
        if not_supported_layers:
            log.error(
                "Following layers are not supported by the plugin for specified device %s:\n %s",
                device, ', '.join(not_supported_layers))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using "
                "--cpu_extension command line argument")
            sys.exit(1)

    # input / output check
    print("len(net.inputs.keys()):  ", len(net.inputs.keys()))
    assert len(net.inputs.keys()) == 1, "Eyenet must have only single input"
    assert len(
        net.outputs) == 1, "Eyenet must have only single output topologies"

    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    log.info("Loading IR to the plugin...")
    exec_net = plugin.load(network=net)
    shape = net.inputs[input_blob].shape  # pylint: disable=E1136
    del net

    return exec_net, plugin, input_blob, out_blob, shape
コード例 #22
0
def initiateMYRIAD():

    logging.basicConfig(format="[ %(levelname)s ] %(message)s",
                        level=logging.INFO,
                        stream=sys.stdout)
    log = logging.getLogger()

    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    device = 'MYRIAD'  #CPU, GPU, FPGA or MYRIAD
    plugin_dir = None

    # ------------- 1. Plugin initialization for specified device and load extensions library if specified -------------
    plugin = IEPlugin(device=device, plugin_dirs=plugin_dir)
    # if args.cpu_extension and 'CPU' in args.device:
    #     plugin.add_cpu_extension(args.cpu_extension)

    # -------------------- 2. Reading the IR generated by the Model Optimizer (.xml and .bin files) --------------------
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)

    # ---------------------------------- 3. Load CPU extension for support specific layer ------------------------------
    if plugin.device == "CPU":
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [
            l for l in net.layers.keys() if l not in supported_layers
        ]
        if len(not_supported_layers) != 0:
            log.error(
                "Following layers are not supported by the plugin for specified device {}:\n {}"
                .format(plugin.device, ', '.join(not_supported_layers)))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)

    # assert len(net.inputs.keys()) == 1, "Sample supports only YOLO V3 based single input topologies"
    # assert len(net.outputs) == 3, "Sample supports only YOLO V3 based triple output topologies"
    # exec_net = plugin.load(network=net, num_requests=2)
    # plugin = IEPlugin(device=args.device, plugin_dirs=None)
    return net, plugin
コード例 #23
0
ファイル: main.py プロジェクト: YukariSonz/EdgeDeepSpeech
def main():
    log.basicConfig(format=" [ %(levelname)s] %(message)s", level=log.INFO, stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model #File path for the model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    print(model_xml)
    print(model_bin)

    plugin = IEPlugin(device = args.device, plugin_dirs = args.plugin_dir)
    print(plugin.device)
    if args.cpu_extension and 'CPU' in args.device:
        plugin.add_cpu_extension(args.cpu_extension)

    net = IENetwork(model = model_xml, weights = model_bin)
    if plugin.device == "CPU":
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers]
        if len(not_supported_layers) !=0:
            log.error("Some layers are not supported by the plugin")
            sys.exit(1)

    #assert len(net.inputs.keys()) == 2 # 2 inputs for DeepSpeech
    #assert len(net.outputs) == 2048 # [464,2048,2048]
    print(net.inputs.keys())
    print(net.inputs['input_node'].shape)
    print(net.outputs)
    print(net.outputs['Softmax'].shape) # Output is a softmax function


    voice = sound_decode(args.input)




    exec_net = plugin.load(network = net, num_requests = 1)
    print(exec_net)
    #exec_net.requests[0].inputs['data'][:] = voice
    res = exec_net.infer({'data': voice})
    print("haha")
コード例 #24
0
def main():
    # line for log configuration
    log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout)
    # parser for the arguments
    args = build_argparser().parse_args()
    # get xml model argument
    model_xml = args.model
    model2_xml = "C:\Intel\computer_vision_sdk_2018.4.420\deployment_tools\intel_models\emotions-recognition-retail-0003\FP32\emotions-recognition-retail-0003.xml"
    #model2_xml = "C:\Intel\computer_vision_sdk_2018.4.420\deployment_tools\intel_models\head-pose-estimation-adas-0001\FP32\head-pose-estimation-adas-0001.xml"
    #model2_xml = "C:\Intel\computer_vision_sdk_2018.4.420\deployment_tools\intel_models/age-gender-recognition-retail-0013\FP32/age-gender-recognition-retail-0013.xml"
    #model2_xml = "C:\Intel\computer_vision_sdk_2018.4.420\deployment_tools\intel_models/facial-landmarks-35-adas-0001\FP32/facial-landmarks-35-adas-0001.xml"
    # get weight model argument
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    model2_bin = os.path.splitext(model2_xml)[0] + ".bin"
    # Hardware plugin initialization for specified device and
    log.info("Initializing plugin for {} device...".format(args.device))
    plugin = IEPlugin(device=args.device, plugin_dirs=args.plugin_dir)
    # load extensions library if specified
    if args.cpu_extension and 'CPU' in args.device:
        plugin.add_cpu_extension(args.cpu_extension)
    # Read intermediate representation of the model
    log.info("Reading IR...")
    net = IENetwork.from_ir(model=model_xml, weights=model_bin)
    net2 = IENetwork.from_ir(model=model2_xml, weights=model2_bin)
    # check if the model is supported
    if plugin.device == "CPU":
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers]
        if len(not_supported_layers) != 0:
            log.error("Following layers are not supported by the plugin for specified device {}:\n {}".
                      format(plugin.device, ', '.join(not_supported_layers)))
            log.error("Please try to specify cpu extensions library path in demo's command line parameters using -l "
                      "or --cpu_extension command line argument")
            sys.exit(1)
    # check if the input and output of model is the right format, here we expect just one input (one image) and one output type (bounding boxes)
    assert len(net.inputs.keys()) == 1, "Demo supports only single input topologies"
    assert len(net.outputs) <= 3, "Demo supports only single output topologies"
    # start the iterator on the input nodes
    input_blob = next(iter(net.inputs))
    input_blob2 = next(iter(net2.inputs))
コード例 #25
0
 def __init__(self, model_name, device, fp="FP32", debug=False, ncs=1):
     OpvExec.__init__(self, ncs)
     assert(fp in ('FP16', 'FP32'))
     self.name = model_name
     self._debug = debug
     if (self._HasValidMachine()):
         self.ClearMachine()
         if (self._debug == True):
             print("Loaded Machine Released")
     
     #Load the .xml and .bin files for the model (and .labels if it exists)
     model_xml = "models/"+model_name+"/"+fp+"/"+model_name+".xml"
     model_bin = os.path.splitext(model_xml)[0] + ".bin"
     model_labels = os.path.splitext(model_xml)[0] + ".labels"
     if (os.path.exists(model_labels)):
         self.labels = np.loadtxt(model_labels, dtype="str", delimiter="\n")
     net = IENetwork(model=model_xml, weights=model_bin)
     net.batch_size = 1
     
     #Initialize the hardware device (e.g. CPU / NCS2)
     plugin = IEPlugin(device=device)
     if plugin.device == "CPU":
         supported_layers = plugin.get_supported_layers(net)
         not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers]
         if len(not_supported_layers) != 0:
             print("[ERROR] These layers are not supported by the plugin for specified device {}:\n {}".
                       format(plugin.device, ', '.join(not_supported_layers)))
             del net
             del plugin
             assert(len(not_supported_layers)==0)
     
     self.input_layers = [*net.inputs] #TODO: make these two into just a single dict
     self.input_shapes = {layer: net.inputs[layer].shape for layer in net.inputs}
     self.output_layers = [*net.outputs]
     
     self._SetMachine(plugin.load(network=net))
     del net
     del plugin
     if (self._debug):
         print("[INFO] Model " + model_name + " Loaded and Ready on NCS device "+str(ncs))
コード例 #26
0
    def __init__(self):
        self._model = model_xml
        self._weights = model_bin
        self._labels_map = {}
        
        # Load labels
        with open(labels_path, 'r') as f:
            for line in f:
                (key, val) = line.split(':')
                self._labels_map[int(key)] = val.rstrip().lower()

        # Load CPU plugin?
        plugin = IEPlugin(device=device, plugin_dirs=lib_dir)
        if device == 'CPU':
            plugin.add_cpu_extension(cpu_lib)
        
        network = IENetwork(model=self._model, weights=self._weights)

        supported_layers = plugin.get_supported_layers(network)
        not_supported_layers = [l for l in network.layers.keys() if l not in supported_layers]
        if len(not_supported_layers) != 0:
            raise Exception("Following layers are not supported by the plugin for specified device {}:\n {}".
                      format(plugin.device, ", ".join(not_supported_layers)))

        iter_inputs = iter(network.inputs)
        self._input_blob_name = next(iter_inputs)
        self._output_blob_name = next(iter(network.outputs))

        self._require_image_info = False

        # NOTE: handeling for the inclusion of `image_info` in OpenVino2019
        if 'image_info' in network.inputs:
            self._require_image_info = True
        if self._input_blob_name == 'image_info':
            self._input_blob_name = next(iter_inputs)

        self._net = plugin.load(network=network, num_requests=2)
        input_type = network.inputs[self._input_blob_name]
        self._input_layout = input_type if isinstance(input_type, list) else input_type.shape
コード例 #27
0
def load_xml_bin(model_path):
    model_xml = model_path
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    # Plugin initialization for specified device and load extensions library if specified
    plugin = IEPlugin(device='CPU', plugin_dirs=None)
    plugin.add_cpu_extension('lib/libcpu_extension_sse4.so')
    # Read IR
    net = IENetwork(model=model_xml, weights=model_bin)

    if plugin.device == "CPU":
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers]
        if len(not_supported_layers) != 0:
            sys.exit(1)

    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    exec_net = plugin.load(network=net, num_requests=2)
    # Read and pre-process input image
    n, c, h, w = net.inputs[input_blob].shape
    del net
    return n, c, h, w, exec_net, input_blob, out_blob
コード例 #28
0
def main():
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    # Plugin initialization for specified device and load extensions library if specified
    log.info("Initializing plugin for {} device...".format(args.device))
    plugin = IEPlugin(device=args.device, plugin_dirs=args.plugin_dir)
    if args.cpu_extension and 'CPU' in args.device:
        plugin.add_cpu_extension(args.cpu_extension)

    # Read IR
    log.info("Reading IR...")
    net = IENetwork.from_ir(model=model_xml, weights=model_bin)

    if "CPU" in plugin.device:
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [
            l for l in net.layers.keys() if l not in supported_layers
        ]
        if len(not_supported_layers) != 0:
            log.error(
                "Following layers are not supported by the plugin for specified device {}:\n {}"
                .format(plugin.device, ', '.join(not_supported_layers)))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)
    assert len(
        net.inputs.keys()) == 1, "Sample supports only single input topologies"
    assert len(
        net.outputs) == 1, "Sample supports only single output topologies"
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    log.info("Loading IR to the plugin...")
    exec_net = plugin.load(network=net, num_requests=2)
    # Read and pre-process input image
    n, c, h, w = net.inputs[input_blob]
    del net

    predictions = []
    data = Input(args.input_type, args.input)
    cur_request_id = 0

    fps = 25
    out_width = 640
    out_height = 480
    if args.dump_output_video:
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(args.path_to_output_video, fourcc, fps,
                              (int(out_width), int(out_height)))

    while not data.is_finished():
        frame, img_id = data.get_next_item()
        initial_h, initial_w, channels = frame.shape
        in_frame = cv2.resize(frame, (w, h))
        in_frame = in_frame.transpose(
            (2, 0, 1))  # Change data layout from HWC to CHW
        in_frame = in_frame.reshape((n, c, h, w))

        exec_net.start_async(request_id=cur_request_id,
                             inputs={input_blob: in_frame})
        if exec_net.requests[cur_request_id].wait(-1) == 0:

            # Parse detection results of the current request
            res = exec_net.requests[cur_request_id].outputs[out_blob]
            coco_detections = []
            for obj in res[0][0]:
                # Draw only objects when probability more than specified threshold
                if obj[2] > args.prob_threshold:
                    x1 = float(obj[3] * initial_w)
                    y1 = float(obj[4] * initial_h)
                    x2 = float(obj[5] * initial_w)
                    y2 = float(obj[6] * initial_h)

                    x_, y_ = round(x1, 1), round(y1, 1)
                    w_ = round(x2 - x1, 1)
                    h_ = round(y2 - y1, 1)
                    class_id = int(obj[1])

                    coco_det = {}
                    coco_det['image_id'] = img_id
                    coco_det['category_id'] = class_id
                    coco_det['bbox'] = [x_, y_, w_, h_]
                    coco_det['score'] = round(float(obj[2]), 1)
                    coco_detections.append(coco_det)

                    # Draw box and label\class_id
                    cv2.rectangle(frame, (int(x1), int(y1)),
                                  (int(x2), int(y2)), (255, 0, 0), 2)
                    cv2.putText(
                        frame,
                        str(class_id) + ' ' + str(round(obj[2] * 100, 1)) +
                        ' %', (int(x1), int(y1) - 7), cv2.FONT_HERSHEY_SIMPLEX,
                        0.5, (0, 0, 255), 2)
            predictions.extend(coco_detections)

        if args.dump_output_video:
            img_resized = cv2.resize(frame, (out_width, out_height))
            out.write(img_resized)
        if args.show:
            cv2.imshow("Detection Results", frame)
            key = cv2.waitKey(10)
            if key == 27:
                break

    if args.dump_predictions_to_json:
        with open(args.output_json_path, 'w') as output_file:
            json.dump(predictions, output_file, sort_keys=True, indent=4)

    cv2.destroyAllWindows()
    del exec_net
    del plugin
コード例 #29
0
class GazeEstimation:
    '''
    Class for the Gaze Estimation Model.
    '''
    def __init__(self,
                 model_name,
                 device='CPU',
                 extensions=None,
                 async_mode=False):
        '''
        TODO: Use this to set your instance variables.
        '''
        self.model = model_name
        self.device = device
        self.extensions = extensions
        self.bin = model_name + 'bin'
        self.xml = model_name + 'xml'
        self.async_mode = async_mode

    def load_model(self):
        '''
        TODO: You will need to complete this method.
        This method is for loading the model to the device specified by the user.
        If your model requires any Plugins, this is where you can load them.
        '''
        self.plugin = IEPlugin(device=self.device)
        self.model = IENetwork(model=self.xml, weights=self.bin)
        t0 = time.time()
        self.net = self.plugin.load(self.model)
        t1 = time.time()
        print('Gaze Estimation Load time = ', (t1 - t0))

        self.input_name = [item for item in self.model.inputs.keys()]
        self.pose_shape = self.model.inputs[self.input_name[0]].shape
        self.eye_shape = self.model.inputs[self.input_name[1]].shape
        self.output_name = [item for item in self.model.outputs.keys()]
        #print('self.output_name = ', self.output_name)

        self.check_model()

    def predict(self, pose, eyes):
        '''
        TODO: You will need to complete this method.
        This method is meant for running predictions on the input image.
        '''

        # The left and right eye images both have the same dimension
        self.eye_height = eyes[0].shape[0]
        self.eye_width = eyes[0].shape[1]

        lefte, righte = self.preprocess_input(eyes)

        input_dict = {
            self.input_name[0]: pose,
            self.input_name[1]: lefte,
            self.input_name[2]: righte
        }

        if self.async_mode == True:
            self.net.requests[0].async_mode_infer(input_dict)
        else:
            self.net.requests[0].infer(input_dict)

        if self.net.requests[0].wait(-1) == 0:
            result = self.net.requests[0].outputs

        mouse = self.preprocess_output(result[self.output_name[0]], pose)

        #print('Gaze Inference Time = ', (t1-t0))
        return mouse

    def preprocess_input(self, image_list):
        '''
        Before feeding the data into the model for inference,
        you might have to preprocess it. This function is where you can do that.
        '''

        (n, c, h, w) = self.model.inputs[self.input_name[1]].shape
        #print(image_list[0].shape)
        try:
            pframe_le = cv2.resize(image_list[0], (w, h))
            pframe_le = pframe_le.transpose((2, 0, 1))
            pframe_le = pframe_le.reshape((n, c, h, w))

            pframe_re = cv2.resize(image_list[1], (w, h))
            pframe_re = pframe_re.transpose((2, 0, 1))
            pframe_re = pframe_re.reshape((n, c, h, w))
        except:
            print('Head angle too extreme for model to pick up eyes / gaze')
            print('Try Again')
            exit(1)

        return pframe_le, pframe_re

    def preprocess_output(self, outputs, pose):
        '''
        Before feeding the output of this model to the next model,
        you might have to preprocess the output. This function is where you can do that.
        '''

        roll = pose[2]
        #outputs = outputs / np.linalg.norm(outputs)
        cos = math.cos(roll * math.pi / 180)
        sin = math.sin(roll * math.pi / 180)

        x = outputs[0][0] * cos + outputs[0][1] * sin
        y = -outputs[0][0] * sin + outputs[0][1] * cos

        return [x, y]

    def check_model(self):
        supported_layers = self.plugin.get_supported_layers(self.model)
        unsupported_layers = [
            l for l in self.model.layers.keys() if l not in supported_layers
        ]

        if len(unsupported_layers) != 0:
            print('Unsupported layers found: {}'.format(unsupported_layers))
            if self.extensions != None and self.device == 'CPU':
                print('Attempting to add extension...')
                try:
                    self.plugin.add_cpu_extension(self.extensions, self.device)
                except:
                    print('Error while adding extension')
                    exit(1)
コード例 #30
0
def main():
    infer_time = []
    log.basicConfig(format="[ %(levelname)s ] %(message)s",
                    level=log.INFO,
                    stream=sys.stdout)
    args = build_argparser().parse_args()
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"

    # Plugin initialization for specified device and load extensions library if specified
    plugin = IEPlugin(device=args.device, plugin_dirs=args.plugin_dir)
    if args.cpu_extension and 'CPU' in args.device:
        plugin.add_cpu_extension(args.cpu_extension)

    # Read IR
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork.from_ir(model=model_xml, weights=model_bin)

    if "CPU" in plugin.device:
        supported_layers = plugin.get_supported_layers(net)
        not_supported_layers = [
            l for l in net.layers.keys() if l not in supported_layers
        ]
        if len(not_supported_layers) != 0:
            log.error(
                "Following layers are not supported by the plugin for specified device {}:\n {}"
                .format(plugin.device, ', '.join(not_supported_layers)))
            log.error(
                "Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
            sys.exit(1)

    assert len(
        net.inputs.keys()) == 1, "Sample supports only single input topologies"
    assert len(
        net.outputs) == 1, "Sample supports only single output topologies"

    log.info("Preparing input blobs")
    #    input_blob = next(iter(net.inputs))
    #    out_blob = next(iter(net.outputs))
    input_blob = net.inputs
    out_blob = net.outputs
    net.batch_size = 1

    # Read and pre-process input images
    n, c, h, w = net.inputs['input.1'].shape
    images = np.ndarray(shape=(n, c, h, w))
    image = cv2.imread(args.input)
    if image.shape[:-1] != (h, w):
        log.warning("Image {} is resized from {} to {}".format(
            args.input, image.shape[:-1], (h, w)))
        image = cv2.resize(image, (w, h))
    image = image.transpose((2, 0, 1))  # Change data layout from HWC to CHW
    images[0] = image
    log.info("Batch size is {}".format(n))

    # Loading model to the plugin
    log.info("Loading model to the plugin")
    exec_net = plugin.load(network=net)
    del net

    # Start sync inference
    log.info("Starting inference ({} iterations)".format(args.number_iter))
    for i in range(args.number_iter):
        t0 = time()
        #res = exec_net.infer(inputs={input_blob: images})
        res = exec_net.infer({'input.1': images[0]})
        infer_time.append((time() - t0) * 1000)
    log.info("Average running time of one iteration: {} ms".format(
        np.average(np.asarray(infer_time))))
    if args.perf_counts:
        perf_counts = exec_net.requests[0].get_perf_counts()
        log.info("Performance counters:")
        print("{:<70} {:<15} {:<15} {:<15} {:<10}".format(
            'name', 'layer_type', 'exet_type', 'status', 'real_time, us'))
        for layer, stats in perf_counts.items():
            print("{:<70} {:<15} {:<15} {:<15} {:<10}".format(
                layer, stats['layer_type'], stats['exec_type'],
                stats['status'], stats['real_time']))

    #show img in bgr order
    r = res['254'][0, 0]
    g = res['254'][0, 1]
    b = res['254'][0, 2]
    img_bgr = cv2.merge([b, g, r])
    img = cv2.normalize(img_bgr,
                        None,
                        alpha=0,
                        beta=255,
                        norm_type=cv2.NORM_MINMAX,
                        dtype=cv2.CV_32F)
    img.astype(np.uint8)
    cv2.imwrite('result.jpg', img)
コード例 #31
0
def main():
  log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout)
  args = build_argparser().parse_args()
  model_xml = args.model
  model_bin = os.path.splitext(model_xml)[0] + ".bin"
  # Plugin initialization for specified device and load extensions library if specified
  log.info("Initializing plugin for {} device...".format(args.device))
  plugin = IEPlugin(device=args.device, plugin_dirs=args.plugin_dir)
  if args.cpu_extension and 'CPU' in args.device:
    plugin.add_cpu_extension(args.cpu_extension)

  # Read IR
  log.info("Reading IR...")
  net = IENetwork.from_ir(model=model_xml, weights=model_bin)

  if "CPU" in plugin.device:
    supported_layers = plugin.get_supported_layers(net)
    not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers]
    if len(not_supported_layers) != 0:
      log.error("Following layers are not supported by the plugin for specified device {}:\n {}".
                format(plugin.device, ', '.join(not_supported_layers)))
      log.error("Please try to specify cpu extensions library path in sample's command line parameters using -l "
                "or --cpu_extension command line argument")
      sys.exit(1)
  assert len(net.inputs.keys()) == 1, "Sample supports only single input topologies"
  assert len(net.outputs) == 1, "Sample supports only single output topologies"
  input_blob = next(iter(net.inputs))
  out_blob = next(iter(net.outputs))
  log.info("Loading IR to the plugin...")
  exec_net = plugin.load(network=net, num_requests=2)
  # Read and pre-process input image
  n, c, h, w = net.inputs[input_blob]
  del net

  predictions = []
  data = Input(args.input_type, args.input)
  cur_request_id = 0

  fps = 25
  out_width = 640
  out_height = 480
  if args.dump_output_video:
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(args.path_to_output_video, fourcc, fps, (int(out_width), int(out_height)))

  while not data.is_finished():
    frame, img_id = data.get_next_item()
    initial_h, initial_w, channels = frame.shape
    in_frame = cv2.resize(frame, (w, h))
    in_frame = in_frame.transpose((2, 0, 1))  # Change data layout from HWC to CHW
    in_frame = in_frame.reshape((n, c, h, w))

    exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame})
    if exec_net.requests[cur_request_id].wait(-1) == 0:

      # Parse detection results of the current request
      res = exec_net.requests[cur_request_id].outputs[out_blob]
      coco_detections = []
      for obj in res[0][0]:
        # Draw only objects when probability more than specified threshold
        if obj[2] > args.prob_threshold:
          x1 = float(obj[3] * initial_w)
          y1 = float(obj[4] * initial_h)
          x2 = float(obj[5] * initial_w)
          y2 = float(obj[6] * initial_h)

          x_, y_ = round(x1, 1), round(y1, 1)
          w_ = round(x2 - x1, 1)
          h_ = round(y2 - y1, 1)
          class_id = int(obj[1])

          coco_det = {}
          coco_det['image_id'] = img_id
          coco_det['category_id'] = class_id
          coco_det['bbox'] = [x_, y_, w_, h_]
          coco_det['score'] = round(float(obj[2]), 1)
          coco_detections.append(coco_det)

          # Draw box and label\class_id
          cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (255, 0, 0), 2)
          cv2.putText(frame, str(class_id) + ' ' + str(round(obj[2] * 100, 1)) + ' %', (int(x1), int(y1) - 7),
                      cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
      predictions.extend(coco_detections)

    if args.dump_output_video:
      img_resized = cv2.resize(frame, (out_width, out_height))
      out.write(img_resized)
    if args.show:
      cv2.imshow("Detection Results", frame)
      key = cv2.waitKey(10)
      if key == 27:
        break

  if args.dump_predictions_to_json:
    with open(args.output_json_path, 'w') as output_file:
      json.dump(predictions, output_file, sort_keys=True, indent=4)

  cv2.destroyAllWindows()
  del exec_net
  del plugin