Example #1
0
    def test_plot_onnx(self):

        cst = numpy.array([[1, 2]], dtype=numpy.float32)
        onx = OnnxConcat('X', 'Y', cst, output_names=['Z'],
                         op_version=12)
        X = numpy.array([[1, 2], [3, 4]], dtype=numpy.float64)
        Y = numpy.array([[8, 9], [10, 11], [12, 13]], dtype=numpy.float64)
        model_def = onx.to_onnx({'X': X.astype(numpy.float32),
                                 'Y': Y.astype(numpy.float32)},
                                outputs=[('Z', FloatTensorType([2]))],
                                target_opset=12)

        import matplotlib.pyplot as plt
        _, ax = plt.subplots(1, 1)

        try:
            plot_onnx(model_def, ax=ax)
        except FileNotFoundError as e:
            if "No such file or directory: 'dot'" in str(e):
                warnings.warn(
                    "Unable to test the dot syntax, dot is mssing", UserWarning)
                return
            raise e
        if __name__ == "__main__":
            temp = get_temp_folder(__file__, "temp_plot_onnx")
            img = os.path.join(temp, "img.png")
            plt.savefig(img)
            plt.show()
        plt.close('all')
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 #3
0
    def test_plot_onnx2(self):

        cst = numpy.array([[1, 2]], dtype=numpy.float32)
        onx = OnnxConcat('X', 'Y', cst, output_names=['Z'], op_version=12)
        X = numpy.array([[1, 2], [3, 4]], dtype=numpy.float64)
        Y = numpy.array([[8, 9], [10, 11], [12, 13]], dtype=numpy.float64)
        model_def = onx.to_onnx(
            {
                'X': X.astype(numpy.float32),
                'Y': Y.astype(numpy.float32)
            },
            outputs=[('Z', FloatTensorType([2]))],
            target_opset={
                '': 14,
                'ai.onnx.ml': 2
            })

        import matplotlib.pyplot as plt
        ax = numpy.array([0])
        self.assertRaise(lambda: plot_onnxs(model_def, model_def, ax=ax),
                         ValueError)

        try:
            ax = plot_onnxs(model_def, model_def, title=["GRAPH1", "GRAPH2"])
        except FileNotFoundError as e:
            if "No such file or directory: 'dot'" in str(e):
                warnings.warn("Unable to test the dot syntax, dot is mssing",
                              UserWarning)
                return
            raise e
        self.assertNotEmpty(ax)
        try:
            ax = plot_onnxs(model_def, model_def, title="GRAPH1")
        except FileNotFoundError as e:
            if "No such file or directory: 'dot'" in str(e):
                warnings.warn("Unable to test the dot syntax, dot is mssing",
                              UserWarning)
                return
            raise e
        self.assertNotEmpty(ax)
        try:
            ax = plot_onnxs(model_def, model_def, title="GRAPH1", ax='new')
        except FileNotFoundError as e:
            if "No such file or directory: 'dot'" in str(e):
                warnings.warn("Unable to test the dot syntax, dot is mssing",
                              UserWarning)
                return
            raise e
        self.assertNotEmpty(ax)
        if __name__ == "__main__":
            temp = get_temp_folder(__file__, "temp_plot_onnx2")
            img = os.path.join(temp, "img.png")
            plt.savefig(img)
            plt.show()
        plt.close('all')
 def test_onnxt_runtime_concat(self):
     cst = numpy.array([[1, 2]], dtype=numpy.float32)
     onx = OnnxConcat('X', 'Y', cst, output_names=['Z'])
     X = numpy.array([[1, 2], [3, 4]], dtype=numpy.float64)
     Y = numpy.array([[8, 9], [10, 11], [12, 13]], dtype=numpy.float64)
     model_def = onx.to_onnx(
         {
             'X': X.astype(numpy.float32),
             'Y': Y.astype(numpy.float32)
         },
         outputs=[('Z', FloatTensorType([2]))])
     oinf = OnnxInference(model_def)
     got = oinf.run({'X': X, 'Y': Y})
     self.assertEqual(list(sorted(got)), ['Z'])
     self.assertEqual(got['Z'].shape, (6, 2))
     exp = numpy.vstack([X, Y, cst])
     self.assertEqualArray(exp, got['Z'])
Example #5
0
    def test_plot_onnx(self):

        cst = numpy.array([[1, 2]], dtype=numpy.float32)
        onx = OnnxConcat('X', 'Y', cst, output_names=['Z'], op_version=12)
        X = numpy.array([[1, 2], [3, 4]], dtype=numpy.float64)
        Y = numpy.array([[8, 9], [10, 11], [12, 13]], dtype=numpy.float64)
        model_def = onx.to_onnx(
            {
                'X': X.astype(numpy.float32),
                'Y': Y.astype(numpy.float32)
            },
            outputs=[('Z', FloatTensorType([2]))],
            target_opset={
                '': 14,
                'ai.onnx.ml': 2
            })

        import matplotlib.pyplot as plt
        self.assertRaise(lambda: plot_onnxs(*[]), ValueError)

        try:
            ax = plot_onnxs(model_def, title="GRAPH")
        except FileNotFoundError as e:
            if "No such file or directory: 'dot'" in str(e):
                warnings.warn("Unable to test the dot syntax, dot is mssing",
                              UserWarning)
                return
            raise e
        self.assertNotEmpty(ax)

        try:
            ax = plot_onnxs(model_def, title="GRAPH", ax='new')
        except FileNotFoundError as e:
            if "No such file or directory: 'dot'" in str(e):
                warnings.warn("Unable to test the dot syntax, dot is mssing",
                              UserWarning)
                return
            raise e
        self.assertNotEmpty(ax)
        plt.close('all')
def custom_classifier_converter(scope, operator, container):
    op = operator.raw_operator
    X = operator.inputs[0]
    outputs = operator.outputs
    opv = container.target_opset
    y_list = [
        OnnxReshape(
            OnnxSubEstimator(est, X, op_version=opv)[1],
            np.array([-1, 1], dtype=np.int64), op_version=opv)
        for est in op.estimators_]
    y_matrix = OnnxConcat(*y_list, axis=1, op_version=opv)
    probs = OnnxSoftmax(y_matrix, axis=1, op_version=opv,
                        output_names=[outputs[1]])
    probs.add_to(scope, container)
    labels = OnnxArgMax(probs, axis=1, keepdims=0, op_version=opv,
                        output_names=[outputs[0]])
    labels.add_to(scope, container)