Пример #1
0
    def test_glm_classifier(self):
        iris = load_iris()
        X = iris.data[:, :2]
        y = iris.target
        y[y == 2] = 1

        # scikit-learn has changed the default value for multi_class.
        lr = LogisticRegression(multi_class='ovr')
        lr.fit(X, y)
        lr_coreml = coremltools.converters.sklearn.convert(lr)
        lr_onnx = convert(lr_coreml.get_spec())
        self.assertTrue(lr_onnx is not None)
        self.validate_zipmap(lr_onnx)
        dump_data_and_model(X.astype(numpy.float32), lr, lr_onnx, basename="CmlbinLogitisticRegression",
                            allow_failure="StrictVersion(onnx.__version__) < StrictVersion('1.3.0')")

        # Ensure there is a probability output
        svm = LinearSVC()
        svm.fit(X, y)
        svm_coreml = coremltools.converters.sklearn.convert(svm)
        svm_onnx = convert(svm_coreml.get_spec())
        self.assertTrue(svm_onnx is not None)
        self.validate_zipmap(svm_onnx)
        dump_data_and_model(X.astype(numpy.float32), svm, svm_onnx, basename="CmlBinLinearSVC-NoProb",
                            allow_failure=True)
    def test_glm_regressor(self):
        X, y = make_regression(n_features=4, random_state=0)

        lr = LinearRegression()
        lr.fit(X, y)
        lr_coreml = coremltools.converters.sklearn.convert(lr)
        lr_onnx = convert(lr_coreml.get_spec())
        self.assertTrue(lr_onnx is not None)

        svr = LinearSVR()
        svr.fit(X, y)
        svr_coreml = coremltools.converters.sklearn.convert(svr)
        svr_onnx = convert(svr_coreml.get_spec())
        self.assertTrue(svr_onnx is not None)
 def test_support_vector_classifier_multiclass_with_prob(self):
     svm, X = self._fit_multi_classification(SVC(probability=True, gamma=0.5))
     svm_coreml = coremltools.converters.sklearn.convert(svm)
     svm_onnx = convert(svm_coreml.get_spec())
     self.assertTrue(svm_onnx is not None)
     self.validate_zipmap(svm_onnx)
     self._check_model_outputs(svm_onnx, ['classLabel', 'classProbability'])
 def test_one_hot_encoder(self):
     script_dir = os.path.dirname(__file__)
     relative_path = "../data/onehot_simple.mlmodel"
     abs_file = os.path.join(script_dir, relative_path)
     model_coreml = coremltools.utils.load_spec(abs_file)
     model_onnx = convert(model_coreml)
     self.assertTrue(model_onnx is not None)
Пример #5
0
 def test_scaler(self):
     model = StandardScaler()
     data = numpy.array([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]], dtype=numpy.float32)
     model.fit(data)
     model_coreml = coremltools.converters.sklearn.convert(model)
     model_onnx = convert(model_coreml.get_spec())
     self.assertTrue(model_onnx is not None)
     dump_data_and_model(data, model, model_onnx, basename="CmlStandardScalerFloat32")
    def test_support_vector_regressor(self):
        X, y = make_regression(n_features=4, random_state=0)

        svm = SVR()
        svm.fit(X, y)
        svm_coreml = coremltools.converters.sklearn.convert(svm)
        svm_onnx = convert(svm_coreml.get_spec())
        self.assertTrue(svm_onnx is not None)
 def test_tree_ensemble_classifier(self):
     X = [[0, 1], [1, 1], [2, 0]]
     y = [1, 0, 1]
     model = RandomForestClassifier().fit(X, y)
     model_coreml = coremltools.converters.sklearn.convert(model)
     model_onnx = convert(model_coreml.get_spec())
     self.assertTrue(model_onnx is not None)
     self.validate_zipmap(model_onnx)
 def test_support_vector_classifier_multiclass_no_prob(self):
     svm, X = self._fit_multi_classification(SVC(gamma=0.5))
     svm_coreml = coremltools.converters.sklearn.convert(svm)
     svm_onnx = convert(svm_coreml.get_spec())
     self.assertTrue(svm_onnx is not None)
     nodes = svm_onnx.graph.node
     self.assertEqual(len(nodes), 1)
     self._check_model_outputs(svm_onnx, ['classLabel'])
 def test_support_vector_classifier_binary_no_prob(self):
     svm = self._fit_binary_classification(SVC())
     svm_coreml = coremltools.converters.sklearn.convert(svm)
     svm_onnx = convert(svm_coreml.get_spec())
     self.assertTrue(svm_onnx is not None)
     # This should not have a probability output and will be a single node
     nodes = svm_onnx.graph.node
     self.assertEqual(len(nodes), 1)
     self._check_model_outputs(svm_onnx, ['classLabel'])
Пример #10
0
 def test_support_vector_regressor(self):
     X, y = make_regression(n_features=4, random_state=0)
  
     svm = SVR(gamma=1./len(X))
     svm.fit(X, y)
     svm_coreml = coremltools.converters.sklearn.convert(svm)
     svm_onnx = convert(svm_coreml.get_spec())
     self.assertTrue(svm_onnx is not None)
     dump_data_and_model(X.astype(numpy.float32), svm, svm_onnx, basename="CmlRegSVR-Dec3")
 def test_dict_vectorizer(self):
     model = DictVectorizer()
     data = [{'amy': 1., 'chin': 200.}, {'nice': 3., 'amy': 1.}]
     model.fit_transform(data)
     model_coreml = coremltools.converters.sklearn.convert(model)
     model_onnx = convert(model_coreml.get_spec())
     self.assertTrue(model_onnx is not None)
     dump_data_and_model(data, model, model_onnx, basename="CmlDictVectorizer-OneOff-SkipDim1",
                         allow_failure="StrictVersion(onnx.__version__) < StrictVersion('1.3.0')")
Пример #12
0
 def test_tree_ensemble_regressor(self):
     X, y = make_regression(n_features=4, random_state=0)
     model = RandomForestRegressor().fit(X, y)
     model_coreml = coremltools.converters.sklearn.convert(model)
     model_onnx = convert(model_coreml.get_spec())
     self.assertTrue(model_onnx is not None)
     dump_data_and_model(X.astype(numpy.float32),
                         model,
                         model_onnx,
                         basename="CmlRegRandomForestRegressor-Dec3")
Пример #13
0
    def test_glm_classifier(self):
        iris = load_iris()
        X = iris.data[:, :2]
        y = iris.target
        y[y == 2] = 1

        lr = LogisticRegression()
        lr.fit(X, y)
        lr_coreml = coremltools.converters.sklearn.convert(lr)
        lr_onnx = convert(lr_coreml.get_spec())
        self.assertTrue(lr_onnx is not None)
        self.validate_zipmap(lr_onnx)

        # Ensure there is a probability output
        svm = LinearSVC()
        svm.fit(X, y)
        svm_coreml = coremltools.converters.sklearn.convert(svm)
        svm_onnx = convert(svm_coreml.get_spec())
        self.assertTrue(svm_onnx is not None)
        self.validate_zipmap(svm_onnx)
 def test_support_vector_classifier_binary_no_prob(self):
     svm, X = self._fit_binary_classification(SVC(gamma=0.5))
     svm_coreml = coremltools.converters.sklearn.convert(svm)
     svm_onnx = convert(svm_coreml.get_spec())
     self.assertTrue(svm_onnx is not None)
     # This should not have a probability output and will be a single node
     nodes = svm_onnx.graph.node
     self.assertEqual(len(nodes), 1)
     self._check_model_outputs(svm_onnx, ['classLabel'])
     dump_data_and_model(X, svm, svm_onnx, basename="CmlBinSVC-Out0",
                         allow_failure=True)
Пример #15
0
 def test_imputer(self):
     model = Imputer(missing_values='NaN', strategy='mean', axis=0)
     data = [[1, 2], [np.nan, 3], [7, 6]]
     model.fit(data)
     model_coreml = coremltools.converters.sklearn.convert(model)
     model_onnx = convert(model_coreml.get_spec())
     self.assertTrue(model_onnx is not None)
     dump_data_and_model(np.array(data, dtype=np.float32),
                         model,
                         model_onnx,
                         basename="CmlImputerMeanFloat32")
Пример #16
0
    def test_glm_regressor(self):
        X, y = make_regression(n_features=4, random_state=0)

        lr = LinearRegression()
        lr.fit(X, y)
        lr_coreml = coremltools.converters.sklearn.convert(lr)
        lr_onnx = convert(lr_coreml.get_spec())
        self.assertTrue(lr_onnx is not None)
        dump_data_and_model(X.astype(numpy.float32),
                            lr,
                            lr_onnx,
                            basename="CmlLinearRegression-Dec4")

        svr = LinearSVR()
        svr.fit(X, y)
        svr_coreml = coremltools.converters.sklearn.convert(svr)
        svr_onnx = convert(svr_coreml.get_spec())
        self.assertTrue(svr_onnx is not None)
        dump_data_and_model(X.astype(numpy.float32),
                            svr,
                            svr_onnx,
                            basename="CmlLinearSvr-Dec4")
Пример #17
0
 def test_tree_ensemble_classifier(self):
     X = numpy.array([[0, 1], [1, 1], [2, 0]], dtype=numpy.float32)
     y = [1, 0, 1]
     model = RandomForestClassifier().fit(X, y)
     model_coreml = coremltools.converters.sklearn.convert(model)
     model_onnx = convert(model_coreml.get_spec())
     self.assertTrue(model_onnx is not None)
     self.validate_zipmap(model_onnx)
     dump_data_and_model(
         X,
         model,
         model_onnx,
         basename="CmlBinRandomForestClassifier",
         allow_failure=
         "StrictVersion(onnx.__version__) < StrictVersion('1.3.0')")
    def test_conversion_one_column(self):
        scikit_data = [[0], [1], [2], [4], [3], [2], [4], [5], [6], [7]]
        scikit_data = numpy.asarray(scikit_data, dtype='d')
        scikit_data_multiple_cols = [[0, 1],  [1, 0], [2, 2], [3, 3], [4, 4]]
        scikit_data_multiple_cols = numpy.asarray(scikit_data_multiple_cols, dtype='d')
        scikit_model = OneHotEncoder()

        # scikit_model.fit(scikit_data)
        # model_coreml = coremltools.converters.sklearn.convert(scikit_model, 'single_feature', 'out')

        scikit_model.fit(scikit_data_multiple_cols)
        try:
            model_coreml = coremltools.converters.sklearn.convert(scikit_model, ['feature_1', 'feature_2'], 'out')
        except Exception as e:
            warnings.warn("Unable to run convert OneHotEncoder with coreml.")
            return
        model_onnx = convert(model_coreml)                                     
        self.assertTrue(model_onnx is not None)
        dump_data_and_model(scikit_data, scikit_model, model_onnx, basename="CmlOneHotEncoder-SkipDim1")
Пример #19
0
 def test_imputer(self):
     try:
         model = Imputer(missing_values='NaN', strategy='mean', axis=0)
     except TypeError:
         model = Imputer(missing_values=np.nan, strategy='mean')
         model.axis = 0
     data = [[1, 2], [np.nan, 3], [7, 6]]
     model.fit(data)
     from onnxmltools.convert.coreml.convert import convert
     import coremltools  # noqa
     try:
         model_coreml = coremltools.converters.sklearn.convert(model)
     except ValueError as e:
         if 'not supported' in str(e):
             # Python 2.7 + scikit-learn 0.22
             return
     model_onnx = convert(model_coreml.get_spec())
     self.assertTrue(model_onnx is not None)
     dump_data_and_model(np.array(data, dtype=np.float32),
                         model, model_onnx, basename="CmlImputerMeanFloat32")
Пример #20
0
 def test_dict_vectorizer(self):
     model = DictVectorizer()
     model.fit_transform([{'amy': 1, 'chin': 200}, {'nice': 3, 'amy': 1}])
     model_coreml = coremltools.converters.sklearn.convert(model)
     model_onnx = convert(model_coreml.get_spec())
     self.assertTrue(model_onnx is not None)
 def test_imputer(self):
     model = Imputer(missing_values='NaN', strategy='mean', axis=0)
     model.fit([[1, 2], [np.nan, 3], [7, 6]])
     model_coreml = coremltools.converters.sklearn.convert(model)
     model_onnx = convert(model_coreml.get_spec())
     self.assertTrue(model_onnx is not None)
Пример #22
0
 def test_scaler(self):
     model = StandardScaler()
     model.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]])
     model_coreml = coremltools.converters.sklearn.convert(model)
     model_onnx = convert(model_coreml.get_spec())
     self.assertTrue(model_onnx is not None)
 def test_tree_ensemble_regressor(self):
     X, y = make_regression(n_features=4, random_state=0)
     model = RandomForestRegressor().fit(X, y)
     model_coreml = coremltools.converters.sklearn.convert(model)
     model_onnx = convert(model_coreml.get_spec())
     self.assertTrue(model_onnx is not None)