coremltools icon indicating copy to clipboard operation
coremltools copied to clipboard

Error converting sklearn linear regression model

Open IAINATTWATER opened this issue 4 years ago • 13 comments

❓Question

System Information

  • If applicable

Conversion fails with - Exception has occurred: NameError name '_tree' is not defined.

Model is a linear regression but converter appears to be looking for a decision tree one. There's also a warning about scikit-learn version 0.22.1 is not supported. Minimum required version: 0.17. Maximum required version: 0.19.2. Disabling scikit-learn conversion API.

anyone seen this?

Iain

IAINATTWATER avatar Jun 30 '20 17:06 IAINATTWATER

@IAINATTWATER thanks for reporting. We are looking into this.

anilkatti avatar Jul 01 '20 05:07 anilkatti

Is there a resolution for this ?

sriharsha-sammeta avatar Oct 26 '20 22:10 sriharsha-sammeta

Not that I’m aware of.

Iain.

On Oct 26, 2020, at 6:51 PM, Harsha Sammeta [email protected] wrote:

 Is there a resolution for this ?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

IAINATTWATER avatar Oct 26 '20 23:10 IAINATTWATER

+Also running into this issue when converting a RandomForestClassifier created in sklearn 0.23.2 (python 3.7). Coremltools conversion is being disabled due to the versioning mismatch.

Downgraded sklearn to 0.19.2 at the moment as a workaround, but this issue needs to be addressed as soon possible.

steven-dicristofaro avatar Nov 10 '20 23:11 steven-dicristofaro

Were you able to downgrade sklearn via pip? Whenever I try to downgrade to 0.19.2 or lower it fails.

imcauley avatar Nov 10 '20 23:11 imcauley

I use a PyCharm managed venv, but I just spun up another one through the terminal and ran:

pip install 'scikit-learn==0.19.2' 

and it worked fine. You could also try using the --force-reinstall flag if that would help.

Screen Shot 2020-11-10 at 6 46 42 PM

steven-dicristofaro avatar Nov 10 '20 23:11 steven-dicristofaro

Thanks. Will give it a try.

Sent from my iPhone

On Nov 10, 2020, at 6:46 PM, steven-dicristofaro [email protected] wrote:

 I use a PyCharm managed venv, but I just spun up another one through the terminal and ran:

pip install 'scikit-learn==0.19.2' and it worked fine. You could also try using the --force-reinstall flag if that would help.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

IAINATTWATER avatar Nov 10 '20 23:11 IAINATTWATER

also for those who have some flexibility in their model deployment form factor - just discovered this library for python that looks mighty interesting https://github.com/BayesWitnesses/m2cgen#supported-models . Haven't tried it out yet, but IN THEORY it seems we could just transpile to C and bring that into a swift app with a bridging header or via .podspec.

+Java transpiling for any Android guys out there that need this to be X-platform

steven-dicristofaro avatar Nov 11 '20 22:11 steven-dicristofaro

At least for for linear regression, it seems to work fine with the most recent version of Scikit Learn (0.24.1) if you just update the max supported version.

TobyRoseman avatar Feb 05 '21 21:02 TobyRoseman

Hi and sorry for the post but I have the same problem for SVC model from sklearn models. I load my model with pickle because it's also pre-trained, the model works on conda. But when I want to convert this to ml, I have : name '_tree' is not defined Anyone has the same problem with SVC ? Sorry again for the post :)

Eddy872 avatar Dec 10 '21 01:12 Eddy872

@Eddy872 This was my exact problem. I wish I had a better solution for you but I ended up just switching to a model that was actually supported. It's been so long I can't quite remember which one I used. Sorry to be the bearer of bad news :(

imcauley avatar Dec 10 '21 02:12 imcauley

@imcauley Thank you, maybe you can help me, which models are supported ? models from sklearn ?

Eddy872 avatar Dec 10 '21 03:12 Eddy872

I resolved! The problem is the Python version. You need creating a PyCharm Project with python version 3.7 and install scikit-learn 0.19.2

scaiciia avatar Jul 07 '22 22:07 scaiciia

We now support the latest version of Scikit-Learn (1.1.2). So I believe this is fixed.

Can anyone confirm this is fixed? Or give us step to reproduce the issue, if it is still an issue.

TobyRoseman avatar Oct 24 '22 19:10 TobyRoseman

Since I have not heard anything here, I'm going to close this issue. If this has not been fixed by supporting a new version of scikit-learn, please let us know.

TobyRoseman avatar Feb 15 '23 01:02 TobyRoseman

scikit-learn version 1.3.0 is not supported. Minimum required version: 0.17. Maximum required version: 1.1.2. Disabling scikit-learn conversion API.

Same thing again, and probably will keep being a thing since it's a hardcoded semver

coremltools/_deps/__init__.py line 58 if anyone needs to get around the pin

rromanchuk avatar Oct 05 '23 00:10 rromanchuk

Getting error when using flag --quantize-nbits 6. ValueError: Input contains NaN, infinity or a value too large for dtype('float64'). I tried downgrading python, torch and scikit-learn but nothing changes. Anyone has a tip on how to solve this?

citruslab avatar Nov 21 '23 19:11 citruslab

I got

  File "/Users/user2/Documents/anchorz/data_research/lib/action_detection/batch_convert/training.py", line 67, in use_sklearn
    coreml_model = ct.converters.sklearn.convert(gb_clf)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/coremltools/converters/sklearn/_converter.py", line 148, in convert
    from ._converter_internal import _convert_sklearn_model
  File "/opt/homebrew/lib/python3.11/site-packages/coremltools/converters/sklearn/_converter_internal.py", line 24, in <module>
    from . import (_SVC, _SVR, _decision_tree_classifier, _decision_tree_regressor,
  File "/opt/homebrew/lib/python3.11/site-packages/coremltools/converters/sklearn/_decision_tree_regressor.py", line 16, in <module>
    sklearn_class = _tree.DecisionTreeRegressor
                    ^^^^^
NameError: name '_tree' is not defined

using: scikit-learn==1.3.0 python==3.11.5


resolved it by downgrade to scikit-learn==1.1.2 and python==3.9.6.

anz-ryu avatar Dec 11 '23 02:12 anz-ryu