def compare_with_pytorch(self,
                             pytorch_model,
                             input_shape_with_batch,
                             compare_result=True,
                             rtol=1e-6,
                             atol=1e-6):
        input_shape_with_batch = to_list(input_shape_with_batch)
        onnx_path = self.dump_pytorch_to_onnx(pytorch_model,
                                              input_shape_with_batch)
        onnx_model = onnx.load(onnx_path)
        # TODO: we only consider single  output for now.
        input_data_with_batch = [
            np.random.uniform(0, 1, shape).astype(np.float32)
            for shape in input_shape_with_batch
        ]
        # coutput = caffe2.python.onnx.backend.run_model(onnx_model, input_data_with_batch)[0]

        pytorch_out = pytorch_model.forward(
            self._convert_ndarray_to_tensor(input_data_with_batch))
        zmodel = OnnxLoader(onnx_model.graph).to_keras()
        zoutput = zmodel.forward(input_data_with_batch[0] if len(
            input_data_with_batch) == 1 else input_data_with_batch)
        if compare_result:
            self.assert_allclose(pytorch_out.detach().numpy(), zoutput, rtol,
                                 atol)
 def compare_with_pytorch(self, pytorch_model, input_shape_with_batch):
     onnx_path = self.dump_pytorch_to_onnx(pytorch_model, input_shape_with_batch)
     onnx_model = onnx.load(onnx_path)
     # TODO: we only consider single input and output for now.
     input_data_with_batch = np.random.uniform(0, 1, input_shape_with_batch).astype(np.float32)
     # coutput = caffe2.python.onnx.backend.run_model(onnx_model, input_data_with_batch)[0]
     pytorch_out = pytorch_model.forward(torch.from_numpy(input_data_with_batch))
     zmodel = OnnxLoader(onnx_model.graph).to_keras()
     zoutput = zmodel.forward(input_data_with_batch)
     self.assert_allclose(pytorch_out.detach().numpy(), zoutput)
    def test_conv_with_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 with padding
        node_with_padding = 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=[1, 1, 1, 1],
        )
        y_with_padding = np.array([[[
            [12., 21., 27., 33., 24.],  # (1, 1, 5, 5) output tensor
            [33., 54., 63., 72., 51.],
            [63., 99., 108., 117., 81.],
            [93., 144., 153., 162., 111.],
            [72., 111., 117., 123., 84.]
        ]]]).astype(np.float32)
        output = OnnxLoader.run_node(node_with_padding, [x, W])
        np.testing.assert_almost_equal(output["y"], y_with_padding, decimal=5)
 def test_softmax(self):
     node = helper.make_node('Softmax', inputs=['x'], outputs=['y'])
     x = np.array([[-1, 0, 1]]).astype(np.float32)
     # expected output [[0.09003058, 0.24472848, 0.66524094]]
     y = np.exp(x) / np.sum(np.exp(x), axis=1)
     output = OnnxLoader.run_node(node, [x])
     np.testing.assert_almost_equal(output["y"], y, decimal=5)
    def test_onnx_elu_default(self):
        node = onnx.helper.make_node('Elu', inputs=['x'], outputs=['y'])

        x = np.random.randn(3, 4, 5).astype(np.float32)
        y = np.clip(x, 0, np.inf) + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 1.0
        output = OnnxLoader.run_node(node, [x])
        np.testing.assert_almost_equal(output["y"], y, decimal=5)
Exemple #6
0
 def test_maxpool2d_pads01(self):
     import pytest
     with pytest.raises(Exception) as e_info:
         node = onnx.helper.make_node('MaxPool',
                                      inputs=['x'],
                                      outputs=['y'],
                                      kernel_shape=[3, 3],
                                      pads=[0, 0, 1, 1])
         x = np.random.randn(1, 3, 28, 28).astype(np.float32)
         x_shape = np.shape(x)
         kernel_shape = (3, 3)
         strides = (1, 1)
         pad_top = pad_left = 0
         pad_bottom = pad_right = 1
         pad_shape = [pad_top + pad_bottom, pad_left + pad_right]
         out_shape = pool_op_common.get_output_shape(
             'VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)
         padded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom),
                             (pad_left, pad_right)),
                         mode='constant',
                         constant_values=np.nan)
         y = pool_op_common.pool(padded, x_shape, kernel_shape, strides,
                                 out_shape, pad_shape, 'MAX')
         output = OnnxLoader.run_node(node, [x])
         np.testing.assert_almost_equal(output["y"], y, decimal=5)
Exemple #7
0
    def test_lrn(self):
        import math
        alpha = 0.0002
        beta = 0.5
        bias = 2.0
        nsize = 3
        node = onnx.helper.make_node('LRN',
                                     inputs=['x'],
                                     outputs=['y'],
                                     alpha=alpha,
                                     beta=beta,
                                     bias=bias,
                                     size=nsize)
        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_neg(self):
        node = onnx.helper.make_node(
            'Neg',
            inputs=['x'],
            outputs=['y'],
        )

        x = np.array([-4, 2]).astype(np.float32).reshape([2, 1])
        y = np.negative(x)
        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.negative(x)
        output = OnnxLoader.run_node(node, [x])
        np.testing.assert_almost_equal(output["y"], y, decimal=5)
    def test_sub(self):
        node = onnx.helper.make_node(
            'Sub',
            inputs=['x', 'y'],
            outputs=['z'],
        )

        x = np.array([1, 2, 3]).astype(np.float32).reshape([3, 1])
        y = np.array([3, 2, 1]).astype(np.float32).reshape([3, 1])
        z = x - y
        output = OnnxLoader.run_node(node, [x, y])
        np.testing.assert_almost_equal(output["z"], z, decimal=5)

        x = np.random.randn(3, 4, 5).astype(np.float32)
        y = np.random.randn(3, 4, 5).astype(np.float32)
        z = x - y
        output = OnnxLoader.run_node(node, [x, y])
        np.testing.assert_almost_equal(output["z"], z, decimal=5)
 def test_onnx_tanh(self):
     node = onnx.helper.make_node(
         'Tanh',
         inputs=['x'],
         outputs=['y'],
     )
     x = np.random.randn(3, 4, 5).astype(np.float32)
     y = np.tanh(x)
     output = OnnxLoader.run_node(node, [x])
     np.testing.assert_almost_equal(output["y"], y)
 def test_onnx_log(self):
     node = onnx.helper.make_node(
         'Log',
         inputs=['x'],
         outputs=['y'],
     )
     x = np.exp(np.random.randn(3, 4, 5).astype(np.float32))
     y = np.log(x)
     output = OnnxLoader.run_node(node, [x])
     np.testing.assert_almost_equal(output["y"], y, decimal=5)
Exemple #12
0
 def test_sigmoid(self):
     node = helper.make_node(
         'Sigmoid',
         inputs=['x'],
         outputs=['y'],
     )
     x = np.array([[-1, 0, 1]]).astype(np.float32)
     y = 1.0 / (1.0 + np.exp(np.negative(x)))  # expected output [0.26894143, 0.5, 0.7310586]
     output = OnnxLoader.run_node(node, [x])
     np.testing.assert_almost_equal(output["y"], y, decimal=5)
Exemple #13
0
 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)
    def test_squeeze_none(self):
        node = onnx.helper.make_node(
            'Squeeze',
            inputs=['x'],
            outputs=['y'],
        )
        x = np.random.randn(1, 1, 4, 5).astype(np.float32)
        y = np.squeeze(x)

        output = OnnxLoader.run_node(node, [x])
        np.testing.assert_almost_equal(output["y"], y, decimal=5)
Exemple #15
0
    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)
Exemple #16
0
    def test_unsqueeze_axis1(self):
        node = onnx.helper.make_node(
            'Unsqueeze',
            inputs=['x'],
            outputs=['y'],
            axes=[1],
        )
        x = np.random.randn(3, 1, 4, 5).astype(np.float32)
        y = np.expand_dims(x, axis=1)

        output = OnnxLoader.run_node(node, [x])
        np.testing.assert_almost_equal(output["y"], y, decimal=5)
Exemple #17
0
    def test_div2(self):
        node = onnx.helper.make_node(
            'Div',
            inputs=['x', 'y'],
            outputs=['z'],
        )

        x = np.random.randn(3, 4, 5).astype(np.float32)
        y = np.random.rand(3, 4, 5).astype(np.float32) + 1.0
        z = x / y
        output = OnnxLoader.run_node(node, [x, y])
        np.testing.assert_almost_equal(output["z"], z, decimal=5)
Exemple #18
0
    def test_div1(self):
        node = onnx.helper.make_node(
            'Div',
            inputs=['x', 'y'],
            outputs=['z'],
        )

        x = np.array([3, 4]).astype(np.float32).reshape([2, 1])
        y = np.array([1, 2]).astype(np.float32).reshape([2, 1])
        z = x / y
        output = OnnxLoader.run_node(node, [x, y])
        np.testing.assert_almost_equal(output["z"], z, 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_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_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_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_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)
Exemple #24
0
    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)
Exemple #25
0
    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)
Exemple #26
0
 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)
Exemple #27
0
    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)
Exemple #28
0
    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)
Exemple #29
0
    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_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)