Пример #1
0
    def _get_available_classifier_factories(self):
        # FIXME: Replace this logic with a proper plugin mechanism
        from lazyflow.classifiers import VigraRfLazyflowClassifierFactory, SklearnLazyflowClassifierFactory, \
                                         ParallelVigraRfLazyflowClassifierFactory, VigraRfPixelwiseClassifierFactory,\
                                         LazyflowVectorwiseClassifierFactoryABC, LazyflowPixelwiseClassifierFactoryABC
        classifiers = OrderedDict()
        classifiers["Parallel Random Forest (VIGRA)"] = ParallelVigraRfLazyflowClassifierFactory(100)
        
        try:
            from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
            from sklearn.naive_bayes import GaussianNB
            from sklearn.tree import DecisionTreeClassifier
            from sklearn.neighbors import KNeighborsClassifier
            from sklearn.lda import LDA
            from sklearn.qda import QDA
            from sklearn.svm import SVC, NuSVC
            classifiers["Random Forest (scikit-learn)"] = SklearnLazyflowClassifierFactory( RandomForestClassifier, 100 )
            classifiers["Gaussian Naive Bayes (scikit-learn)"] = SklearnLazyflowClassifierFactory( GaussianNB )
            classifiers["AdaBoost (scikit-learn)"] = SklearnLazyflowClassifierFactory( AdaBoostClassifier, n_estimators=100 )
            classifiers["Single Decision Tree (scikit-learn)"] = SklearnLazyflowClassifierFactory( DecisionTreeClassifier, max_depth=5 )
            classifiers["K-Neighbors (scikit-learn)"] = SklearnLazyflowClassifierFactory( KNeighborsClassifier )
            classifiers["LDA (scikit-learn)"] = SklearnLazyflowClassifierFactory( LDA )
            classifiers["QDA (scikit-learn)"] = SklearnLazyflowClassifierFactory( QDA )
            classifiers["SVM C-Support (scikit-learn)"] = SklearnLazyflowClassifierFactory( SVC, probability=True )
            classifiers["SVM Nu-Support (scikit-learn)"] = SklearnLazyflowClassifierFactory( NuSVC, probability=True )
        except ImportError:
            import warnings
            warnings.warn("Couldn't import sklearn. Scikit-learn classifiers not available.")

        # Debug classifiers
        classifiers["Parallel Random Forest with Variable Importance (VIGRA)"] = ParallelVigraRfLazyflowClassifierFactory(100, variable_importance_enabled=True)        
        classifiers["(debug) Single-threaded Random Forest (VIGRA)"] = VigraRfLazyflowClassifierFactory(100)
        classifiers["(debug) Pixelwise Random Forest (VIGRA)"] = VigraRfPixelwiseClassifierFactory(100)
        
        return classifiers
    def testBasic(self):
        features = numpy.indices((100, 100)).astype(numpy.float32) + 0.5
        features = numpy.rollaxis(features, 0, 3)
        features = vigra.taggedView(features, "xyc")
        labels = numpy.zeros((100, 100, 1), dtype=numpy.uint8)
        labels = vigra.taggedView(labels, "xyc")

        labels[10, 10] = 1
        labels[10, 11] = 1
        labels[20, 20] = 2
        labels[20, 21] = 2

        graph = Graph()
        opTrain = OpTrainPixelwiseClassifierBlocked(graph=graph)
        opTrain.ClassifierFactory.setValue(
            VigraRfPixelwiseClassifierFactory(10))
        opTrain.Images.resize(1)
        opTrain.Labels.resize(1)
        opTrain.nonzeroLabelBlocks.resize(1)
        opTrain.Images[0].setValue(features)
        opTrain.Labels[0].setValue(labels)
        nonzero_slicings = [
            numpy.s_[10:20, 10:20, 0:1], numpy.s_[20:30, 20:30, 0:1]
        ]
        opTrain.nonzeroLabelBlocks[0].setValue(nonzero_slicings)
        opTrain.MaxLabel.setValue(2)

        opTrain.Labels[0].setDirty(numpy.s_[10:11, 10:12])
        opTrain.Labels[0].setDirty(numpy.s_[20:21, 20:22])
        opTrain.Labels[0].setDirty(numpy.s_[30:31, 30:32])

        trained_classifier = opTrain.Classifier.value

        # This isn't much of a test at the moment...
        assert isinstance(trained_classifier, VigraRfPixelwiseClassifier)

        # Try predicting while we're at it...
        opPredict = OpPixelwiseClassifierPredict(graph=graph)
        opPredict.Image.connect(opTrain.Images[0])
        opPredict.LabelsCount.connect(opTrain.MaxLabel)
        opPredict.Classifier.connect(opTrain.Classifier)

        predictions = opPredict.PMaps[:].wait()
        assert predictions.shape == features.shape[:-1] + (
            2, )  # We used 2 input labels above.