|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +from sklearn.base import is_regressor |
| 4 | +from ..proto import onnx_proto |
| 5 | +from ..common._registration import register_converter |
| 6 | +from ..common._topology import Scope, Operator |
| 7 | +from ..common._container import ModelComponentContainer |
| 8 | +from ..common._apply_operation import apply_cast, apply_concat, apply_reshape |
| 9 | +from ..common.data_types import guess_proto_type, Int64TensorType |
| 10 | +from .._supported_operators import sklearn_operator_name_map |
| 11 | + |
| 12 | + |
| 13 | +def _iteration_one_versus(scope, container, inputs, i, estimator, cl_type, |
| 14 | + proto_dtype, use_raw_scores=True, prob_shape=None): |
| 15 | + op_type = sklearn_operator_name_map[type(estimator)] |
| 16 | + |
| 17 | + this_operator = scope.declare_local_operator(op_type, raw_model=estimator) |
| 18 | + this_operator.inputs = inputs |
| 19 | + |
| 20 | + if is_regressor(estimator): |
| 21 | + score_name = scope.declare_local_variable('score_%d' % i, cl_type()) |
| 22 | + this_operator.outputs.append(score_name) |
| 23 | + |
| 24 | + if hasattr(estimator, 'coef_') and len(estimator.coef_.shape) == 2: |
| 25 | + raise RuntimeError( |
| 26 | + "OneVsRestClassifier or OneVsOneClassifier accepts " |
| 27 | + "regressor with only one target.") |
| 28 | + p1 = score_name.onnx_name |
| 29 | + return None, None, p1 |
| 30 | + |
| 31 | + if container.has_options(estimator, 'raw_scores'): |
| 32 | + options = {'raw_scores': use_raw_scores} |
| 33 | + elif container.has_options(estimator, 'zipmap'): |
| 34 | + options = {'zipmap': False} |
| 35 | + else: |
| 36 | + options = None |
| 37 | + if options is not None: |
| 38 | + container.add_options(id(estimator), options) |
| 39 | + scope.add_options(id(estimator), options) |
| 40 | + |
| 41 | + label_name = scope.declare_local_variable( |
| 42 | + 'label_%d' % i, Int64TensorType()) |
| 43 | + prob_name = scope.declare_local_variable( |
| 44 | + 'proba_%d' % i, inputs[0].type.__class__()) |
| 45 | + this_operator.outputs.append(label_name) |
| 46 | + this_operator.outputs.append(prob_name) |
| 47 | + |
| 48 | + # gets the label for the class 1 |
| 49 | + label = scope.get_unique_variable_name('lab_%d' % i) |
| 50 | + apply_reshape(scope, label_name.onnx_name, label, container, |
| 51 | + desired_shape=(-1, 1)) |
| 52 | + cast_label = scope.get_unique_variable_name('cast_lab_%d' % i) |
| 53 | + apply_cast(scope, label, cast_label, container, |
| 54 | + to=proto_dtype) |
| 55 | + |
| 56 | + # get the probability for the class 1 |
| 57 | + if prob_shape is None: |
| 58 | + # shape to use to reshape score |
| 59 | + cst0 = scope.get_unique_variable_name('cst0') |
| 60 | + container.add_initializer(cst0, onnx_proto.TensorProto.INT64, [1], [0]) |
| 61 | + shape = scope.get_unique_variable_name('shape') |
| 62 | + container.add_node('Shape', [inputs[0].full_name], [shape]) |
| 63 | + first_dim = scope.get_unique_variable_name('dim') |
| 64 | + container.add_node('Gather', [shape, cst0], [first_dim]) |
| 65 | + cst_1 = scope.get_unique_variable_name('cst_1') |
| 66 | + container.add_initializer( |
| 67 | + cst_1, onnx_proto.TensorProto.INT64, [1], [-1]) |
| 68 | + prob_shape = scope.get_unique_variable_name('shape') |
| 69 | + apply_concat(scope, [first_dim, cst_1], prob_shape, container, axis=0) |
| 70 | + |
| 71 | + prob_reshaped = scope.get_unique_variable_name('prob_%d' % i) |
| 72 | + container.add_node('Reshape', [prob_name.onnx_name, prob_shape], |
| 73 | + [prob_reshaped]) |
| 74 | + |
| 75 | + cst1 = scope.get_unique_variable_name('cst1') |
| 76 | + container.add_initializer(cst1, onnx_proto.TensorProto.INT64, [1], [1]) |
| 77 | + cst2 = scope.get_unique_variable_name('cst2') |
| 78 | + container.add_initializer(cst2, onnx_proto.TensorProto.INT64, [1], [2]) |
| 79 | + |
| 80 | + prob1 = scope.get_unique_variable_name('prob1_%d' % i) |
| 81 | + container.add_node( |
| 82 | + 'Slice', [prob_reshaped, cst1, cst2, cst1], prob1) |
| 83 | + return prob_shape, cast_label, prob1 |
| 84 | + |
| 85 | + |
| 86 | +def convert_one_vs_one_classifier(scope: Scope, operator: Operator, |
| 87 | + container: ModelComponentContainer): |
| 88 | + |
| 89 | + proto_dtype = guess_proto_type(operator.inputs[0].type) |
| 90 | + if proto_dtype != onnx_proto.TensorProto.DOUBLE: |
| 91 | + proto_dtype = onnx_proto.TensorProto.FLOAT |
| 92 | + op = operator.raw_operator |
| 93 | + |
| 94 | + # shape to use to reshape score |
| 95 | + cst0 = scope.get_unique_variable_name('cst0') |
| 96 | + container.add_initializer(cst0, onnx_proto.TensorProto.INT64, [1], [0]) |
| 97 | + cst1 = scope.get_unique_variable_name('cst1') |
| 98 | + container.add_initializer(cst1, onnx_proto.TensorProto.INT64, [1], [1]) |
| 99 | + cst2 = scope.get_unique_variable_name('cst2') |
| 100 | + container.add_initializer(cst2, onnx_proto.TensorProto.INT64, [1], [2]) |
| 101 | + shape = scope.get_unique_variable_name('shape') |
| 102 | + container.add_node('Shape', [operator.inputs[0].full_name], [shape]) |
| 103 | + first_dim = scope.get_unique_variable_name('dim') |
| 104 | + container.add_node('Gather', [shape, cst0], [first_dim]) |
| 105 | + cst_1 = scope.get_unique_variable_name('cst_1') |
| 106 | + container.add_initializer(cst_1, onnx_proto.TensorProto.INT64, [1], [-1]) |
| 107 | + prob_shape = scope.get_unique_variable_name('shape') |
| 108 | + apply_concat(scope, [first_dim, cst_1], prob_shape, container, axis=0) |
| 109 | + |
| 110 | + label_names = [] |
| 111 | + prob_names = [] |
| 112 | + prob_shape = None |
| 113 | + cl_type = operator.inputs[0].type.__class__ |
| 114 | + for i, estimator in enumerate(op.estimators_): |
| 115 | + prob_shape, cast_label, prob1 = _iteration_one_versus( |
| 116 | + scope, container, operator.inputs, i, estimator, cl_type, |
| 117 | + proto_dtype, True, prob_shape=prob_shape) |
| 118 | + |
| 119 | + label_names.append(cast_label) |
| 120 | + prob_names.append(prob1) |
| 121 | + |
| 122 | + conc_lab_name = scope.get_unique_variable_name('concat_out_ovo_label') |
| 123 | + apply_concat(scope, label_names, conc_lab_name, container, axis=1) |
| 124 | + conc_prob_name = scope.get_unique_variable_name('concat_out_ovo_prob') |
| 125 | + apply_concat(scope, prob_names, conc_prob_name, container, axis=1) |
| 126 | + |
| 127 | + # calls _ovr_decision_function |
| 128 | + this_operator = scope.declare_local_operator( |
| 129 | + "SklearnOVRDecisionFunction", op) |
| 130 | + |
| 131 | + cl_type = operator.inputs[0].type.__class__ |
| 132 | + label = scope.declare_local_variable("label", cl_type()) |
| 133 | + container.add_node('Identity', [conc_lab_name], [label.onnx_name]) |
| 134 | + prob_score = scope.declare_local_variable("prob_score", cl_type()) |
| 135 | + container.add_node('Identity', [conc_prob_name], [prob_score.onnx_name]) |
| 136 | + |
| 137 | + this_operator.inputs.append(label) |
| 138 | + this_operator.inputs.append(prob_score) |
| 139 | + |
| 140 | + ovr_name = scope.declare_local_variable('ovr_output', cl_type()) |
| 141 | + this_operator.outputs.append(ovr_name) |
| 142 | + |
| 143 | + output_name = operator.outputs[1].full_name |
| 144 | + container.add_node('Identity', [ovr_name.onnx_name], [output_name]) |
| 145 | + |
| 146 | + container.add_node( |
| 147 | + 'ArgMax', 'ovr_output', operator.outputs[0].full_name, axis=1) |
| 148 | + |
| 149 | + |
| 150 | +register_converter('SklearnOneVsOneClassifier', |
| 151 | + convert_one_vs_one_classifier, |
| 152 | + options={'zipmap': [True, False, 'columns'], |
| 153 | + 'nocl': [True, False], |
| 154 | + 'output_class_labels': [False, True]}) |
0 commit comments