Search in sources :

Example 1 with HistogramScene

use of boofcv.alg.scene.HistogramScene in project BoofCV by lessthanoptimal.

the class ExampleClassifySceneKnn method computeHistograms.

/**
 * For all the images in the training data set it computes a {@link HistogramScene}.  That data structure
 * contains the word histogram and the scene that the histogram belongs to.
 */
private List<HistogramScene> computeHistograms(FeatureToWordHistogram_F64 featuresToHistogram) {
    List<String> scenes = getScenes();
    // Processed results which will be passed into the k-NN algorithm
    List<HistogramScene> memory;
    memory = new ArrayList<>();
    for (int sceneIndex = 0; sceneIndex < scenes.size(); sceneIndex++) {
        String scene = scenes.get(sceneIndex);
        System.out.println("   " + scene);
        List<String> imagePaths = train.get(scene);
        for (String path : imagePaths) {
            GrayU8 image = UtilImageIO.loadImage(path, GrayU8.class);
            // reset before processing a new image
            featuresToHistogram.reset();
            describeImage.process(image);
            for (TupleDesc_F64 d : describeImage.getDescriptions()) {
                featuresToHistogram.addFeature(d);
            }
            featuresToHistogram.process();
            // The histogram is already normalized so that it sums up to 1.  This provides invariance
            // against the overall number of features changing.
            double[] histogram = featuresToHistogram.getHistogram();
            // Create the data structure used by the KNN classifier
            HistogramScene imageHist = new HistogramScene(NUMBER_OF_WORDS);
            imageHist.setHistogram(histogram);
            imageHist.type = sceneIndex;
            memory.add(imageHist);
        }
    }
    return memory;
}
Also used : TupleDesc_F64(boofcv.struct.feature.TupleDesc_F64) HistogramScene(boofcv.alg.scene.HistogramScene) GrayU8(boofcv.struct.image.GrayU8)

Example 2 with HistogramScene

use of boofcv.alg.scene.HistogramScene in project BoofCV by lessthanoptimal.

the class ExampleClassifySceneKnn method loadAndCreateClassifier.

public void loadAndCreateClassifier() {
    // load results from a file
    List<HistogramScene> memory = UtilIO.load(HISTOGRAM_FILE_NAME);
    AssignCluster<double[]> assignment = UtilIO.load(CLUSTER_FILE_NAME);
    FeatureToWordHistogram_F64 featuresToHistogram = new FeatureToWordHistogram_F64(assignment, HISTOGRAM_HARD);
    // Provide the training results to K-NN and it will preprocess these results for quick lookup later on
    // Can use this classifier with saved results and avoid the
    classifier = new ClassifierKNearestNeighborsBow<>(nn, describeImage, featuresToHistogram);
    classifier.setClassificationData(memory, getScenes().size());
    classifier.setNumNeighbors(NUM_NEIGHBORS);
}
Also used : HistogramScene(boofcv.alg.scene.HistogramScene) FeatureToWordHistogram_F64(boofcv.alg.scene.FeatureToWordHistogram_F64)

Example 3 with HistogramScene

use of boofcv.alg.scene.HistogramScene in project BoofCV by lessthanoptimal.

the class ExampleClassifySceneKnn method learnAndSave.

/**
 * Process all the data in the training data set to learn the classifications.  See code for details.
 */
public void learnAndSave() {
    System.out.println("======== Learning Classifier");
    // Either load pre-computed words or compute the words from the training images
    AssignCluster<double[]> assignment;
    if (new File(CLUSTER_FILE_NAME).exists()) {
        assignment = UtilIO.load(CLUSTER_FILE_NAME);
    } else {
        System.out.println(" Computing clusters");
        assignment = computeClusters();
    }
    // Use these clusters to assign features to words
    FeatureToWordHistogram_F64 featuresToHistogram = new FeatureToWordHistogram_F64(assignment, HISTOGRAM_HARD);
    // Storage for the work histogram in each image in the training set and their label
    List<HistogramScene> memory;
    if (!new File(HISTOGRAM_FILE_NAME).exists()) {
        System.out.println(" computing histograms");
        memory = computeHistograms(featuresToHistogram);
        UtilIO.save(memory, HISTOGRAM_FILE_NAME);
    }
}
Also used : HistogramScene(boofcv.alg.scene.HistogramScene) File(java.io.File) FeatureToWordHistogram_F64(boofcv.alg.scene.FeatureToWordHistogram_F64)

Example 4 with HistogramScene

use of boofcv.alg.scene.HistogramScene in project BoofCV by lessthanoptimal.

the class ExampleClassifySceneKnn method main.

public static void main(String[] args) {
    ConfigDenseSurfFast surfFast = new ConfigDenseSurfFast(new DenseSampling(8, 8));
    ConfigDenseSurfStable surfStable = new ConfigDenseSurfStable(new DenseSampling(8, 8));
    ConfigDenseSift sift = new ConfigDenseSift(new DenseSampling(6, 6));
    ConfigDenseHoG hog = new ConfigDenseHoG();
    DescribeImageDense<GrayU8, TupleDesc_F64> desc = (DescribeImageDense) FactoryDescribeImageDense.surfFast(surfFast, GrayU8.class);
    // FactoryDescribeImageDense.surfStable(surfStable, GrayU8.class);
    // FactoryDescribeImageDense.sift(sift, GrayU8.class);
    // FactoryDescribeImageDense.hog(hog, ImageType.single(GrayU8.class));
    ComputeClusters<double[]> clusterer = FactoryClustering.kMeans_F64(null, MAX_KNN_ITERATIONS, 20, 1e-6);
    clusterer.setVerbose(true);
    NearestNeighbor<HistogramScene> nn = FactoryNearestNeighbor.exhaustive();
    ExampleClassifySceneKnn example = new ExampleClassifySceneKnn(desc, clusterer, nn);
    File trainingDir = new File(UtilIO.pathExample("learning/scene/train"));
    File testingDir = new File(UtilIO.pathExample("learning/scene/test"));
    if (!trainingDir.exists() || !testingDir.exists()) {
        String addressSrc = "http://boofcv.org/notwiki/largefiles/bow_data_v001.zip";
        File dst = new File(trainingDir.getParentFile(), "bow_data_v001.zip");
        try {
            DeepBoofDataBaseOps.download(addressSrc, dst);
            DeepBoofDataBaseOps.decompressZip(dst, dst.getParentFile(), true);
            System.out.println("Download complete!");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else {
        System.out.println("Delete and download again if there are file not found errors");
        System.out.println("   " + trainingDir);
        System.out.println("   " + testingDir);
    }
    example.loadSets(trainingDir, null, testingDir);
    // train the classifier
    example.learnAndSave();
    // now load it for evaluation purposes from the files
    example.loadAndCreateClassifier();
    // test the classifier on the test set
    Confusion confusion = example.evaluateTest();
    confusion.getMatrix().print();
    System.out.println("Accuracy = " + confusion.computeAccuracy());
    // Show confusion matrix
    // Not the best coloration scheme...  perfect = red diagonal and blue elsewhere.
    ShowImages.showWindow(new ConfusionMatrixPanel(confusion.getMatrix(), example.getScenes(), 400, true), "Confusion Matrix", true);
// For SIFT descriptor the accuracy is          54.0%
// For  "fast"  SURF descriptor the accuracy is 52.2%
// For "stable" SURF descriptor the accuracy is 49.4%
// For HOG                                      53.3%
// SURF results are interesting. "Stable" is significantly better than "fast"!
// One explanation is that the descriptor for "fast" samples a smaller region than "stable", by a
// couple of pixels at scale of 1.  Thus there is less overlap between the features.
// Reducing the size of "stable" to 0.95 does slightly improve performance to 50.5%, can't scale it down
// much more without performance going down
}
Also used : ConfusionMatrixPanel(boofcv.gui.learning.ConfusionMatrixPanel) TupleDesc_F64(boofcv.struct.feature.TupleDesc_F64) IOException(java.io.IOException) Confusion(boofcv.struct.learning.Confusion) HistogramScene(boofcv.alg.scene.HistogramScene) GrayU8(boofcv.struct.image.GrayU8) DescribeImageDense(boofcv.abst.feature.dense.DescribeImageDense) File(java.io.File)

Aggregations

HistogramScene (boofcv.alg.scene.HistogramScene)4 FeatureToWordHistogram_F64 (boofcv.alg.scene.FeatureToWordHistogram_F64)2 TupleDesc_F64 (boofcv.struct.feature.TupleDesc_F64)2 GrayU8 (boofcv.struct.image.GrayU8)2 File (java.io.File)2 DescribeImageDense (boofcv.abst.feature.dense.DescribeImageDense)1 ConfusionMatrixPanel (boofcv.gui.learning.ConfusionMatrixPanel)1 Confusion (boofcv.struct.learning.Confusion)1 IOException (java.io.IOException)1