|
| 1 | +# ------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for |
| 4 | +# license information. |
| 5 | +# -------------------------------------------------------------------------- |
| 6 | + |
| 7 | +from ..common._container import LibSvmModelContainer |
| 8 | +from ..common._topology import * |
| 9 | + |
| 10 | + |
| 11 | +def _parse_libsvm_simple_model(scope, model, inputs): |
| 12 | + ''' |
| 13 | + This function handles all non-pipeline models. |
| 14 | +
|
| 15 | + :param scope: Scope object |
| 16 | + :param model: A libsvm object (e.g., OneHotEncoder and LogisticRegression) |
| 17 | + :param inputs: A list of variables |
| 18 | + :return: A list of output variables which will be passed to next stage |
| 19 | + ''' |
| 20 | + |
| 21 | + if model.get_svm_type() in (0, 1): |
| 22 | + label_variable = scope.declare_local_variable('label', FloatTensorType()) |
| 23 | + probability_map_variable = scope.declare_local_variable('probabilities', FloatTensorType()) |
| 24 | + this_operator = scope.declare_local_operator("LibSvmSVC", model) |
| 25 | + this_operator.inputs = inputs |
| 26 | + this_operator.outputs.append(label_variable) |
| 27 | + this_operator.outputs.append(probability_map_variable) |
| 28 | + elif model.get_svm_type() in (4, 3): |
| 29 | + # We assume that all scikit-learn operator can only produce a single float tensor. |
| 30 | + variable = scope.declare_local_variable('variable', FloatTensorType()) |
| 31 | + this_operator = scope.declare_local_operator("LibSvmSVR", model) |
| 32 | + this_operator.inputs = inputs |
| 33 | + this_operator.outputs.append(variable) |
| 34 | + else: |
| 35 | + raise ValueError("Unknown SVM type '{0}'".format(model.get_svm_type())) |
| 36 | + return this_operator.outputs |
| 37 | + |
| 38 | + |
| 39 | +def _parse_libsvm(scope, model, inputs): |
| 40 | + ''' |
| 41 | + This is a delegate function. It doesn't nothing but invoke the correct parsing function according to the input |
| 42 | + model's type. |
| 43 | + :param scope: Scope object |
| 44 | + :param model: A scikit-learn object (e.g., OneHotEncoder and LogisticRegression) |
| 45 | + :param inputs: A list of variables |
| 46 | + :return: The output variables produced by the input model |
| 47 | + ''' |
| 48 | + return _parse_libsvm_simple_model(scope, model, inputs) |
| 49 | + |
| 50 | + |
| 51 | +def parse_libsvm(model, initial_types=None, target_opset=None, |
| 52 | + custom_conversion_functions=None, |
| 53 | + custom_shape_calculators=None): |
| 54 | + # Put svmlib object into an abstract container so that our framework |
| 55 | + # can work seamlessly on models created |
| 56 | + # with different machine learning tools. |
| 57 | + raw_model_container = LibSvmModelContainer(model) |
| 58 | + |
| 59 | + # Declare a computational graph. It will become a representation of |
| 60 | + # the input scikit-learn model after parsing. |
| 61 | + topology = Topology(raw_model_container, |
| 62 | + initial_types=initial_types, |
| 63 | + target_opset=target_opset, |
| 64 | + custom_conversion_functions=custom_conversion_functions, |
| 65 | + custom_shape_calculators=custom_shape_calculators) |
| 66 | + |
| 67 | + # Declare an object to provide variables' and operators' naming mechanism. |
| 68 | + # In contrast to CoreML, one global scope |
| 69 | + # is enough for parsing scikit-learn models. |
| 70 | + scope = topology.declare_scope('__root__') |
| 71 | + |
| 72 | + # Declare input variables. They should be the inputs of the scikit-learn model |
| 73 | + # you want to convert into ONNX. |
| 74 | + inputs = [] |
| 75 | + for var_name, initial_type in initial_types: |
| 76 | + inputs.append(scope.declare_local_variable(var_name, initial_type)) |
| 77 | + |
| 78 | + # The object raw_model_container is a part of the topology we're going to return. |
| 79 | + # We use it to store the inputs of |
| 80 | + # the libsvm's computational graph. |
| 81 | + for variable in inputs: |
| 82 | + raw_model_container.add_input(variable) |
| 83 | + |
| 84 | + # Parse the input libsvm model as a Topology object. |
| 85 | + outputs = _parse_libsvm(scope, model, inputs) |
| 86 | + |
| 87 | + # THe object raw_model_container is a part of the topology we're going to return. |
| 88 | + # We use it to store the outputs of |
| 89 | + # the scikit-learn's computational graph. |
| 90 | + for variable in outputs: |
| 91 | + raw_model_container.add_output(variable) |
| 92 | + |
| 93 | + return topology |
0 commit comments