示例#1
0
def test_lrn():
    input_image_shape = (2, 3, 2, 1)
    input_image = np.arange(int(
        np.prod(input_image_shape))).reshape(input_image_shape).astype("f")
    axes = np.array([1], dtype=np.int64)
    runtime = get_runtime()
    model = ov.lrn(ov.constant(input_image),
                   ov.constant(axes),
                   alpha=1.0,
                   beta=2.0,
                   bias=1.0,
                   size=3)
    computation = runtime.computation(model)
    result = computation()
    assert np.allclose(
        result,
        np.array(
            [
                [[[0.0], [0.05325444]], [[0.03402646], [0.01869806]],
                 [[0.06805293], [0.03287071]]],
                [[[0.00509002], [0.00356153]], [[0.00174719], [0.0012555]],
                 [[0.00322708], [0.00235574]]],
            ],
            dtype=np.float32,
        ),
    )

    # Test LRN default parameter values
    model = ov.lrn(ov.constant(input_image), ov.constant(axes))
    computation = runtime.computation(model)
    result = computation()
    assert np.allclose(
        result,
        np.array(
            [
                [[[0.0], [0.35355338]], [[0.8944272], [1.0606602]],
                 [[1.7888544], [1.767767]]],
                [[[0.93704253], [0.97827977]], [[1.2493901], [1.2577883]],
                 [[1.5617375], [1.5372968]]],
            ],
            dtype=np.float32,
        ),
    )
示例#2
0
def test_adaptive_max_pool():
    runtime = get_runtime()
    input = np.reshape([
        0, 4, 1, 3, -2, -5, -2, -2, 1, -3, 1, -3, -4, 0, -2, 1, -1, -2, 3, -1,
        -3, -1, -2, 3, 4, -3, -4, 1, 2, 0, -4, -5, -2, -2, -3, 2, 3, 1, -5, 2,
        -4, -2
    ], (2, 3, 7))
    input_tensor = ov.constant(input)
    output_shape = ov.constant(np.array([3], dtype=np.int32))

    adaptive_pool_node = ov.adaptive_max_pool(input_tensor, output_shape)
    computation = runtime.computation(adaptive_pool_node)
    adaptive_pool_results = computation()
    expected_results = np.reshape(
        [4, 3, -2, 1, 1, 0, 1, 3, 3, 3, 4, 1, 2, -2, -2, 3, 2, 2], (2, 3, 3))

    expected_indices = np.reshape(
        [1, 3, 4, 1, 3, 6, 1, 4, 4, 2, 3, 6, 0, 4, 4, 1, 4, 4], (2, 3, 3))

    assert np.allclose(adaptive_pool_results,
                       [expected_results, expected_indices])
示例#3
0
def test_constant_folding():
    node_constant = ov.constant(
        np.array([[0.0, 0.1, -0.1], [-2.5, 2.5, 3.0]], dtype=np.float32))
    node_ceil = ov.ceiling(node_constant)
    func = Function(node_ceil, [], "TestFunction")

    assert count_ops_of_type(func, node_ceil) == 1
    assert count_ops_of_type(func, node_constant) == 1

    pass_manager = Manager()
    pass_manager.register_pass("ConstantFolding")
    pass_manager.run_passes(func)

    assert count_ops_of_type(func, node_ceil) == 0
    assert count_ops_of_type(func, node_constant) == 1

    new_const = func.get_results()[0].input(0).get_source_output().get_node()

    values_out = new_const.get_vector()
    values_expected = [0.0, 1.0, 0.0, -2.0, 3.0, 3.0]

    assert np.allclose(values_out, values_expected)
示例#4
0
def test_group_convolution_backprop_data_output_shape():
    runtime = get_runtime()

    data_shape = [1, 1, 1, 10]
    filters_shape = [1, 1, 1, 1, 5]
    strides = [1, 1]

    data_node = ov.parameter(data_shape, name="Data", dtype=np.float32)
    filters_node = ov.parameter(filters_shape,
                                name="Filters",
                                dtype=np.float32)
    output_shape_node = ov.constant(np.array([1, 14], dtype=np.int64))

    model = ov.group_convolution_backprop_data(data_node,
                                               filters_node,
                                               strides,
                                               output_shape_node,
                                               auto_pad="same_upper")

    data_value = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
                          dtype=np.float32).reshape(data_shape)

    filters_value = np.array([1.0, 2.0, 3.0, 2.0, 1.0],
                             dtype=np.float32).reshape(filters_shape)

    computation = runtime.computation(model, data_node, filters_node)
    result = computation(data_value, filters_value)

    expected = np.array(
        [
            0.0, 1.0, 4.0, 10.0, 18.0, 27.0, 36.0, 45.0, 54.0, 63.0, 62.0,
            50.0, 26.0, 9.0
        ],
        dtype=np.float32,
    ).reshape(1, 1, 1, 14)

    assert np.allclose(result, expected)