コード例 #1
0
ファイル: test_ops_reshape.py プロジェクト: SDxKeeper/dldt
def test_shape_of():
    input_tensor = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                            dtype=np.float32)

    result = run_op_node([input_tensor], ng.shape_of)

    assert np.allclose(result, [3, 3])
コード例 #2
0
ファイル: test_ops_reshape.py プロジェクト: SDxKeeper/dldt
def test_strided_slice():
    input_tensor = np.arange(2 * 3 * 4, dtype=np.float32).reshape((2, 3, 4))
    begin = np.array([1, 0], dtype=np.int32)
    end = np.array([0, 0], dtype=np.int32)
    strides = np.array([1, 1], dtype=np.int32)
    begin_mask = np.array([0, 0, 0], dtype=np.int32)
    end_mask = np.array([0, 0, 0], dtype=np.int32)
    new_axis_mask = np.array([0, 1, 0], dtype=np.int32)
    shrink_axis_mask = np.array([1, 0, 0], dtype=np.int32)
    ellipsis_mask = np.array([0, 0, 0], dtype=np.int32)

    result = run_op_node(
        [input_tensor],
        ng.strided_slice,
        begin,
        end,
        strides,
        begin_mask,
        end_mask,
        new_axis_mask,
        shrink_axis_mask,
        ellipsis_mask,
    )

    expected = np.array([12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
                        dtype=np.float32).reshape((1, 3, 4))

    assert np.allclose(result, expected)
コード例 #3
0
def test_broadcast_1():
    input_data = np.array([1, 2, 3], dtype=np.int32)

    new_shape = [3, 3]
    expected = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
    result = run_op_node([input_data], ng.broadcast, new_shape)
    assert np.allclose(result, expected)
コード例 #4
0
def test_convert_to_uint(destination_type, expected_type):
    np.random.seed(133391)
    input_data = np.ceil(np.random.rand(2, 3, 4) * 16).astype(np.float32)
    expected = np.array(input_data, dtype=expected_type)
    result = run_op_node([input_data], ng.convert, destination_type)
    assert np.allclose(result, expected)
    assert np.array(result).dtype == expected_type
コード例 #5
0
def test_range():
    start = 5
    stop = 35
    step = 5

    result = run_op_node([start, stop, step], ng.range)
    assert np.allclose(result, [5, 10, 15, 20, 25, 30])
コード例 #6
0
def test_reduce_mean_op(ng_api_helper, numpy_function, reduction_axes):
    shape = [2, 4, 3, 2]
    np.random.seed(133391)
    input_data = np.random.randn(*shape).astype(np.float32)

    expected = numpy_function(input_data, axis=tuple(reduction_axes))
    result = run_op_node([input_data], ng_api_helper, reduction_axes)
    assert np.allclose(result, expected)
コード例 #7
0
ファイル: test_ops_unary.py プロジェクト: SDxKeeper/dldt
def test_unary_op_array(ng_api_fn, numpy_fn, range_start, range_end):
    np.random.seed(133391)
    input_data = (range_start + np.random.rand(2, 3, 4) *
                  (range_end - range_start)).astype(np.float32)
    expected = numpy_fn(input_data)

    result = run_op_node([input_data], ng_api_fn)
    assert np.allclose(result, expected, rtol=0.001)
コード例 #8
0
def test_select():
    cond = np.array([[False, False], [True, False], [True, True]])
    then_node = np.array([[-1, 0], [1, 2], [3, 4]], dtype=np.int32)
    else_node = np.array([[11, 10], [9, 8], [7, 6]], dtype=np.int32)
    excepted = np.array([[11, 10], [1, 8], [3, 4]], dtype=np.int32)

    result = run_op_node([cond, then_node, else_node], ng.select)
    assert np.allclose(result, excepted)
コード例 #9
0
def test_multiply():
    A = np.arange(48, dtype=np.int32).reshape((8, 1, 6, 1))
    B = np.arange(35, dtype=np.int32).reshape((7, 1, 5))

    expected = np.multiply(A, B)
    result = run_op_node([A, B], ng.multiply)

    assert np.allclose(result, expected)
コード例 #10
0
def test_convert_to_float(destination_type, rand_range, in_dtype,
                          expected_type):
    np.random.seed(133391)
    input_data = np.random.randint(*rand_range, size=(2, 2), dtype=in_dtype)
    expected = np.array(input_data, dtype=expected_type)
    result = run_op_node([input_data], ng.convert, destination_type)
    assert np.allclose(result, expected)
    assert np.array(result).dtype == expected_type
コード例 #11
0
def test_power_v1():
    A = np.arange(48, dtype=np.float32).reshape((8, 1, 6, 1))
    B = np.arange(20, dtype=np.float32).reshape((4, 1, 5))

    expected = np.power(A, B)
    result = run_op_node([A, B], ng.power)

    assert np.allclose(result, expected)
コード例 #12
0
def test_broadcast_3():
    input_data = np.array([1, 2, 3], dtype=np.int32)
    new_shape = [3, 3]
    axis_mapping = [0]
    expected = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

    result = run_op_node([input_data], ng.broadcast, new_shape, axis_mapping,
                         "EXPLICIT")
    assert np.allclose(result, expected)
コード例 #13
0
def test_tile():
    input_tensor = np.arange(6, dtype=np.int32).reshape((2, 1, 3))
    repeats = np.array([2, 1], dtype=np.int32)

    result = run_op_node([input_tensor], ng.tile, repeats)

    expected = np.array([0, 1, 2, 0, 1, 2, 3, 4, 5, 3, 4, 5]).reshape((2, 2, 3))

    assert np.allclose(result, expected)
コード例 #14
0
ファイル: test_ops_unary.py プロジェクト: SDxKeeper/dldt
def test_softmax():
    axis = 1
    input_tensor = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)

    result = run_op_node([input_tensor], ng.softmax, axis)

    expected = [[0.09003056, 0.24472842, 0.6652409],
                [0.09003056, 0.24472842, 0.6652409]]

    assert np.allclose(result, expected)
コード例 #15
0
ファイル: test_ops_reshape.py プロジェクト: SDxKeeper/dldt
def test_reshape_v1():
    A = np.arange(1200, dtype=np.float32).reshape((2, 5, 5, 24))
    shape = np.array([0, -1, 4], dtype=np.int32)
    special_zero = True

    expected_shape = np.array([2, 150, 4])
    expected = np.reshape(A, expected_shape)
    result = run_op_node([A], ng.reshape, shape, special_zero)

    assert np.allclose(result, expected)
コード例 #16
0
def test_one_hot():
    data = np.array([0, 1, 2], dtype=np.int32)
    depth = 2
    on_value = 5
    off_value = 10
    axis = -1
    excepted = [[5, 10], [10, 5], [10, 10]]

    result = run_op_node([data, depth, on_value, off_value], ng.one_hot, axis)
    assert np.allclose(result, excepted)
コード例 #17
0
ファイル: test_ops_reshape.py プロジェクト: SDxKeeper/dldt
def test_transpose():
    input_tensor = np.arange(3 * 3 * 224 * 224, dtype=np.int32).reshape(
        (3, 3, 224, 224))
    input_order = np.array([0, 2, 3, 1], dtype=np.int32)

    result = run_op_node([input_tensor], ng.transpose, input_order)

    expected = np.transpose(input_tensor, input_order)

    assert np.allclose(result, expected)
コード例 #18
0
ファイル: test_ops_unary.py プロジェクト: SDxKeeper/dldt
def test_sigmoid():
    input_data = np.array([-3.14, -1.0, 0.0, 2.71001, 1000.0],
                          dtype=np.float32)
    result = run_op_node([input_data], ng.sigmoid)

    def sigmoid(x):
        return 1.0 / (1.0 + np.exp(-x))

    expected = np.array(list(map(sigmoid, input_data)))

    assert np.allclose(result, expected)
コード例 #19
0
def test_gather():
    input_data = np.array([1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2],
                          np.float32).reshape((3, 3))
    input_indices = np.array([0, 2], np.int32).reshape(1, 2)
    input_axis = np.array([1], np.int32)

    expected = np.array([1.0, 1.2, 2.0, 2.2, 3.0, 3.2],
                        dtype=np.float32).reshape((3, 1, 2))

    result = run_op_node([input_data], ng.gather, input_indices, input_axis)
    assert np.allclose(result, expected)
コード例 #20
0
def test_gather_batch_dims_1_negative_indices():

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

    input_indices = np.array([[0, 1, -2], [-2, 0, 0]], np.int32)
    input_axis = np.array([1], np.int32)
    batch_dims = 1

    expected = np.array([[1, 2, 4], [9, 6, 6]], np.float32)

    result = run_op_node([input_data], ng.gather, input_indices, input_axis,
                         batch_dims)
    assert np.allclose(result, expected)
コード例 #21
0
def test_batch_norm_inference():
    data = np.array([[1.0, 2.0, 3.0], [-1.0, -2.0, -3.0]], dtype=np.float32)
    gamma = np.array([2.0, 3.0, 4.0], dtype=np.float32)
    beta = np.array([0.0, 0.0, 0.0], dtype=np.float32)
    mean = np.array([0.0, 0.0, 0.0], dtype=np.float32)
    variance = np.array([1.0, 1.0, 1.0], dtype=np.float32)
    epsilon = 9.99e-06
    excepted = np.array([[2.0, 6.0, 12.0], [-2.0, -6.0, -12.0]],
                        dtype=np.float32)

    result = run_op_node([data, gamma, beta, mean, variance],
                         ng.batch_norm_inference, epsilon)

    assert np.allclose(result, excepted)
コード例 #22
0
def test_matmul(shape_a, shape_b, transpose_a, transpose_b):
    np.random.seed(133391)
    left_input = -100.0 + np.random.rand(*shape_a).astype(np.float32) * 200.0
    right_input = -100.0 + np.random.rand(*shape_b).astype(np.float32) * 200.0

    result = run_op_node([left_input, right_input], ng.matmul, transpose_a,
                         transpose_b)

    if transpose_a:
        left_input = np.transpose(left_input)
    if transpose_b:
        right_input = np.transpose(right_input)

    expected = np.matmul(left_input, right_input)
    assert np.allclose(result, expected)
コード例 #23
0
ファイル: test_ops_unary.py プロジェクト: SDxKeeper/dldt
def test_round_away():
    float_dtype = np.float32
    data = ng.parameter(Shape([3, 10]), dtype=float_dtype, name="data")

    node = ng.round(data, "HALF_AWAY_FROM_ZERO")
    assert node.get_type_name() == "Round"
    assert node.get_output_size() == 1
    assert list(node.get_output_shape(0)) == [3, 10]
    assert node.get_output_element_type(0) == Type.f32

    input_tensor = np.array([-2.5, -1.5, -0.5, 0.5, 0.9, 1.5, 2.3, 2.5, 3.5],
                            dtype=np.float32)
    expected = [-3.0, -2.0, -1.0, 1.0, 1.0, 2.0, 2.0, 3.0, 4.0]

    result = run_op_node([input_tensor], ng.round, "HALF_AWAY_FROM_ZERO")
    assert np.allclose(result, expected)
コード例 #24
0
def test_convolution_v1():
    input_tensor = np.arange(-128, 128, 1, dtype=np.float32).reshape(1, 1, 16, 16)
    filters = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)
    filters[0, 0, 0, 0] = -1
    filters[0, 0, 1, 1] = -1
    filters[0, 0, 2, 2] = -1
    filters[0, 0, 0, 2] = -1
    filters[0, 0, 2, 0] = -1
    strides = np.array([1, 1])
    pads_begin = np.array([0, 0])
    pads_end = np.array([0, 0])
    dilations = np.array([1, 1])

    result = run_op_node([input_tensor, filters], ng.convolution, strides, pads_begin, pads_end, dilations)

    expected = convolution2d(input_tensor[0, 0], filters[0, 0]).reshape(1, 1, 14, 14)

    assert np.allclose(result, expected)
コード例 #25
0
def test_mvn_no_variance():
    data = np.array([
        1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6,
        7, 8, 9
    ],
                    dtype=np.float32).reshape([1, 3, 3, 3])
    axes = np.array([2, 3], dtype=np.int64)
    epsilon = 1e-9
    normalize_variance = False
    eps_mode = "outside_sqrt"
    excepted = np.array([
        -4, -3, -2, -1, 0, 1, 2, 3, 4, -4, -3, -2, -1, 0, 1, 2, 3, 4, -4, -3,
        -2, -1, 0, 1, 2, 3, 4
    ],
                        dtype=np.float32).reshape([1, 3, 3, 3])

    result = run_op_node([data], ng.mvn, axes, normalize_variance, epsilon,
                         eps_mode)

    assert np.allclose(result, excepted)
コード例 #26
0
def test_lrn_factory():
    alpha = 0.0002
    beta = 0.5
    bias = 2.0
    nsize = 3
    axis = np.array([1], dtype=np.int32)
    x = np.array(
        [[
            [
                [0.31403765, -0.16793324, 1.388258, -0.6902954],
                [-0.3994045, -0.7833511, -0.30992958, 0.3557573],
                [-0.4682631, 1.1741459, -2.414789, -0.42783254],
            ],
            [
                [-0.82199496, -0.03900861, -0.43670088, -0.53810567],
                [-0.10769883, 0.75242394, -0.2507971, 1.0447186],
                [-1.4777364, 0.19993274, 0.925649, -2.282516],
            ],
        ]],
        dtype=np.float32,
    )
    excepted = np.array(
        [[
            [
                [0.22205527, -0.11874668, 0.98161197, -0.4881063],
                [-0.2824208, -0.553902, -0.21915273, 0.2515533],
                [-0.33109877, 0.8302269, -1.7073234, -0.3024961],
            ],
            [
                [-0.5812307, -0.02758324, -0.30878326, -0.38049328],
                [-0.07615435, 0.53203356, -0.17733987, 0.7387126],
                [-1.0448756, 0.14137045, 0.6544598, -1.6138376],
            ],
        ]],
        dtype=np.float32,
    )
    result = run_op_node([x], ng.lrn, axis, alpha, beta, bias, nsize)

    assert np.allclose(result, excepted)
コード例 #27
0
def test_mvn():
    data = np.array([
        1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6,
        7, 8, 9
    ],
                    dtype=np.float32).reshape([1, 3, 3, 3])
    axes = np.array([2, 3], dtype=np.int64)
    epsilon = 1e-9
    normalize_variance = True
    eps_mode = "outside_sqrt"
    excepted = np.array([
        -1.5491934, -1.161895, -0.7745967, -0.38729835, 0., 0.38729835,
        0.7745967, 1.161895, 1.5491934, -1.5491934, -1.161895, -0.7745967,
        -0.38729835, 0., 0.38729835, 0.7745967, 1.161895, 1.5491934,
        -1.5491934, -1.161895, -0.7745967, -0.38729835, 0., 0.38729835,
        0.7745967, 1.161895, 1.5491934
    ],
                        dtype=np.float32).reshape([1, 3, 3, 3])

    result = run_op_node([data], ng.mvn, axes, normalize_variance, epsilon,
                         eps_mode)

    assert np.allclose(result, excepted)
コード例 #28
0
def test_convert_to_bool(destination_type, input_data):
    expected = np.array(input_data, dtype=bool)
    result = run_op_node([input_data], ng.convert, destination_type)
    assert np.allclose(result, expected)
    assert np.array(result).dtype == bool
コード例 #29
0
def test_broadcast_2():
    input_data = np.arange(4, dtype=np.int32)
    new_shape = [3, 4, 2, 4]
    expected = np.broadcast_to(input_data, new_shape)
    result = run_op_node([input_data], ng.broadcast, new_shape)
    assert np.allclose(result, expected)
コード例 #30
0
def test_result():
    node = np.array([[11, 10], [1, 8], [3, 4]])
    result = run_op_node([node], ng.result)
    assert np.allclose(result, node)