コード例 #1
0
    def test_default(self):
        pad = OnnxPad(mode='constant',
                      value=1.5,
                      pads=[0, 1, 0, 1],
                      op_version=10)

        X = helper.make_tensor_value_info('X', onnx.TensorProto.FLOAT,
                                          [None, 2])
        model_def = pad.to_onnx({pad.inputs[0].name: X}, target_opset=10)
        onnx.checker.check_model(model_def)
コード例 #2
0
    def test_pad(self):
        from skl2onnx.algebra.onnx_ops import OnnxPad

        X = helper.make_tensor_value_info('X', TensorProto.FLOAT, [1, 2])

        pad = OnnxPad('X', output_names=['Y'],
                      mode='constant', value=1.5,
                      pads=[0, 1, 0, 1])

        model_def = pad.to_onnx({'X': X})
        onnx.checker.check_model(model_def)
コード例 #3
0
    def test_pad_opset(self):

        pad = OnnxPad('X',
                      output_names=['Y'],
                      mode='constant',
                      value=1.5,
                      pads=[0, 1, 0, 1],
                      op_version=2)

        X = np.array([[0, 1]], dtype=np.float32)
        model_def = pad.to_onnx({'X': X}, target_opset=10)
        onnx.checker.check_model(model_def)

        def predict_with_onnxruntime(model_def, *inputs):
            sess = ort.InferenceSession(model_def.SerializeToString())
            names = [i.name for i in sess.get_inputs()]
            dinputs = {name: input for name, input in zip(names, inputs)}
            res = sess.run(None, dinputs)
            names = [o.name for o in sess.get_outputs()]
            return {name: output for name, output in zip(names, res)}

        Y = predict_with_onnxruntime(model_def, X)
        assert_almost_equal(np.array([[1.5, 0., 1., 1.5]], dtype=np.float32),
                            Y['Y'])
コード例 #4
0
onnx.checker.check_model(model_def)
print('The model is checked!')

#####################################
# Same example with sklearn-onnx
# ++++++++++++++++++++++++++++++
#
# Every operator has its own class in *sklearn-onnx*.
# The list is dynamically created based on the installed
# onnx package.

from skl2onnx.algebra.onnx_ops import OnnxPad  # noqa

pad = OnnxPad('X',
              output_names=['Y'],
              mode='constant',
              value=1.5,
              pads=[0, 1, 0, 1],
              op_version=10)

model_def = pad.to_onnx({'X': X}, target_opset=10)

print('The model is:\n{}'.format(model_def))
onnx.checker.check_model(model_def)
print('The model is checked!')

####################################
# Inputs and outputs can also be skipped.

pad = OnnxPad(mode='constant', value=1.5, pads=[0, 1, 0, 1], op_version=10)

model_def = pad.to_onnx({pad.inputs[0]: X}, target_opset=10)
コード例 #5
0
    def test_onnxt_runtime_pad(self):
        data = np.array([[1.0, 1.2], [2.3, 3.4], [4.5, 5.7]], dtype=np.float32)
        pads = np.array([0, 2, 0, 0], dtype=np.int64)
        constant_value = np.array([0.0], dtype=np.float32)
        exp = np.array(
            [[0.0, 0.0, 1.0, 1.2], [0.0, 0.0, 2.3, 3.4], [0.0, 0.0, 4.5, 5.7]],
            dtype=np.float32)
        onx = OnnxPad('data',
                      'pads',
                      constant_value,
                      output_names=['Y'],
                      op_version=TARGET_OPSET)
        model_def = onx.to_onnx({
            'data': data,
            'pads': pads
        },
                                target_opset=TARGET_OPSET)
        oinf = InferenceSession(model_def.SerializeToString())
        got = oinf.run(None, {'data': data, 'pads': pads})
        assert_almost_equal(exp, got[0])

        data = np.array([[1.0, 1.2], [2.3, 3.4], [4.5, 5.7]], dtype=np.float32)
        pads = np.array([0, 2, 0, 0], dtype=np.int64)
        constant_value = np.array([0.0], dtype=np.float32)
        exp = np.array(
            [[0, 1.2, 1.0, 1.2], [0, 3.4, 2.3, 3.4], [0, 5.7, 4.5, 5.7]],
            dtype=np.float32)
        onx = OnnxPad('data',
                      'pads',
                      output_names=['Y'],
                      mode='reflect',
                      op_version=TARGET_OPSET)
        model_def = onx.to_onnx({
            'data': data,
            'pads': pads
        },
                                target_opset=TARGET_OPSET)
        oinf = InferenceSession(model_def.SerializeToString())
        got = oinf.run(None, {'data': data, 'pads': pads})
        try:
            assert_almost_equal(exp, got[0])
        except AssertionError as e:
            warnings.warn(e)

        data = np.array([[1.0, 1.2], [2.3, 3.4], [4.5, 5.7]], dtype=np.float32)
        pads = np.array([0, 2, 0, 0], dtype=np.int64)
        constant_value = np.array([0.0], dtype=np.float32)
        exp = np.array(
            [[1.0, 1.0, 1.0, 1.2], [2.3, 2.3, 2.3, 3.4], [4.5, 4.5, 4.5, 5.7]],
            dtype=np.float32)
        onx = OnnxPad('data',
                      'pads',
                      output_names=['Y'],
                      mode='edge',
                      op_version=TARGET_OPSET)
        model_def = onx.to_onnx({
            'data': data,
            'pads': pads
        },
                                target_opset=TARGET_OPSET)
        oinf = InferenceSession(model_def.SerializeToString())
        got = oinf.run(None, {'data': data, 'pads': pads})
        assert_almost_equal(exp, got[0])
コード例 #6
0
onnx.checker.check_model(model_def)
print('The model is checked!')

#####################################
# Same example with sklearn-onnx
# ++++++++++++++++++++++++++++++
#
# Every operator has its own class in *sklearn-onnx*.
# The list is dynamically created based on the installed
# onnx package.

from skl2onnx.algebra.onnx_ops import OnnxPad  # noqa

pad = OnnxPad('X',
              output_names=['Y'],
              mode='constant',
              value=1.5,
              pads=[0, 1, 0, 1])

model_def = pad.to_onnx({'X': X})

print('The model is:\n{}'.format(model_def))
onnx.checker.check_model(model_def)
print('The model is checked!')

####################################
# Inputs and outputs can also be skipped.

pad = OnnxPad(mode='constant', value=1.5, pads=[0, 1, 0, 1])

model_def = pad.to_onnx({pad.inputs[0].name: X})