|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +import unittest |
| 3 | +import packaging.version as pv |
| 4 | +import onnx |
| 5 | +from sklearn.utils._testing import ignore_warnings |
| 6 | +from sklearn.exceptions import ConvergenceWarning |
| 7 | +from onnxruntime import __version__ as ort_version |
| 8 | + |
| 9 | + |
| 10 | +class TestInvestigateOnnxmltools(unittest.TestCase): |
| 11 | + |
| 12 | + @unittest.skipIf( |
| 13 | + pv.Version(ort_version) < pv.Version("1.17.3"), |
| 14 | + reason="opset 19 not implemented", |
| 15 | + ) |
| 16 | + @ignore_warnings(category=(ConvergenceWarning, FutureWarning)) |
| 17 | + def test_issue_1102(self): |
| 18 | + from typing import Any |
| 19 | + from sklearn.datasets import make_regression |
| 20 | + import lightgbm |
| 21 | + import numpy |
| 22 | + import onnxruntime |
| 23 | + import skl2onnx |
| 24 | + from onnx.reference import ReferenceEvaluator |
| 25 | + from onnxmltools.convert.lightgbm.operator_converters.LightGbm import ( |
| 26 | + convert_lightgbm, |
| 27 | + ) |
| 28 | + from skl2onnx import update_registered_converter, to_onnx |
| 29 | + from skl2onnx.common import shape_calculator |
| 30 | + from sklearn import base, multioutput, pipeline, preprocessing |
| 31 | + |
| 32 | + def Normalizer() -> list[tuple[str, Any]]: |
| 33 | + return [ |
| 34 | + ("cast64", skl2onnx.sklapi.CastTransformer(dtype=numpy.float64)), |
| 35 | + ("scaler", preprocessing.StandardScaler()), |
| 36 | + ("cast32", skl2onnx.sklapi.CastTransformer(dtype=numpy.float32)), |
| 37 | + ] |
| 38 | + |
| 39 | + def Embedder(**kwargs: dict[str, Any]) -> list[tuple[str, Any]]: |
| 40 | + return [("basemodel", lightgbm.LGBMRegressor(**kwargs))] |
| 41 | + |
| 42 | + def BoL2EmotionV2( |
| 43 | + backbone_kwargs: dict[str, Any] | None = None, |
| 44 | + ) -> base.BaseEstimator: |
| 45 | + backbone = Embedder(**(backbone_kwargs or {})) |
| 46 | + normalizer = Normalizer() |
| 47 | + model = pipeline.Pipeline([*normalizer, *backbone]) |
| 48 | + return multioutput.MultiOutputRegressor(model) |
| 49 | + |
| 50 | + model = BoL2EmotionV2() |
| 51 | + X, y = make_regression(100, n_features=4, n_targets=2) |
| 52 | + model.fit(X, y) |
| 53 | + |
| 54 | + update_registered_converter( |
| 55 | + lightgbm.LGBMRegressor, |
| 56 | + "LightGbmLGBMRegressor", |
| 57 | + shape_calculator.calculate_linear_regressor_output_shapes, |
| 58 | + convert_lightgbm, |
| 59 | + options={"split": None}, |
| 60 | + ) |
| 61 | + |
| 62 | + sample = X.astype(numpy.float32) |
| 63 | + |
| 64 | + exported = to_onnx( |
| 65 | + model, |
| 66 | + X=sample, |
| 67 | + name="BoL2emotion", |
| 68 | + target_opset={"": 19, "ai.onnx.ml": 2}, |
| 69 | + ) |
| 70 | + expected = model.predict(sample) |
| 71 | + onnx.shape_inference.infer_shapes(exported) |
| 72 | + |
| 73 | + ref = ReferenceEvaluator(exported) |
| 74 | + got = ref.run(None, dict(X=sample))[0] |
| 75 | + numpy.testing.assert_allclose(expected, got, 1e-4) |
| 76 | + |
| 77 | + sess = onnxruntime.InferenceSession( |
| 78 | + exported.SerializeToString(), providers=["CPUExecutionProvider"] |
| 79 | + ) |
| 80 | + got = sess.run(None, dict(X=sample))[0] |
| 81 | + numpy.testing.assert_allclose(expected, got, 1e-4) |
| 82 | + |
| 83 | + with open("dump_model.onnx", "wb") as f: |
| 84 | + f.write(exported.SerializeToString()) |
| 85 | + |
| 86 | + sess = onnxruntime.InferenceSession( |
| 87 | + "dump_model.onnx", providers=["CPUExecutionProvider"] |
| 88 | + ) |
| 89 | + got = sess.run(None, dict(X=sample))[0] |
| 90 | + numpy.testing.assert_allclose(expected, got, 1e-4) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + unittest.main(verbosity=2) |
0 commit comments