コード例 #1
0
    def test_onnx_example_constant_of_shape(self):
        x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((3, 2))

        opv = _TARGET_OPSET_
        cop2 = OnnxConstantOfShape(OnnxShape('input', op_version=opv),
                                   output_names=['mat'],
                                   op_version=opv)
        model_def = cop2.to_onnx({'input': x},
                                 outputs=[('mat', FloatTensorType())])
        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'input': x})
        exp = np.zeros((3, 2), dtype=np.float32)
        assert_almost_equal(exp, res[0])

        tensor_value = onnx.helper.make_tensor("value", onnx.TensorProto.FLOAT,
                                               (1, ), [-5])
        cop2 = OnnxConstantOfShape(OnnxShape('input', op_version=opv),
                                   value=tensor_value,
                                   output_names=['mat'],
                                   op_version=opv)
        model_def = cop2.to_onnx({'input': x},
                                 outputs=[('mat', FloatTensorType())])
        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'input': x})
        exp = np.full((3, 2), -5.)
        assert_almost_equal(exp, res[0])
コード例 #2
0
 def test_onnxt_runtime_constant_of_shape(self):
     x = numpy.array([2, 2], dtype=numpy.int64)
     y = numpy.zeros((2, 2))
     onx = OnnxConstantOfShape('X', output_names=['Y'])
     model_def = onx.to_onnx({'X': x.astype(numpy.float32)},
                             outputs=[('Y', FloatTensorType())])
     got = OnnxInference(model_def).run({'X': x})
     self.assertEqualArray(y, got['Y'])
コード例 #3
0
    def test_constant_of_shape(self):
        for opset in range(20, 8, -1):
            if opset > TARGET_OPSET:
                continue
            for value in [
                    np.array([5], dtype=np.float32),
                    np.array(5, dtype=np.float32)
            ]:
                with self.subTest(opset=opset, value=value):
                    tensor_value = onnx.helper.make_tensor(
                        "value", onnx.TensorProto.FLOAT, [1], [5])

                    cst = OnnxConstantOfShape('X',
                                              value=tensor_value,
                                              op_version=opset,
                                              output_names=['Y'])
                    shape = np.array([3, 4], dtype=np.int64)
                    onx = cst.to_onnx({'X': shape},
                                      target_opset=opset,
                                      outputs=[('Y', FloatTensorType())])
                    sess = InferenceSession(onx.SerializeToString())
                    res = sess.run(None, {'X': shape})
                    assert_almost_equal(
                        res[0], np.full(tuple(shape), 5, dtype=np.float32))

                    cst = OnnxConstantOfShape('X',
                                              value=value,
                                              op_version=opset,
                                              output_names=['Y'])
                    shape = np.array([3, 4], dtype=np.int64)
                    onx = cst.to_onnx({'X': shape},
                                      target_opset=opset,
                                      outputs=[('Y', FloatTensorType())])
                    sess = InferenceSession(onx.SerializeToString())
                    res = sess.run(None, {'X': shape})
                    assert_almost_equal(
                        res[0], np.full(tuple(shape), 5, dtype=np.float32))

        for opset in [TARGET_OPSET]:
            for value in [5, np.float32(5)]:
                with self.subTest(opset=opset, value=value):
                    with self.assertRaises(TypeError):
                        OnnxConstantOfShape('X',
                                            value=value,
                                            op_version=opset,
                                            output_names=['Y'])
コード例 #4
0
 def test_constant_of_shape(self):
     x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape(
         (3, 2))
     tensor_value = make_tensor("value", TensorProto.FLOAT, (1, ), [-5])  # pylint: disable=E1101
     cop2 = OnnxConstantOfShape(OnnxShape('input'),
                                value=tensor_value,
                                output_names=['mat'])
     model_def = cop2.to_onnx({'input': x},
                              outputs=[('mat', FloatTensorType())])
     oinf = OnnxInference(model_def, skip_run=True)
     dot = oinf.to_dot()
     self.assertIn('ConstantOfShape', dot)