コード例 #1
0
def _scikit_learn_before_022():
    if '.dev' in __version__:
        return StrictVersion(
            __version__.split(".dev")[0]) < StrictVersion("0.22")
    if '.post' in __version__:
        return StrictVersion(
            __version__.split(".post")[0]) < StrictVersion("0.22")
    return StrictVersion(__version__) < StrictVersion("0.22")
コード例 #2
0
ファイル: utils.py プロジェクト: cosanlab/py-feat
def load_h5(file_name="pyfeat_aus_to_landmarks.h5"):
    """Load the h5 PLS model for plotting.

    Args:
        file_name (str, optional): Specify model to load.. Defaults to 'blue.h5'.

    Returns:
        model: PLS model
    """
    try:
        hf = h5py.File(os.path.join(get_resource_path(), file_name), "r")
        d1 = hf.get("coef")
        d2 = hf.get("x_mean")
        d3 = hf.get("y_mean")
        d4 = hf.get("x_std")
        model = PLSRegression(len(d1))
        model.coef_ = np.array(d1)
        if int(__version__.split(".")[1]) < 24:
            model.x_mean_ = np.array(d2)
            model.y_mean_ = np.array(d3)
            model.x_std_ = np.array(d4)
        else:
            model._x_mean = np.array(d2)
            model._y_mean = np.array(d3)
            model._x_std = np.array(d4)
        hf.close()
    except Exception as e:
        print("Unable to load data ", file_name, ":", e)
    return model
コード例 #3
0
def plot_cluster(cluster):
    '''
    Plot scatter diagram for final points that using multi-dimensional scaling for data

    Args:
            cluster : DensityPeakCluster object
    '''
    logger.info("PLOT: cluster result, start multi-dimensional scaling")
    dp = np.zeros((cluster.max_id, cluster.max_id), dtype=np.float32)
    cls = []
    for i in range(1, cluster.max_id):
        for j in range(i + 1, cluster.max_id + 1):
            dp[i - 1, j - 1] = cluster.distances[(i, j)]
            dp[j - 1, i - 1] = cluster.distances[(i, j)]
        cls.append(cluster.cluster[i])
    cls.append(cluster.cluster[cluster.max_id])
    cls = np.array(cls, dtype=np.float32)
    fo = open(r'./tmp.txt', 'w')
    fo.write('\n'.join(map(str, cls)))
    fo.close()
    version = sklearn_version.split('.')
    if int(version[0]) > 0 or int(version[1]) > 14:
        mds = manifold.MDS(max_iter=200,
                           eps=1e-4,
                           n_init=1,
                           dissimilarity='precomputed')
    else:
        mds = manifold.MDS(max_iter=200, eps=1e-4, n_init=1)
    dp_mds = mds.fit_transform(dp)
    logger.info("PLOT: end mds, start plot")
    plot_scatter_diagram(1,
                         dp_mds[:, 0],
                         dp_mds[:, 1],
                         title='cluster',
                         style_list=cls)
コード例 #4
0
def ordinal_encoder_support():
    # pv.Version does not work with development versions
    vers = '.'.join(sklearn_version.split('.')[:2])
    if pv.Version(vers) < pv.Version("0.20.0"):
        return False
    if pv.Version(onnxruntime.__version__) < pv.Version("0.3.0"):
        return False
    return pv.Version(vers) >= pv.Version("0.20.0")
コード例 #5
0
def get_folds(data):
    """returns correct folding generator for different versions of sklearn"""
    if sklearn_version.split('.')[1] == '18':
        # Module model_selection is in the distribution
        kf = KFold(n_splits=5, shuffle=True, random_state=1)
        return kf.split(data)
    else:
        # Module model_selection is not in the distribution
        kf = KFold(n=len(data), n_folds=5, shuffle=True, random_state=1)
        return kf
コード例 #6
0
import numpy as np
from numpy.testing import assert_almost_equal
from onnxruntime import InferenceSession
from sklearn import __version__ as sklv
try:
    from sklearn.ensemble import IsolationForest
except ImportError:
    IsolationForest = None
from skl2onnx import to_onnx
from test_utils import dump_data_and_model, TARGET_OPSET
try:
    from onnxruntime.capi.onnxruntime_pybind11_state import NotImplemented
except ImportError:
    NotImplemented = RuntimeError

sklv2 = '.'.join(sklv.split('.')[:2])


class TestSklearnIsolationForest(unittest.TestCase):
    @unittest.skipIf(IsolationForest is None, reason="old scikit-learn")
    @unittest.skipIf(StrictVersion(sklv2) < StrictVersion('0.22.0'),
                     reason="tree structure is different.")
    def test_isolation_forest(self):
        isol = IsolationForest(n_estimators=3, random_state=0)
        data = np.array([[-1.1, -1.2], [0.3, 0.2], [0.5, 0.4], [100., 99.]],
                        dtype=np.float32)
        model = isol.fit(data)
        model_onnx = to_onnx(model, data, target_opset=TARGET_OPSET)
        self.assertIsNotNone(model_onnx)
        dump_data_and_model(data,
                            model,
コード例 #7
0
from sklearn import __version__ as sklver
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.multiclass import OneVsRestClassifier
from sklearn.multioutput import MultiOutputClassifier
from sklearn.tree import DecisionTreeClassifier
from skl2onnx import to_onnx
try:
    from sklearn.utils._testing import ignore_warnings
except ImportError:
    from sklearn.utils.testing import ignore_warnings
from sklearn.exceptions import ConvergenceWarning
from test_utils import TARGET_OPSET

sklver = '.'.join(sklver.split('.')[:2])


class TestConvertOptions(unittest.TestCase):
    @staticmethod
    def get_model_classifiers():
        models = [
            # BaggingClassifier,
            # BernoulliNB,
            # CategoricalNB,
            # CalibratedClassifierCV,
            # ComplementNB,
            DecisionTreeClassifier(max_depth=2),
            # ExtraTreeClassifier,
            # ExtraTreesClassifier,
            # GaussianNB,
コード例 #8
0
ファイル: clustering.py プロジェクト: GFZ/spechomo
    def compute_clusters(self, nmax_spectra=100000):
        """Compute the cluster means and labels.

        :param nmax_spectra:    maximum number of spectra to be included (pseudo-randomly selected (reproducable))
        :return:
        """
        from sklearn.cluster import KMeans  # avoids static TLS ImportError here
        from sklearn import __version__ as skver

        _old_environ = dict(os.environ)

        try:
            kwargs_kmeans = dict(n_clusters=self.n_clusters,
                                 random_state=0,
                                 verbose=self.v)
            # scikit-learn>0.23 uses all cores by default; number is adjustable via OMP_NUM_THREADS
            if float('%s.%s' % tuple(skver.split('.')[:2])) < 0.23:
                kwargs_kmeans['n_jobs'] = self.CPUs
            else:
                os.environ['OMP_NUM_THREADS '] = str(self.CPUs)

            # data reduction in case we have too many spectra
            if self.spectra.shape[0] > nmax_spectra:
                if self.v:
                    print('Reducing data...')
                idxs_specIncl = np.random.RandomState(seed=0).choice(
                    range(self.n_spectra), nmax_spectra)
                idxs_specNotIncl = np.array(
                    range(self.n_spectra
                          ))[~np.in1d(range(self.n_spectra), idxs_specIncl)]
                spectra_incl = self.spectra[idxs_specIncl, :]
                spectra_notIncl = self.spectra[idxs_specNotIncl, :]

                if self.v:
                    print('Fitting KMeans...')
                kmeans = KMeans(**kwargs_kmeans)
                distmatrix_incl = kmeans.fit_transform(spectra_incl)

                if self.v:
                    print('Computing full resolution labels...')
                labels = np.zeros((self.n_spectra, ),
                                  dtype=kmeans.labels_.dtype)
                distances = np.zeros((self.n_spectra, ),
                                     dtype=distmatrix_incl.dtype)

                labels[idxs_specIncl] = kmeans.labels_
                distances[idxs_specIncl] = np.min(distmatrix_incl, axis=1)

                if self.sam_classassignment:
                    # override cluster labels with labels computed via SAM (distances have be recomputed then)
                    print('Using SAM class assignment.')
                    SC = SAM_Classifier(kmeans.cluster_centers_,
                                        CPUs=self.CPUs)
                    im_sam_labels = SC.classify(self.im)
                    sam_labels = im_sam_labels.flatten()[self.goodSpecMask]
                    self._spectral_angles = SC.angles_deg.flatten()[
                        self.goodSpecMask]

                    # update distances at those positions where SAM assigns different class labels
                    distsPos2update = labels != sam_labels
                    distsPos2update[idxs_specNotIncl] = True
                    distances[distsPos2update] = \
                        self.compute_euclidian_distance_for_labelled_spectra(
                            self.spectra[distsPos2update, :], sam_labels[distsPos2update], kmeans.cluster_centers_)

                    kmeans.labels_ = sam_labels

                else:
                    distmatrix_specNotIncl = kmeans.transform(spectra_notIncl)
                    labels[idxs_specNotIncl] = np.argmin(
                        distmatrix_specNotIncl, axis=1)
                    distances[idxs_specNotIncl] = np.min(
                        distmatrix_specNotIncl, axis=1)

                    kmeans.labels_ = labels

            else:
                if self.v:
                    print('Fitting KMeans...')
                kmeans = KMeans(**kwargs_kmeans)

                distmatrix = kmeans.fit_transform(self.spectra)
                kmeans_labels = kmeans.labels_
                distances = np.min(distmatrix, axis=1)

                if self.sam_classassignment:
                    # override cluster labels with labels computed via SAM (distances have be recomputed then)
                    print('Using SAM class assignment.')
                    SC = SAM_Classifier(kmeans.cluster_centers_,
                                        CPUs=self.CPUs)
                    im_sam_labels = SC.classify(self.im)
                    sam_labels = im_sam_labels.flatten()[self.goodSpecMask]
                    self._spectral_angles = SC.angles_deg.flatten()[
                        self.goodSpecMask]

                    # update distances at those positions where SAM assigns different class labels
                    distsPos2update = kmeans_labels != sam_labels
                    distances[distsPos2update] = \
                        self.compute_euclidian_distance_for_labelled_spectra(
                            self.spectra[distsPos2update, :], sam_labels[distsPos2update], kmeans.cluster_centers_)
        finally:
            os.environ.clear()
            os.environ.update(_old_environ)

        self.clusters = kmeans
        self._spectral_distances = distances

        return self.clusters
コード例 #9
0
ファイル: model.py プロジェクト: rizac/sdaas
def _get_sklearn_version_tuple():
    try:
        from sklearn import __version__
        return tuple(int(_) for _ in __version__.split('.'))
    except (ImportError, ValueError, TypeError):
        return None
コード例 #10
0
def check_scikit_version():
    # StrictVersion does not work with development versions
    vers = '.'.join(sklearn_version.split('.')[:2])
    return StrictVersion(vers) >= StrictVersion("0.21.0")
コード例 #11
0
def one_hot_encoder_supports_drop():
    # StrictVersion does not work with development versions
    vers = '.'.join(sklearn_version.split('.')[:2])
    return StrictVersion(vers) >= StrictVersion("0.21.0")
コード例 #12
0
from logging import getLogger
import numpy
from numpy.testing import assert_almost_equal
from onnxruntime import InferenceSession
from sklearn.datasets import load_linnerud, make_multilabel_classification
from sklearn.multioutput import MultiOutputRegressor, MultiOutputClassifier
from sklearn.linear_model import Ridge, LogisticRegression
try:
    from sklearn.utils._testing import ignore_warnings
except ImportError:
    from sklearn.utils.testing import ignore_warnings
from sklearn import __version__ as skl_ver
from skl2onnx import to_onnx
from test_utils import dump_data_and_model, TARGET_OPSET

skl_ver = ".".join(skl_ver.split('.')[:2])


class TestMultiOutputConverter(unittest.TestCase):
    def setUp(self):
        if __name__ == "__main__":
            log = getLogger('skl2onnx')
            log.disabled = True
            # log.setLevel(logging.DEBUG)
            # logging.basicConfig(level=logging.DEBUG)
            pass

    def test_multi_output_regressor(self):
        X, y = load_linnerud(return_X_y=True)
        clf = MultiOutputRegressor(Ridge(random_state=123)).fit(X, y)
        onx = to_onnx(clf,
コード例 #13
0
class TestSklearnSVM(unittest.TestCase):
    def _fit_binary_classification(self, model):
        iris = load_iris()
        X = iris.data[:, :3]
        y = iris.target
        y[y == 2] = 1
        model.fit(X, y)
        return model, X[:5].astype(numpy.float32)

    def _fit_one_class_svm(self, model):
        iris = load_iris()
        X = iris.data[:, :3]
        model.fit(X)
        return model, X[10:15].astype(numpy.float32)

    def _fit_multi_classification(self, model, nbclass=4):
        iris = load_iris()
        X = iris.data[:, :3]
        y = iris.target
        if nbclass == 4:
            y[-10:] = 3
        model.fit(X, y)
        return model, X[:5].astype(numpy.float32)

    def _fit_multi_regression(self, model):
        iris = load_iris()
        X = iris.data[:, :3]
        y = numpy.vstack([iris.target, iris.target]).T
        model.fit(X, y)
        return model, X[:5].astype(numpy.float32)

    def _check_attributes(self, node, attribute_test):
        attributes = node.attribute
        attribute_map = {}
        for attribute in attributes:
            attribute_map[attribute.name] = attribute

        for k, v in attribute_test.items():
            self.assertTrue(k in attribute_map)
            if v is not None:
                attrib = attribute_map[k]
                if isinstance(v, str):
                    self.assertEqual(attrib.s, v.encode(encoding="UTF-8"))
                elif isinstance(v, int):
                    self.assertEqual(attrib.i, v)
                elif isinstance(v, float):
                    self.assertEqual(attrib.f, v)
                elif isinstance(v, list):
                    self.assertEqual(attrib.ints, v)
                else:
                    self.fail("Unknown type")

    def test_convert_svc_binary_linear_pfalse(self):
        model, X = self._fit_binary_classification(
            SVC(kernel="linear",
                probability=False,
                decision_function_shape='ovo'))

        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([None, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "LINEAR",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnBinSVCLinearPF-NoProbOpp",
            allow_failure="StrictVersion(onnxruntime.__version__)"
            " < StrictVersion('0.5.0')")
        model_onnx = convert_sklearn(
            model,
            "SVC", [("input", FloatTensorType([None, X.shape[1]]))],
            options={id(model): {
                         'zipmap': False
                     }})
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnBinSVCLinearPF-NoProbOpp",
            allow_failure="StrictVersion(onnxruntime.__version__)"
            " < StrictVersion('0.5.0')")

    def test_convert_svc_binary_linear_ptrue(self):
        model, X = self._fit_binary_classification(
            SVC(kernel="linear", probability=True))

        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([None, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "LINEAR",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnBinSVCLinearPT",
            allow_failure="StrictVersion(onnxruntime.__version__)"
            " <= StrictVersion('0.4.0')")

    def test_convert_svc_multi_linear_pfalse(self):
        model, X = self._fit_multi_classification(
            SVC(kernel="linear",
                probability=False,
                decision_function_shape='ovo'))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([None, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "LINEAR",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnMclSVCLinearPF-Dec4",
            allow_failure="StrictVersion(onnxruntime.__version__)"
            " < StrictVersion('0.5.0')")

    def test_convert_svc_multi_linear_pfalse_ovr(self):
        model, X = self._fit_multi_classification(
            SVC(kernel="linear",
                probability=False,
                decision_function_shape='ovr'))
        try:
            convert_sklearn(model, "SVC",
                            [("input", FloatTensorType([None, X.shape[1]]))])
            raise AssertionError('not implemented')
        except RuntimeError:
            pass

    def test_convert_svc_multi_linear_ptrue(self):
        model, X = self._fit_multi_classification(
            SVC(kernel="linear", probability=True))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([None, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "LINEAR",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnMclSVCLinearPT-Dec4",
            allow_failure="StrictVersion(onnxruntime.__version__)"
            " <= StrictVersion('0.4.0')")

    def test_convert_svr_linear(self):
        model, X = self._fit_binary_classification(SVR(kernel="linear"))
        model_onnx = convert_sklearn(
            model, "SVR", [("input", FloatTensorType([None, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        self._check_attributes(
            nodes[0],
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "LINEAR",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
            },
        )
        dump_data_and_model(X,
                            model,
                            model_onnx,
                            basename="SklearnRegSVRLinear-Dec3")

    def test_convert_nusvc_binary_pfalse(self):
        model, X = self._fit_binary_classification(
            NuSVC(probability=False, decision_function_shape='ovo'))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([None, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "RBF",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnBinNuSVCPF-NoProbOpp",
            allow_failure="StrictVersion(onnxruntime.__version__)"
            " < StrictVersion('0.5.0')")

    @unittest.skipIf(StrictVersion(ort_version) <= StrictVersion("0.4.0"),
                     reason="use of recent Cast operator")
    def test_convert_nusvc_binary_ptrue(self):
        model, X = self._fit_binary_classification(NuSVC(probability=True))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([None, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "RBF",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnBinNuSVCPT",
            allow_failure="StrictVersion(onnxruntime.__version__)"
            " <= StrictVersion('0.4.0')")

    def test_convert_nusvc_multi_pfalse(self):
        model, X = self._fit_multi_classification(
            NuSVC(probability=False, nu=0.1, decision_function_shape='ovo'))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([None, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "RBF",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnMclNuSVCPF-Dec2",
            allow_failure="StrictVersion(onnxruntime.__version__)"
            " < StrictVersion('0.5.0')")

    def test_convert_svc_multi_pfalse_4(self):
        model, X = self._fit_multi_classification(
            SVC(probability=False, decision_function_shape='ovo'), 4)
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([None, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnMcSVCPF",
            allow_failure="StrictVersion(onnxruntime.__version__)"
            " < StrictVersion('0.5.0')")

    @unittest.skipIf(StrictVersion(sk__version__.split('dev')[0].strip('.')) <
                     StrictVersion("0.22.0"),
                     reason="break_ties introduced after 0.22.dev")
    def test_convert_svc_multi_pfalse_4_break_ties(self):
        model, X = self._fit_multi_classification(
            SVC(probability=True, break_ties=True), 4)
        model_onnx = convert_sklearn(
            model, "unused", [("input", FloatTensorType([None, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        dump_data_and_model(
            X.astype(numpy.float32),
            model,
            model_onnx,
            basename="SklearnMcSVCPFBTF",
            allow_failure="StrictVersion(onnxruntime.__version__)"
            " < StrictVersion('0.5.0')")

    def test_convert_svc_multi_ptrue_4(self):
        model, X = self._fit_multi_classification(SVC(probability=True), 4)
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([None, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnMcSVCPF4",
            allow_failure="StrictVersion(onnxruntime.__version__)"
            " <= StrictVersion('0.4.0')")

    def test_convert_nusvc_multi_ptrue(self):
        model, X = self._fit_multi_classification(
            NuSVC(probability=True, nu=0.1))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([None, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "RBF",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnMclNuSVCPT",
            allow_failure="StrictVersion(onnxruntime.__version__)"
            " <= StrictVersion('0.4.0')")

    def test_convert_nusvr(self):
        model, X = self._fit_binary_classification(NuSVR())
        model_onnx = convert_sklearn(
            model, "SVR", [("input", FloatTensorType([None, X.shape[1]]))])
        node = model_onnx.graph.node[0]
        self.assertIsNotNone(node)
        self._check_attributes(
            node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "RBF",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
            },
        )
        dump_data_and_model(X, model, model_onnx, basename="SklearnRegNuSVR")

    def test_convert_nusvr_default(self):
        model, X = self._fit_binary_classification(NuSVR())
        model_onnx = convert_sklearn(
            model, "SVR", [("input", FloatTensorType([None, X.shape[1]]))])
        self.assertIsNotNone(model_onnx)
        dump_data_and_model(X, model, model_onnx, basename="SklearnRegNuSVR2")

    def test_convert_svr_int(self):
        model, X = fit_regression_model(SVR(), is_int=True)
        model_onnx = convert_sklearn(
            model,
            "SVR",
            [("input", Int64TensorType([None, X.shape[1]]))],
        )
        self.assertIsNotNone(model_onnx)
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnSVRInt-Dec4",
            allow_failure="StrictVersion(onnxruntime.__version__)"
            " <= StrictVersion('0.2.1')")

    def test_convert_nusvr_int(self):
        model, X = fit_regression_model(NuSVR(), is_int=True)
        model_onnx = convert_sklearn(
            model,
            "NuSVR",
            [("input", Int64TensorType([None, X.shape[1]]))],
        )
        self.assertIsNotNone(model_onnx)
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnNuSVRInt-Dec4",
            allow_failure="StrictVersion(onnxruntime.__version__)"
            " <= StrictVersion('0.2.1')")

    @unittest.skipIf(StrictVersion(onnx.__version__) < StrictVersion("1.4.1"),
                     reason="operator sign available since opset 9")
    def test_convert_oneclasssvm(self):
        model, X = self._fit_one_class_svm(OneClassSVM())
        model_onnx = convert_sklearn(
            model, "OCSVM", [("input", FloatTensorType([None, X.shape[1]]))])
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnBinOneClassSVM",
            allow_failure="StrictVersion(onnxruntime.__version__)"
            " < StrictVersion('0.5.0')")
コード例 #14
0
class TestSklearnSVM(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        update_registered_converter(
            SVC_raw,
            "SVC_raw",
            calculate_sklearn_svm_output_shapes,
            convert_sklearn_svm)
        update_registered_converter(
            NuSVC_raw,
            "NuSVC_raw",
            calculate_sklearn_svm_output_shapes,
            convert_sklearn_svm)

    def _fit_binary_classification(self, model):
        iris = load_iris()
        X = iris.data[:, :3]
        y = iris.target
        y[y == 2] = 1
        model.fit(X, y)
        return model, X[:5].astype(numpy.float32)

    def _fit_multi_classification(self, model, nbclass=4):
        iris = load_iris()
        X = iris.data[:, :3]
        y = iris.target
        if nbclass == 4:
            y[-10:] = 3
        model.fit(X, y)
        return model, X[:5].astype(numpy.float32)

    def _fit_multi_regression(self, model):
        iris = load_iris()
        X = iris.data[:, :3]
        y = numpy.vstack([iris.target, iris.target]).T
        model.fit(X, y)
        return model, X[:5].astype(numpy.float32)

    def _check_attributes(self, node, attribute_test):
        attributes = node.attribute
        attribute_map = {}
        for attribute in attributes:
            attribute_map[attribute.name] = attribute

        for k, v in attribute_test.items():
            self.assertTrue(k in attribute_map)
            if v is not None:
                attrib = attribute_map[k]
                if isinstance(v, str):
                    self.assertEqual(attrib.s, v.encode(encoding="UTF-8"))
                elif isinstance(v, int):
                    self.assertEqual(attrib.i, v)
                elif isinstance(v, float):
                    self.assertEqual(attrib.f, v)
                elif isinstance(v, list):
                    self.assertEqual(attrib.ints, v)
                else:
                    self.fail("Unknown type")

    def test_convert_svc_binary_linear_pfalse(self):
        model, X = self._fit_binary_classification(
            SVC(kernel="linear", probability=False))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([1, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "LINEAR",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnBinSVCLinearPF-NoProbOpp",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " < StrictVersion('0.5.0')"
        )

    def test_convert_svc_binary_linear_ptrue(self):
        model, X = self._fit_binary_classification(
            SVC(kernel="linear", probability=True))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([1, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "LINEAR",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnBinSVCLinearPT",
        )

    def test_convert_svc_multi_linear_pfalse(self):
        model, X = self._fit_multi_classification(
            SVC_raw(kernel="linear", probability=False))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([1, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "LINEAR",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnMclSVCLinearPF-Dec4",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " < StrictVersion('0.5.0')"
        )

    def test_convert_svc_multi_linear_ptrue(self):
        model, X = self._fit_multi_classification(
            SVC_raw(kernel="linear", probability=False))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([1, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "LINEAR",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnMclSVCLinearPT-Dec4",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " < StrictVersion('0.5.0')"
        )

    def test_convert_svr_linear(self):
        model, X = self._fit_binary_classification(SVR(kernel="linear"))
        model_onnx = convert_sklearn(
            model, "SVR", [("input", FloatTensorType([1, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        self._check_attributes(
            nodes[0],
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "LINEAR",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
            },
        )
        dump_data_and_model(X,
                            model,
                            model_onnx,
                            basename="SklearnRegSVRLinear-Dec3")

    def test_convert_nusvc_binary_pfalse(self):
        model, X = self._fit_binary_classification(NuSVC(probability=False))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([1, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "RBF",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnBinNuSVCPF-NoProbOpp",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " < StrictVersion('0.5.0')"
        )

    def test_convert_nusvc_binary_ptrue(self):
        model, X = self._fit_binary_classification(NuSVC(probability=True))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([1, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "RBF",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnBinNuSVCPT",
        )

    def test_convert_nusvc_multi_pfalse(self):
        model, X = self._fit_multi_classification(
            NuSVC_raw(probability=False, nu=0.1))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([1, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "RBF",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnMclNuSVCPF-Dec2",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " < StrictVersion('0.5.0')"
        )

    def test_convert_svc_multi_pfalse_4(self):
        model, X = self._fit_multi_classification(
            SVC_raw(probability=False), 4)
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([1, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnMcSVCPF",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " < StrictVersion('0.5.0')"
        )

    @unittest.skipIf(
        StrictVersion(sk__version__.split('dev')[0].strip('.'))
        < StrictVersion("0.22.0"),
        reason="break_ties introduced after 0.22.dev")
    def test_convert_svc_multi_pfalse_4_break_ties(self):
        model, X = self._fit_multi_classification(
            SVC_raw(probability=False, break_ties=True), 4)
        model_onnx = convert_sklearn(
            model, "SVC_raw", [("input", FloatTensorType([1, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        dump_data_and_model(
            X.astype(numpy.float32),
            model,
            model_onnx,
            basename="SklearnMcSVCPFBTF",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " < StrictVersion('0.5.0')"
        )

    def test_convert_svc_multi_ptrue_4(self):
        model, X = self._fit_multi_classification(SVC(probability=True), 4)
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([1, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnMcSVCPF4",
        )

    def test_convert_nusvc_multi_ptrue(self):
        model, X = self._fit_multi_classification(
            NuSVC(probability=True, nu=0.1))
        model_onnx = convert_sklearn(
            model, "SVC", [("input", FloatTensorType([1, X.shape[1]]))])
        nodes = model_onnx.graph.node
        self.assertIsNotNone(nodes)
        svc_node = nodes[0]
        self._check_attributes(
            svc_node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "RBF",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
                "vectors_per_class": None,
            },
        )
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnMclNuSVCPT",
        )

    def test_convert_nusvr(self):
        model, X = self._fit_binary_classification(NuSVR())
        model_onnx = convert_sklearn(
            model, "SVR", [("input", FloatTensorType([1, X.shape[1]]))])
        node = model_onnx.graph.node[0]
        self.assertIsNotNone(node)
        self._check_attributes(
            node,
            {
                "coefficients": None,
                "kernel_params": None,
                "kernel_type": "RBF",
                "post_transform": None,
                "rho": None,
                "support_vectors": None,
            },
        )
        dump_data_and_model(X, model, model_onnx,
                            basename="SklearnRegNuSVR")

    def test_convert_nusvr_default(self):
        model, X = self._fit_binary_classification(NuSVR())
        model_onnx = convert_sklearn(
            model, "SVR", [("input", FloatTensorType([1, X.shape[1]]))])
        self.assertIsNotNone(model_onnx)
        dump_data_and_model(X, model, model_onnx, basename="SklearnRegNuSVR2")

    def test_convert_svr_int(self):
        model, X = fit_regression_model(
            SVR(), is_int=True)
        model_onnx = convert_sklearn(
            model,
            "SVR",
            [("input", Int64TensorType(X.shape))],
        )
        self.assertIsNotNone(model_onnx)
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnSVRInt-Dec4",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " <= StrictVersion('0.2.1')"
        )

    def test_convert_nusvr_int(self):
        model, X = fit_regression_model(
            NuSVR(), is_int=True)
        model_onnx = convert_sklearn(
            model,
            "NuSVR",
            [("input", Int64TensorType(X.shape))],
        )
        self.assertIsNotNone(model_onnx)
        dump_data_and_model(
            X,
            model,
            model_onnx,
            basename="SklearnNuSVRInt-Dec4",
            allow_failure="StrictVersion(onnxruntime.__version__)"
                          " <= StrictVersion('0.2.1')"
        )
コード例 #15
0
def one_hot_encoder_supports_string():
    # pv.Version does not work with development versions
    vers = '.'.join(sklearn_version.split('.')[:2])
    return pv.Version(vers) >= pv.Version("0.20.0")
コード例 #16
0
from sklearn.svm import SVC, LinearSVC
from skl2onnx import convert_sklearn, to_onnx
from skl2onnx.common.data_types import (
    FloatTensorType,
    Int64TensorType,
    StringTensorType,
)
from sklearn.multioutput import MultiOutputClassifier
from skl2onnx.common.data_types import onnx_built_with_ml
from test_utils import (dump_data_and_model, fit_classification_model,
                        TARGET_OPSET)
from onnxruntime import __version__ as ort_version, InferenceSession

# StrictVersion does not work with development versions
ort_version = ".".join(ort_version.split('.')[:2])
skl_version = ".".join(skl_version.split('.')[:2])


def check_scikit_version():
    return StrictVersion(skl_version) >= StrictVersion("0.22")


class PipeConcatenateInput:
    def __init__(self, pipe):
        self.pipe = pipe

    def transform(self, inp):
        if isinstance(inp, (numpy.ndarray, pandas.DataFrame)):
            return self.pipe.transform(inp)
        elif isinstance(inp, dict):
            keys = list(sorted(inp.keys()))
コード例 #17
0
def isskl023():
    "Tells if :epkg:`scikit-learn` is more recent than 0.23."
    v1 = ".".join(skl_version.split('.')[:2])
    return StrictVersion(v1) >= StrictVersion('0.23')
コード例 #18
0
def _scikit_learn_before_022():
    return StrictVersion(__version__.split(".dev")[0]) < StrictVersion("0.22")
コード例 #19
0
    from onnxruntime.capi.onnxruntime_pybind11_state import Fail as OrtFail
except ImportError:
    OrtFail = RuntimeError
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn import __version__ as sklver
try:
    from sklearn.gaussian_process import GaussianProcessClassifier
except ImportError:
    GaussianProcessClassifier = None
from skl2onnx.common.data_types import FloatTensorType, DoubleTensorType
from skl2onnx import to_onnx
from skl2onnx.helpers.onnx_helper import change_onnx_domain
from test_utils import dump_data_and_model, TARGET_OPSET

sklver_ = ".".join(sklver.split('.')[:2])


class TestSklearnGaussianProcessClassifier(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        try:
            from ortcustomops import (onnx_op, PyCustomOpDef, get_library_path)
        except ImportError:
            return

        @onnx_op(op_type="SolveFloat",
                 inputs=[PyCustomOpDef.dt_float, PyCustomOpDef.dt_float],
                 outputs=[PyCustomOpDef.dt_float])
        def solveopf(a, b):
            # The user custom op implementation here.
コード例 #20
0
def ordinal_encoder_support():
    # StrictVersion does not work with development versions
    vers = '.'.join(sklearn_version.split('.')[:2])
    return StrictVersion(vers) >= StrictVersion("0.20.0")
コード例 #21
0
from lazyflow.request import Request, RequestPool
from lazyflow.roi import roiToSlice

import numpy as np

import vigra


logger = logging.getLogger(__name__)


try:
    from sklearn.svm import SVC
    havesklearn = True
    from sklearn import __version__ as sklearnVersion
    svcTakesScaleC = int(sklearnVersion.split('.')[1]) < 11
except ImportError:
    logger.warning("Could not import dependency 'sklearn' for SVMs")
    havesklearn = False

_defaultBinSize = 30


############################
############################           
############################
###                      ###
###  DETECTION OPERATOR  ###
###                      ###
############################
############################