How to use coremltools - 10 common examples

To help you get started, we’ve selected a few coremltools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github apple / coremltools / coremltools / converters / nnssa / frontend / graph_pass / type_inference.py View on Github external
if is_symbolic_or_known(idim):
                            node.attr['split'] = [idim // num_split] * num_split
                            output_shape[split_dim] = idim // num_split
                        else:
                            node.attr['split'] = [-1] * num_split
                            output_shape[split_dim] = -1
                        shapes = [output_shape] * num_split
                        try_materialize = True
                    else:
                        assert (np.sum(split_size) == idim or is_symbolic_or_unknown(idim))
                        node.attr['split'] = list(split_size)
                        shapes = [output_shape[:] for _ in range(len(split_size))]
                        for idx, s in enumerate(split_size):
                            shapes[idx][split_dim] = s

            types = [builtins.tensor(datatype, tuple(shape)) for shape in shapes]
        else:
            types = [
                builtins.tensor(datatype, tuple(shape)) for shape in node.attr['_output_shapes']
            ]
        rettype = builtins.tuple(types)
        if try_materialize:
            value = try_get_non_sym_val(self.gdict[node.inputs[value_idx]])
            if value is not None:
                node.attr["symbolic_value"] = rettype()
                node.attr["symbolic_value"].val = np.split(value, num_split, axis=split_dim)
        return rettype
github onnx / onnxmltools / tests / coreml / test_cml_AllNeuralNetworkConverters.py View on Github external
def test_split_converter(self):
        input_dim = (8, 1, 1)
        output_dim = (4, 1, 1)
        inputs = [('input', datatypes.Array(*input_dim))]
        outputs = [('output1', datatypes.Array(*output_dim)), ('output2', datatypes.Array(*output_dim))]
        builder = NeuralNetworkBuilder(inputs, outputs)
        builder.add_split(name='Split', input_name='input', output_names=['output1', 'output2'])
        model_onnx = convert_coreml(builder.spec)
        self.assertTrue(model_onnx is not None)
github onnx / onnxmltools / tests / coreml / test_cml_OneHotEncoderConverter.py View on Github external
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")
github onnx / onnxmltools / tests / coreml / test_cml_TreeEnsembleRegressorConverter.py View on Github external
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")
github onnx / onnxmltools / tests / coreml / test_cml_GLMRegressorConverter.py View on Github external
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")
github onnx / onnxmltools / tests / coreml / test_cml_SupportVectorRegressorConverter.py View on Github external
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")
github onnx / onnxmltools / tests / coreml / test_cml_ScalerConverter.py View on Github external
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")
github onnx / onnxmltools / tests / coreml / test_cml_GLMClassifierConverter.py View on Github external
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)
github onnx / onnxmltools / tests / coreml / test_cml_ImputerConverter.py View on Github external
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")
github onnx / onnxmltools / tests / coreml / test_cml_GLMClassifierConverter.py View on Github external
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)