Exemple #1
0
def test_ngraph_preprocess_steps(algorithm, color_format1, color_format2,
                                 is_failing):
    shape = [1, 1, 3, 3]
    parameter_a = ops.parameter(shape, dtype=np.float32, name="A")
    model = parameter_a
    function = Function(model, [parameter_a], "TestFunction")
    layout1 = ov.Layout("NCWH")
    layout2 = ov.Layout("NCHW")

    custom_processor = PrePostProcessor(function)
    inp = custom_processor.input()
    inp.tensor().set_layout(layout1).set_color_format(color_format1, [])
    inp.preprocess().mean(1.).resize(
        algorithm, 3, 3).convert_layout(layout2).convert_color(color_format2)

    if is_failing:
        with pytest.raises(RuntimeError) as e:
            function = custom_processor.build()
        assert "is not convertible to" in str(e.value)
    else:
        function = custom_processor.build()
        input_data = np.array([[[[1, 2, 3], [4, 5, 6],
                                 [7, 8, 9]]]]).astype(np.float32)
        expected_output = np.array([[[[0, 3, 6], [1, 4, 7],
                                      [2, 5, 8]]]]).astype(np.float32)

        runtime = get_runtime()
        computation = runtime.computation(function)
        output = computation(input_data)
        assert np.equal(output, expected_output).all()
Exemple #2
0
def test_ngraph_preprocess_postprocess_layout():
    shape = [1, 1, 3, 3]
    parameter_a = ops.parameter(shape, dtype=np.float32, name="A")
    model = parameter_a
    function = Function(model, [parameter_a], "TestFunction")
    layout1 = ov.Layout("NCWH")
    layout2 = ov.Layout("NCHW")

    p = PrePostProcessor(function)
    inp = p.input()
    inp.tensor().set_layout(layout1)
    inp.preprocess().mean(1.).convert_layout(layout2).reverse_channels()
    out = p.output()
    out.postprocess().convert_layout([0, 1, 2, 3])
    function = p.build()

    input_data = np.array([[[[1, 2, 3], [4, 5, 6], [7, 8,
                                                    9]]]]).astype(np.float32)
    expected_output = np.array([[[[0, 3, 6], [1, 4, 7],
                                  [2, 5, 8]]]]).astype(np.float32)

    runtime = get_runtime()
    computation = runtime.computation(function)
    output = computation(input_data)
    assert np.equal(output, expected_output).all()
Exemple #3
0
def test_ngraph_preprocess_output_postprocess():
    shape = [2, 2]
    parameter_a = ops.parameter(shape, dtype=np.int32, name="A")
    model = parameter_a
    function = Function(model, [parameter_a], "TestFunction")
    layout1 = ov.Layout("NCHW")
    layout2 = ov.Layout("NHWC")
    layout3 = [0, 1]

    @custom_preprocess_function
    def custom_postprocess(output: Output):
        return ops.abs(output)

    p = PrePostProcessor(function)
    inp = p.input()
    inp.tensor().set_layout(layout1)
    inp.preprocess().convert_element_type(Type.f32).mean([1., 2.])
    out = p.output()
    out.postprocess().convert_element_type(Type.f32) \
                     .convert_layout(layout2) \
                     .convert_layout(layout3).custom(custom_postprocess)
    function = p.build()

    input_data = np.array([[-1, -2], [-3, -4]]).astype(np.int32)
    expected_output = np.array([[2, 4], [4, 6]]).astype(np.float32)

    runtime = get_runtime()
    computation = runtime.computation(function)
    output = computation(input_data)
    assert np.equal(output, expected_output).all()
Exemple #4
0
def test_layout_helpers():
    layout = ov.Layout("NCHWD")
    assert(layout_helpers.has_batch(layout))
    assert(layout_helpers.has_channels(layout))
    assert(layout_helpers.has_depth(layout))
    assert(layout_helpers.has_height(layout))
    assert(layout_helpers.has_width(layout))

    assert layout_helpers.batch_idx(layout) == 0
    assert layout_helpers.channels_idx(layout) == 1
    assert layout_helpers.height_idx(layout) == 2
    assert layout_helpers.width_idx(layout) == 3
    assert layout_helpers.depth_idx(layout) == 4

    layout = ov.Layout("N...C")
    assert(layout_helpers.has_batch(layout))
    assert(layout_helpers.has_channels(layout))
    assert not(layout_helpers.has_depth(layout))
    assert not(layout_helpers.has_height(layout))
    assert not (layout_helpers.has_width(layout))

    assert layout_helpers.batch_idx(layout) == 0
    assert layout_helpers.channels_idx(layout) == -1

    with pytest.raises(RuntimeError):
        layout_helpers.height_idx(layout)

    with pytest.raises(RuntimeError):
        layout_helpers.width_idx(layout)

    with pytest.raises(RuntimeError):
        layout_helpers.depth_idx(layout)

    layout = ov.Layout("NC?")
    assert(layout_helpers.has_batch(layout))
    assert(layout_helpers.has_channels(layout))
    assert not(layout_helpers.has_depth(layout))
    assert not(layout_helpers.has_height(layout))
    assert not (layout_helpers.has_width(layout))

    assert layout_helpers.batch_idx(layout) == 0
    assert layout_helpers.channels_idx(layout) == 1

    with pytest.raises(RuntimeError):
        layout_helpers.height_idx(layout)

    with pytest.raises(RuntimeError):
        layout_helpers.width_idx(layout)

    with pytest.raises(RuntimeError):
        layout_helpers.depth_idx(layout)
Exemple #5
0
def test_ngraph_preprocess_spatial_static_shape():
    shape = [2, 2, 2]
    parameter_a = ops.parameter(shape, dtype=np.int32, name="A")
    model = parameter_a
    function = Function(model, [parameter_a], "TestFunction")
    layout = ov.Layout("CHW")

    color_format = ColorFormat.RGB

    p = PrePostProcessor(function)
    inp = p.input()
    inp.tensor().set_layout(layout).set_spatial_static_shape(
        2, 2).set_color_format(color_format, [])
    inp.preprocess().convert_element_type(Type.f32).mean([1., 2.])
    inp.network().set_layout(layout)
    out = p.output()
    out.tensor().set_layout(layout).set_element_type(Type.f32)
    out.network().set_layout(layout)
    function = p.build()

    input_data = np.array([[[1, 2], [3, 4]], [[5, 6], [7,
                                                       8]]]).astype(np.int32)
    expected_output = np.array([[[0, 1], [2, 3]], [[3, 4],
                                                   [5, 6]]]).astype(np.float32)

    runtime = get_runtime()
    computation = runtime.computation(function)
    output = computation(input_data)
    assert np.equal(output, expected_output).all()
Exemple #6
0
def test_ngraph_preprocess_resize_algorithm():
    shape = [1, 1, 3, 3]
    parameter_a = ops.parameter(shape, dtype=np.float32, name="A")
    model = parameter_a
    function = Function(model, [parameter_a], "TestFunction")
    resize_alg = ResizeAlgorithm.RESIZE_CUBIC
    layout1 = ov.Layout("NCWH")

    function = PrePostProcessor(function)\
        .input(InputInfo()
               .tensor(InputTensorInfo()
                       .set_layout(layout1))
               .preprocess(PreProcessSteps()
                           .mean(1.)
                           .resize(resize_alg, 3, 3))
               )\
        .build()

    input_data = np.array([[[[1, 2, 3], [4, 5, 6], [7, 8,
                                                    9]]]]).astype(np.float32)
    expected_output = np.array([[[[0, 1, 2], [3, 4, 5],
                                  [6, 7, 8]]]]).astype(np.float32)

    runtime = get_runtime()
    computation = runtime.computation(function)
    output = computation(input_data)
    assert np.equal(output, expected_output).all()
Exemple #7
0
def test_ngraph_preprocess_reverse_channels():
    shape = [1, 2, 2, 2]
    parameter_a = ops.parameter(shape, dtype=np.float32, name="A")
    model = parameter_a
    function = Function(model, [parameter_a], "TestFunction")
    layout1 = ov.Layout("NCWH")

    function = PrePostProcessor(function)\
        .input(InputInfo()
               .tensor(InputTensorInfo()
                       .set_layout(layout1))
               .preprocess(PreProcessSteps()
                           .mean(1.)
                           .reverse_channels()
                           )
               ) \
        .build()

    input_data = np.array([[[[1, 2], [3, 4]], [[5, 6],
                                               [7, 8]]]]).astype(np.float32)
    expected_output = np.array([[[[4, 5], [6, 7]],
                                 [[0, 1], [2, 3]]]]).astype(np.float32)

    runtime = get_runtime()
    computation = runtime.computation(function)
    output = computation(input_data)
    assert np.equal(output, expected_output).all()
Exemple #8
0
def test_layout():
    layout = ov.Layout("NCWH")
    layout2 = ov.Layout("NCWH")
    scalar = ov.Layout.scalar()
    scalar2 = ov.Layout.scalar()

    assert layout == layout2
    assert layout != scalar
    assert scalar == scalar2
    assert scalar2 != layout2

    assert str(scalar) == str(scalar2)
    assert not(scalar.has_name("N"))
    assert not(scalar.has_name("C"))
    assert not(scalar.has_name("W"))
    assert not(scalar.has_name("H"))
    assert not(scalar.has_name("D"))

    assert layout.to_string() == layout2.to_string()
    assert layout.has_name("N")
    assert layout.has_name("C")
    assert layout.has_name("W")
    assert layout.has_name("H")
    assert not(layout.has_name("D"))
    assert layout.get_index_by_name("N") == 0
    assert layout.get_index_by_name("C") == 1
    assert layout.get_index_by_name("W") == 2
    assert layout.get_index_by_name("H") == 3

    layout = ov.Layout("NC?")
    layout2 = ov.Layout("N")
    assert layout != layout2
    assert str(layout) != str(layout2)
    assert layout.has_name("N")
    assert layout.has_name("C")
    assert not(layout.has_name("W"))
    assert not(layout.has_name("H"))
    assert not(layout.has_name("D"))
    assert layout.get_index_by_name("N") == 0
    assert layout.get_index_by_name("C") == 1

    layout = ov.Layout("N...C")
    assert layout.has_name("N")
    assert not(layout.has_name("W"))
    assert not(layout.has_name("H"))
    assert not(layout.has_name("D"))
    assert layout.has_name("C")
    assert layout.get_index_by_name("C") == -1

    layout = ov.Layout()
    assert not(layout.has_name("W"))
    assert not(layout.has_name("H"))
    assert not(layout.has_name("D"))
    assert not(layout.has_name("C"))

    layout = ov.Layout("N...C")
    assert layout == "N...C"
    assert layout != "NC?"
Exemple #9
0
def test_ngraph_function_api():
    shape = [2, 2]
    parameter_a = ops.parameter(shape, dtype=np.float32, name="A")
    parameter_b = ops.parameter(shape, dtype=np.float32, name="B")
    parameter_c = ops.parameter(shape, dtype=np.float32, name="C")
    model = (parameter_a + parameter_b) * parameter_c

    assert parameter_a.element_type == Type.f32
    assert parameter_a.partial_shape == PartialShape([2, 2])
    parameter_a.layout = ov.Layout("NCWH")
    assert parameter_a.layout == ov.Layout("NCWH")
    function = Function(model, [parameter_a, parameter_b, parameter_c], "TestFunction")

    function.get_parameters()[1].set_partial_shape(PartialShape([3, 4, 5]))

    ordered_ops = function.get_ordered_ops()
    op_types = [op.get_type_name() for op in ordered_ops]
    assert op_types == ["Parameter", "Parameter", "Parameter", "Add", "Multiply", "Result"]
    assert len(function.get_ops()) == 6
    assert function.get_output_size() == 1
    assert ["A", "B", "C"] == [input.get_node().friendly_name for input in function.inputs]
    assert ["Result"] == [output.get_node().get_type_name() for output in function.outputs]
    assert function.input(0).get_node().friendly_name == "A"
    assert function.output(0).get_node().get_type_name() == "Result"
    assert function.input(tensor_name="A").get_node().friendly_name == "A"
    assert function.output().get_node().get_type_name() == "Result"
    assert function.get_output_op(0).get_type_name() == "Result"
    assert function.get_output_element_type(0) == parameter_a.get_element_type()
    assert list(function.get_output_shape(0)) == [2, 2]
    assert (function.get_parameters()[1].get_partial_shape()) == PartialShape([3, 4, 5])
    assert len(function.get_parameters()) == 3
    results = function.get_results()
    assert len(results) == 1
    assert results[0].get_output_element_type(0) == Type.f32
    assert results[0].get_output_partial_shape(0) == PartialShape([2, 2])
    results[0].layout = ov.Layout("NC")
    assert results[0].layout.to_string() == ov.Layout("NC")
    assert function.get_friendly_name() == "TestFunction"
Exemple #10
0
def test_ngraph_preprocess_mean_vector():
    shape = [2, 2]
    parameter_a = ops.parameter(shape, dtype=np.float32, name="A")
    model = parameter_a
    function = Function(model, [parameter_a], "TestFunction")
    layout = ov.Layout("NCHW")

    p = PrePostProcessor(function)
    p.input().tensor().set_layout(layout)
    p.input().preprocess().mean([1., 2.])
    function = p.build()

    input_data = np.array([[1, 2], [3, 4]]).astype(np.float32)
    expected_output = np.array([[0, 0], [2, 2]]).astype(np.float32)

    runtime = get_runtime()
    computation = runtime.computation(function)
    output = computation(input_data)
    assert np.equal(output, expected_output).all()
Exemple #11
0
def test_ngraph_preprocess_scale_vector():
    shape = [2, 2]
    parameter_a = ops.parameter(shape, dtype=np.float32, name="A")
    model = parameter_a
    function = Function(model, [parameter_a], "TestFunction")
    layout = ov.Layout("NCHW")

    function = PrePostProcessor(function)\
        .input(InputInfo()
               .tensor(InputTensorInfo().set_layout(layout))
               .preprocess(PreProcessSteps()
                           .scale([0.5, 2.])
                           )
               )\
        .build()

    input_data = np.array([[1, 2], [3, 4]]).astype(np.float32)
    expected_output = np.array([[2, 1], [6, 2]]).astype(np.float32)

    runtime = get_runtime()
    computation = runtime.computation(function)
    output = computation(input_data)
    assert np.equal(output, expected_output).all()