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.float) + 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() opFeatureMatrixCache = OpFeatureMatrixCache(graph=graph) opFeatureMatrixCache.FeatureImage.setValue(features) opFeatureMatrixCache.LabelImage.setValue(labels) opFeatureMatrixCache.NonZeroLabelBlocks.setValue(0) opFeatureMatrixCache.LabelImage.setDirty(numpy.s_[10:11, 10:12]) opFeatureMatrixCache.LabelImage.setDirty(numpy.s_[20:21, 20:22]) opFeatureMatrixCache.LabelImage.setDirty(numpy.s_[30:31, 30:32]) opTrain = OpTrainClassifierFromFeatureVectors(graph=graph) opTrain.ClassifierFactory.setValue( VigraRfLazyflowClassifierFactory(10)) opTrain.MaxLabel.setValue(2) opTrain.LabelAndFeatureMatrix.connect( opFeatureMatrixCache.LabelAndFeatureMatrix) trained_classifer = opTrain.Classifier.value # This isn't much of a test at the moment... assert isinstance(trained_classifer, VigraRfLazyflowClassifier)
def testBasic(self): features = numpy.indices( (100,100) ).astype(numpy.float) + 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 = OpTrainClassifierBlocked( graph=graph ) opTrain.ClassifierFactory.setValue( VigraRfLazyflowClassifierFactory(10) ) opTrain.Images.resize(1) opTrain.Labels.resize(1) opTrain.nonzeroLabelBlocks.resize(1) opTrain.Images[0].setValue( features ) opTrain.Labels[0].setValue( labels ) opTrain.nonzeroLabelBlocks[0].setValue(0) # Dummy for now (not used by operator yet) 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, VigraRfLazyflowClassifier )
def execute(self, slot, subindex, roi, result): assert len(self.Train) == 2, "need data and target" assert len(self.Valid) == 2, "need data and target" assert roi.start[0] == 0 assert roi.stop[0] == 1 train = self.Train[0][...].wait() valid = self.Valid[0][...].wait() X = np.concatenate((train, valid), axis=0) assert len(self.Train[1].meta.shape) == 2,\ "target needs to be a matrix" assert len(self.Valid[1].meta.shape) == 2,\ "target needs to be a matrix" train = self.Train[1][...].wait() valid = self.Valid[1][...].wait() y = np.concatenate((train, valid), axis=0) y = np.argmax(y, axis=1) factory = VigraRfLazyflowClassifierFactory() classifier = factory.create_and_train(X, y) result[0] = classifier