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])
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]) data = np.random.randn(1, 3, 4, 5).astype(np.float32) expected_output = np.squeeze(data, axis=0) node = onnx.helper.make_node('Squeeze', inputs=['x'], outputs=['y'], axes=[0]) ng_results = convert_and_calculate(node, [data], [expected_output]) assert np.array_equal(ng_results, [expected_output])
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()
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])
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])
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()
def test_unsqueeze(): data = np.random.randn(3, 4, 5).astype(np.float32) expected_output = np.expand_dims(data, axis=0) node = onnx.helper.make_node('Unsqueeze', inputs=['x'], outputs=['y'], axes=[0]) ng_results = convert_and_calculate(node, [data], [expected_output]) assert np.array_equal(ng_results, [expected_output]) expected_output = np.reshape(data, [1, 3, 4, 5, 1]) node = onnx.helper.make_node('Unsqueeze', inputs=['x'], outputs=['y'], axes=[0, 4]) ng_results = convert_and_calculate(node, [data], [expected_output]) assert np.array_equal(ng_results, [expected_output]) expected_output = np.reshape(data, [1, 3, 1, 4, 5]) node = onnx.helper.make_node('Unsqueeze', inputs=['x'], outputs=['y'], axes=[0, 2]) ng_results = convert_and_calculate(node, [data], [expected_output]) assert np.array_equal(ng_results, [expected_output])
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])
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)
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])
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])
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])
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]
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])
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])
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])
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 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([[2, 3, 4]]) node = onnx.helper.make_node('Slice', inputs=['x'], outputs=['y'], starts=[0, 1], ends=[-1, 1000]) ng_results = convert_and_calculate(node, [data], [expected_output]) assert np.array_equal(ng_results, [expected_output]) node = onnx.helper.make_node('Slice', inputs=['x'], outputs=['y'], axes=[0, 1], starts=[0, 0], ends=[3, 10]) data = np.random.randn(20, 10, 5).astype(np.float32) expected_output = data[0:3, 0:10] ng_results = convert_and_calculate(node, [data], [expected_output]) assert np.array_equal(ng_results, [expected_output]) # default axes node = onnx.helper.make_node('Slice', inputs=['x'], outputs=['y'], starts=[0, 0, 3], ends=[20, 10, 4]) data = np.random.randn(20, 10, 5).astype(np.float32) expected_output = data[:, :, 3:4] ng_results = convert_and_calculate(node, [data], [expected_output]) assert np.array_equal(ng_results, [expected_output]) # end out of bounds node = onnx.helper.make_node('Slice', inputs=['x'], outputs=['y'], axes=[1], starts=[1], ends=[1000]) data = np.random.randn(20, 10, 5).astype(np.float32) expected_output = data[:, 1:1000] ng_results = convert_and_calculate(node, [data], [expected_output]) assert np.array_equal(ng_results, [expected_output]) # negative value node = onnx.helper.make_node('Slice', inputs=['x'], outputs=['y'], axes=[1], starts=[0], ends=[-1]) data = np.random.randn(20, 10, 5).astype(np.float32) expected_output = data[:, 0:-1] ng_results = convert_and_calculate(node, [data], [expected_output]) assert np.array_equal(ng_results, [expected_output]) # start ouf of bounds node = onnx.helper.make_node('Slice', inputs=['x'], outputs=['y'], axes=[1], starts=[1000], ends=[1000]) data = np.random.randn(20, 10, 5).astype(np.float32) expected_output = data[:, 1000:1000] ng_results = convert_and_calculate(node, [data], [expected_output]) assert np.array_equal(ng_results, [expected_output])
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]) 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 test_case, 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=[s for s in in_args], outputs=['output'], axis=i, ) expected_output = np.concatenate(values, i) ng_results = convert_and_calculate(node, [v for v in values], [expected_output]) assert np.array_equal(ng_results, [expected_output])
def test_squeeze_exceptions(): data = np.random.randn(1, 3, 4, 5).astype(np.float32) expected_output = np.squeeze(data, axis=0) node = onnx.helper.make_node('Squeeze', inputs=['x'], outputs=['y'], axes=[-1]) with pytest.raises(ValueError): convert_and_calculate(node, [data], [expected_output]) node = onnx.helper.make_node('Squeeze', inputs=['x'], outputs=['y'], axes=[4]) with pytest.raises(ValueError): convert_and_calculate(node, [data], [expected_output]) node = onnx.helper.make_node('Squeeze', inputs=['x'], outputs=['y'], axes=[1]) with pytest.raises(ValueError): convert_and_calculate(node, [data], [expected_output])
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])
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)
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_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])
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])