Exemple #1
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(ValueError):
        convert_and_calculate(node, [data], [data])
Exemple #2
0
def test_logical(onnx_op, numpy_func):
    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]])
    input_b = np.array([[0, 0, 0], [1, 1, 1], [-1, -1, -1]])
    expected_output = numpy_func(input_a, input_b)
    ng_results = convert_and_calculate(node, [input_a, input_b], [expected_output])
    assert np.array_equal(ng_results, [expected_output])

    input_a = np.array([[0, 1, -1], [0, 1, -1], [0, 1, -1]])
    input_b = np.array(1)
    expected_output = numpy_func(input_a, input_b)
    ng_results = convert_and_calculate(node, [input_a, input_b], [expected_output])
    assert np.array_equal(ng_results, [expected_output])
def test_batch_norm_test_mode():
    data = np.arange(48).reshape(1, 3, 4, 4)
    scale = np.ones((3,)).astype(np.float32)  # Gamma
    bias = np.zeros((3,)).astype(np.float32)  # Beta
    mean = np.mean(data, axis=(0, 2, 3))
    var = np.var(data, axis=(0, 2, 3))

    expected_output = np.array(
        [[[[-1.62694025, -1.41001487, -1.19308949, -0.97616416],
           [-0.75923878, -0.54231346, -0.32538807, -0.10846269],
           [0.10846269, 0.32538807, 0.54231334, 0.75923872],
           [0.9761641, 1.19308949, 1.41001487, 1.62694025]],

          [[-1.62694049, -1.41001511, -1.19308972, -0.97616434],
           [-0.7592392, -0.54231358, -0.32538843, -0.10846281],
           [0.10846233, 0.32538795, 0.5423131, 0.75923872],
           [0.97616386, 1.19308949, 1.41001463, 1.62694025]],

          [[-1.62694025, -1.41001511, -1.19308949, -0.97616434],
           [-0.75923872, -0.54231358, -0.32538795, -0.10846233],
           [0.10846233, 0.32538795, 0.54231358, 0.7592392],
           [0.97616386, 1.19308949, 1.41001511, 1.62694073]]]], dtype=np.float32)

    node = make_batch_norm_node(is_test=1, spatial=1)
    result = convert_and_calculate(node, [data, scale, bias, mean, var], [expected_output])
    assert np.isclose(result, expected_output).all()

    scale = np.broadcast_to(0.1, (3,)).astype(np.float32)  # Gamma
    bias = np.broadcast_to(1, (3,)).astype(np.float32)  # Beta

    expected_output = np.array(
        [[[[0.83730596, 0.85899848, 0.88069105, 0.90238357],
           [0.92407608, 0.94576865, 0.96746117, 0.98915374],
           [1.01084626, 1.03253877, 1.05423129, 1.07592392],
           [1.09761643, 1.11930895, 1.14100146, 1.16269398]],

          [[0.83730596, 0.85899854, 0.88069105, 0.90238357],
           [0.92407608, 0.94576865, 0.96746117, 0.98915374],
           [1.01084626, 1.03253877, 1.05423141, 1.07592392],
           [1.09761643, 1.11930895, 1.14100146, 1.16269398]],

          [[0.83730596, 0.85899848, 0.88069105, 0.90238357],
           [0.92407614, 0.94576865, 0.96746117, 0.98915374],
           [1.01084626, 1.03253877, 1.05423141, 1.07592392],
           [1.09761643, 1.11930895, 1.14100146, 1.16269398]]]], dtype=np.float32)

    node = make_batch_norm_node(is_test=1, spatial=1)
    result = convert_and_calculate(node, [data, scale, bias, mean, var], [expected_output])
    assert np.isclose(result, expected_output).all()
Exemple #4
0
def test_transpose():
    data = np.arange(120).reshape(2, 3, 4, 5)

    node = onnx.helper.make_node('Transpose', inputs=['x'], outputs=['y'])
    expected_output = data.T
    ng_results = convert_and_calculate(node, [data], [expected_output])
    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 = convert_and_calculate(node, [data], [expected_output])
    assert np.array_equal(ng_results, [expected_output])
Exemple #5
0
def test_mean():
    data = [np.array([1, 2, 3]), np.array([4, 5, 6]), np.array([7, 8, 9])]
    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 = convert_and_calculate(node, data, [expected_output])
    assert np.array_equal(ng_results, [expected_output])
Exemple #6
0
def test_logical_not():
    input_data = np.array([[0, 1, -1], [0, 1, -1], [0, 1, -1]])
    expected_output = np.logical_not(input_data)

    node = onnx.helper.make_node('Not', inputs=['X'], outputs=['Y'])
    ng_results = convert_and_calculate(node, [input_data], [expected_output])
    assert np.array_equal(ng_results, [expected_output])
Exemple #7
0
def test_variadic(onnx_op, numpy_func):
    data = [np.array([1, 2, 3]), np.array([4, 5, 6]), np.array([7, 8, 9])]
    node = onnx.helper.make_node(onnx_op, inputs=['data_0', 'data_1', 'data_2'], outputs=['y'])
    expected_output = reduce(numpy_func, data)

    ng_results = convert_and_calculate(node, data, [expected_output])
    assert np.array_equal(ng_results, [expected_output])
Exemple #8
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 = convert_and_calculate(node, [x], [y])
    assert np.array_equal(ng_results, [y])
Exemple #9
0
def test_flatten(axis, expected_output):
    data = np.arange(120).reshape(2, 3, 4, 5)
    node = onnx.helper.make_node('Flatten',
                                 inputs=['x'],
                                 outputs=['y'],
                                 axis=axis)
    ng_results = convert_and_calculate(node, [data], [expected_output])
    assert np.array_equal(ng_results, [expected_output])
def test_parametric_relu(x, slope):
    def parametic_relu(x, slope):
        return np.where(x < 0, slope * x, x)

    x, slope = np.array(x), np.array(slope)
    expected_output = parametic_relu(x, slope)
    node = onnx.helper.make_node('PRelu', inputs=['x', 'slope'], outputs=['y'])
    output = convert_and_calculate(node, [x, slope], [expected_output]).pop()
    assert np.allclose(output, expected_output)
Exemple #11
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 = convert_and_calculate(node, [x], [y])
    assert np.array_equal(ng_results, [y])
Exemple #12
0
def import_and_compute(op_type, input_data_left, input_data_right,
                       **node_attributes):
    input_data_left = np.array(input_data_left)
    input_data_right = np.array(input_data_right)
    node = onnx.helper.make_node(op_type,
                                 inputs=['x', 'y'],
                                 outputs=['z'],
                                 **node_attributes)
    return convert_and_calculate(node, [input_data_left, input_data_right],
                                 [input_data_left])[0]
Exemple #13
0
def test_reshape():
    data = np.arange(2560).reshape(16, 4, 4, 10)
    node = onnx.helper.make_node('Reshape',
                                 inputs=['x'],
                                 outputs=['y'],
                                 shape=(256, 10))
    expected_output = data.reshape(256, 10)

    ng_results = convert_and_calculate(node, [data], [expected_output])
    assert np.array_equal(ng_results, [expected_output])
Exemple #14
0
def test_squeeze():
    data = np.arange(6).reshape(1, 2, 3, 1)
    expected_output = data.reshape(2, 3)

    node = onnx.helper.make_node('Squeeze',
                                 inputs=['x'],
                                 outputs=['y'],
                                 axes=[0, 3])
    ng_results = convert_and_calculate(node, [data], [expected_output])
    assert np.array_equal(ng_results, [expected_output])
Exemple #15
0
def test_concat():
    a = np.array([[1, 2], [3, 4]])
    b = np.array([[5, 6]])
    expected_output = np.concatenate((a, b), axis=0)
    node = onnx.helper.make_node('Concat',
                                 inputs=['x', 'y'],
                                 outputs=['z'],
                                 axis=0)
    ng_results = convert_and_calculate(node, [a, b], [expected_output])
    assert np.array_equal(ng_results, [expected_output])

    a = np.array([[1, 2], [3, 4]])
    b = np.array([[5, 6]]).T
    expected_output = np.concatenate((a, b), axis=1)
    node = onnx.helper.make_node('Concat',
                                 inputs=['x', 'y'],
                                 outputs=['z'],
                                 axis=1)
    ng_results = convert_and_calculate(node, [a, b], [expected_output])
    assert np.array_equal(ng_results, [expected_output])
Exemple #16
0
def test_padding():
    node = onnx.helper.make_node('Pad',
                                 inputs=['x'],
                                 outputs=['y'],
                                 pads=[1, 1, 1, 1])
    x = np.ones((2, 2), dtype=np.float32)
    y = np.pad(x, pad_width=1, mode='constant')

    ng_results = convert_and_calculate(node, [x], [y])
    assert np.array_equal(ng_results, [y])

    node = onnx.helper.make_node('Pad',
                                 inputs=['x'],
                                 outputs=['y'],
                                 mode='constant',
                                 pads=[0, 0, 1, 3, 0, 0, 2, 4])
    x = np.random.randn(1, 3, 4, 5).astype(np.float32)
    y = np.pad(x, pad_width=((0, 0), (0, 0), (1, 2), (3, 4)), mode='constant')

    ng_results = convert_and_calculate(node, [x], [y])
    assert np.array_equal(ng_results, [y])
Exemple #17
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 = convert_and_calculate(node, [x], [y])
    assert np.array_equal(ng_results, [y])
Exemple #18
0
def test_slice():
    data = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

    expected_output = np.array([[5, 6, 7]])
    node = onnx.helper.make_node('Slice',
                                 inputs=['x'],
                                 outputs=['y'],
                                 axes=[0, 1],
                                 starts=[1, 0],
                                 ends=[2, 3])
    ng_results = convert_and_calculate(node, [data], [expected_output])
    assert np.array_equal(ng_results, [expected_output])

    expected_output = np.array([[1, 2, 3, 4]])
    node = onnx.helper.make_node('Slice',
                                 inputs=['x'],
                                 outputs=['y'],
                                 starts=[0],
                                 ends=[-1])
    ng_results = convert_and_calculate(node, [data], [expected_output])
    assert np.array_equal(ng_results, [expected_output])
Exemple #19
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 = convert_and_calculate(node, [x], [y])

    assert np.array_equal(ng_results, [y])
Exemple #20
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 = convert_and_calculate(node, [x], [y])
    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 = convert_and_calculate(node, [x], [y])
    assert np.array_equal(ng_results, [y])
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 convert_and_calculate(node, data_inputs, data_inputs).pop()
Exemple #22
0
def test_log(input_data):
    expected_output = np.log(input_data)
    node = onnx.helper.make_node('Log', inputs=['x'], outputs=['y'])
    ng_results = convert_and_calculate(node, [input_data], [expected_output])
    assert np.allclose(ng_results, [expected_output])
Exemple #23
0
def test_floor(input_data):
    expected_output = np.floor(input_data)
    node = onnx.helper.make_node('Floor', inputs=['x'], outputs=['y'])
    ng_results = convert_and_calculate(node, [input_data], [expected_output])
    assert np.array_equal(ng_results, [expected_output])
Exemple #24
0
def test_split(node, expected_output):
    data = np.arange(8).reshape(2, 4)
    ng_results = convert_and_calculate(node, [data], expected_output)
    assert all_arrays_equal(ng_results, expected_output)