Example #1
0
    def test_onnx_rename_names_type(self):
        rows = []

        def flog(*s):
            rows.append(" ".join(map(str, s)))

        dtype = numpy.float32
        x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape(
            (3, 2))
        cop = OnnxAdd('X',
                      numpy.array([1], dtype=dtype),
                      op_version=TARGET_OPSET)
        cop2 = OnnxAdd('X',
                       numpy.array([1], dtype=dtype),
                       op_version=TARGET_OPSET)
        cop3 = OnnxAdd('X',
                       numpy.array([2], dtype=dtype),
                       op_version=TARGET_OPSET,
                       output_names=['inter'])
        cop4 = OnnxSub(OnnxMul(cop, cop3, op_version=TARGET_OPSET),
                       cop2,
                       output_names=['final'],
                       op_version=TARGET_OPSET)
        model_def = cop4.to_onnx({'X': x})
        oinf1 = OnnxInference(model_def)
        new_model = onnx_rename_names(model_def,
                                      verbose=1,
                                      fLOG=flog,
                                      strategy='type')
        total = "\n".join(rows)
        self.assertIn("'Ad_Addcst' -> 'i_05'", total)
        oinf2 = OnnxInference(new_model)
        y1 = oinf1.run({'X': x})
        y2 = oinf2.run({'X': x})
        self.assertEqualArray(y1['final'], y2['final'])
    def test_onnx_reversed_order(self):
        idi = np.identity(2)
        idi2 = np.identity(2) * 2

        onx = OnnxAdd(OnnxAdd('X', idi, op_version=TARGET_OPSET),
                      idi2,
                      output_names=['Y'],
                      op_version=TARGET_OPSET)
        model_def = onx.to_onnx({'X': idi.astype(np.float32)})
        self.assertEqual(len(model_def.graph.output), 1)
        onx = OnnxAdd(idi2,
                      OnnxAdd('X', idi, op_version=TARGET_OPSET),
                      output_names=['Y'],
                      op_version=TARGET_OPSET)
        model_def = onx.to_onnx({'X': idi.astype(np.float32)})
        onnx2 = model_def.SerializeToString()
        self.assertIsInstance(onx.outputs, list)
        self.assertEqual(len(onx.outputs), 1)
        self.assertIsInstance(onx.outputs[0], tuple)
        self.assertEqual(len(onx.outputs[0]), 2)
        self.assertIsInstance(onx.outputs[0][1], DoubleTensorType)
        # There should be 2 outputs here, bug in ONNX?
        self.assertEqual(len(model_def.graph.output), 1)
        reload = load_model(BytesIO(onnx2))
        self.assertEqual(len(reload.graph.output), 1)
        assert reload is not None
Example #3
0
def _onnx_axpyw2(target_opset=None, dtype=numpy.float32):
    """
    Returns the ONNX graph for function
    :math:`Y, Z = f(X1, X2, G, \\alpha, \\beta) = (Y, Z)`
    where :math:`Z = \\beta G + \\alpha X1` and
    :math:`Y = \\beta * Z + \\alpha X1 + X2`.

    .. gdot::
        :script: DOT-SECTION

        from mlprodict.onnxrt import OnnxInference
        from onnxcustom.utils.onnx_function import function_onnx_graph

        model_onnx = function_onnx_graph('axpy')
        oinf = OnnxInference(model_onnx, inplace=False)

        print("DOT-SECTION", oinf.to_dot())
    """
    from skl2onnx.algebra.onnx_ops import OnnxAdd, OnnxMul
    s1 = OnnxMul('X1', 'alpha', op_version=target_opset)
    s2 = OnnxMul('G', 'beta', op_version=target_opset)
    Z = OnnxAdd(s1, s2, op_version=target_opset, output_names=['Z'])
    s2_2 = OnnxMul(Z, 'beta', op_version=target_opset)
    s2_3 = OnnxAdd(s1, s2_2, op_version=target_opset)
    Y = OnnxAdd(s2_3, 'X2', op_version=target_opset, output_names=['Y'])
    var_type = dtype_to_var_type(dtype)
    varsx = [('X1', var_type()), ('X2', var_type()), ('G', var_type()),
             ('alpha', var_type([1])), ('beta', var_type([1]))]
    onx = Y.to_onnx(varsx,
                    outputs=[('Y', var_type()), ('Z', var_type())],
                    target_opset=target_opset,
                    other_outputs=[Z])
    return onx
Example #4
0
    def test_onnx_reversed_order(self):
        idi = np.identity(2)
        idi2 = np.identity(2) * 2

        onx = OnnxAdd(OnnxAdd('X',
                              idi,
                              op_version=onnx.defs.onnx_opset_version()),
                      idi2,
                      output_names=['Y'],
                      op_version=onnx.defs.onnx_opset_version())
        model_def = onx.to_onnx({'X': idi.astype(np.float32)})
        self.assertEqual(len(model_def.graph.output), 1)
        onx = OnnxAdd(idi2,
                      OnnxAdd('X',
                              idi,
                              op_version=onnx.defs.onnx_opset_version()),
                      output_names=['Y'],
                      op_version=onnx.defs.onnx_opset_version())
        model_def = onx.to_onnx({'X': idi.astype(np.float32)})
        onnx2 = model_def.SerializeToString()
        self.assertEqual(onx.outputs, ['Y'])
        # There should be 2 outputs here, bug in ONNX?
        self.assertEqual(len(model_def.graph.output), 1)
        reload = load_model(BytesIO(onnx2))
        self.assertEqual(len(reload.graph.output), 1)
        assert reload is not None
    def test_onnx_remove_redundant_subgraphs_full(self):
        from skl2onnx.algebra.complex_functions import onnx_squareform_pdist
        cop = OnnxAdd(OnnxIdentity('input',
                                   op_version=get_opset_number_from_onnx()),
                      'input',
                      op_version=get_opset_number_from_onnx())
        cdist = onnx_squareform_pdist(cop,
                                      dtype=numpy.float32,
                                      op_version=get_opset_number_from_onnx())
        cdist2 = onnx_squareform_pdist(cop,
                                       dtype=numpy.float32,
                                       op_version=get_opset_number_from_onnx())
        cop2 = OnnxAdd(cdist,
                       cdist2,
                       output_names=['cdist'],
                       op_version=get_opset_number_from_onnx())

        model_def = cop2.to_onnx({'input': FloatTensorType()},
                                 outputs=[('cdist', FloatTensorType())],
                                 target_opset=get_opset_number_from_onnx())
        stats = onnx_statistics(model_def, optim=False)
        new_model = onnx_optimisations(model_def)
        stats2 = onnx_statistics(new_model, optim=False)
        self.assertLess(stats2['size'], stats['size'])
        self.assertLess(stats2['nnodes'], stats['nnodes'])
        self.assertLess(stats2['op_Identity'], stats['op_Identity'])
    def test_onnx_example_cdist_in(self):
        x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((3, 2))
        x2 = np.array([1.1, 2.1, 4.01, 5.01, 5.001, 4.001, 0,
                       0]).astype(np.float32).reshape((4, 2))
        cop = OnnxAdd('input', 'input')
        cop2 = OnnxIdentity(onnx_cdist(cop, x2, dtype=np.float32),
                            output_names=['cdist'])

        model_def = cop2.to_onnx(inputs=[('input',
                                          FloatTensorType([None, None]))],
                                 outputs=[('cdist', FloatTensorType())])

        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'input': x})
        exp = scipy_cdist(x * 2, x2, metric="sqeuclidean")
        assert_almost_equal(exp, res[0], decimal=5)

        x = np.array(
            [[6.1, 2.8, 4.7, 1.2], [5.7, 3.8, 1.7, 0.3], [7.7, 2.6, 6.9, 2.3],
             [6.0, 2.9, 4.5, 1.5], [6.8, 2.8, 4.8, 1.4], [5.4, 3.4, 1.5, 0.4],
             [5.6, 2.9, 3.6, 1.3], [6.9, 3.1, 5.1, 2.3]],
            dtype=np.float32)
        cop = OnnxAdd('input', 'input')
        cop2 = OnnxIdentity(onnx_cdist(cop, x, dtype=np.float32),
                            output_names=['cdist'])

        model_def = cop2.to_onnx(inputs=[('input',
                                          FloatTensorType([None, None]))],
                                 outputs=[('cdist', FloatTensorType())])

        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'input': x})
        exp = scipy_cdist(x * 2, x, metric="sqeuclidean")
        assert_almost_equal(exp, res[0], decimal=4)
    def test_softmax(self):
        X = np.random.randn(100, 4).astype(np.float32)
        y = X.sum(axis=1) + np.random.randn(100) / 10
        y = y.astype(np.float32)
        self.assertEqual(y.shape, (100, ))
        weight = np.random.randn(4, 1).astype(np.float32)
        intercept = np.random.randn(1).astype(np.float32)

        node = OnnxAdd(OnnxMatMul('X', weight, op_version=TARGET_OPSET),
                       intercept,
                       op_version=TARGET_OPSET)
        nn_onnx = node.to_onnx({'X': X}, target_opset=TARGET_OPSET)
        with open("debug_ort_add.onnx", "wb") as f:
            f.write(nn_onnx.SerializeToString())
        self.assertEqual(len(nn_onnx.graph.output), 1)

        node = OnnxMatMul('X', weight, op_version=TARGET_OPSET)
        nn_onnx = node.to_onnx({'X': X}, target_opset=TARGET_OPSET)
        self.assertEqual(len(nn_onnx.graph.output), 1)

        node = OnnxSoftmax(OnnxAdd(OnnxMatMul('X',
                                              weight,
                                              op_version=TARGET_OPSET),
                                   intercept,
                                   op_version=TARGET_OPSET),
                           op_version=TARGET_OPSET)
        nn_onnx = node.to_onnx({'X': X}, target_opset=TARGET_OPSET)
        self.assertEqual(len(nn_onnx.graph.output), 1)
Example #8
0
    def test_onnx_subgraphs2(self):
        x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape(
            (3, 2))
        cop = OnnxAdd(OnnxIdentity('input', op_version=TARGET_OPSET),
                      'input',
                      op_version=TARGET_OPSET)
        cdist = onnx_squareform_pdist(cop,
                                      dtype=numpy.float32,
                                      op_version=TARGET_OPSET)
        id1 = [id(a) for a in cdist.onx_op.graph_algebra['body']]
        cdist2 = onnx_squareform_pdist(cop,
                                       dtype=numpy.float32,
                                       op_version=TARGET_OPSET)
        id2 = [id(a) for a in cdist2.onx_op.graph_algebra['body']]
        self.assertNotEqual(id1, id2)
        cop2 = OnnxAdd(cdist,
                       cdist2,
                       output_names=['cdist'],
                       op_version=TARGET_OPSET)

        model_def = cop2.to_onnx({'input': FloatTensorType([None, None])},
                                 outputs=[('cdist',
                                           FloatTensorType([None, None]))],
                                 target_opset=TARGET_OPSET)
        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'input': x})
        self.assertEqual(len(res), 1)
 def test_onnxt_json(self):
     idi = numpy.identity(2)
     idi2 = numpy.identity(2) * 2
     onx = OnnxAdd(OnnxAdd('X', idi), idi2, output_names=['Y'])
     model_def = onx.to_onnx({'X': idi.astype(numpy.float32)})
     oinf = OnnxInference(model_def)
     js = oinf.to_json()
     self.assertIn('"initializers": {', js)
def pyod_iforest_converter(scope, operator, container):
    op = operator.raw_operator
    opv = container.target_opset
    out = operator.outputs

    # We retrieve the unique input.
    X = operator.inputs[0]

    # In most case, computation happen in floats.
    # But it might be with double. ONNX is very strict
    # about types, every constant should have the same
    # type as the input.
    dtype = guess_numpy_type(X.type)

    detector = op.detector_  # Should be IForest from scikit-learn.
    lab_pred = OnnxSubEstimator(detector, X, op_version=opv)
    scores = OnnxIdentity(lab_pred[1], op_version=opv)

    # labels
    threshold = op.threshold_
    above = OnnxLess(scores,
                     np.array([threshold], dtype=dtype),
                     op_version=opv)
    labels = OnnxCast(above,
                      op_version=opv,
                      to=onnx_proto.TensorProto.INT64,
                      output_names=out[:1])

    # probabilities
    train_scores = op.decision_scores_
    scaler = MinMaxScaler().fit(train_scores.reshape(-1, 1))
    scores_ = OnnxMul(scores, np.array([-1], dtype=dtype), op_version=opv)
    print(scaler.min_)
    print(scaler.scale_)

    scaled = OnnxMul(scores_, scaler.scale_.astype(dtype), op_version=opv)
    scaled_centered = OnnxAdd(scaled,
                              scaler.min_.astype(dtype),
                              op_version=opv)
    clipped = OnnxClip(scaled_centered,
                       np.array([0], dtype=dtype),
                       np.array([1], dtype=dtype),
                       op_version=opv)
    clipped_ = OnnxAdd(OnnxMul(clipped,
                               np.array([-1], dtype=dtype),
                               op_version=opv),
                       np.array([1], dtype=dtype),
                       op_version=opv)

    scores_2d = OnnxConcat(clipped_,
                           clipped,
                           axis=1,
                           op_version=opv,
                           output_names=out[1:])

    labels.add_to(scope, container)
    scores_2d.add_to(scope, container)
Example #11
0
def build_ort_add(op_version=12):
    node1 = OnnxAdd('x', 'y', op_version=op_version)
    node2 = OnnxAdd(node1, 'y', op_version=op_version)
    node = OnnxAdd(node2, 'y', op_version=op_version, output_names=['z'])
    onx = node.to_onnx(inputs=[('x', FloatTensorType()),
                               ('y', FloatTensorType())],
                       target_opset=op_version)
    sess = InferenceSession(onx.SerializeToString())
    return lambda x, y: sess.run(None, {'x': x, 'y': y})
 def test_onnxt_run(self):
     idi = numpy.identity(2)
     idi2 = numpy.identity(2) * 2
     onx = OnnxAdd(OnnxAdd('X', idi), idi2, output_names=['Y'])
     model_def = onx.to_onnx({'X': idi.astype(numpy.float32)})
     oinf = OnnxInference(model_def)
     X = numpy.array([[1, 1], [3, 3]])
     y = oinf.run({'X': X.astype(numpy.float32)})
     exp = numpy.array([[4, 1], [3, 6]], dtype=numpy.float32)
     self.assertEqual(list(y), ['Y'])
     self.assertEqualArray(y['Y'], exp)
Example #13
0
 def generate_onnx_graph(dim, nbnode, input_name='X1'):
     i1 = input_name
     for i in range(nbnode - 1):
         i2 = (np.ones((1, dim)) * nbnode * 10).astype(np.float32)
         node = OnnxAdd(i1, i2)
         i1 = node
     i2 = (np.ones((1, dim)) * nbnode * 10).astype(np.float32)
     node = OnnxAdd(i1, i2, output_names=['Y'])
     onx = node.to_onnx([(input_name, FloatTensorType((None, dim)))],
                        outputs=[('Y', FloatTensorType())])
     return onx
Example #14
0
 def test_onnxt_dot_onnx(self):
     idi = numpy.identity(2).astype(numpy.float32)
     idi2 = (numpy.identity(2) * 2).astype(numpy.float32)
     onx = OnnxAdd(OnnxAdd('X', idi, op_version=TARGET_OPSET),
                   idi2,
                   output_names=['Y'],
                   op_version=TARGET_OPSET)
     model_def = onx.to_onnx({'X': idi.astype(numpy.float32)},
                             target_opset=TARGET_OPSET)
     oinf = OnnxInference(model_def)
     dot = oinf.to_dot(use_onnx=True)
     self.assertIn('[label="Ad_Addcst1"', dot)
Example #15
0
 def test_onnxt_text_seq(self):
     idi = numpy.identity(2).astype(numpy.float32)
     idi2 = (numpy.identity(2) * 2).astype(numpy.float32)
     onx = OnnxAdd(OnnxAdd('X', idi, op_version=TARGET_OPSET),
                   idi2,
                   output_names=['Y'],
                   op_version=TARGET_OPSET)
     model_def = onx.to_onnx({'X': idi.astype(numpy.float32)},
                             target_opset=TARGET_OPSET)
     oinf = OnnxInference(model_def)
     text = oinf.to_text(kind='seq')
     self.assertIn('input:', text)
    def test_onnx_example_cdist_in(self):
        from skl2onnx.algebra.complex_functions import onnx_cdist
        x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape(
            (3, 2))
        x2 = numpy.array([1.1, 2.1, 4.01, 5.01, 5.001, 4.001, 0,
                          0]).astype(numpy.float32).reshape((4, 2))
        cop = OnnxAdd('input',
                      'input',
                      op_version=get_opset_number_from_onnx())
        cop2 = OnnxIdentity(onnx_cdist(
            cop,
            x2,
            dtype=numpy.float32,
            op_version=get_opset_number_from_onnx()),
                            output_names=['cdist'],
                            op_version=get_opset_number_from_onnx())

        model_def = cop2.to_onnx(inputs=[('input',
                                          FloatTensorType([None, None]))],
                                 outputs=[('cdist',
                                           FloatTensorType(None, None))],
                                 target_opset=get_opset_number_from_onnx())

        sess = OnnxInference(model_def)
        res = sess.run({'input': x})
        exp = scipy_cdist(x * 2, x2, metric="sqeuclidean")
        self.assertEqualArray(exp, res['cdist'], decimal=5)

        x = numpy.array(
            [[6.1, 2.8, 4.7, 1.2], [5.7, 3.8, 1.7, 0.3], [7.7, 2.6, 6.9, 2.3],
             [6., 2.9, 4.5, 1.5], [6.8, 2.8, 4.8, 1.4], [5.4, 3.4, 1.5, 0.4],
             [5.6, 2.9, 3.6, 1.3], [6.9, 3.1, 5.1, 2.3]],
            dtype=numpy.float32)
        cop = OnnxAdd('input',
                      'input',
                      op_version=get_opset_number_from_onnx())
        cop2 = OnnxIdentity(onnx_cdist(
            cop,
            x,
            dtype=numpy.float32,
            op_version=get_opset_number_from_onnx()),
                            output_names=['cdist'],
                            op_version=get_opset_number_from_onnx())

        model_def = cop2.to_onnx(inputs=[('input',
                                          FloatTensorType([None, None]))],
                                 outputs=[('cdist', FloatTensorType())],
                                 target_opset=get_opset_number_from_onnx())

        sess = OnnxInference(model_def)
        res = sess.run({'input': x})
        exp = scipy_cdist(x * 2, x, metric="sqeuclidean")
        self.assertEqualArray(exp, res['cdist'], decimal=4)
Example #17
0
 def test_onnxt_json(self):
     idi = numpy.identity(2).astype(numpy.float32)
     idi2 = (numpy.identity(2) * 2).astype(numpy.float32)
     onx = OnnxAdd(OnnxAdd('X', idi, op_version=TARGET_OPSET),
                   idi2,
                   output_names=['Y'],
                   op_version=TARGET_OPSET)
     model_def = onx.to_onnx({'X': idi.astype(numpy.float32)},
                             target_opset=TARGET_OPSET)
     oinf = OnnxInference(model_def)
     js = oinf.to_json()
     self.assertIn('"initializers": {', js)
Example #18
0
    def test_onnx_remove_redundant_subgraphs(self):
        from skl2onnx.algebra.complex_functions import onnx_squareform_pdist
        x = numpy.array([1, 2, 4, 5, 5, 4]).astype(numpy.float32).reshape(
            (3, 2))
        cop = OnnxAdd(OnnxIdentity('input', op_version=TARGET_OPSET),
                      'input',
                      op_version=TARGET_OPSET)
        cdist = onnx_squareform_pdist(cop,
                                      dtype=numpy.float32,
                                      op_version=TARGET_OPSET)
        cdist2 = onnx_squareform_pdist(cop,
                                       dtype=numpy.float32,
                                       op_version=TARGET_OPSET)
        cop2 = OnnxAdd(cdist,
                       cdist2,
                       output_names=['cdist'],
                       op_version=TARGET_OPSET)

        model_def = cop2.to_onnx({'input': FloatTensorType()},
                                 outputs=[('cdist', FloatTensorType())],
                                 target_opset=TARGET_OPSET)
        c1 = model_def.SerializeToString()
        stats = onnx_statistics(model_def, optim=False)
        c2 = model_def.SerializeToString()
        self.assertEqual(c1, c2)
        self.assertIn('subgraphs', stats)
        self.assertGreater(stats['subgraphs'], 1)
        self.assertGreater(stats['op_Identity'], 2)

        new_model = onnx_remove_node_redundant(model_def)
        stats2 = onnx_statistics(new_model, optim=False)
        self.assertEqual(stats['subgraphs'], 2)
        # The test is unstable, probably due to variables names.
        # They should be renamed before checking redundancy.
        self.assertIn(stats2['subgraphs'], (1, 2))

        oinf1 = OnnxInference(model_def)
        oinf2 = OnnxInference(new_model)
        y1 = oinf1.run({'input': x})['cdist']
        y2 = oinf2.run({'input': x})['cdist']
        self.assertEqualArray(y1, y2)

        new_model = onnx_remove_node_redundant(model_def)
        stats3 = onnx_statistics(new_model, optim=False)
        self.assertEqual(stats2, stats3)

        new_model = onnx_remove_node(model_def)
        stats3 = onnx_statistics(new_model, optim=False)
        self.assertLess(stats3['size'], stats2['size'])
        self.assertLess(stats3['nnodes'], stats2['nnodes'])
        self.assertLess(stats3['op_Identity'], stats2['op_Identity'])
    def test_onnx_example_cdist_in_custom_ops(self):
        x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((3, 2))
        x2 = np.array([1.1, 2.1, 4.01, 5.01, 5.001, 4.001, 0, 0]).astype(
            np.float32).reshape((4, 2))
        opv = _TARGET_OPSET_
        cop = OnnxAdd(
            'input', 'input', op_version=opv)
        cop2 = OnnxIdentity(
            OnnxCDist(cop, x2, op_version=opv),
            output_names=['cdist'],
            op_version=opv)

        model_def = cop2.to_onnx(
            inputs=[('input', FloatTensorType([None, None]))],
            outputs=[('cdist', FloatTensorType())])

        try:
            sess = InferenceSession(model_def.SerializeToString())
        except RuntimeError as e:
            if "CDist is not a registered" in str(e):
                return
        res = sess.run(None, {'input': x})
        exp = scipy_cdist(x * 2, x2, metric="sqeuclidean")
        assert_almost_equal(exp, res[0], decimal=5)

        x = np.array([[6.1, 2.8, 4.7, 1.2],
                      [5.7, 3.8, 1.7, 0.3],
                      [7.7, 2.6, 6.9, 2.3],
                      [6.0, 2.9, 4.5, 1.5],
                      [6.8, 2.8, 4.8, 1.4],
                      [5.4, 3.4, 1.5, 0.4],
                      [5.6, 2.9, 3.6, 1.3],
                      [6.9, 3.1, 5.1, 2.3]], dtype=np.float32)
        cop = OnnxAdd(
            'input', 'input', op_version=opv)
        cop2 = OnnxIdentity(
            OnnxCDist(cop, x,
                      op_version=opv),
            output_names=['cdist'],
            op_version=opv)

        model_def = cop2.to_onnx(
            inputs=[('input', FloatTensorType([None, None]))],
            outputs=[('cdist', FloatTensorType())])

        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'input': x})
        exp = scipy_cdist(x * 2, x, metric="sqeuclidean")
        assert_almost_equal(exp, res[0], decimal=4)
Example #20
0
 def test_onnxt_run(self):
     idi = numpy.identity(2, dtype=numpy.float32)
     idi2 = (numpy.identity(2) * 2).astype(numpy.float32)
     onx = OnnxAdd(OnnxAdd('X', idi, op_version=TARGET_OPSET),
                   idi2,
                   output_names=['Y'],
                   op_version=TARGET_OPSET)
     model_def = onx.to_onnx({'X': idi.astype(numpy.float32)},
                             target_opset=TARGET_OPSET)
     oinf = OnnxInference(model_def)
     X = numpy.array([[1, 1], [3, 3]])
     y = oinf.run({'X': X.astype(numpy.float32)})
     exp = numpy.array([[4, 1], [3, 6]], dtype=numpy.float32)
     self.assertEqual(list(y), ['Y'])
     self.assertEqualArray(y['Y'], exp)
 def test_onnxt_dot(self):
     idi = numpy.identity(2)
     idi2 = numpy.identity(2) * 2
     onx = OnnxAdd(OnnxAdd('X', idi), idi2, output_names=['Y'])
     model_def = onx.to_onnx({'X': idi.astype(numpy.float32)})
     oinf = OnnxInference(model_def)
     dot = oinf.to_dot()
     self.assertIn('Add [', dot)
     self.assertIn('Add1 [', dot)
     self.assertIn('Add\\n(Ad_Add)', dot)
     self.assertIn('Add\\n(Ad_Add1)', dot)
     self.assertIn('X -> Ad_Add;', dot)
     self.assertIn('Ad_Addcst1 -> Ad_Add1;', dot)
     self.assertIn('Ad_Addcst -> Ad_Add;', dot)
     self.assertIn('Ad_Add1 -> Y;', dot)
 def test_onnxt_graph(self):
     idi = numpy.identity(2)
     idi2 = numpy.identity(2) * 2
     onx = OnnxAdd(OnnxAdd('X', idi), idi2, output_names=['Y'])
     model_def = onx.to_onnx({'X': idi.astype(numpy.float32)})
     oinf = OnnxInference(model_def)
     js = oinf.to_sequence()
     self.assertIn('inits', js)
     self.assertIn('inputs', js)
     self.assertIn('outputs', js)
     self.assertIn('intermediate', js)
     self.assertIn('nodes', js)
     self.assertIn('sequence', js)
     self.assertEqual(len(js['sequence']), 2)
     self.assertEqual(len(js['intermediate']), 2)
Example #23
0
def _onnx_axpy(target_opset=None, dtype=numpy.float32):
    """
    Returns the ONNX graph for function
    :math:`Y = f(X1, X2, \\alpha) = \\alpha X1 + X2`.

    .. gdot::
        :script: DOT-SECTION

        from mlprodict.onnxrt import OnnxInference
        from onnxcustom.utils.onnx_function import function_onnx_graph

        model_onnx = function_onnx_graph('axpy')
        oinf = OnnxInference(model_onnx, inplace=False)

        print("DOT-SECTION", oinf.to_dot())
    """
    from skl2onnx.algebra.onnx_ops import OnnxAdd, OnnxMul
    res = OnnxAdd(OnnxMul('X1', 'alpha', op_version=target_opset),
                  'X2',
                  op_version=target_opset,
                  output_names=['Y'])
    var_type = dtype_to_var_type(dtype)
    varsx = [('X1', var_type()), ('X2', var_type()), ('alpha', var_type([1]))]
    onx = res.to_onnx(varsx,
                      outputs=[('Y', var_type())],
                      target_opset=target_opset)
    return onx
    def test_onnxt_idi(self):
        idi = numpy.identity(2)
        onx = OnnxAdd('X', idi, output_names=['Y'])
        model_def = onx.to_onnx({'X': idi.astype(numpy.float32)})

        oinf = OnnxInference(model_def)
        res = str(oinf)
        self.assertIn('op_type: "Add"', res)

        sb = model_def.SerializeToString()
        oinf = OnnxInference(sb)
        res = str(oinf)
        self.assertIn('op_type: "Add"', res)

        sb = BytesIO(model_def.SerializeToString())
        oinf = OnnxInference(sb)
        res = str(oinf)
        self.assertIn('op_type: "Add"', res)

        temp = get_temp_folder(__file__, "temp_onnxrt_idi")
        name = os.path.join(temp, "m.onnx")
        with open(name, "wb") as f:
            f.write(model_def.SerializeToString())

        oinf = OnnxInference(name)
        res = str(oinf)
        self.assertIn('op_type: "Add"', res)
Example #25
0
    def test_onnx_init_sparse_coo(self):
        row = np.array([0, 0, 1, 3, 1], dtype=np.float32)
        col = np.array([0, 2, 1, 3, 1], dtype=np.float32)
        data = np.array([1, 1, 1, 1, 1], dtype=np.float32)
        X = coo_matrix((data, (row, col)), shape=(4, 4))

        node = OnnxAdd(
            'X', X, output_names=['Y'],
            op_version=TARGET_OPSET)

        model_def = node.to_onnx(
            {'X': X}, outputs=[('Y', FloatTensorType())])

        try:
            sess = InferenceSession(model_def.SerializeToString())
        except (RuntimeError, OrtInvalidArgument):
            # Sparse tensor is not supported for constant.
            return
        try:
            res = sess.run(None, {'X': X})[0]
        except RuntimeError as e:
            # Sparse tensor is not supported for constant.
            warnings.warn(
                "Unable to run with %r\n---\n%s\n%s" % (
                    {'X': X}, model_def, e))
            return
        assert_almost_equal(X + X, res)
Example #26
0
 def test_onnx_simple_text_plot_toy(self):
     x = numpy.random.randn(10, 3).astype(numpy.float32)
     node1 = OnnxAdd('X', x, op_version=15)
     node2 = OnnxSub('X', x, op_version=15)
     node3 = OnnxAbs(node1, op_version=15)
     node4 = OnnxAbs(node2, op_version=15)
     node5 = OnnxDiv(node3, node4, op_version=15)
     node6 = OnnxAbs(node5, output_names=['Y'], op_version=15)
     onx = node6.to_onnx({'X': x.astype(numpy.float32)},
                         outputs={'Y': x},
                         target_opset=15)
     text = onnx_simple_text_plot(onx, verbose=False)
     expected = textwrap.dedent("""
     Add(X, Ad_Addcst) -> Ad_C0
       Abs(Ad_C0) -> Ab_Y0
     Identity(Ad_Addcst) -> Su_Subcst
       Sub(X, Su_Subcst) -> Su_C0
         Abs(Su_C0) -> Ab_Y02
         Div(Ab_Y0, Ab_Y02) -> Di_C0
           Abs(Di_C0) -> Y
     """).strip(" \n")
     self.assertIn(expected, text)
     text2, out, err = self.capture(
         lambda: onnx_simple_text_plot(onx, verbose=True))
     self.assertEqual(text, text2)
     self.assertIn('BEST:', out)
     self.assertEmpty(err)
Example #27
0
    def test_onnx_example_pdist_in(self):
        opv = _TARGET_OPSET_
        x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((3, 2))
        cop = OnnxAdd('input', 'input', op_version=opv)
        cop2 = OnnxIdentity(onnx_squareform_pdist(cop,
                                                  dtype=np.float32,
                                                  op_version=opv),
                            output_names=['pdist'],
                            op_version=opv)

        model_def = cop2.to_onnx(inputs=[('input',
                                          FloatTensorType([None, None]))],
                                 outputs=[('pdist', FloatTensorType())])

        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'input': x})
        exp = squareform(pdist(x * 2, metric="sqeuclidean"))
        assert_almost_equal(exp, res[0])

        x = np.array([1, 2, 4, 5]).astype(np.float32).reshape((2, 2))
        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'input': x})
        exp = squareform(pdist(x * 2, metric="sqeuclidean"))
        assert_almost_equal(exp, res[0])

        x = np.array([1, 2, 4, 5, 5, 6]).astype(np.float32).reshape((2, 3))
        x = np.array([1, 2, 4, 5, 5, 4]).astype(np.float32).reshape((2, 3))
        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'input': x})
        exp = squareform(pdist(x * 2, metric="sqeuclidean"))
        assert_almost_equal(exp, res[0])
Example #28
0
    def test_onnx_if_algebra_direct(self):

        opv = TARGET_OPSET
        x1 = np.array([[0, 3], [7, 0]], dtype=np.float32)
        x2 = np.array([[1, 0], [2, 0]], dtype=np.float32)

        node = OnnxAdd(
            'x1', 'x2', output_names=['absxythen'], op_version=opv)
        then_body = node.to_onnx(
            {'x1': x1, 'x2': x2}, target_opset=opv,
            outputs=[('absxythen', FloatTensorType())])
        node = OnnxSub(
            'x1', 'x2', output_names=['absxyelse'], op_version=opv)
        else_body = node.to_onnx(
            {'x1': x1, 'x2': x2}, target_opset=opv,
            outputs=[('absxyelse', FloatTensorType())])
        del else_body.graph.input[:]
        del then_body.graph.input[:]

        cond = OnnxGreater(
            OnnxReduceSum('x1', op_version=opv),
            OnnxReduceSum('x2', op_version=opv),
            op_version=opv)
        ifnode = OnnxIf(cond, then_branch=then_body.graph,
                        else_branch=else_body.graph,
                        op_version=opv, output_names=['y'])
        model_def = ifnode.to_onnx(
            {'x1': x1, 'x2': x2}, target_opset=opv,
            outputs=[('y', FloatTensorType())])

        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'x1': x1, 'x2': x2})
        assert_almost_equal(x1 + x2, res[0])
    def test_onnx_example_algebra(self):
        initial = np.array([0, 0]).astype(np.float32).reshape((2,))
        x = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2))

        opv = _TARGET_OPSET_
        add_node = OnnxAdd(
            'sum_in', 'next', output_names=['sum_out'],
            op_version=opv)
        id_node = OnnxIdentity(
            add_node, output_names=['scan_out'],
            op_version=opv)
        scan_body = id_node.to_onnx(
            {'sum_in': initial, 'next': initial},
            outputs=[('sum_out', FloatTensorType()),
                     ('scan_out', FloatTensorType())])

        node = OnnxScan('initial', 'x', output_names=['y', 'z'],
                        num_scan_inputs=1, body=scan_body.graph,
                        op_version=opv)
        model_def = node.to_onnx(
            {'initial': initial, 'x': x},
            outputs=[('y', FloatTensorType()),
                     ('z', FloatTensorType())])

        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'initial': initial, 'x': x})

        y = np.array([9, 12]).astype(np.float32).reshape((2,))
        z = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2))
        assert_almost_equal(y, res[0])
        assert_almost_equal(z, res[1])
Example #30
0
    def test_onnx_if_algebra_indirect_unnamed_clear_input(self):

        opv = TARGET_OPSET
        x1 = np.array([[0, 3], [7, 0]], dtype=np.float32)
        x2 = np.array([[1, 0], [2, 0]], dtype=np.float32)

        node_xy = OnnxMul('x1', 'x2', op_version=opv)
        node_then = OnnxAdd(
            'x1', 'xy', output_names=['absxythen'], op_version=opv)
        then_body = node_then.to_onnx(
            {'x1': x1, 'xy': x2}, target_opset=opv,
            outputs=[('absxythen', FloatTensorType())])
        node_else = OnnxSub(
            'x1', 'x2', output_names=['absxyelse'], op_version=opv)
        else_body = node_else.to_onnx(
            {'x1': x1, 'x2': x2}, target_opset=opv,
            outputs=[('absxyelse', FloatTensorType())])

        cond = OnnxGreater(
            OnnxReduceSum('x1', op_version=opv),
            OnnxReduceSum('x2', op_version=opv),
            op_version=opv)
        ifnode = OnnxIf(cond, then_branch=then_body.graph,
                        else_branch=else_body.graph,
                        op_version=opv, output_names=['y'],
                        global_context={'xy': node_xy},
                        clear_subgraph_inputs=True)
        model_def = ifnode.to_onnx(
            {'x1': x1, 'x2': x2}, target_opset=opv,
            outputs=[('y', FloatTensorType())])

        sess = InferenceSession(model_def.SerializeToString())
        res = sess.run(None, {'x1': x1, 'x2': x2})
        assert_almost_equal(x1 + x1 * x2, res[0])