Example usage for weka.core DenseInstance setWeight

List of usage examples for weka.core DenseInstance setWeight

Introduction

In this page you can find the example usage for weka.core DenseInstance setWeight.

Prototype

@Override
public final void setWeight(double weight) 

Source Link

Document

Sets the weight of an instance.

Usage

From source file:moa.cluster.Riffle.java

License:Apache License

/**
 * Converts cluster to an instance//from   w w w  .  j a  va 2 s . co  m
 * @return Instance version of the centroid
 */
public Instance toInstance() {
    DenseInstance ret = new DenseInstance(0.0, centroid);
    ret.setWeight(0.0);
    ret.setDataset(this.instances);
    return ret;
}

From source file:trainableSegmentation.WekaSegmentation.java

License:GNU General Public License

/**
 * Add instances to two classes from lists of coordinates in a random
 * and balanced way.//from www  . j a  va  2 s.  c o m
 * White pixels will be added to the corresponding class 1 and
 * black pixels will be added to class 2.
 *
 * @param classPoints list of 3D coordinates to be used (x, y, slice)
 * @param fsa feature stack array
 * @param weights weight image
 * @param whiteClassName name of the class which receives the white pixels
 * @param blackClassName name of the class which receives the black pixels
 * @param numSamples number of samples to add of each class
 * 
 * @return false if error
 */
public boolean addRandomBalancedBinaryData(List<Point3f>[] classPoints, FeatureStackArray fsa,
        ImagePlus weights, String whiteClassName, String blackClassName, int numSamples) {

    // Detect class indexes
    int whiteClassIndex = 0;
    for (whiteClassIndex = 0; whiteClassIndex < this.getClassLabels().length; whiteClassIndex++)
        if (whiteClassName.equalsIgnoreCase(this.getClassLabels()[whiteClassIndex]))
            break;
    if (whiteClassIndex == this.getClassLabels().length) {
        IJ.log("Error: class named '" + whiteClassName + "' not found.");
        return false;
    }
    int blackClassIndex = 0;
    for (blackClassIndex = 0; blackClassIndex < this.getClassLabels().length; blackClassIndex++)
        if (blackClassName.equalsIgnoreCase(this.getClassLabels()[blackClassIndex]))
            break;
    if (blackClassIndex == this.getClassLabels().length) {
        IJ.log("Error: class named '" + blackClassName + "' not found.");
        return false;
    }

    // Create loaded training data if it does not exist yet
    if (null == loadedTrainingData) {
        IJ.log("Initializing loaded data...");
        // Create instances
        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))));
            }

        // Update list of names of loaded classes
        // (we assume the first two default class names)
        loadedClassNames = new ArrayList<String>();
        for (int i = 0; i < numOfClasses; i++)
            loadedClassNames.add(getClassLabels()[i]);
        attributes.add(new Attribute("class", loadedClassNames));
        loadedTrainingData = new Instances("segment", attributes, 1);

        loadedTrainingData.setClassIndex(loadedTrainingData.numAttributes() - 1);
    }

    final int width = weights.getWidth();

    // Select random samples from both classes
    Random rand = new Random();
    for (int i = 0; i < numSamples; i++) {
        int randomBlack = rand.nextInt(classPoints[0].size());
        int randomWhite = rand.nextInt(classPoints[1].size());

        // add random black sample
        final int blackZ = (int) (classPoints[0].get(randomBlack).z);
        final int blackX = (int) (classPoints[0].get(randomBlack).x);
        final int blackY = (int) (classPoints[0].get(randomBlack).y);

        DenseInstance blackInstance = fsa.get(blackZ).createInstance(blackX, blackY, blackClassIndex);

        blackInstance.setWeight(((float[]) weights.getImageStack().getProcessor(blackZ + 1).getPixels())[blackX
                + blackY * width]);

        loadedTrainingData.add(blackInstance);

        // add random white sample
        final int whiteZ = (int) (classPoints[1].get(randomWhite).z);
        final int whiteX = (int) (classPoints[1].get(randomWhite).x);
        final int whiteY = (int) (classPoints[1].get(randomWhite).y);

        DenseInstance whiteInstance = fsa.get(whiteZ).createInstance(whiteX, whiteY, whiteClassIndex);

        whiteInstance.setWeight(((float[]) weights.getImageStack().getProcessor(whiteZ + 1).getPixels())[whiteX
                + whiteY * width]);

        loadedTrainingData.add(whiteInstance);
    }

    IJ.log("Added " + numSamples + " instances of '" + whiteClassName + "'.");
    IJ.log("Added " + numSamples + " instances of '" + blackClassName + "'.");

    IJ.log("Training dataset updated (" + loadedTrainingData.numInstances() + " instances, "
            + loadedTrainingData.numAttributes() + " attributes, " + loadedTrainingData.numClasses()
            + " classes).");

    return true;
}

From source file:trainableSegmentation.WekaSegmentation.java

License:GNU General Public License

/**
 * Add instances to two classes from a label (binary) image in a random
 * and balanced way./*from w  ww. j ava  2  s .c  o  m*/
 * White pixels will be added to the corresponding class 1 and
 * black pixels will be added to class 2.
 *
 * @param labelImage binary image
 * @param mask mask image
 * @param weights weight image
 * @param featureStack corresponding feature stack
 * @param whiteClassName name of the class which receives the white pixels
 * @param blackClassName name of the class which receives the black pixels
 * @param numSamples number of samples to add of each class
 * @param mask binary mask image to prevent some pixel to be selected (null if all pixels are eligible)
 * @return false if error
 */
public boolean addRandomBalancedBinaryData(ImageProcessor labelImage, ImageProcessor mask,
        ImageProcessor weights, FeatureStack featureStack, String whiteClassName, String blackClassName,
        int numSamples) {
    // Update features if necessary
    if (featureStack.getSize() < 2) {
        IJ.log("Creating feature stack...");
        featureStack.updateFeaturesMT();
        filterFeatureStackByList(this.featureNames, featureStack);
        updateFeatures = false;
        IJ.log("Feature stack is now updated.");
    }

    // Detect class indexes
    int whiteClassIndex = 0;
    for (whiteClassIndex = 0; whiteClassIndex < this.getClassLabels().length; whiteClassIndex++)
        if (whiteClassName.equalsIgnoreCase(this.getClassLabels()[whiteClassIndex]))
            break;
    if (whiteClassIndex == this.getClassLabels().length) {
        IJ.log("Error: class named '" + whiteClassName + "' not found.");
        return false;
    }
    int blackClassIndex = 0;
    for (blackClassIndex = 0; blackClassIndex < this.getClassLabels().length; blackClassIndex++)
        if (blackClassName.equalsIgnoreCase(this.getClassLabels()[blackClassIndex]))
            break;
    if (blackClassIndex == this.getClassLabels().length) {
        IJ.log("Error: class named '" + blackClassName + "' not found.");
        return false;
    }

    // Create loaded training data if it does not exist yet
    if (null == loadedTrainingData) {
        IJ.log("Initializing loaded data...");
        // Create instances
        ArrayList<Attribute> attributes = new ArrayList<Attribute>();
        for (int i = 1; i <= featureStack.getSize(); i++) {
            String attString = featureStack.getSliceLabel(i);
            attributes.add(new Attribute(attString));
        }

        if (featureStack.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))));
            }

        // Update list of names of loaded classes
        // (we assume the first two default class names)
        loadedClassNames = new ArrayList<String>();
        for (int i = 0; i < numOfClasses; i++)
            loadedClassNames.add(getClassLabels()[i]);
        attributes.add(new Attribute("class", loadedClassNames));
        loadedTrainingData = new Instances("segment", attributes, 1);

        loadedTrainingData.setClassIndex(loadedTrainingData.numAttributes() - 1);
    }

    // Create lists of coordinates of pixels of both classes
    ArrayList<Point> blackCoordinates = new ArrayList<Point>();
    ArrayList<Point> whiteCoordinates = new ArrayList<Point>();
    final int width = labelImage.getWidth();
    final int height = labelImage.getHeight();

    for (int y = 0; y < height; y++)
        for (int x = 0; x < width; x++) {
            // White pixels are added to the class 1
            // and black to class 2
            if (null != mask && mask.getPixelValue(x, y) > 0) {
                if (labelImage.getPixelValue(x, y) > 0)
                    whiteCoordinates.add(new Point(x, y));
                else
                    blackCoordinates.add(new Point(x, y));
            }
        }

    // Select random samples from both classes
    Random rand = new Random();
    for (int i = 0; i < numSamples; i++) {
        int randomBlack = rand.nextInt(blackCoordinates.size());
        int randomWhite = rand.nextInt(whiteCoordinates.size());

        DenseInstance blackSample = featureStack.createInstance(blackCoordinates.get(randomBlack).x,
                blackCoordinates.get(randomBlack).y, blackClassIndex);
        blackSample.setWeight(weights.getPixelValue(blackCoordinates.get(randomBlack).x,
                blackCoordinates.get(randomBlack).y));
        loadedTrainingData.add(blackSample);

        DenseInstance whiteSample = featureStack.createInstance(whiteCoordinates.get(randomWhite).x,
                whiteCoordinates.get(randomWhite).y, whiteClassIndex);

        whiteSample.setWeight(weights.getPixelValue(whiteCoordinates.get(randomWhite).x,
                whiteCoordinates.get(randomWhite).y));
        loadedTrainingData.add(whiteSample);
    }

    IJ.log("Added " + numSamples + " instances of '" + whiteClassName + "'.");
    IJ.log("Added " + numSamples + " instances of '" + blackClassName + "'.");

    IJ.log("Training dataset updated (" + loadedTrainingData.numInstances() + " instances, "
            + loadedTrainingData.numAttributes() + " attributes, " + loadedTrainingData.numClasses()
            + " classes).");

    return true;
}