I have a trained scikit-learn model that uses a multi-output decision tree (as a RandomForestRegressor
). No custom configuration was explicitly made to the Random Forest Regression model to enable multi-output behavior, as multi-output behavior is built-in. Basically, as long as you fit multi-output training data to the model, the model will switch to multi-output mode behind the scenes.
Additionally, the RandomForestRegressor
is a supported transformer that the CoreML conversion scripts supply. However, during the conversion, I get this error w/ stack trace:
ValueError: Expected only 1 output in the scikit-learn tree.
Traceback (most recent call last):
File "/Users/user0/Desktop/model_convert.py", line 7, in <module>
coreml_model = sklearn_to_ml.convert(model)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_converter.py", line 146, in convert
sk_obj, input_features, output_feature_names, class_labels = None)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_converter_internal.py", line 297, in _convert_sklearn_model
last_spec = last_sk_m.convert(last_sk_obj, current_input_features, output_features)._spec
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_random_forest_regressor.py", line 53, in convert
return _MLModel(_convert_tree_ensemble(model, feature_names, target))
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 195, in convert_tree_ensemble
scaling = scaling, mode = mode, n_classes = n_classes, tree_index = tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 68, in _recurse
_recurse(coreml_tree, scikit_tree, tree_id, left_child_id, scaling, mode, n_classes, tree_index)
File "/Library/Python/2.7/site-packages/coremltools/converters/sklearn/_tree_ensemble.py", line 75, in _recurse
raise ValueError('Expected only 1 output in the scikit-learn tree.')
ValueError: Expected only 1 output in the scikit-learn tree.
The simple conversion code is below:
from coremltools.converters import sklearn as sklearn_to_ml
from sklearn.externals import joblib
model = joblib.load("ms5000.pkl")
print("Converting model")
coreml_model = sklearn_to_ml.convert(model)
print("Saving CoreML model")
coreml_model.save("ms5000.mlmodel")
What can I do to enable the CoreML conversion script to handle multi-output decision trees? Is there a change I can make to the existing scripts without reinventing the wheel with a new script entirely?
CoreML is (right now) a brand new thing, so there is currently not any known sources of third-party conversion scripts.
The "Models" section of the coremltools
documentation provides extensive documentation of how to use Python to produce a CoreML model. That being said, you can translate any machine learning model to a CoreML model using the model interfaces provided in the documentation.
At the moment, coremltools
does not support multi-output regression models. If you don't wish to reinvent the wheel, you will need to convert the model to a single output model by introducing a new input that corresponds to which output the current prediction is for.
Either way, the documentation is there so that should get you started.