Example #1
0
def test_node_input_values():
    shapes = [Shape([3]), Shape([3])]
    data1 = np.array([1, 2, 3])
    data2 = np.array([3, 2, 1])

    node = ops.add(data1, data2)

    assert node.get_input_size() == 2

    assert np.equal(
        [input_node.get_shape() for input_node in node.input_values()],
        shapes).all()

    assert np.equal([
        node.input_value(i).get_shape() for i in range(node.get_input_size())
    ], shapes).all()

    assert np.allclose([
        input_node.get_node().get_vector()
        for input_node in node.input_values()
    ], [data1, data2])

    assert np.allclose([
        node.input_value(i).get_node().get_vector()
        for i in range(node.get_input_size())
    ], [data1, data2])
Example #2
0
def test_convolution_with_non_zero_padding():
    element_type = Type.f32
    image_shape = Shape([1, 1, 10, 10])
    filter_shape = Shape([1, 1, 3, 3])
    data = Parameter(element_type, image_shape)
    filters = Parameter(element_type, filter_shape)
    parameter_list = [data, filters]

    image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    filter_arr = (np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)) * -1
    filter_arr[0][0][1][1] = 1
    strides = [1, 1]
    dilations = [2, 2]
    pads_begin = [2, 1]
    pads_end = [1, 2]

    model = ov.convolution(data, filters, strides, pads_begin, pads_end,
                           dilations)
    function = Model([model], parameter_list, "test")

    runtime = get_runtime()
    computation = runtime.computation(function, *parameter_list)
    result = computation(image_arr, filter_arr)[0]

    expected = convolution2d(image_arr[0][0], filter_arr[0][0], strides,
                             dilations, pads_begin,
                             pads_end).reshape([1, 1, 9, 9])
    assert np.allclose(result, expected)
Example #3
0
def test_convolution_simple():

    element_type = Type.f32
    image_shape = Shape([1, 1, 16, 16])
    filter_shape = Shape([1, 1, 3, 3])
    data = Parameter(element_type, image_shape)
    filters = Parameter(element_type, filter_shape)
    parameter_list = [data, filters]

    image_arr = np.arange(-128, 128, 1, dtype=np.float32).reshape(1, 1, 16, 16)
    filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)
    filter_arr[0][0][0][0] = -1
    filter_arr[0][0][1][1] = -1
    filter_arr[0][0][2][2] = -1
    filter_arr[0][0][0][2] = -1
    filter_arr[0][0][2][0] = -1

    strides = [1, 1]
    pads_begin = [0, 0]
    pads_end = [0, 0]
    dilations = [1, 1]

    model = ov.convolution(data, filters, strides, pads_begin, pads_end,
                           dilations)
    function = Model([model], parameter_list, "test")

    runtime = get_runtime()
    computation = runtime.computation(function, *parameter_list)
    result = computation(image_arr, filter_arr)[0]

    expected = convolution2d(image_arr[0][0],
                             filter_arr[0][0]).reshape(1, 1, 14, 14)
    assert np.allclose(result, expected)
Example #4
0
def test_proposal_props():
    float_dtype = np.float32
    batch_size = 1
    post_nms_topn = 20
    probs = ov.parameter(Shape([batch_size, 8, 255, 255]), dtype=float_dtype, name="probs")
    deltas = ov.parameter(Shape([batch_size, 16, 255, 255]), dtype=float_dtype, name="bbox_deltas")
    im_info = ov.parameter(Shape([4]), dtype=float_dtype, name="im_info")

    attrs = {
        "base_size": np.uint32(85),
        "pre_nms_topn": np.uint32(10),
        "post_nms_topn": np.uint32(post_nms_topn),
        "nms_thresh": np.float32(0.34),
        "feat_stride": np.uint32(16),
        "min_size": np.uint32(32),
        "ratio": np.array([0.1, 1.5, 2.0, 2.5], dtype=np.float32),
        "scale": np.array([2, 3, 3, 4], dtype=np.float32),
    }

    node = ov.proposal(probs, deltas, im_info, attrs)

    assert node.get_type_name() == "Proposal"
    assert node.get_output_size() == 2

    assert list(node.get_output_shape(0)) == [batch_size * post_nms_topn, 5]
    assert list(node.get_output_shape(1)) == [batch_size * post_nms_topn]
    assert node.get_output_element_type(0) == Type.f32
    assert node.get_output_element_type(1) == Type.f32
Example #5
0
def test_get_constant_from_source_failed():
    dtype = np.int
    input1 = ov.opset8.parameter(Shape([5, 5]), dtype=dtype, name="input_1")
    input2 = ov.opset8.parameter(Shape([1]), dtype=dtype, name="input_2")
    reshape = ov.opset8.reshape(input1, input2, special_zero=True)
    folded_const = ov.utils.get_constant_from_source(reshape.input(1).get_source_output())

    assert folded_const is None
Example #6
0
def test_evaluate_invalid_input_shape():
    model = create_test_model()
    with pytest.raises(RuntimeError) as e:
        assert model.evaluate(
            [Tensor("float32", Shape([2, 1]))],
            [Tensor("float32", Shape([3, 1])), Tensor("float32", Shape([3, 1]))],
        )
    assert "must be compatible with the partial shape: {2,1}" in str(e.value)
def test_get_batch():
    param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1")
    param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2")
    add = ops.add(param1, param2)
    func = Model(add, [param1, param2], "TestFunction")
    param = func.get_parameters()[0]
    param.set_layout(Layout("NC"))
    assert get_batch(func) == 2
Example #8
0
def test_batched_tensors(device):
    batch = 4
    one_shape = Shape([1, 2, 2, 2])
    batch_shape = Shape([batch, 2, 2, 2])
    one_shape_size = np.prod(one_shape)

    core = Core()

    core.register_plugin("openvino_template_plugin", "TEMPLATE")

    data1 = ops.parameter(batch_shape, np.float32)
    data1.set_friendly_name("input0")
    data1.get_output_tensor(0).set_names({"tensor_input0"})
    data1.set_layout(Layout("N..."))

    constant = ops.constant([1], np.float32)

    op1 = ops.add(data1, constant)
    op1.set_friendly_name("Add0")

    res1 = ops.result(op1)
    res1.set_friendly_name("Result0")
    res1.get_output_tensor(0).set_names({"tensor_output0"})

    model = Model([res1], [data1])

    compiled = core.compile_model(model, "TEMPLATE")

    buffer = np.zeros([one_shape_size * batch * 2], dtype=np.float32)

    req = compiled.create_infer_request()

    tensors = []

    for i in range(0, batch):
        _start = i * one_shape_size * 2
        # Use of special constructor for Tensor.
        # It creates a Tensor from pointer, thus it requires only
        # one element from original buffer, and shape to "crop".
        tensor = Tensor(buffer[_start:(_start + 1)], one_shape)
        tensors.append(tensor)

    req.set_input_tensors(tensors)  # using list overload!

    actual_tensor = req.get_tensor("tensor_output0")
    actual = actual_tensor.data
    for test_num in range(0, 5):
        for i in range(0, batch):
            tensors[i].data[:] = test_num + 10

        req.infer()  # Adds '1' to each element

        # Reference values for each batch:
        _tmp = np.array([test_num + 11] * one_shape_size,
                        dtype=np.float32).reshape([2, 2, 2])

        for j in range(0, batch):
            assert np.array_equal(actual[j], _tmp)
def test_set_batch_default_batch_size():
    param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1")
    param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2")
    add = ops.add(param1, param2)
    func = Model(add, [param1, param2], "TestFunction")
    func_param1 = func.get_parameters()[0]
    func_param1.set_layout(Layout("NC"))
    set_batch(func)
    assert func.is_dynamic()
Example #10
0
def test_swish_props_with_beta():
    float_dtype = np.float32
    data = ov.parameter(Shape([3, 10]), dtype=float_dtype, name="data")
    beta = ov.parameter(Shape([]), dtype=float_dtype, name="beta")

    node = ov.swish(data, beta)
    assert node.get_type_name() == "Swish"
    assert node.get_output_size() == 1
    assert list(node.get_output_shape(0)) == [3, 10]
    assert node.get_output_element_type(0) == Type.f32
Example #11
0
def test_get_constant_from_source_success():
    dtype = np.int
    input1 = ov.opset8.parameter(Shape([5, 5]), dtype=dtype, name="input_1")
    input2 = ov.opset8.parameter(Shape([25]), dtype=dtype, name="input_2")
    shape_of = ov.opset8.shape_of(input2, name="shape_of")
    reshape = ov.opset8.reshape(input1, shape_of, special_zero=True)
    folded_const = ov.utils.get_constant_from_source(reshape.input(1).get_source_output())

    assert folded_const is not None
    assert folded_const.get_vector() == [25]
Example #12
0
def test_get_batch_CHWN():
    param1 = ops.parameter(Shape([3, 1, 3, 4]), dtype=np.float32, name="data1")
    param2 = ops.parameter(Shape([3, 1, 3, 4]), dtype=np.float32, name="data2")
    param3 = ops.parameter(Shape([3, 1, 3, 4]), dtype=np.float32, name="data3")
    add = ops.add(param1, param2)
    add2 = ops.add(add, param3)
    func = Model(add2, [param1, param2, param3], "TestFunction")
    param = func.get_parameters()[0]
    param.set_layout(Layout("CHWN"))
    assert get_batch(func) == 4
def test_evaluate():
    param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1")
    param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2")
    add = ops.add(param1, param2)
    func = Model(add, [param1, param2], "TestFunction")

    input1 = np.array([2, 1], dtype=np.float32).reshape(2, 1)
    input2 = np.array([3, 7], dtype=np.float32).reshape(2, 1)
    out_tensor = Tensor("float32", Shape([2, 1]))

    assert func.evaluate([out_tensor], [Tensor(input1), Tensor(input2)])
    assert np.allclose(out_tensor.data, np.array([5, 8]).reshape(2, 1))
Example #14
0
    def _fill_input(self, model, image_batch):
        """Matches network input name with corresponding input batch
        :param model: IENetwork instance
        :param image_batch: list of ndarray images or list with a dictionary of inputs mapping
        """
        input_info = model.inputs

        if isinstance(image_batch[0], dict):
            feed_dict = {}
            input_blobs = {get_clean_name(in_node.get_node().friendly_name): in_node for in_node in input_info}
            for input_name in image_batch[0].keys():
                input_blob = input_blobs[input_name]
                input_blob_name = self._get_input_any_name(input_blob)
                feed_dict[input_blob_name] = np.reshape(image_batch[0][input_name], input_blob.shape)
            return feed_dict

        if len(input_info) == 1:
            input_blob = next(iter(input_info))
            input_blob_name = self._get_input_any_name(input_blob)
            image_batch = {input_blob_name: np.reshape(image_batch, input_blob.shape)}
            if Shape(image_batch[input_blob_name].shape) != input_info[0].shape:
                raise ValueError(f"Incompatible input shapes. "
                                 f"Cannot infer {Shape(image_batch[input_blob_name].shape)} into {input_info[0].shape}."
                                 f"Try to specify the layout of the model.")
            return image_batch

        if len(input_info) == 2:
            image_info_nodes = list(filter(
                lambda x: len(x.shape) == 2, input_info))

            if len(image_info_nodes) != 1:
                raise Exception('Two inputs networks must contain exactly one ImageInfo node')

            image_info_node = image_info_nodes[0]
            image_info_name = self._get_input_any_name(image_info_node)
            image_tensor_node = next(iter(filter(
                lambda x: x.get_any_name() != image_info_name, input_info)))
            image_tensor_name = image_tensor_node.get_any_name()

            image_tensor = (image_tensor_name, np.reshape(image_batch, input_blob.shape))
            if Shape(image_tensor[1].shape) != image_tensor_node.shape:
                raise ValueError(f"Incompatible input shapes. "
                                 f"Cannot infer {Shape(image_tensor[1].shape)} into {image_tensor_node.shape}."
                                 f"Try to specify the layout of the model.")

            ch, height, width = image_batch[0].shape
            image_info = (image_info_name,
                          np.stack(np.array([(height, width, ch)] * len(image_batch)), axis=0))

            return dict((k, v) for k, v in [image_tensor, image_info])

        raise Exception('Unsupported number of inputs')
def test_validate_nodes_and_infer_types():
    param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1")
    param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2")
    add = ops.add(param1, param2)
    func = Model(add, [param1, param2], "TestFunction")

    invalid_shape = Shape([3, 7])
    param3 = ops.parameter(invalid_shape, dtype=np.float32, name="data3")
    func.replace_parameter(0, param3)

    with pytest.raises(RuntimeError) as e:
        func.validate_nodes_and_infer_types()
    assert "Argument shapes are inconsistent" in str(e.value)
Example #16
0
def test_gather_elements():
    indices_type = np.int32
    data_dtype = np.float32
    data = ov.parameter(Shape([2, 5]), dtype=data_dtype, name="data")
    indices = ov.parameter(Shape([2, 100]), dtype=indices_type, name="indices")
    axis = 1
    expected_shape = [2, 100]

    node = ov.gather_elements(data, indices, axis)
    assert node.get_type_name() == "GatherElements"
    assert node.get_output_size() == 1
    assert list(node.get_output_shape(0)) == expected_shape
    assert node.get_output_element_type(0) == Type.f32
Example #17
0
def test_reshape():

    element_type = Type.f32
    shape = Shape([2, 3])
    A = Parameter(element_type, shape)
    parameter_list = [A]
    function = Model([ov.reshape(A, Shape([3, 2]), special_zero=False)], parameter_list, "test")

    runtime = get_runtime()
    computation = runtime.computation(function, *parameter_list)
    result = computation(np.array(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32), dtype=np.float32))[0]

    expected = np.reshape(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32), (3, 2))
    assert np.allclose(result, expected)
Example #18
0
def test_simple_loop():
    X = ov.parameter(Shape([32, 1, 10]), np.float32, "X")
    Y = ov.parameter(Shape([32, 1, 10]), np.float32, "Y")
    M = ov.parameter(Shape([32, 1, 10]), np.float32, "M")

    input_shape = Shape([])

    current_iteration = ov.parameter(Shape([1]), np.int64)
    X_i = ov.parameter(input_shape, np.float32)
    Y_i = ov.parameter(input_shape, np.float32)
    M_body = ov.parameter(input_shape, np.float32)
    bool_val = np.array([1], dtype=np.bool)
    bool_val[0] = True
    body_condition = ov.constant(bool_val)
    trip_count = ov.constant(10, dtype=np.int64)
    exec_condition = ov.constant(True, dtype=np.bool)

    sum = ov.add(X_i, Y_i)
    Zo = ov.multiply(sum, M_body)

    body = Model([body_condition, Zo], [current_iteration, X_i, Y_i, M_body],
                 "body_function")

    loop = ov.loop(trip_count, exec_condition)
    loop.set_function(body)
    loop.set_invariant_input(X_i, X.output(0))
    loop.set_invariant_input(Y_i, Y.output(0))
    loop.set_merged_input(M_body, M.output(0), Zo.output(0))
    loop.set_special_body_ports([-1, 0])

    out0 = loop.get_iter_value(body_condition.output(0), -1)
    out1 = loop.get_iter_value(Zo.output(0), -1)
    out2 = loop.get_concatenated_slices(Zo.output(0), 0, 1, 1, -1, 1)

    result0 = ov.result(out0)
    result1 = ov.result(out1)
    result2 = ov.result(out2)

    out0_shape = [1]
    out1_shape = [32, 1, 10]
    out2_shape = [32, 10, 10]

    assert list(result0.get_output_shape(0)) == out0_shape
    assert list(result1.get_output_shape(0)) == out1_shape
    assert list(result2.get_output_shape(0)) == out2_shape

    assert list(loop.get_output_shape(0)) == out0_shape
    assert list(loop.get_output_shape(1)) == out1_shape
    assert list(loop.get_output_shape(2)) == out2_shape
def test_evaluate_invalid_input_shape():
    param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1")
    param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2")
    add = ops.add(param1, param2)
    func = Model(add, [param1, param2], "TestFunction")

    with pytest.raises(RuntimeError) as e:
        assert func.evaluate(
            [Tensor("float32", Shape([2, 1]))],
            [
                Tensor("float32", Shape([3, 1])),
                Tensor("float32", Shape([3, 1]))
            ],
        )
    assert "must be compatible with the partial shape: {2,1}" in str(e.value)
Example #20
0
def test_infer_dynamic_model(device):
    core = Core()
    param = ops.parameter(PartialShape([-1, -1]))
    model = Model(ops.relu(param), [param])
    compiled = core.compile_model(model, device)
    assert compiled.input().partial_shape.is_dynamic
    request = compiled.create_infer_request()

    shape1 = [1, 28]
    request.infer([np.random.normal(size=shape1)])
    assert request.get_input_tensor().shape == Shape(shape1)

    shape2 = [1, 32]
    request.infer([np.random.normal(size=shape2)])
    assert request.get_input_tensor().shape == Shape(shape2)
Example #21
0
def test_input_get_shape(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.output(0)
    input_node = input.get_node().inputs()[0]
    assert str(input_node.get_shape()) == str(Shape([1, 10]))
Example #22
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)
Example #23
0
def get_test_function():
    element_type = Type.f32
    param = Parameter(element_type, Shape([1, 3, 22, 22]))
    relu = ops.relu(param)
    func = Model([relu], [param], "test")
    assert func is not None
    return func
Example #24
0
def roi_pooling(
    input: NodeInput,
    coords: NodeInput,
    output_size: TensorShape,
    spatial_scale: NumericData,
    method: str,
    name: Optional[str] = None,
) -> Node:
    """Return a node which produces an ROIPooling operation.

    @param input:          Input feature map {N, C, ...}
    @param coords:         Coordinates of bounding boxes
    @param output_size:    Height/Width of ROI output features (shape)
    @param spatial_scale:  Ratio of input feature map over input image size (float)
    @param method:         Method of pooling - string: "max" or "bilinear"
    @return               ROIPooling node
    """
    method = method.lower()
    return _get_node_factory_opset2().create(
        "ROIPooling",
        as_nodes(input, coords),
        {
            "output_size": Shape(output_size),
            "spatial_scale": spatial_scale,
            "method": method
        },
    )
Example #25
0
def test_input_shape_read_only():
    shape = Shape([1, 10])
    param = ov.parameter(shape, dtype=np.float32)
    model = Model(ov.relu(param), [param])
    ref_shape = model.input().shape
    ref_shape[0] = Dimension(3)
    assert model.input().shape == shape
Example #26
0
def test_node_input():
    shape = [2, 2]
    parameter_a = ops.parameter(shape, dtype=np.float32, name="A")
    parameter_b = ops.parameter(shape, dtype=np.float32, name="B")

    model = parameter_a + parameter_b

    model_inputs = model.inputs()

    assert len(model_inputs) == 2
    assert [input_node.get_index() for input_node in model_inputs] == [0, 1]
    assert np.equal(
        [input_node.get_element_type() for input_node in model_inputs],
        model.get_element_type(),
    ).all()
    assert np.equal([input_node.get_shape() for input_node in model_inputs],
                    Shape(shape)).all()
    assert np.equal(
        [input_node.get_partial_shape() for input_node in model_inputs],
        PartialShape(shape),
    ).all()

    input0 = model.input(0)
    input1 = model.input(1)

    assert [input0.get_index(), input1.get_index()] == [0, 1]
def test_const_output_get_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_shape = Shape([1, 3, 32, 32])
    assert str(node.get_shape()) == str(expected_shape)
    assert str(node.shape) == str(expected_shape)
def test_set_batch_int():
    param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1")
    param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2")
    add = ops.add(param1, param2)
    func = Model(add, [param1, param2], "TestFunction")
    func_param1 = func.get_parameters()[0]
    func_param2 = func.get_parameters()[1]
    # batch == 2
    func_param1.set_layout(Layout("NC"))
    assert get_batch(func) == 2
    # set batch to 1
    set_batch(func, 1)
    assert get_batch(func) == 1
    # check if shape of param 1 has changed
    assert str(func_param1.get_output_shape(0) == {1, 1})
    # check if shape of param 2 has not changed
    assert str(func_param2.get_output_shape(0) == {2, 1})
Example #29
0
def test_evaluate():
    model = create_test_model()
    input1 = np.array([2, 1], dtype=np.float32).reshape(2, 1)
    input2 = np.array([3, 7], dtype=np.float32).reshape(2, 1)
    out_tensor = Tensor("float32", Shape([2, 1]))

    assert model.evaluate([out_tensor], [Tensor(input1), Tensor(input2)])
    assert np.allclose(out_tensor.data, np.array([5, 8]).reshape(2, 1))
Example #30
0
def test_hsigmoid():
    float_dtype = np.float32
    data = ov.parameter(Shape([3, 10]), dtype=float_dtype, name="data")

    node = ov.hsigmoid(data)
    assert node.get_type_name() == "HSigmoid"
    assert node.get_output_size() == 1
    assert list(node.get_output_shape(0)) == [3, 10]
    assert node.get_output_element_type(0) == Type.f32