Exemplo n.º 1
0
def test_hardmax_special_cases():
    def hardmax_2d(data):
        return np.eye(data.shape[1], dtype=data.dtype)[np.argmax(data, axis=1)]

    np.random.seed(133391)
    data = np.random.rand(3, 4, 5).astype(np.float32)

    # default axis=1
    expected = hardmax_2d(data.reshape(3, 20)).reshape(3, 4, 5)
    node = onnx.helper.make_node("Hardmax", inputs=["x"], outputs=["y"])
    ng_results = run_node(node, [data], opset_version=12)
    assert np.allclose(ng_results, [expected])

    expected = hardmax_2d(data.reshape(12, 5)).reshape(3, 4, 5)
    node = onnx.helper.make_node("Hardmax",
                                 inputs=["x"],
                                 outputs=["y"],
                                 axis=-1)
    ng_results = run_node(node, [data], opset_version=12)
    assert np.allclose(ng_results, [expected])

    with pytest.raises(RuntimeError):
        node = onnx.helper.make_node("Hardmax",
                                     inputs=["x"],
                                     outputs=["y"],
                                     axis=3)
        ng_results = run_node(node, [data], opset_version=12)

    # For multiple occurrences of the maximal values, the first occurrence is selected
    # for one-hot output
    data = np.array([[3, 3, 3, 1]]).astype(np.float32)
    expected = np.array([[1, 0, 0, 0]]).astype(np.float32)
    node = onnx.helper.make_node("Hardmax", inputs=["x"], outputs=["y"])
    ng_results = run_node(node, [data], opset_version=12)
    assert np.allclose(ng_results, [expected])
Exemplo n.º 2
0
def test_unsqueeze():
    data = np.random.randn(3, 4, 5).astype(np.float32)
    expected_output = np.expand_dims(data, axis=0)
    axes = np.array([0]).astype(np.int64)
    node = onnx.helper.make_node("Unsqueeze",
                                 inputs=["x", "axes"],
                                 outputs=["y"])
    ng_results = run_node(node, [data, axes])
    assert np.array_equal(ng_results, [expected_output])

    expected_output = np.reshape(data, [1, 3, 4, 5, 1])
    axes = np.array([0, 4]).astype(np.int64)
    node = onnx.helper.make_node("Unsqueeze",
                                 inputs=["x", "axes"],
                                 outputs=["y"])
    ng_results = run_node(node, [data, axes])
    assert np.array_equal(ng_results, [expected_output])

    expected_output = np.reshape(data, [1, 3, 1, 4, 5])
    axes = np.array([0, 2]).astype(np.int64)
    node = onnx.helper.make_node("Unsqueeze",
                                 inputs=["x", "axes"],
                                 outputs=["y"])
    ng_results = run_node(node, [data, axes])
    assert np.array_equal(ng_results, [expected_output])
Exemplo n.º 3
0
def test_reduce_sum_square(reduction_axes):
    shape = [2, 4, 3, 2]
    np.random.seed(133391)
    input_data = np.random.uniform(-100, 100, shape).astype(np.float32)

    expected = np.sum(np.square(input_data),
                      keepdims=True,
                      axis=reduction_axes)
    node = onnx.helper.make_node("ReduceSumSquare",
                                 inputs=["x"],
                                 outputs=["y"],
                                 axes=reduction_axes)
    ng_result = np.array(run_node(node, [input_data]).pop())
    assert np.array_equal(expected.shape, ng_result.shape)
    assert np.allclose(expected, ng_result)

    expected = np.sum(np.square(input_data),
                      keepdims=False,
                      axis=reduction_axes)
    node = onnx.helper.make_node("ReduceSumSquare",
                                 inputs=["x"],
                                 outputs=["y"],
                                 keepdims=0,
                                 axes=reduction_axes)
    ng_result = np.array(run_node(node, [input_data]).pop())
    assert np.array_equal(expected.shape, ng_result.shape)
    assert np.allclose(expected, ng_result)
Exemplo n.º 4
0
def test_depth_to_space():
    b, c, h, w = shape = (2, 8, 3, 3)
    blocksize = 2
    data = np.random.random_sample(shape).astype(np.float32)
    tmp = np.reshape(data,
                     [b, blocksize, blocksize, c // (blocksize**2), h, w])
    tmp = np.transpose(tmp, [0, 3, 4, 1, 5, 2])
    expected_output = np.reshape(
        tmp, [b, c // (blocksize**2), h * blocksize, w * blocksize])

    node = onnx.helper.make_node("DepthToSpace",
                                 inputs=["x"],
                                 outputs=["y"],
                                 blocksize=blocksize)
    ng_results = run_node(node, [data])
    assert np.array_equal(ng_results, [expected_output])

    # (1, 4, 2, 3) input tensor
    data = np.array([[
        [[0, 1, 2], [3, 4, 5]],
        [[6, 7, 8], [9, 10, 11]],
        [[12, 13, 14], [15, 16, 17]],
        [[18, 19, 20], [21, 22, 23]],
    ]]).astype(np.float32)
    # (1, 1, 4, 6) output tensor
    expected_output = np.array([[[
        [0, 6, 1, 7, 2, 8],
        [12, 18, 13, 19, 14, 20],
        [3, 9, 4, 10, 5, 11],
        [15, 21, 16, 22, 17, 23],
    ]]]).astype(np.float32)

    ng_results = run_node(node, [data])
    assert np.array_equal(ng_results, [expected_output])
Exemplo n.º 5
0
def test_flatten_exception():
    data = np.arange(120).reshape([2, 3, 4, 5])
    node = onnx.helper.make_node("Flatten",
                                 inputs=["x"],
                                 outputs=["y"],
                                 axis=5)

    with pytest.raises(RuntimeError):
        run_node(node, [data])
Exemplo n.º 6
0
def test_split_1d():
    # 1D
    data = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float32)

    node = onnx.helper.make_node("Split",
                                 inputs=["input"],
                                 outputs=["z", "w"],
                                 axis=0)
    expected_outputs = [
        np.array([1.0, 2.0, 3.0]).astype(np.float32),
        np.array([4.0, 5.0, 6.0]).astype(np.float32),
    ]
    ng_results = run_node(node, [data])
    assert all_arrays_equal(ng_results, expected_outputs)

    splits = np.array([2, 3, 1]).astype(np.int64)
    node = onnx.helper.make_node("Split",
                                 inputs=["input", "splits"],
                                 outputs=["y", "z", "w"],
                                 axis=0)
    expected_outputs = [
        np.array([1.0, 2.0]).astype(np.float32),
        np.array([3.0, 4.0, 5.0]).astype(np.float32),
        np.array([6.0]).astype(np.float32),
    ]
    ng_results = run_node(node, [data, splits])
    assert all_arrays_equal(ng_results, expected_outputs)

    # Default values
    data = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float32)

    node = onnx.helper.make_node("Split",
                                 inputs=["input"],
                                 outputs=["y", "z", "w"])
    expected_outputs = [
        np.array([1.0, 2.0]).astype(np.float32),
        np.array([3.0, 4.0]).astype(np.float32),
        np.array([5.0, 6.0]).astype(np.float32),
    ]
    ng_results = run_node(node, [data])
    assert all_arrays_equal(ng_results, expected_outputs)

    splits = np.array([2, 4]).astype(np.int64)
    node = onnx.helper.make_node("Split",
                                 inputs=["input", "splits"],
                                 outputs=["y", "z"],
                                 split=[2, 4])
    expected_outputs = [
        np.array([1.0, 2.0]).astype(np.float32),
        np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),
    ]
    ng_results = run_node(node, [data, splits])
    assert all_arrays_equal(ng_results, expected_outputs)
Exemplo n.º 7
0
def test_transpose():
    data = np.arange(120, dtype=np.int32).reshape([2, 3, 4, 5])

    node = onnx.helper.make_node("Transpose", inputs=["x"], outputs=["y"])
    expected_output = data.T
    ng_results = run_node(node, [data])
    assert np.array_equal(ng_results, [expected_output])

    node = onnx.helper.make_node("Transpose",
                                 inputs=["x"],
                                 outputs=["y"],
                                 perm=(3, 1, 0, 2))
    expected_output = np.transpose(data, axes=(3, 1, 0, 2))
    ng_results = run_node(node, [data])
    assert np.array_equal(ng_results, [expected_output])
Exemplo n.º 8
0
def test_concat():
    a = np.array([[1, 2], [3, 4]], dtype=np.int32)
    b = np.array([[5, 6]], dtype=np.int32)

    node = onnx.helper.make_node("Concat", inputs=["x"], outputs=["z"], axis=0)
    ng_results = run_node(node, [a])
    assert np.array_equal(ng_results, [a])

    expected_output = np.concatenate((a, b), axis=0)
    node = onnx.helper.make_node("Concat",
                                 inputs=["x", "y"],
                                 outputs=["z"],
                                 axis=0)
    ng_results = run_node(node, [a, b])
    assert np.array_equal(ng_results, [expected_output])

    a = np.array([[1, 2], [3, 4]], dtype=np.int32)
    b = np.array([[5, 6]], dtype=np.int32).T
    expected_output = np.concatenate((a, b), axis=1)
    node = onnx.helper.make_node("Concat",
                                 inputs=["x", "y"],
                                 outputs=["z"],
                                 axis=1)
    ng_results = run_node(node, [a, b])
    assert np.array_equal(ng_results, [expected_output])

    test_cases = {
        "1d": ([1, 2], [3, 4]),
        "2d": ([[1, 2], [3, 4]], [[5, 6], [7, 8]]),
        "3d": (
            [[[1, 2], [3, 4]], [[5, 6], [7, 8]]],
            [[[9, 10], [11, 12]], [[13, 14], [15, 16]]],
        ),
    }

    for _, values in test_cases.items():
        values = [np.asarray(v) for v in values]
        for i in range(len(values[0].shape)):
            in_args = ["value" + str(k) for k in range(len(values))]
            node = onnx.helper.make_node(
                "Concat",
                inputs=list(in_args),
                outputs=["output"],
                axis=i,
            )
            expected_output = np.concatenate(values, i)
            ng_results = run_node(node, np.array(values, dtype=np.int32))
            assert np.array_equal(ng_results, [expected_output])
Exemplo n.º 9
0
def import_and_compute(op_type, input_data, **node_attrs):
    data_inputs = [np.array(input_data)]
    node = onnx.helper.make_node(op_type,
                                 inputs=["x"],
                                 outputs=["y"],
                                 **node_attrs)
    return run_node(node, data_inputs).pop()
Exemplo n.º 10
0
def test_pool_global_max(ndarray_1x1x4x4):
    node = onnx.helper.make_node("GlobalMaxPool", inputs=["x"], outputs=["y"])

    x = ndarray_1x1x4x4
    y = np.array([26], dtype=np.float32).reshape([1, 1, 1, 1])

    ng_results = run_node(node, [x])
    assert np.array_equal(ng_results, [y])
Exemplo n.º 11
0
def test_flatten(axis, expected_output):
    data = np.arange(120, dtype=np.int32).reshape([2, 3, 4, 5])
    node = onnx.helper.make_node("Flatten",
                                 inputs=["x"],
                                 outputs=["y"],
                                 axis=axis)
    ng_results = run_node(node, [data])
    assert np.array_equal(ng_results, [expected_output])
Exemplo n.º 12
0
def test_logical_not():
    input_data = np.array([[False, True, True], [False, True, False],
                           [False, False, True]])
    expected_output = np.logical_not(input_data)

    node = onnx.helper.make_node("Not", inputs=["X"], outputs=["Y"])
    ng_results = run_node(node, [input_data])
    assert np.array_equal(ng_results, [expected_output])
Exemplo n.º 13
0
def test_logical(onnx_op, numpy_func, data_type):
    node = onnx.helper.make_node(onnx_op,
                                 inputs=["A", "B"],
                                 outputs=["C"],
                                 broadcast=1)

    input_a = np.array([[0, 1, -1], [0, 1, -1], [0, 1, -1]]).astype(data_type)
    input_b = np.array([[0, 0, 0], [1, 1, 1], [-1, -1, -1]]).astype(data_type)
    expected_output = numpy_func(input_a, input_b)
    ng_results = run_node(node, [input_a, input_b], opset_version=4)
    assert np.array_equal(ng_results, [expected_output])

    input_a = np.array([[0, 1, -1], [0, 1, -1], [0, 1, -1]]).astype(data_type)
    input_b = np.array(1).astype(data_type)
    expected_output = numpy_func(input_a, input_b)
    ng_results = run_node(node, [input_a, input_b], opset_version=4)
    assert np.array_equal(ng_results, [expected_output])
Exemplo n.º 14
0
def test_pool_global_average_3d(ndarray_1x1x4x4):
    x = np.broadcast_to(ndarray_1x1x4x4, (1, 1, 4, 4, 4))

    node = onnx.helper.make_node("GlobalAveragePool",
                                 inputs=["x"],
                                 outputs=["y"])
    y = np.array([18.5], dtype=np.float32).reshape([1, 1, 1, 1, 1])
    ng_results = run_node(node, [x])
    assert np.array_equal(ng_results, [y])
Exemplo n.º 15
0
def test_logsoftmax():
    def logsoftmax_2d(x):
        max_x = np.max(x, axis=1).reshape((-1, 1))
        exp_x = np.exp(x - max_x)
        return x - max_x - np.log(np.sum(exp_x, axis=1).reshape((-1, 1)))

    np.random.seed(133391)
    data = np.random.randn(3, 4, 5).astype(np.float32)

    node = onnx.helper.make_node("LogSoftmax",
                                 inputs=["x"],
                                 outputs=["y"],
                                 axis=0)
    expected = logsoftmax_2d(data.reshape(1, 60)).reshape(3, 4, 5)
    ng_results = run_node(node, [data], opset_version=12)
    assert np.allclose(ng_results, [expected])

    node = onnx.helper.make_node("LogSoftmax",
                                 inputs=["x"],
                                 outputs=["y"],
                                 axis=1)
    expected = logsoftmax_2d(data.reshape(3, 20)).reshape(3, 4, 5)
    ng_results = run_node(node, [data], opset_version=12)
    assert np.allclose(ng_results, [expected])

    # default axis is 1
    node = onnx.helper.make_node("LogSoftmax", inputs=["x"], outputs=["y"])
    ng_results = run_node(node, [data], opset_version=12)
    assert np.allclose(ng_results, [expected])

    node = onnx.helper.make_node("LogSoftmax",
                                 inputs=["x"],
                                 outputs=["y"],
                                 axis=2)
    expected = logsoftmax_2d(data.reshape(12, 5)).reshape(3, 4, 5)
    ng_results = run_node(node, [data], opset_version=12)
    assert np.allclose(ng_results, [expected])

    with pytest.raises(RuntimeError):
        node = onnx.helper.make_node("LogSoftmax",
                                     inputs=["x"],
                                     outputs=["y"],
                                     axis=3)
        ng_results = run_node(node, [data], opset_version=12)
Exemplo n.º 16
0
def test_reshape_opset5_param_err():
    original_shape = [2, 3, 4]
    output_shape = np.array([4, 2, 3], dtype=np.int32)
    input_data = np.random.random_sample(original_shape).astype(np.float32)
    reshape_node = onnx.helper.make_node("Reshape",
                                         inputs=["x", "y"],
                                         outputs=["z"])
    ng_result = run_node(reshape_node, [input_data, output_shape],
                         opset_version=5)
    assert ng_result[0].shape == output_shape
Exemplo n.º 17
0
def test_squeeze():
    data = np.arange(6, dtype=np.int32).reshape([1, 2, 3, 1])
    expected_output = data.reshape([2, 3])

    axes = np.array([0, 3]).astype(np.int64)
    node = onnx.helper.make_node("Squeeze",
                                 inputs=["x", "axes"],
                                 outputs=["y"])
    ng_results = run_node(node, [data, axes])
    assert np.array_equal(ng_results, [expected_output])

    data = np.random.randn(1, 3, 4, 5).astype(np.float32)
    expected_output = np.squeeze(data, axis=0)
    axes = np.array([0]).astype(np.int64)
    node = onnx.helper.make_node("Squeeze",
                                 inputs=["x", "axes"],
                                 outputs=["y"])
    ng_results = run_node(node, [data, axes])
    assert np.array_equal(ng_results, [expected_output])
Exemplo n.º 18
0
def test_reshape():
    input_data = np.arange(2560, dtype=np.int32).reshape([16, 4, 4, 10])
    reshape_node = onnx.helper.make_node("Reshape",
                                         inputs=["x"],
                                         outputs=["y"],
                                         shape=(256, 10))
    expected_output = input_data.reshape([256, 10])

    ng_results = run_node(reshape_node, [input_data], opset_version=4)
    assert np.array_equal(ng_results, [expected_output])
Exemplo n.º 19
0
def test_parametric_relu(x, slope):
    def parametic_relu(x, slope):
        return np.where(x < 0, slope * x, x)

    x, slope = np.array(x).astype(np.float32), np.array(slope).astype(
        np.float32)
    expected_output = parametic_relu(x, slope)
    node = onnx.helper.make_node("PRelu", inputs=["x", "slope"], outputs=["y"])
    output = run_node(node, [x, slope]).pop()
    assert np.allclose(output, expected_output)
Exemplo n.º 20
0
def test_reduce_log_sum_default_axes():
    shape = [2, 4, 3, 2]
    np.random.seed(133391)
    input_data = np.random.uniform(0, 1, shape).astype(np.float32)

    expected = np.log(np.sum(input_data, keepdims=True))
    node = onnx.helper.make_node("ReduceLogSum", inputs=["x"], outputs=["y"])
    ng_result = np.array(run_node(node, [input_data]).pop())
    assert np.array_equal(expected.shape, ng_result.shape)
    assert np.allclose(expected, ng_result)

    expected = np.log(np.sum(input_data, keepdims=False))
    node = onnx.helper.make_node("ReduceLogSum",
                                 inputs=["x"],
                                 outputs=["y"],
                                 keepdims=0)
    ng_result = np.array(run_node(node, [input_data]).pop())
    assert np.array_equal(expected.shape, ng_result.shape)
    assert np.allclose(expected, ng_result)
Exemplo n.º 21
0
def test_pool_average_3d(ndarray_1x1x4x4):
    x = np.broadcast_to(ndarray_1x1x4x4, (1, 1, 4, 4, 4))
    node = onnx.helper.make_node("AveragePool",
                                 inputs=["x"],
                                 outputs=["y"],
                                 kernel_shape=(2, 2, 2),
                                 strides=(2, 2, 2))
    y = np.array([[[13.5, 15.5], [21.5, 23.5]], [[13.5, 15.5], [21.5, 23.5]]],
                 dtype=np.float32).reshape([1, 1, 2, 2, 2])
    ng_results = run_node(node, [x])
    assert np.array_equal(ng_results, [y])
Exemplo n.º 22
0
def test_softsign():
    def softsign(x):
        return x / (1 + np.abs(x))

    np.random.seed(133391)
    data = np.random.randn(3, 4, 5).astype(np.float32)

    node = onnx.helper.make_node("Softsign", inputs=["x"], outputs=["y"])
    expected = softsign(data)
    ng_results = run_node(node, [data])
    assert np.allclose(ng_results, [expected])
Exemplo n.º 23
0
def test_softplus():
    def softplus(x):
        return np.where(x < 20, np.log(np.exp(x) + 1), x)

    np.random.seed(133391)
    data = np.random.randn(3, 4, 5).astype(np.float32)

    node = onnx.helper.make_node("Softplus", inputs=["x"], outputs=["y"])
    expected = softplus(data)
    ng_results = run_node(node, [data])
    assert np.allclose(ng_results, [expected])
Exemplo n.º 24
0
def test_pool_max(ndarray_1x1x4x4):
    node = onnx.helper.make_node("MaxPool",
                                 inputs=["x"],
                                 outputs=["y"],
                                 kernel_shape=(2, 2),
                                 strides=(2, 2))

    x = ndarray_1x1x4x4
    y = np.array([[16, 18], [24, 26]], dtype=np.float32).reshape([1, 1, 2, 2])

    ng_results = run_node(node, [x])
    assert np.array_equal(ng_results, [y])
Exemplo n.º 25
0
def test_pool_average(ndarray_1x1x4x4):
    x = ndarray_1x1x4x4
    node = onnx.helper.make_node("AveragePool",
                                 inputs=["x"],
                                 outputs=["y"],
                                 kernel_shape=(2, 2),
                                 strides=(2, 2))
    y = np.array([[13.5, 15.5], [21.5, 23.5]],
                 dtype=np.float32).reshape([1, 1, 2, 2])
    ng_results = run_node(node, [x])
    assert np.array_equal(ng_results, [y])

    node = onnx.helper.make_node("AveragePool",
                                 inputs=["x"],
                                 outputs=["y"],
                                 kernel_shape=(2, 2),
                                 strides=(2, 2),
                                 pads=(1, 1, 1, 1))
    y = np.array([[11, 12.5, 14], [17, 18.5, 20], [23, 24.5, 26]],
                 dtype=np.float32).reshape([1, 1, 3, 3])
    ng_results = run_node(node, [x])
    assert np.array_equal(ng_results, [y])
Exemplo n.º 26
0
def test_hardsigmoid():
    def hardsigmoid(data, alpha=0.2, beta=0.5):
        return np.clip(alpha * data + beta, 0, 1)

    np.random.seed(133391)
    alpha = np.random.rand()
    beta = np.random.rand()
    data = np.random.rand(3, 4, 5).astype(np.float32)

    expected = hardsigmoid(data, alpha, beta)
    node = onnx.helper.make_node("HardSigmoid",
                                 inputs=["x"],
                                 outputs=["y"],
                                 alpha=alpha,
                                 beta=beta)
    ng_results = run_node(node, [data])
    assert np.allclose(ng_results, [expected])

    expected = hardsigmoid(data)
    node = onnx.helper.make_node("HardSigmoid", inputs=["x"], outputs=["y"])
    ng_results = run_node(node, [data])
    assert np.allclose(ng_results, [expected])
Exemplo n.º 27
0
def test_variadic(onnx_op, numpy_func):
    data = [
        np.array([1, 2, 3], dtype=np.int32),
        np.array([4, 5, 6], dtype=np.int32),
        np.array([7, 8, 9], dtype=np.int32),
    ]
    node = onnx.helper.make_node(onnx_op,
                                 inputs=["data_0", "data_1", "data_2"],
                                 outputs=["y"])
    expected_output = reduce(numpy_func, data)

    ng_results = run_node(node, data)
    assert np.array_equal(ng_results, [expected_output])
Exemplo n.º 28
0
def test_hardmax(axis, dim1, dim2):
    def hardmax_2d(data):
        return np.eye(data.shape[1], dtype=data.dtype)[np.argmax(data, axis=1)]

    np.random.seed(133391)
    data = np.random.rand(3, 4, 5).astype(np.float32)
    expected = hardmax_2d(data.reshape(dim1, dim2)).reshape(3, 4, 5)
    node = onnx.helper.make_node("Hardmax",
                                 inputs=["x"],
                                 outputs=["y"],
                                 axis=axis)
    ng_results = run_node(node, [data], opset_version=12)
    assert np.allclose(ng_results, [expected])
Exemplo n.º 29
0
def test_mean():
    data = [
        np.array([1, 2, 3], dtype=np.int32),
        np.array([4, 5, 6], dtype=np.int32),
        np.array([7, 8, 9], dtype=np.int32),
    ]
    node = onnx.helper.make_node("Mean",
                                 inputs=["data_0", "data_1", "data_2"],
                                 outputs=["y"])
    expected_output = reduce(np.add, data) / len(data)

    ng_results = run_node(node, data)
    assert np.array_equal(ng_results, [expected_output])
Exemplo n.º 30
0
def test_split_2d_splits_input():
    data = np.arange(8, dtype=np.int32).reshape(2, 4)
    splits = np.array([3, 1]).astype(np.int64)
    node = onnx.helper.make_node("Split",
                                 inputs=["x", "splits"],
                                 outputs=["a", "b"],
                                 axis=1)
    expected_outputs = [
        np.array([[0, 1, 2], [4, 5, 6]], dtype=np.int32),
        np.array([[3], [7]], dtype=np.int32),
    ]
    ng_results = run_node(node, [data, splits])
    assert all_arrays_equal(ng_results, expected_outputs)