How to Convert a MLPC from SKLearn to CoreML

291 Views Asked by At

I am currently working on a project to train, build and deploy a Machine Learning model for a course. I have trained the following model:

# Modelo Nº 5
from sklearn.neural_network import MLPClassifier
model_5 = MLPClassifier(hidden_layer_sizes = (TOTAL_INPUTS,),
                        activation = 'relu', 
                        solver = 'adam', 
                        batch_size = 1000, 
                        verbose = True, 
                        early_stopping = True, 
                        n_iter_no_change = 5,
                        max_iter = 100,
                        validation_fraction = 0.2)
model_5.fit(x_train, y_train.reshape((y_train.shape[0], )))

I would like to know if its possible to convert this trained model to the .mlmodel format of CoreML to then use it in a simple iOS App.

I've tried the following code:

import coremltools
coreml_model = coremltools.converters.sklearn.convert(model_5)
coreml_model.short_description = "CIFAR10 MLPC SKLearn Classifier"
coreml_model.save("cifar10_sklearn_mlpc.mlmodel")

But I get this error:

error screenshot

Thanks in advance

1

There are 1 best solutions below

0
On

Installing an older version of sklearn will get rid of that error, but then you will get a new one that says that mlpclassifier is not compatible with coremltools.converters.sklearn. So the answer is that you can't convert an MLPC to coreml simply because the coremltools library doesn't support that specific conversion. A list of sklearn models that can be used to convert to coreml can be found at the link below. I have found that grade boosting classifier performs with the most similar accuracy, but that might only apply to my application.

https://apple.github.io/coremltools/source/coremltools.converters.sklearn.html