コード例 #1
0
def test_const_output_get_target_inputs(device):
    core = Core()
    func = core.read_model(model=test_net_xml, weights=test_net_bin)
    exec_net = core.compile_model(func, device)
    outputs = exec_net.outputs
    for node in outputs:
        assert isinstance(node.get_target_inputs(), set)
コード例 #2
0
def test_const_output_get_partial_shape(device):
    core = Core()
    func = core.read_model(model=test_net_xml, weights=test_net_bin)
    exec_net = core.compile_model(func, device)
    node = exec_net.input("data")
    expected_partial_shape = PartialShape([1, 3, 32, 32])
    assert node.get_partial_shape() == expected_partial_shape
コード例 #3
0
def test_output_type(device):
    core = Core()
    func = core.read_model(model=test_net_xml, weights=test_net_bin)
    exec_net = core.compile_model(func, device)
    output = exec_net.output(0)
    output_type = output.get_element_type().get_type_name()
    assert output_type == "f32"
コード例 #4
0
class Runtime(object):
    """Represents an nGraph runtime environment."""
    def __init__(self, backend_name: str) -> None:
        self.backend_name = backend_name
        log.debug("Creating Inference Engine for %s" % backend_name)
        self.backend = Core()
        assert backend_name in self.backend.available_devices, (
            'The requested device "' + backend_name + '" is not supported!')

    def set_config(self, config: Dict[str, str]) -> None:
        """Set the inference engine configuration."""
        self.backend.set_config(config, device_name=self.backend_name)

    def __repr__(self) -> str:
        return "<Runtime: Backend='{}'>".format(self.backend_name)

    def computation(self, node_or_function: Union[Node, Function],
                    *inputs: Node) -> "Computation":
        """Return a callable Computation object."""
        if isinstance(node_or_function, Node):
            ng_function = Function(node_or_function, inputs,
                                   node_or_function.name)
            return Computation(self, ng_function)
        elif isinstance(node_or_function, Function):
            return Computation(self, node_or_function)
        else:
            raise TypeError(
                "Runtime.computation must be called with an nGraph Function object "
                "or an nGraph node object an optionally Parameter node objects. "
                "Called with: %s",
                node_or_function,
            )
コード例 #5
0
def test_core_class():
    input_shape = [1, 3, 4, 4]
    param = ov.parameter(input_shape, np.float32, name="parameter")
    relu = ov.relu(param, name="relu")
    func = Function([relu], [param], "test")
    func.get_ordered_ops()[2].friendly_name = "friendly"

    cnn_network = IENetwork(func)

    core = Core()
    core.set_config({}, device_name="CPU")
    executable_network = core.compile_model(cnn_network, "CPU", {})

    td = TensorDesc("FP32", input_shape, "NCHW")

    # from IPython import embed; embed()

    request = executable_network.create_infer_request()
    input_data = np.random.rand(*input_shape) - 0.5

    expected_output = np.maximum(0.0, input_data)

    input_blob = Blob(td, input_data)

    request.set_input({"parameter": input_blob})
    request.infer()

    result = request.get_blob("relu").buffer

    assert np.allclose(result, expected_output)
コード例 #6
0
def test_read_network():
    ie_core = Core()
    net = ie_core.read_network(model=test_net_xml, weights=test_net_bin)
    assert isinstance(net, IENetwork)

    net = ie_core.read_network(model=test_net_xml)
    assert isinstance(net, IENetwork)
コード例 #7
0
def test_read_model_from_ir():
    core = Core()
    func = core.read_model(model=test_net_xml, weights=test_net_bin)
    assert isinstance(func, Function)

    func = core.read_model(model=test_net_xml)
    assert isinstance(func, Function)
コード例 #8
0
def test_input_get_index(device):
    core = Core()
    func = core.read_model(model=test_net_xml, weights=test_net_bin)
    exec_net = core.compile_model(func, device)
    input = exec_net.input(0)
    expected_idx = 0
    assert input.get_index() == expected_idx
コード例 #9
0
def test_cancel(device):
    ie_core = Core()
    net = ie_core.read_network(test_net_xml, test_net_bin)
    exec_net = ie_core.load_network(net, device)
    img = read_image()
    td = TensorDesc("FP32", [1, 3, 32, 32], "NCHW")
    input_blob = Blob(td, img)
    request = exec_net.create_infer_request()

    def callback(req, code, array):
        array.append(42)

    data = []
    request.set_completion_callback(callback, data)
    request.set_input({"data": input_blob})
    request.async_infer()
    request.cancel()
    with pytest.raises(RuntimeError) as e:
        request.wait()
    assert "[ INFER_CANCELLED ]" in str(e.value)
    # check if callback has executed
    assert data == [42]

    request.async_infer()
    status = request.wait()
    assert status == StatusCode.OK
    assert data == [42, 42]
コード例 #10
0
def test_const_output_docs(device):
    core = Core()
    func = core.read_model(model=test_net_xml, weights=test_net_bin)
    exec_net = core.compile_model(func, device)
    node = exec_net.input(0)
    exptected_string = "openvino.impl.ConstOutput wraps ov::Output<Const ov::Node >"
    assert node.__doc__ == exptected_string
コード例 #11
0
def test_inputs(device):
    core = Core()
    func = core.read_model(model=test_net_xml, weights=test_net_bin)
    exec_net = core.compile_model(func, device)
    inputs = exec_net.inputs
    assert isinstance(inputs, list)
    assert len(inputs) == 1
コード例 #12
0
def test_output_shape(device):
    core = Core()
    func = core.read_model(model=test_net_xml, weights=test_net_bin)
    exec_net = core.compile_model(func, device)
    output = exec_net.output(0)
    expected_shape = Shape([1, 10])
    assert str(output.get_shape()) == str(expected_shape)
コード例 #13
0
def test_tensor_setter(device):
    core = Core()
    func = core.read_model(test_net_xml, test_net_bin)
    exec_net_1 = core.compile_model(model=func, device_name=device)
    exec_net_2 = core.compile_model(model=func, device_name=device)

    img = read_image()
    tensor = Tensor(img)

    request1 = exec_net_1.create_infer_request()
    request1.set_tensor("data", tensor)
    t1 = request1.get_tensor("data")

    assert np.allclose(tensor.data, t1.data, atol=1e-2, rtol=1e-2)

    res = request1.infer({0: tensor})
    res_1 = np.sort(res[0])
    t2 = request1.get_tensor("fc_out")
    assert np.allclose(t2.data, res[0].data, atol=1e-2, rtol=1e-2)

    request = exec_net_2.create_infer_request()
    res = request.infer({"data": tensor})
    res_2 = np.sort(request.get_tensor("fc_out").data)
    assert np.allclose(res_1, res_2, atol=1e-2, rtol=1e-2)

    request.set_tensor("data", tensor)
    t3 = request.get_tensor("data")
    assert np.allclose(t3.data, t1.data, atol=1e-2, rtol=1e-2)
コード例 #14
0
def test_blob_setter(device):
    ie_core = Core()
    net = ie_core.read_network(test_net_xml, test_net_bin)
    exec_net_1 = ie_core.load_network(network=net, device_name=device)

    net.input_info["data"].layout = "NHWC"
    exec_net_2 = ie_core.load_network(network=net, device_name=device)

    img = read_image()

    request1 = exec_net_1.create_infer_request()
    tensor_desc = TensorDesc("FP32", [1, 3, img.shape[2], img.shape[3]],
                             "NCHW")
    img_blob1 = Blob(tensor_desc, img)
    request1.set_input({"data": img_blob1})
    request1.infer()
    res_1 = np.sort(request1.get_blob("fc_out").buffer)

    img = np.transpose(img, axes=(0, 2, 3, 1)).astype(np.float32)
    tensor_desc = TensorDesc("FP32", [1, 3, 32, 32], "NHWC")
    img_blob = Blob(tensor_desc, img)
    request = exec_net_2.create_infer_request()
    request.set_blob("data", img_blob)
    request.infer()
    res_2 = np.sort(request.get_blob("fc_out").buffer)
    assert np.allclose(res_1, res_2, atol=1e-2, rtol=1e-2)
コード例 #15
0
def test_infer_new_request_numpy(device):
    ie = Core()
    func = ie.read_model(model=test_net_xml, weights=test_net_bin)
    img = read_image()
    exec_net = ie.compile_model(func, device)
    res = exec_net.infer_new_request({"data": img})
    assert np.argmax(res) == 2
コード例 #16
0
def test_get_metric_tuple_of_three_ints():
    ie = Core()
    param = ie.get_metric("CPU", "RANGE_FOR_ASYNC_INFER_REQUESTS")
    assert isinstance(param, tuple), "Parameter value for 'RANGE_FOR_ASYNC_INFER_REQUESTS' " \
                                     f"metric must be tuple but {type(param)} is returned"
    assert all(isinstance(v, int) for v in param), "Not all of the parameter values for " \
                                                   "'RANGE_FOR_ASYNC_INFER_REQUESTS' metric are integers!"
コード例 #17
0
def test_get_metric_tuple_of_two_ints():
    ie = Core()
    param = ie.get_metric("CPU", "RANGE_FOR_STREAMS")
    assert isinstance(param, tuple), "Parameter value for 'RANGE_FOR_STREAMS' " \
                                     f"metric must be tuple but {type(param)} is returned"
    assert all(isinstance(v, int) for v in param), \
        "Not all of the parameter values for 'RANGE_FOR_STREAMS' metric are integers!"
コード例 #18
0
def test_get_metric_list_of_str():
    ie = Core()
    param = ie.get_metric("CPU", "OPTIMIZATION_CAPABILITIES")
    assert isinstance(param, list), "Parameter value for 'OPTIMIZATION_CAPABILITIES' " \
                                    f"metric must be a list but {type(param)} is returned"
    assert all(isinstance(v, str) for v in param), \
        "Not all of the parameter values for 'OPTIMIZATION_CAPABILITIES' metric are strings!"
コード例 #19
0
def test_inputs_docs(device):
    core = Core()
    func = core.read_model(model=test_net_xml, weights=test_net_bin)
    exec_net = core.compile_model(func, device)
    inputs = exec_net.inputs
    input_0 = inputs[0]
    expected_string = "openvino.impl.ConstOutput wraps ov::Output<Const ov::Node >"
    assert input_0.__doc__ == expected_string
コード例 #20
0
def test_read_net_from_buffer():
    ie_core = Core()
    with open(test_net_bin, "rb") as f:
        bin = f.read()
    with open(model_path()[0], "rb") as f:
        xml = f.read()
    net = ie_core.read_network(model=xml, weights=bin)
    assert isinstance(net, IENetwork)
コード例 #21
0
def test_read_net_from_buffer():
    core = Core()
    with open(test_net_bin, "rb") as f:
        bin = f.read()
    with open(model_path()[0], "rb") as f:
        xml = f.read()
    func = core.read_model(model=xml, weights=bin)
    assert isinstance(func, Function)
コード例 #22
0
ファイル: onnx_helpers.py プロジェクト: pavel-esir/openvino
def import_onnx_model(model: onnx.ModelProto) -> Function:
    onnx.checker.check_model(model)
    model_byte_string = model.SerializeToString()
    ie = Core()
    func = ie.read_model(bytes(model_byte_string),
                         Tensor(type=np.uint8, shape=[]))

    return func
コード例 #23
0
def test_get_config(device):
    core = Core()
    if core.get_metric(device, "FULL_DEVICE_NAME") == "arm_compute::NEON":
        pytest.skip("Can't run on ARM plugin due-to CPU dependent test")
    func = core.read_model(model=test_net_xml, weights=test_net_bin)
    exec_net = core.compile_model(func, device)
    config = exec_net.get_config("PERF_COUNT")
    assert config == "NO"
コード例 #24
0
def test_get_version(device):
    ie = Core()
    version = ie.get_versions(device)
    assert isinstance(version, dict), "Returned version must be a dictionary"
    assert device in version, "{} plugin version wasn't found in versions"
    assert hasattr(version[device], "major"), "Returned version has no field 'major'"
    assert hasattr(version[device], "minor"), "Returned version has no field 'minor'"
    assert hasattr(version[device], "description"), "Returned version has no field 'description'"
    assert hasattr(version[device], "build_number"), "Returned version has no field 'build_number'"
コード例 #25
0
def test_inputs_get_friendly_name(device):
    core = Core()
    func = core.read_model(model=test_net_xml, weights=test_net_bin)
    exec_net = core.compile_model(func, device)
    inputs = exec_net.inputs
    input_0 = inputs[0]
    node = input_0.get_node()
    name = node.friendly_name
    assert name == "data"
コード例 #26
0
def test_get_input(device):
    core = Core()
    func = core.read_model(model=test_net_xml, weights=test_net_bin)
    exec_net = core.compile_model(func, device)
    input = exec_net.input()
    input_node = input.get_node()
    name = input_node.friendly_name
    assert isinstance(input, ConstOutput)
    assert name == "data"
コード例 #27
0
def test_infer_new_request_wrong_port_name(device):
    ie = Core()
    func = ie.read_model(model=test_net_xml, weights=test_net_bin)
    img = read_image()
    tensor = Tensor(img)
    exec_net = ie.compile_model(func, device)
    with pytest.raises(RuntimeError) as e:
        exec_net.infer_new_request({"_data_": tensor})
    assert "Port for tensor name _data_ was not found." in str(e.value)
コード例 #28
0
def test_output_set_friendly_name(device):
    core = Core()
    func = core.read_model(model=test_net_xml, weights=test_net_bin)
    exec_net = core.compile_model(func, device)
    output = exec_net.output(0)
    output_node = output.get_node()
    output_node.set_friendly_name("output_1")
    name = output_node.friendly_name
    assert isinstance(output, ConstOutput)
    assert name == "output_1"
コード例 #29
0
def test_infer_tensor_wrong_input_data(device):
    ie = Core()
    func = ie.read_model(model=test_net_xml, weights=test_net_bin)
    img = read_image()
    img = np.ascontiguousarray(img)
    tensor = Tensor(img, shared_memory=True)
    exec_net = ie.compile_model(func, device)
    with pytest.raises(TypeError) as e:
        exec_net.infer_new_request({4.5: tensor})
    assert "Incompatible key type!" in str(e.value)
コード例 #30
0
def test_infer_new_request_tensor_numpy_copy(device):
    ie = Core()
    func = ie.read_model(model=test_net_xml, weights=test_net_bin)
    img = read_image()
    tensor = Tensor(img)
    exec_net = ie.compile_model(func, device)
    res_tensor = exec_net.infer_new_request({"data": tensor})
    res_img = exec_net.infer_new_request({"data": tensor})
    assert np.argmax(res_tensor) == 2
    assert np.argmax(res_tensor) == np.argmax(res_img)