onnx / sklearn-onnx

Convert scikit-learn models and pipelines to ONNX
Apache License 2.0
557 stars 104 forks source link

Type case unsupported for symbolic shape inference. inferred=5 when running Lightgbm and XGboost models for check_onnx_model_mobile_usability #858

Open maroxtn opened 2 years ago

maroxtn commented 2 years ago

Bug Report

When following the guide on how to convert lightgbm and xgboost classifiers to the onnx format, then checking if the model is mobile compatible python -m onnxruntime.tools.check_onnx_model_mobile_usability pipeline_xgboost.onnx I get this error:

INFO:  Checking pipeline_xgboost.onnx for usability with ORT Mobile.
Traceback (most recent call last):
  File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.7/dist-packages/onnxruntime/tools/check_onnx_model_mobile_usability.py", line 60, in <module>
    check_usability()
  File "/usr/local/lib/python3.7/dist-packages/onnxruntime/tools/check_onnx_model_mobile_usability.py", line 42, in check_usability
    try_eps = usability_checker.analyze_model(args.model_path, skip_optimize=False, logger=logger)
  File "/usr/local/lib/python3.7/dist-packages/onnxruntime/tools/mobile_helpers/usability_checker.py", line 518, in analyze_model
    try_eps = checker(str(model_path.resolve(strict=True)), logger)
  File "/usr/local/lib/python3.7/dist-packages/onnxruntime/tools/mobile_helpers/usability_checker.py", line 443, in checker
    model_with_shape_info = onnx.shape_inference.infer_shapes(model)
  File "/usr/local/lib/python3.7/dist-packages/onnx/shape_inference.py", line 41, in infer_shapes
    inferred_model_str = C.infer_shapes(model_str, check_type, strict_mode, data_prop)
onnx.onnx_cpp2py_export.shape_inference.InferenceError: [ShapeInferenceError] (op_type:ZipMap, node name: ZipMap): [ShapeInferenceError] type case unsupported for symbolic shape inference. inferred=5

Guides I followed: http://onnx.ai/sklearn-onnx/auto_tutorial/plot_gexternal_lightgbm.html and https://onnx.ai/sklearn-onnx/auto_tutorial/plot_gexternal_xgboost.html .

System information

Reproduction instructions

Expected behavior

The model passing the mobile compatibility check

Notes

I was able to isolate the error by :

import onnx
model = onnx.load("pipeline_xgboost.onnx")
onnx.shape_inference.infer_shapes(model)

But even then, I couldn't figure out what's wrong with the shapes.

xadupre commented 2 years ago

For historical reason, the converter adds an operator ZipMap by default to represent probabilities. To remove it and get a matrix of probabilities instead of a zipmap operator, argument options={'zipmap': False} must be added.