List of usage examples for weka.classifiers AbstractClassifier makeCopy
public static Classifier makeCopy(Classifier model) throws Exception
From source file:nl.uva.sne.commons.ClusterUtils.java
private static FilteredClassifier buildModel(int[] indicesToRemove, int classIndex, Instances trainDataset, Classifier cl) throws Exception { FilteredClassifier model = new FilteredClassifier(); model.setClassifier(AbstractClassifier.makeCopy(cl)); Remove remove = new Remove(); remove.setAttributeIndicesArray(indicesToRemove); remove.setInputFormat(trainDataset); remove.setInvertSelection(false);/*from w ww. j a v a 2 s.c o m*/ model.setFilter(remove); trainDataset.setClassIndex(classIndex); model.buildClassifier(trainDataset); // int foldHash = trainDataset.toString().hashCode(); // String modelKey = createKey(indicesToRemove, foldHash); // existingModels.put(modelKey, model); return model; }
From source file:org.dkpro.similarity.algorithms.ml.ClassifierSimilarityMeasure.java
License:Open Source License
public ClassifierSimilarityMeasure(WekaClassifier classifier, File trainArff, File testArff) throws Exception { CLASSIFIER = getClassifier(classifier); // Get all instances Instances train = getTrainInstances(trainArff); test = getTestInstances(testArff);//from ww w. ja va 2 s . c om // Apply log filter Filter logFilter = new LogFilter(); logFilter.setInputFormat(train); train = Filter.useFilter(train, logFilter); logFilter.setInputFormat(test); test = Filter.useFilter(test, logFilter); Classifier clsCopy; try { // Copy the classifier clsCopy = AbstractClassifier.makeCopy(CLASSIFIER); // Build the classifier filteredClassifier = clsCopy; filteredClassifier.buildClassifier(train); Evaluation eval = new Evaluation(train); eval.evaluateModel(filteredClassifier, test); System.out.println(eval.toSummaryString()); System.out.println(eval.toMatrixString()); } catch (Exception e) { throw new SimilarityException(e); } }
From source file:org.dkpro.similarity.algorithms.ml.LinearRegressionSimilarityMeasure.java
License:Open Source License
public LinearRegressionSimilarityMeasure(File trainArff, File testArff, boolean useLogFilter) throws Exception { // Get all instances Instances train = getTrainInstances(trainArff); test = getTestInstances(testArff);/*from ww w . ja v a2 s . c o m*/ // Apply log filter if (useLogFilter) { Filter logFilter = new LogFilter(); logFilter.setInputFormat(train); train = Filter.useFilter(train, logFilter); logFilter.setInputFormat(test); test = Filter.useFilter(test, logFilter); } Classifier clsCopy; try { // Copy the classifier clsCopy = AbstractClassifier.makeCopy(CLASSIFIER); // Build the classifier filteredClassifier = clsCopy; filteredClassifier.buildClassifier(train); Evaluation eval = new Evaluation(train); eval.evaluateModel(filteredClassifier, test); System.out.println(filteredClassifier.toString()); } catch (Exception e) { throw new SimilarityException(e); } }
From source file:tr.gov.ulakbim.jDenetX.classifiers.WEKAClassifier.java
License:Open Source License
public void buildClassifier() { try {// w w w .j av a 2s . com if ((classifier instanceof UpdateableClassifier) == false) { Classifier auxclassifier = AbstractClassifier.makeCopy(classifier); auxclassifier.buildClassifier(instancesBuffer); classifier = auxclassifier; isBufferStoring = false; } } catch (Exception e) { System.err.println("Building WEKA Classifier: " + e.getMessage()); } }
From source file:trainableSegmentation.WekaSegmentation.java
License:GNU General Public License
/** * Apply current classifier to a given image in a complete concurrent way. * This method is experimental, it divides the image(s) in pieces and that * can cause artifacts using some filters. * * @param imp image (2D single image or stack) * @param numThreads The number of threads to use. Set to zero for * auto-detection./*w w w . j a va 2 s. co m*/ * @param probabilityMaps create probability maps for each class instead of * a classification * @return result image */ public ImagePlus applyClassifierMT(final ImagePlus imp, int numThreads, final boolean probabilityMaps) { if (numThreads == 0) numThreads = Prefs.getThreads(); final int numClasses = numOfClasses; final int numChannels = (probabilityMaps ? numClasses : 1); IJ.log("Classifying data from image " + imp.getTitle() + " using " + numThreads + " thread(s)..."); // Set proper class names (skip empty list ones) ArrayList<String> classNames = new ArrayList<String>(); if (null == loadedClassNames) { for (int i = 0; i < numOfClasses; i++) for (int j = 0; j < trainingImage.getImageStackSize(); j++) if (examples[j].get(i).size() > 0) { classNames.add(getClassLabels()[i]); break; } } else classNames = loadedClassNames; // Create instances information (each instance needs a pointer to this) ArrayList<Attribute> attributes = new ArrayList<Attribute>(); for (int i = 1; i <= featureStackArray.getNumOfFeatures(); i++) { String attString = featureStackArray.getLabel(i); attributes.add(new Attribute(attString)); } if (featureStackArray.useNeighborhood()) for (int i = 0; i < 8; i++) { IJ.log("Adding extra attribute original_neighbor_" + (i + 1) + "..."); attributes.add(new Attribute(new String("original_neighbor_" + (i + 1)))); } attributes.add(new Attribute("class", classNames)); Instances dataInfo = new Instances("segment", attributes, 1); dataInfo.setClassIndex(dataInfo.numAttributes() - 1); final long start = System.currentTimeMillis(); // Initialize executor service if (exe.isShutdown()) exe = Executors.newFixedThreadPool(numThreads); // counter to display the progress final AtomicInteger counter = new AtomicInteger(); // slice dimensions final int height = imp.getHeight(); final int width = imp.getWidth(); final int pad = (int) maximumSigma; // Calculate number of rows per thread // (with this division we may miss one row, // but it will be added to the last thread) int numOfRows = height * imp.getImageStackSize() / numThreads; // set each slice in a thread Future<ArrayList<ImagePlus>> fu[] = new Future[numThreads]; ArrayList<int[]> imagePad = new ArrayList<int[]>(); ArrayList<ImagePlus> list[] = new ArrayList[numThreads]; // Divide work among available threads //IJ.log("Dividing image data among the " + numThreads + " available threads..."); //final long time1 = System.currentTimeMillis(); for (int i = 0; i < numThreads; i++) { list[i] = new ArrayList<ImagePlus>(); if (Thread.currentThread().isInterrupted()) return null; // Calculate list of images to be classified on each thread int firstRow = i * numOfRows; int lastRow = i < (numThreads - 1) ? (i + 1) * numOfRows - 1 : height * imp.getImageStackSize() - 1; //IJ.log("Thread " + i + ": first row = " + firstRow + ", last row = " + lastRow); int r = firstRow; int rowsToDo = lastRow - firstRow + 1; while (r < lastRow) { final int slice = r / height; final int begin = r - slice * height; final int end = (begin + rowsToDo) > height ? height - 1 : begin + rowsToDo - 1; // Create image ImageProcessor sliceImage = imp.getImageStack().getProcessor(slice + 1); // We pad the images if necessary (for the filtering) final int paddedBegin = begin - pad; final int paddedEnd = end + pad; // Crop the area of the slice that will be process on this thread sliceImage.setRoi(new Rectangle(0, paddedBegin, width, paddedEnd - paddedBegin + 1)); ImageProcessor im = sliceImage.crop(); final ImagePlus ip = new ImagePlus("slice-" + slice + "-" + begin, im); // add image to list list[i].add(ip); //IJ.log(" begin = " + begin + ", end = " + end + ", paddedBegin = " + paddedBegin + ", paddedEnd = " + paddedEnd + ", height = " + height + ", pad = " + pad); // We store the padding number to recover the area of interest later final int padTop = (paddedBegin >= 0) ? pad : pad + paddedBegin; final int padBottom = (paddedEnd < height) ? pad : pad - (paddedEnd - height + 1); //IJ.log(" padTop = " + padTop + ", padBottom = " + padBottom ); imagePad.add(new int[] { slice, /* slice number (starting at 0) */ padTop, /* top padding */ padBottom, /* bottom padding */ end - begin + 1 }); /* size (number of rows) */ int rowsDone = end - begin + 1; r += rowsDone; rowsToDo -= rowsDone; } } //final long time2 = System.currentTimeMillis(); //IJ.log(" Done. Image division took " + (time2-time1) + " ms."); // Create a copy of the classifier for each thread AbstractClassifier[] classifierCopy = new AbstractClassifier[numThreads]; IJ.log("Creating classifier copy for each thread..."); for (int i = 0; i < numThreads; i++) { try { // The Weka random forest classifiers do not need to be duplicated on each thread // (that saves much memory) if (classifier instanceof FastRandomForest || classifier instanceof RandomForest) classifierCopy[i] = classifier; else classifierCopy[i] = (AbstractClassifier) (AbstractClassifier.makeCopy(classifier)); } catch (Exception e) { IJ.log("Error: classifier could not be copied to classify in a multi-thread way."); e.printStackTrace(); } } //final long time3 = System.currentTimeMillis(); //IJ.log(" Done. Classifiers duplication took " + (time3-time2) + " ms."); // Submit the jobs for (int i = 0; i < numThreads; i++) { // classify slice fu[i] = exe .submit(classifyListOfImages(list[i], dataInfo, classifierCopy[i], counter, probabilityMaps)); } final int numInstances = imp.getHeight() * imp.getWidth() * imp.getStackSize(); ScheduledExecutorService monitor = Executors.newScheduledThreadPool(1); ScheduledFuture task = monitor.scheduleWithFixedDelay(new Runnable() { public void run() { IJ.showProgress(counter.get(), numInstances); } }, 0, 1, TimeUnit.SECONDS); // array of images to store the classification results final ArrayList<ImagePlus> classifiedImages = new ArrayList<ImagePlus>(); // Join threads for (int i = 0; i < numThreads; i++) { try { ArrayList<ImagePlus> result = fu[i].get(); for (ImagePlus ip : result) { classifiedImages.add(ip); } } catch (Exception e) { e.printStackTrace(); return null; } finally { task.cancel(true); monitor.shutdownNow(); IJ.showProgress(1); } } // create classified image final ImageStack classified = new ImageStack(imp.getWidth(), imp.getHeight()); for (int i = 0; i < imp.getStackSize(); i++) { if (numChannels > 1) { for (int c = 0; c < numChannels; c++) classified.addSlice("", new FloatProcessor(width, height)); } else classified.addSlice("", new ByteProcessor(width, height)); } // assemble classified image int n = 0; int raw = 0; for (final ImagePlus ip : classifiedImages) { raw = raw % height; //ip.show(); int[] coord = imagePad.get(n); //final int sliceNum = coord[ 0 ] + 1; final int beginPad = coord[1]; final int endPad = coord[2]; final int size = coord[3]; //IJ.log(" coord[0] = " + coord[0] + ", coord[1] = " + coord[1] + ", coord[2] = " + coord[2] + ", coord[3] = " + coord[3] ); for (int c = 0; c < numChannels; c++) { // target image ImageProcessor target = classified.getProcessor(coord[0] * numChannels + c + 1); // source image ImageProcessor source = ip.getImageStack().getProcessor(c + 1); //IJ.log(" set roi = 0, " + beginPad + ", " + width + ", " + (ip.getHeight() - endPad)); source.setRoi(new Rectangle(0, beginPad, width, ip.getHeight() - endPad)); source = source.crop(); // copy target.copyBits(source, 0, raw, Blitter.COPY); } raw += size; n++; } ImagePlus result = new ImagePlus("Classification result", classified); if (probabilityMaps) { result.setDimensions(numOfClasses, imp.getNSlices(), imp.getNFrames()); if (imp.getNSlices() * imp.getNFrames() > 1) result.setOpenAsHyperStack(true); } final long end = System.currentTimeMillis(); IJ.log("Whole image classification took " + (end - start) + " ms."); return result; }
From source file:trainableSegmentation.WekaSegmentation.java
License:GNU General Public License
/** * Apply current classifier to set of instances * @param data set of instances//from ww w.j a v a 2 s . c o m * @param w image width * @param h image height * @param numThreads The number of threads to use. Set to zero for * auto-detection. * @return result image */ public ImagePlus applyClassifier(final Instances data, int w, int h, int numThreads, boolean probabilityMaps) { if (numThreads == 0) numThreads = Prefs.getThreads(); final int numClasses = data.numClasses(); final int numInstances = data.numInstances(); final int numChannels = (probabilityMaps ? numClasses : 1); final int numSlices = (numChannels * numInstances) / (w * h); IJ.showStatus("Classifying image..."); final long start = System.currentTimeMillis(); ExecutorService exe = Executors.newFixedThreadPool(numThreads); final double[][][] results = new double[numThreads][][]; final Instances[] partialData = new Instances[numThreads]; final int partialSize = numInstances / numThreads; Future<double[][]> fu[] = new Future[numThreads]; final AtomicInteger counter = new AtomicInteger(); for (int i = 0; i < numThreads; i++) { if (Thread.currentThread().isInterrupted()) { exe.shutdown(); return null; } if (i == numThreads - 1) partialData[i] = new Instances(data, i * partialSize, numInstances - i * partialSize); else partialData[i] = new Instances(data, i * partialSize, partialSize); AbstractClassifier classifierCopy = null; try { // The Weka random forest classifiers do not need to be duplicated on each thread // (that saves much memory) if (classifier instanceof FastRandomForest || classifier instanceof RandomForest) classifierCopy = classifier; else classifierCopy = (AbstractClassifier) (AbstractClassifier.makeCopy(classifier)); } catch (Exception e) { IJ.log("Error: classifier could not be copied to classify in a multi-thread way."); e.printStackTrace(); } fu[i] = exe.submit(classifyInstances(partialData[i], classifierCopy, counter, probabilityMaps)); } ScheduledExecutorService monitor = Executors.newScheduledThreadPool(1); ScheduledFuture task = monitor.scheduleWithFixedDelay(new Runnable() { public void run() { IJ.showProgress(counter.get(), numInstances); } }, 0, 1, TimeUnit.SECONDS); // Join threads for (int i = 0; i < numThreads; i++) { try { results[i] = fu[i].get(); } catch (InterruptedException e) { //e.printStackTrace(); return null; } catch (ExecutionException e) { e.printStackTrace(); return null; } finally { exe.shutdown(); task.cancel(true); monitor.shutdownNow(); IJ.showProgress(1); } } exe.shutdown(); // Create final array double[][] classificationResult; classificationResult = new double[numChannels][numInstances]; for (int i = 0; i < numThreads; i++) for (int c = 0; c < numChannels; c++) System.arraycopy(results[i][c], 0, classificationResult[c], i * partialSize, results[i][c].length); IJ.showProgress(1.0); final long end = System.currentTimeMillis(); IJ.log("Classifying whole image data took: " + (end - start) + "ms"); double[] classifiedSlice = new double[w * h]; final ImageStack classStack = new ImageStack(w, h); for (int i = 0; i < numSlices / numChannels; i++) { for (int c = 0; c < numChannels; c++) { System.arraycopy(classificationResult[c], i * (w * h), classifiedSlice, 0, w * h); ImageProcessor classifiedSliceProcessor = new FloatProcessor(w, h, classifiedSlice); classStack.addSlice(probabilityMaps ? getClassLabels()[c] : "", classifiedSliceProcessor); } } ImagePlus classImg = new ImagePlus(probabilityMaps ? "Probability maps" : "Classification result", classStack); return classImg; }
From source file:trainableSegmentation.WekaSegmentation.java
License:GNU General Public License
/** * Apply current classifier to a set of feature vectors (given in a feature stack array) * /*from w ww. j a v a 2 s.c o m*/ * @param fsa feature stack array * @param numThreads The number of threads to use. Set to zero for auto-detection. * @param probabilityMaps probability flag. Tue: probability maps are calculated, false: binary classification * @return result image containing the probability maps or the binary classification */ public ImagePlus applyClassifier(final FeatureStackArray fsa, int numThreads, boolean probabilityMaps) { if (numThreads == 0) numThreads = Prefs.getThreads(); ArrayList<String> classNames = null; if (null != loadedClassNames) classNames = loadedClassNames; else { classNames = new ArrayList<String>(); for (int j = 0; j < trainingImage.getImageStackSize(); j++) for (int i = 0; i < numOfClasses; i++) if (examples[j].get(i).size() > 0) if (false == classNames.contains(getClassLabels()[i])) classNames.add(getClassLabels()[i]); } // Create instances information (each instance needs a pointer to this) ArrayList<Attribute> attributes = new ArrayList<Attribute>(); for (int i = 1; i <= fsa.getNumOfFeatures(); i++) { String attString = fsa.getLabel(i); attributes.add(new Attribute(attString)); } if (fsa.useNeighborhood()) for (int i = 0; i < 8; i++) { IJ.log("Adding extra attribute original_neighbor_" + (i + 1) + "..."); attributes.add(new Attribute(new String("original_neighbor_" + (i + 1)))); } attributes.add(new Attribute("class", classNames)); Instances dataInfo = new Instances("segment", attributes, 1); dataInfo.setClassIndex(dataInfo.numAttributes() - 1); // number of classes final int numClasses = classNames.size(); // total number of instances (i.e. feature vectors) final int numInstances = fsa.getSize() * trainingImage.getWidth() * trainingImage.getHeight(); // number of channels of the result image final int numChannels = (probabilityMaps ? numClasses : 1); // number of slices of the result image final int numSlices = (numChannels * numInstances) / (trainingImage.getWidth() * trainingImage.getHeight()); IJ.showStatus("Classifying image..."); final long start = System.currentTimeMillis(); exe = Executors.newFixedThreadPool(numThreads); final double[][][] results = new double[numThreads][][]; final int partialSize = numInstances / numThreads; Future<double[][]> fu[] = new Future[numThreads]; final AtomicInteger counter = new AtomicInteger(); for (int i = 0; i < numThreads; i++) { if (Thread.currentThread().isInterrupted()) return null; int first = i * partialSize; int size = (i == numThreads - 1) ? numInstances - i * partialSize : partialSize; AbstractClassifier classifierCopy = null; try { // The Weka random forest classifiers do not need to be duplicated on each thread // (that saves much memory) if (classifier instanceof FastRandomForest || classifier instanceof RandomForest) classifierCopy = classifier; else classifierCopy = (AbstractClassifier) (AbstractClassifier.makeCopy(classifier)); } catch (Exception e) { IJ.log("Error: classifier could not be copied to classify in a multi-thread way."); e.printStackTrace(); } fu[i] = exe.submit( classifyInstances(fsa, dataInfo, first, size, classifierCopy, counter, probabilityMaps)); } ScheduledExecutorService monitor = Executors.newScheduledThreadPool(1); ScheduledFuture task = monitor.scheduleWithFixedDelay(new Runnable() { public void run() { IJ.showProgress(counter.get(), numInstances); } }, 0, 1, TimeUnit.SECONDS); // Join threads for (int i = 0; i < numThreads; i++) { try { results[i] = fu[i].get(); } catch (InterruptedException e) { //e.printStackTrace(); return null; } catch (ExecutionException e) { e.printStackTrace(); return null; } finally { exe.shutdown(); task.cancel(true); monitor.shutdownNow(); IJ.showProgress(1); } } exe.shutdown(); // Create final array double[][] classificationResult = new double[numChannels][numInstances]; for (int i = 0; i < numThreads; i++) for (int c = 0; c < numChannels; c++) System.arraycopy(results[i][c], 0, classificationResult[c], i * partialSize, results[i][c].length); IJ.showProgress(1.0); final long end = System.currentTimeMillis(); IJ.log("Classifying whole image data took: " + (end - start) + "ms"); double[] classifiedSlice = new double[trainingImage.getWidth() * trainingImage.getHeight()]; final ImageStack classStack = new ImageStack(trainingImage.getWidth(), trainingImage.getHeight()); for (int i = 0; i < numSlices / numChannels; i++) { for (int c = 0; c < numChannels; c++) { System.arraycopy(classificationResult[c], i * (trainingImage.getWidth() * trainingImage.getHeight()), classifiedSlice, 0, trainingImage.getWidth() * trainingImage.getHeight()); ImageProcessor classifiedSliceProcessor = new FloatProcessor(trainingImage.getWidth(), trainingImage.getHeight(), classifiedSlice); classStack.addSlice(probabilityMaps ? getClassLabels()[c] : "", classifiedSliceProcessor); } } ImagePlus classImg = new ImagePlus(probabilityMaps ? "Probability maps" : "Classification result", classStack); return classImg; }