Ejemplo n.º 1
0
def test_ngraph_preprocess_dump():
    shape = [1, 3, 224, 224]
    parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input")
    model = parameter_a
    function = Model(model, [parameter_a], "TestFunction")

    p = PrePostProcessor(function)
    p.input().tensor()\
        .set_layout(ov.Layout("NHWC"))\
        .set_element_type(Type.u8)\
        .set_spatial_dynamic_shape()
    p.input().preprocess()\
        .convert_element_type(Type.f32)\
        .reverse_channels()\
        .mean([1, 2, 3])\
        .scale([4, 5, 6])\
        .resize(ResizeAlgorithm.RESIZE_LINEAR)
    p.input().model().set_layout(ov.Layout("NCHW"))
    p_str = str(p)
    print(p)
    assert "Pre-processing steps (5):" in p_str
    assert "convert type (f32):" in p_str
    assert "reverse channels:" in p_str
    assert "mean (1,2,3):" in p_str
    assert "scale (4,5,6):" in p_str
    assert "resize to model width/height:" in p_str
    assert "Implicit pre-processing steps (1):" in p_str
    assert "convert layout " + ov.Layout("NCHW").to_string() in p_str
Ejemplo n.º 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 = Model(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()
Ejemplo n.º 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 = Model(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()
Ejemplo n.º 4
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 = Model(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()
Ejemplo n.º 5
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)
Ejemplo n.º 6
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 = Model(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.model().set_layout(layout)
    out = p.output()
    out.tensor().set_layout(layout).set_element_type(Type.f32)
    out.model().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()
Ejemplo n.º 7
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=Type.f32, 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_b.element_type == Type.f32
    assert parameter_a.partial_shape == PartialShape([2, 2])
    parameter_a.layout = ov.Layout("NC")
    assert parameter_a.layout == ov.Layout("NC")
    function = Model(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"
Ejemplo n.º 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?"
Ejemplo n.º 9
0
def test_ngraph_preprocess_set_from_tensor():
    shape = [1, 224, 224, 3]
    inp_shape = [1, 480, 640, 3]
    parameter_a = ops.parameter(shape, dtype=np.float32, name="A")
    parameter_a.set_layout(ov.Layout("NHWC"))
    model = parameter_a
    function = Model(model, [parameter_a], "TestFunction")

    input_data = ov.Tensor(Type.i32, inp_shape)
    p = PrePostProcessor(function)
    inp = p.input()
    inp.tensor().set_from(input_data)
    inp.preprocess().resize(ResizeAlgorithm.RESIZE_LINEAR)
    function = p.build()
    assert function.input().shape == ov.Shape(inp_shape)
    assert function.input().element_type == Type.i32
    assert function.output().shape == ov.Shape(shape)
    assert function.output().element_type == Type.f32
Ejemplo n.º 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 = Model(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()
Ejemplo n.º 11
0
def test_get_and_set_layout():
    shape = [2, 2]
    parameter_a = ops.parameter(shape, dtype=np.float32, name="A")
    parameter_b = ops.parameter(shape, dtype=np.float32, name="B")

    model = Model(parameter_a + parameter_b, [parameter_a, parameter_b])

    assert layout_helpers.get_layout(model.input(0)) == ov.Layout()
    assert layout_helpers.get_layout(model.input(1)) == ov.Layout()

    layout_helpers.set_layout(model.input(0), ov.Layout("CH"))
    layout_helpers.set_layout(model.input(1), ov.Layout("HW"))

    assert layout_helpers.get_layout(model.input(0)) == ov.Layout("CH")
    assert layout_helpers.get_layout(model.input(1)) == ov.Layout("HW")
Ejemplo n.º 12
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 = Model(model, [parameter_a], "TestFunction")
    resize_alg = ResizeAlgorithm.RESIZE_CUBIC
    layout1 = ov.Layout("NCWH")

    p = PrePostProcessor(function)
    inp = p.input()
    inp.tensor().set_layout(layout1)
    inp.preprocess().mean(1.).resize(resize_alg, 3, 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, 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()