def test_mul1(self): node = onnx.helper.make_node( 'Mul', inputs=['x', 'y'], outputs=['z'], ) x = np.array([1, 2, 3]).astype(np.float32).reshape([3, 1]) y = np.array([4, 5, 6]).astype(np.float32).reshape([3, 1]) z = x * y # expected output [4., 10., 18.] output = OnnxLoader.run_node(node, [x, y]) np.testing.assert_almost_equal(output['z'], z, decimal=5)
def test_squeeze_axis(self): node = onnx.helper.make_node( 'Squeeze', inputs=['x'], outputs=['y'], axes=[1], ) x = np.random.randn(3, 1, 4, 5).astype(np.float32) y = np.squeeze(x, axis=1) output = OnnxLoader.run_node(node, [x]) np.testing.assert_almost_equal(output["y"], y, decimal=5)
def test_matmul_3d(self): node = onnx.helper.make_node( 'MatMul', inputs=['a', 'b'], outputs=['c'], ) # 3d a = np.random.randn(2, 3, 4).astype(np.float32) b = np.random.randn(2, 4, 3).astype(np.float32) c = np.matmul(a, b) output = OnnxLoader.run_node(node, [a, b]) np.testing.assert_almost_equal(output["c"], c, decimal=5)
def test_onnx_hardsigmoid(self): default_alpha = 0.2 default_beta = 0.5 node = onnx.helper.make_node( 'HardSigmoid', inputs=['x'], outputs=['y'], ) x = np.random.randn(3, 4, 5).astype(np.float32) y = np.clip(x * default_alpha + default_beta, 0, 1) output = OnnxLoader.run_node(node, [x]) np.testing.assert_almost_equal(output["y"], y, decimal=5)
def test_onnx_flatten(self): node = onnx.helper.make_node( 'Flatten', inputs=['a'], outputs=['b'], ) shape = (5, 4, 3, 2) a = np.random.random_sample(shape).astype(np.float32) new_shape = (5, 24) b = np.reshape(a, new_shape) output = OnnxLoader.run_node(node, [a]) np.testing.assert_almost_equal(output["b"], b, decimal=5)
def test_unsqueeze_axis0(self): node = onnx.helper.make_node( 'Unsqueeze', inputs=['x'], outputs=['y'], axes=[0], ) x = np.random.randn(1, 3, 4, 5).astype(np.float32) y = np.expand_dims(x, axis=0) output = OnnxLoader.run_node(node, [x]) np.testing.assert_almost_equal(output["y"], y, decimal=5)
def test_shape(self): node = onnx.helper.make_node( 'Shape', inputs=['x'], outputs=['y'], ) x = np.array([ [1, 2, 3], [4, 5, 6], ]).astype(np.float32) y = np.array([ 2, 3, ]).astype(np.int64) output = OnnxLoader.run_node(node, [x]) np.testing.assert_almost_equal(output["y"], y, decimal=5) x = np.random.randn(3, 4, 5).astype(np.float32) y = np.array(x.shape).astype(np.int64) output = OnnxLoader.run_node(node, [x]) np.testing.assert_almost_equal(output["y"], y, decimal=5)
def test_gt(self): node = helper.make_node( 'Greater', inputs=['x', 'y'], outputs=['greater'], ) x = np.random.randn(3, 4, 5).astype(np.float32) y = np.random.randn(3, 4, 5).astype(np.float32) z = np.greater(x, y) output = OnnxLoader.run_node(node, [x, y]) np.testing.assert_almost_equal(output['greater'], z, decimal=5)
def test_globalaveragepool(self): node = onnx.helper.make_node( 'GlobalAveragePool', inputs=['x'], outputs=['y'], ) x = np.random.randn(2, 3, 7, 5).astype(np.float32) spatial_shape = np.ndim(x) - 2 y = np.average(x, axis=tuple(range(spatial_shape, spatial_shape + 2))) for _ in range(spatial_shape): y = np.expand_dims(y, -1) output = OnnxLoader.run_node(node, [x]) np.testing.assert_almost_equal(output["y"], y, decimal=5)
def test_slice3_default_axes(self): node = onnx.helper.make_node( 'Slice', inputs=['x'], outputs=['y'], starts=[0, 0, 3], ends=[20, 10, 4], ) x = np.random.randn(20, 10, 5).astype(np.float32) y = x[:, :, 3:4] output = OnnxLoader.run_node(node, [x]) np.testing.assert_almost_equal(output["y"], y, decimal=5)
def test_transpose(self): shape = (2, 3, 4) data = np.random.random_sample(shape).astype(np.float32) permutation = (0, 2, 1) node = onnx.helper.make_node('Transpose', inputs=['data'], outputs=['transposed'], perm=permutation) transposed = np.transpose(data, permutation) output = OnnxLoader.run_node(node, [data]) np.testing.assert_almost_equal(output["transposed"], transposed, decimal=5)
def test_leakyrelu(self): node = helper.make_node( 'LeakyRelu', inputs=['x'], outputs=['y'], alpha=0.1 ) x = np.array([-1, 0, 1]).astype(np.float32) # expected output [-0.1, 0., 1.] y = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * 0.1 output = OnnxLoader.run_node(node, [x]) np.testing.assert_almost_equal(output["y"], y, decimal=5)
def test_slice2_neg(self): node = onnx.helper.make_node( 'Slice', inputs=['x'], outputs=['y'], axes=[0, 1], starts=[0, 0], ends=[2, -2], ) x = np.random.randn(20, 10, 5).astype(np.float32) y = x[0:2, 0:-2] output = OnnxLoader.run_node(node, [x]) np.testing.assert_almost_equal(output["y"], y, decimal=5)
def test_reducesum_keepdims(self): shape = [3, 2, 2] axes = [1] keepdims = 1 node = onnx.helper.make_node('ReduceSum', inputs=['data'], outputs=['reduced'], axes=axes, keepdims=keepdims) np.random.seed(0) data = np.random.uniform(-10, 10, shape).astype(np.float32) reduced = np.sum(data, axis=tuple(axes), keepdims=keepdims == 1) output = OnnxLoader.run_node(node, [data]) np.testing.assert_almost_equal(output["reduced"], reduced, decimal=5)
def test_transpose_default(self): import pytest with pytest.raises(Exception) as e_info: shape = (2, 3, 4) data = np.random.random_sample(shape).astype(np.float32) node = onnx.helper.make_node('Transpose', inputs=['data'], outputs=['transposed']) transposed = np.transpose(data) output = OnnxLoader.run_node(node, [data]) np.testing.assert_almost_equal(output["transposed"], transposed, decimal=5)
def test_slice1_start_out_of_bounds(self): with pytest.raises(Exception) as e_info: node = onnx.helper.make_node( 'Slice', inputs=['x'], outputs=['y'], axes=[0], starts=[1000], ends=[1000], ) x = np.random.randn(3, 3, 3).astype(np.float32) y = x[1000:1000] output = OnnxLoader.run_node(node, [x]) np.testing.assert_almost_equal(output["y"], y, decimal=5)
def test_constant(self): values = np.random.randn(5, 5).astype(np.float32) node = onnx.helper.make_node( 'Constant', inputs=[], outputs=['values'], value=onnx.helper.make_tensor( name='const_tensor', data_type=onnx.TensorProto.FLOAT, dims=values.shape, vals=values.flatten().astype(float), ), ) output = OnnxLoader.run_node(node, []) np.testing.assert_almost_equal(output["values"], values, decimal=5)
def test_batch_norm(self): x = np.array([[[[-1, 0, 1]], [[2, 3, 4]]]]).astype(np.float32).reshape((3, 2, 1, 1)) s = np.array([1.0, 1.0]).astype(np.float32).reshape((2, 1)) bias = np.array([0, 0]).astype(np.float32).reshape((2, 1)) mean = np.array([0, 3]).astype(np.float32).reshape((2, 1)) var = np.array([1, 1.5]).astype(np.float32).reshape((2, 1)) y = self._batchnorm_test_mode(x, s, bias, mean, var).astype(np.float32) node = onnx.helper.make_node( 'BatchNormalization', inputs=['x', 's', 'bias', 'mean', 'var'], outputs=['y'], ) output = OnnxLoader.run_node(node, [x, s, bias, mean, var]) np.testing.assert_almost_equal(output["y"], y, decimal=3)
def test_maxpool2d_strides(self): node = helper.make_node('MaxPool', inputs=['x'], outputs=['y'], kernel_shape=[2, 2], strides=[2, 2]) x = np.array([[[ [1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25], ]]]).astype(np.float32) y = np.array([[[[7, 9], [17, 19]]]]).astype(np.float32) output = OnnxLoader.run_node(node, [x]) np.testing.assert_almost_equal(output["y"], y, decimal=5)
def test_maxpool1d(self): node = onnx.helper.make_node( 'MaxPool', inputs=['x'], outputs=['y'], kernel_shape=[2], ) x = np.random.randn(1, 3, 32).astype(np.float32) x_shape = np.array(np.shape(x)) kernel_shape = np.array([2]) strides = [1] out_shape = pool_op_common.get_output_shape('VALID', x_shape[2:], kernel_shape, strides) padded = x y = pool_op_common.pool(padded, x_shape, kernel_shape, strides, out_shape, [0], 'MAX') output = OnnxLoader.run_node(node, [x]) np.testing.assert_almost_equal(output["y"], y, decimal=5)
def test_reducesum_do_not_keepdims(self): axes = [1] keepdims = 0 node = onnx.helper.make_node('ReduceSum', inputs=['data'], outputs=['reduced'], axes=axes, keepdims=keepdims) data = np.array( [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32) reduced = np.sum(data, axis=tuple(axes), keepdims=keepdims == 1) output = OnnxLoader.run_node(node, [data]) np.testing.assert_almost_equal(output["reduced"], reduced, decimal=5)
def test_reducemean_do_not_keepdims(self): shape = [3, 2, 2] axes = [1] keepdims = 0 node = onnx.helper.make_node( 'ReduceMean', inputs=['data'], outputs=['reduced'], axes=axes, keepdims=keepdims) data = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32) reduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1) output = OnnxLoader.run_node(node, [data]) np.testing.assert_almost_equal(output["reduced"], reduced, decimal=5)
def test_concat(self): 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]]]) } # type: Dict[Text, Sequence[Any]] for test_case, values_ in test_cases.items(): values = [np.asarray(v, dtype=np.float32) for v in values_] for i in range(1, 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) y = np.concatenate(values, i) output = OnnxLoader.run_node(node, [v for v in values]) np.testing.assert_almost_equal(output["output"], y, decimal=5)
def test_reshape(self): original_shape = [2, 3, 4] test_cases = { 'reordered_dims': np.array([4, 2, 3], dtype=np.int64), 'reduced_dims': np.array([3, 8], dtype=np.int64), 'extended_dims': np.array([3, 2, 2, 2], dtype=np.int64), 'one_dim': np.array([24], dtype=np.int64) # 'negative_dim': np.array([6, -1, 2], dtype=np.int64), } data = np.random.random_sample(original_shape).astype(np.float32) for test_name, shape in test_cases.items(): node = onnx.helper.make_node( 'Reshape', inputs=['data', 'shape'], outputs=['reshaped'], ) output = OnnxLoader.run_node(node, [data, shape]) reshaped = np.reshape(data, shape) np.testing.assert_almost_equal(output["reshaped"], reshaped, decimal=5)
def test_lrn_default(self): import math alpha = 0.0001 beta = 0.75 bias = 1.0 nsize = 3 node = onnx.helper.make_node('LRN', inputs=['x'], outputs=['y'], size=3) x = np.random.randn(5, 5, 5, 5).astype(np.float32) square_sum = np.zeros((5, 5, 5, 5)).astype(np.float32) for n, c, h, w in np.ndindex(x.shape): square_sum[n, c, h, w] = sum( x[n, max(0, c - int(math.floor((nsize - 1) / 2)) ):min(5, c + int(math.ceil((nsize - 1) / 2)) + 1), h, w]**2) y = x / ((bias + (alpha / nsize) * square_sum)**beta) output = OnnxLoader.run_node(node, [x]) np.testing.assert_almost_equal(output["y"], y, decimal=5)
def test_conv_without_padding(self): x = np.array([[[[0., 1., 2., 3., 4.], # (1, 1, 5, 5) input tensor [5., 6., 7., 8., 9.], [10., 11., 12., 13., 14.], [15., 16., 17., 18., 19.], [20., 21., 22., 23., 24.]]]]).astype(np.float32) W = np.array([[[[1., 1., 1.], # (1, 1, 3, 3) tensor for convolution weights [1., 1., 1.], [1., 1., 1.]]]]).astype(np.float32) # Convolution without padding node_without_padding = onnx.helper.make_node( 'Conv', inputs=['x', 'W'], outputs=['y'], kernel_shape=[3, 3], # Default values for other attributes: strides=[1, 1], dilations=[1, 1], groups=1 pads=[0, 0, 0, 0], ) y_without_padding = np.array([[[[54., 63., 72.], # (1, 1, 3, 3) output tensor [99., 108., 117.], [144., 153., 162.]]]]).astype(np.float32) output = OnnxLoader.run_node(node_without_padding, [x, W]) np.testing.assert_almost_equal(output["y"], y_without_padding, decimal=5)
def test_relu(self): node = helper.make_node('Relu', inputs=['x'], outputs=['y']) x = np.random.randn(3, 4, 5).astype(np.float32) y = np.clip(x, 0, np.inf) output = OnnxLoader.run_node(node, [x]) np.testing.assert_almost_equal(output["y"], y, decimal=5)