OpenImaj - определить фигуру - PullRequest
0 голосов
/ 12 октября 2018

Моя цель состоит в том, чтобы определить форму после обучения классификатора, аналогично тому, что было сделано в главе 12 Руководства OpenIMAJ http://openimaj.org/tutorial/classification101.html.В главе 12 используется класс Caltech101, который мне не помогает, потому что я хочу использовать свой собственный набор изображений для обучения классификатора.Я создал этот рабочий код, основанный на главе 12:

package com.mycompany.video.analytics;

import de.bwaldvogel.liblinear.SolverType;
import org.openimaj.data.DataSource;
import org.openimaj.data.dataset.Dataset;
import org.openimaj.data.dataset.GroupedDataset;
import org.openimaj.data.dataset.ListDataset;
import org.openimaj.data.dataset.VFSGroupDataset;
import org.openimaj.experiment.dataset.sampling.GroupSampler;
import org.openimaj.experiment.dataset.sampling.GroupedUniformRandomisedSampler;
import org.openimaj.experiment.dataset.split.GroupedRandomSplitter;
import org.openimaj.experiment.evaluation.classification.ClassificationEvaluator;
import org.openimaj.experiment.evaluation.classification.ClassificationResult;
import org.openimaj.experiment.evaluation.classification.analysers.confusionmatrix.CMAnalyser;
import org.openimaj.experiment.evaluation.classification.analysers.confusionmatrix.CMResult;
import org.openimaj.feature.DoubleFV;
import org.openimaj.feature.FeatureExtractor;
import org.openimaj.feature.SparseIntFV;
import org.openimaj.feature.local.data.LocalFeatureListDataSource;
import org.openimaj.feature.local.list.LocalFeatureList;
import org.openimaj.image.FImage;
import org.openimaj.image.ImageUtilities;
import org.openimaj.image.feature.dense.gradient.dsift.ByteDSIFTKeypoint;
import org.openimaj.image.feature.dense.gradient.dsift.DenseSIFT;
import org.openimaj.image.feature.dense.gradient.dsift.PyramidDenseSIFT;
import org.openimaj.image.feature.local.aggregate.BagOfVisualWords;
import org.openimaj.image.feature.local.aggregate.BlockSpatialAggregator;
import org.openimaj.io.IOUtils;
import org.openimaj.ml.annotation.ScoredAnnotation;
import org.openimaj.ml.annotation.linear.LiblinearAnnotator;
import org.openimaj.ml.clustering.ByteCentroidsResult;
import org.openimaj.ml.clustering.assignment.HardAssigner;
import org.openimaj.ml.clustering.kmeans.ByteKMeans;
import org.openimaj.util.pair.IntFloatPair;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by yschondorf on 5/29/2018.
 */
public class Chapter12Generic {
    private static String IMAGES_PATH = "C:\\Development\\Video Analytics\\tpImages";

    public static void main(String[] args) {

        try {
            LiblinearAnnotator<FImage, String> trainer = null;
            VFSGroupDataset<FImage> allData = null;
            allData = new VFSGroupDataset<FImage>(
                    IMAGES_PATH,
                    ImageUtilities.FIMAGE_READER);

            GroupedDataset<String, ListDataset<FImage>, FImage> data =
                    GroupSampler.sample(allData, 1, false);

            GroupedRandomSplitter<String, FImage> splits =
                    new GroupedRandomSplitter<String, FImage>(data, 15, 0, 15); // 15 training, 15 testing


            DenseSIFT denseSIFT = new DenseSIFT(5, 7);
            PyramidDenseSIFT<FImage> pyramidDenseSIFT = new PyramidDenseSIFT<FImage>(denseSIFT, 6f, 7);

            GroupedDataset<String, ListDataset<FImage>, FImage> sample =
                    GroupedUniformRandomisedSampler.sample(splits.getTrainingDataset(), 15);

            HardAssigner<byte[], float[], IntFloatPair> assigner = trainQuantiser(sample, pyramidDenseSIFT);

            FeatureExtractor<DoubleFV, FImage> extractor = new PHOWExtractor(pyramidDenseSIFT, assigner);

            //
            // Now we’re ready to construct and train a classifier
            //
            trainer = new LiblinearAnnotator<FImage, String>(
                    extractor, LiblinearAnnotator.Mode.MULTICLASS, SolverType.L2R_L2LOSS_SVC, 1.0, 0.00001);

            Date start = new Date();
            System.out.println("Classifier training: start");
            trainer.train(splits.getTrainingDataset());
            System.out.println("Classifier training: end");
            Date end = new Date();
            long durationSec = (end.getTime() - start.getTime()) / 1000;
            System.out.println("Classifier training duration: " + durationSec + " seconds");

            final GroupedDataset<String, ListDataset<FImage>, FImage> testDataSet = splits.getTestDataset();

            ClassificationEvaluator<CMResult<String>, String, FImage> eval =
                    new ClassificationEvaluator<CMResult<String>, String, FImage>(
                            trainer, testDataSet, new CMAnalyser<FImage, String>(CMAnalyser.Strategy.SINGLE));

            start = new Date();
            System.out.println("Classifier evaluation: start");
            Map<FImage, ClassificationResult<String>> guesses = eval.evaluate();
            System.out.println("Classifier evaluation - tp: end");
            end = new Date();
            durationSec = (end.getTime() - start.getTime()) / 1000;
            System.out.println("Classifier evaluation duration: " + durationSec + " seconds");

            CMResult<String> result = eval.analyse(guesses);
            System.out.println("Result - tp: " + result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * This method extracts the first 10000 dense SIFT features from the images in the dataset, and then clusters them
     * into 300 separate classes. The method then returns a HardAssigner which can be used to assign SIFT features to
     * identifiers
     *
     * @param pyramidDenseSIFT
     * @return
     */
    static HardAssigner<byte[], float[], IntFloatPair> trainQuantiser(
            Dataset<FImage> sample,
//            VFSGroupDataset<FImage> trainingImages,
            PyramidDenseSIFT<FImage> pyramidDenseSIFT)
    {
        System.out.println("trainQuantiser: start");
        Date start = new Date();
        List<LocalFeatureList<ByteDSIFTKeypoint>> allKeys = new ArrayList<LocalFeatureList<ByteDSIFTKeypoint>>();

        int i = 0;

        int total = sample.numInstances();
//        for (FImage image: sample) {
//            ListDataset<FImage> images = trainingImages.get(key);
//            total = images.size();
//            break;
//        }
        for (FImage rec : sample) {
            i++;
            System.out.println(String.format("Analysing image %d out of %d", i, total));
            FImage img = rec.getImage();

            pyramidDenseSIFT.analyseImage(img);
            allKeys.add(pyramidDenseSIFT.getByteKeypoints(0.005f));
        }
        final int numberOfDenseSiftFeaturesToExtract = 10000;
        final int numberOfClassesInCluster = 300;
        if (allKeys.size() > numberOfDenseSiftFeaturesToExtract)
            allKeys = allKeys.subList(0, numberOfDenseSiftFeaturesToExtract);

        ByteKMeans km = ByteKMeans.createKDTreeEnsemble(numberOfClassesInCluster);
        DataSource<byte[]> dataSource = new LocalFeatureListDataSource<ByteDSIFTKeypoint, byte[]>(allKeys);
        System.out.println(String.format(
                "Clustering %d image features into %d classes...",
                numberOfDenseSiftFeaturesToExtract, numberOfClassesInCluster));
        ByteCentroidsResult result = km.cluster(dataSource);
        Date end = new Date();
        System.out.println("trainQuantiser: end");
        System.out.println("trainQuantiser duration: " + (end.getTime() - start.getTime())/1000 + " seconds");
        return result.defaultHardAssigner();
    }

    static class PHOWExtractor implements FeatureExtractor<DoubleFV, FImage> {
        PyramidDenseSIFT<FImage> pdsift;
        HardAssigner<byte[], float[], IntFloatPair> assigner;

        public PHOWExtractor(PyramidDenseSIFT<FImage> pdsift, HardAssigner<byte[], float[], IntFloatPair> assigner)
        {
            this.pdsift = pdsift;
            this.assigner = assigner;
        }

        public DoubleFV extractFeature(FImage object) {
            FImage image = object.getImage();
            pdsift.analyseImage(image);

            BagOfVisualWords<byte[]> bovw = new BagOfVisualWords<byte[]>(assigner);

            BlockSpatialAggregator<byte[], SparseIntFV> spatial = new BlockSpatialAggregator<byte[], SparseIntFV>(
                    bovw, 2, 2);

            return spatial.aggregate(pdsift.getByteKeypoints(0.015f), image.getBounds()).normaliseFV();
        }
    }
}

Код работает и выдает следующий вывод:

trainQuantiser: start
Analysing image 1 out of 15
Analysing image 2 out of 15
Analysing image 3 out of 15
Analysing image 4 out of 15
Analysing image 5 out of 15
Analysing image 6 out of 15
Analysing image 7 out of 15
Analysing image 8 out of 15
Analysing image 9 out of 15
Analysing image 10 out of 15
Analysing image 11 out of 15
Analysing image 12 out of 15
Analysing image 13 out of 15
Analysing image 14 out of 15
Analysing image 15 out of 15
Clustering 10000 image features into 300 classes...
trainQuantiser: end
trainQuantiser duration: 243 seconds
Classifier training: start
iter  1 act 6.283e-01 pre 6.283e-01 delta 1.096e+00 f 1.500e+01 |g| 1.146e+00 CG   1
iter  2 act 2.779e-05 pre 2.779e-05 delta 1.096e+00 f 1.437e+01 |g| 7.555e-03 CG   1
iter  3 act 2.175e-09 pre 2.175e-09 delta 1.096e+00 f 1.437e+01 |g| 6.702e-05 CG   1
iter  4 act 6.626e-13 pre 6.598e-13 delta 1.096e+00 f 1.437e+01 |g| 1.164e-06 CG   1
Classifier training: end
Classifier training duration: 28 seconds
Classifier evaluation: start
Classifier evaluation - tp: end
Classifier evaluation duration: 57 seconds
Result - tp:   Accuracy: 1.000
Error Rate: 0.000

Я не уверен, как мне идти отсюда.Что я действительно хочу, так это не оценить точность классификатора - как это сделано в главе 12, - а использовать классификатор для определения погоды, в которой у нового изображения есть форма, которая мне интересна. Я не нашел никакой документации или примера, которыйпоказывает, как это сделать.Любая помощь будет очень признательна.

Я не нашел какой-либо существенной документации, кроме учебника.Может ли кто-нибудь указать мне, где это?Между тем я просто догадываюсь.Я не могу использовать testDataset, потому что должно быть разделение между обучением классификатора и его использованием.Поэтому я хотел бы обучить классификатора один раз (это занимает много времени - много минут) и сохранить результат (скажем, сериализовать объект тренера выше на диск и десериализовать его при будущих вызовах).Когда я добавляю код для этого и пытаюсь использовать testDataset для нового изображения, я получаю исключение нулевого указателя.Исключение не связано с десериализацией объекта, потому что я получаю исключение также, когда объект еще не находится на диске.Новый код:

package com.mycompany.video.analytics;

import de.bwaldvogel.liblinear.SolverType;
import org.apache.commons.vfs2.FileSystemException;
import org.openimaj.data.DataSource;
import org.openimaj.data.dataset.Dataset;
import org.openimaj.data.dataset.GroupedDataset;
import org.openimaj.data.dataset.ListDataset;
import org.openimaj.data.dataset.VFSGroupDataset;
import org.openimaj.experiment.dataset.sampling.GroupSampler;
import org.openimaj.experiment.dataset.sampling.GroupedUniformRandomisedSampler;
import org.openimaj.experiment.dataset.split.GroupedRandomSplitter;
import org.openimaj.experiment.evaluation.classification.ClassificationEvaluator;
import org.openimaj.experiment.evaluation.classification.ClassificationResult;
import org.openimaj.experiment.evaluation.classification.analysers.confusionmatrix.CMAnalyser;
import org.openimaj.experiment.evaluation.classification.analysers.confusionmatrix.CMResult;
import org.openimaj.feature.DoubleFV;
import org.openimaj.feature.FeatureExtractor;
import org.openimaj.feature.SparseIntFV;
import org.openimaj.feature.local.data.LocalFeatureListDataSource;
import org.openimaj.feature.local.list.LocalFeatureList;
import org.openimaj.image.FImage;
import org.openimaj.image.ImageUtilities;
import org.openimaj.image.feature.dense.gradient.dsift.ByteDSIFTKeypoint;
import org.openimaj.image.feature.dense.gradient.dsift.DenseSIFT;
import org.openimaj.image.feature.dense.gradient.dsift.PyramidDenseSIFT;
import org.openimaj.image.feature.local.aggregate.BagOfVisualWords;
import org.openimaj.image.feature.local.aggregate.BlockSpatialAggregator;
import org.openimaj.io.IOUtils;
import org.openimaj.ml.annotation.ScoredAnnotation;
import org.openimaj.ml.annotation.linear.LiblinearAnnotator;
import org.openimaj.ml.clustering.ByteCentroidsResult;
import org.openimaj.ml.clustering.assignment.HardAssigner;
import org.openimaj.ml.clustering.kmeans.ByteKMeans;
import org.openimaj.util.pair.IntFloatPair;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


public class Chapter12Generic {
    private static String IMAGES_PATH = "C:\\Development\\Video Analytics\\tpImages";
    private static String TEST_IMAGES_PATH = "C:\\Development\\Video Analytics\\testImages";
    private static String TRAINER_DATA_FILE_PATH = "C:\\Development\\Video Analytics\\out\\trainer.dat";

    public static void main(String[] args) throws Exception {

        LiblinearAnnotator<FImage, String> trainer = null;
        File inputDataFile = new File(TRAINER_DATA_FILE_PATH);
        if (inputDataFile.isFile()) {
            trainer = IOUtils.readFromFile(inputDataFile);
        } else {
            VFSGroupDataset<FImage> allData = null;
            allData = new VFSGroupDataset<FImage>(
                    IMAGES_PATH,
                    ImageUtilities.FIMAGE_READER);

            GroupedDataset<String, ListDataset<FImage>, FImage> data =
                    GroupSampler.sample(allData, 1, false);

            GroupedRandomSplitter<String, FImage> splits =
                    new GroupedRandomSplitter<String, FImage>(data, 15, 0, 15); // 15 training, 15 testing


            DenseSIFT denseSIFT = new DenseSIFT(5, 7);
            PyramidDenseSIFT<FImage> pyramidDenseSIFT = new PyramidDenseSIFT<FImage>(denseSIFT, 6f, 7);

            GroupedDataset<String, ListDataset<FImage>, FImage> sample =
                    GroupedUniformRandomisedSampler.sample(splits.getTrainingDataset(), 15);

            HardAssigner<byte[], float[], IntFloatPair> assigner = trainQuantiser(sample, pyramidDenseSIFT);

            FeatureExtractor<DoubleFV, FImage> extractor = new PHOWExtractor(pyramidDenseSIFT, assigner);

            //
            // Now we’re ready to construct and train a classifier
            //
            trainer = new LiblinearAnnotator<FImage, String>(
                    extractor, LiblinearAnnotator.Mode.MULTICLASS, SolverType.L2R_L2LOSS_SVC, 1.0, 0.00001);

            Date start = new Date();
            System.out.println("Classifier training: start");
            trainer.train(splits.getTrainingDataset());
            IOUtils.writeToFile(trainer, inputDataFile);

            System.out.println("Classifier training: end");
            Date end = new Date();
            long durationSec = (end.getTime() - start.getTime()) / 1000;
            System.out.println("Classifier training duration: " + durationSec + " seconds");
        }

//        final GroupedDataset<String, ListDataset<FImage>, FImage> testDataSet = splits.getTestDataset();
        VFSGroupDataset<FImage> testDataSet = new VFSGroupDataset<FImage>(
                TEST_IMAGES_PATH,
                ImageUtilities.FIMAGE_READER);

        ClassificationEvaluator<CMResult<String>, String, FImage> eval =
                new ClassificationEvaluator<CMResult<String>, String, FImage>(
                        trainer, testDataSet, new CMAnalyser<FImage, String>(CMAnalyser.Strategy.SINGLE));

        Date start = new Date();
        System.out.println("Classifier evaluation: start");
        Map<FImage, ClassificationResult<String>> guesses = eval.evaluate();
        System.out.println("Classifier evaluation - tp: end");
        Date end = new Date();
        long durationSec = (end.getTime() - start.getTime()) / 1000;
        System.out.println("Classifier evaluation duration: " + durationSec + " seconds");

        CMResult<String> result = eval.analyse(guesses);
        System.out.println("Result - tp: " + result);      
    }

    /**
     * This method extracts the first 10000 dense SIFT features from the images in the dataset, and then clusters them
     * into 300 separate classes. The method then returns a HardAssigner which can be used to assign SIFT features to
     * identifiers
     *
     * @param pyramidDenseSIFT
     * @return
     */
    static HardAssigner<byte[], float[], IntFloatPair> trainQuantiser(
            Dataset<FImage> sample,
//            VFSGroupDataset<FImage> trainingImages,
            PyramidDenseSIFT<FImage> pyramidDenseSIFT)
    {
        System.out.println("trainQuantiser: start");
        Date start = new Date();
        List<LocalFeatureList<ByteDSIFTKeypoint>> allKeys = new ArrayList<LocalFeatureList<ByteDSIFTKeypoint>>();

        int i = 0;

        int total = sample.numInstances();
//        for (FImage image: sample) {
//            ListDataset<FImage> images = trainingImages.get(key);
//            total = images.size();
//            break;
//        }
        for (FImage rec : sample) {
            i++;
            System.out.println(String.format("Analysing image %d out of %d", i, total));
            FImage img = rec.getImage();

            pyramidDenseSIFT.analyseImage(img);
            allKeys.add(pyramidDenseSIFT.getByteKeypoints(0.005f));
        }
        final int numberOfDenseSiftFeaturesToExtract = 10000;
        final int numberOfClassesInCluster = 300;
        if (allKeys.size() > numberOfDenseSiftFeaturesToExtract)
            allKeys = allKeys.subList(0, numberOfDenseSiftFeaturesToExtract);

        ByteKMeans km = ByteKMeans.createKDTreeEnsemble(numberOfClassesInCluster);
        DataSource<byte[]> dataSource = new LocalFeatureListDataSource<ByteDSIFTKeypoint, byte[]>(allKeys);
        System.out.println(String.format(
                "Clustering %d image features into %d classes...",
                numberOfDenseSiftFeaturesToExtract, numberOfClassesInCluster));
        ByteCentroidsResult result = km.cluster(dataSource);
        Date end = new Date();
        System.out.println("trainQuantiser: end");
        System.out.println("trainQuantiser duration: " + (end.getTime() - start.getTime())/1000 + " seconds");
        return result.defaultHardAssigner();
    }

    static class PHOWExtractor implements FeatureExtractor<DoubleFV, FImage> {
        PyramidDenseSIFT<FImage> pdsift;
        HardAssigner<byte[], float[], IntFloatPair> assigner;

        public PHOWExtractor(PyramidDenseSIFT<FImage> pdsift, HardAssigner<byte[], float[], IntFloatPair> assigner)
        {
            this.pdsift = pdsift;
            this.assigner = assigner;
        }

        public DoubleFV extractFeature(FImage object) {
            FImage image = object.getImage();
            pdsift.analyseImage(image);

            BagOfVisualWords<byte[]> bovw = new BagOfVisualWords<byte[]>(assigner);

            BlockSpatialAggregator<byte[], SparseIntFV> spatial = new BlockSpatialAggregator<byte[], SparseIntFV>(
                    bovw, 2, 2);

            return spatial.aggregate(pdsift.getByteKeypoints(0.015f), image.getBounds()).normaliseFV();
        }
    }
}

Исключение:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:130)
Caused by: java.lang.NullPointerException
    at org.openimaj.experiment.evaluation.classification.analysers.confusionmatrix.CMAnalyser$Strategy$1.add(CMAnalyser.java:80)
    at org.openimaj.experiment.evaluation.classification.analysers.confusionmatrix.CMAnalyser.analyse(CMAnalyser.java:172)
    at org.openimaj.experiment.evaluation.classification.analysers.confusionmatrix.CMAnalyser.analyse(CMAnalyser.java:57)
    at org.openimaj.experiment.evaluation.classification.ClassificationEvaluator.analyse(ClassificationEvaluator.java:190)
    at com.mycompany.video.analytics.Chapter12Generic.main(Chapter12Generic.java:113)

Исключение происходит при вызове

CMResult<String> result = eval.analyse(guesses);

Есть идеи, как это исправить?

Добавление версии 3 кода на основе ответа @ jon.Проблема сейчас в том, что он классифицирует ложное изображение как истинное.

public class Chapter12Generic_v3 {

    // contains an accordion folder with images from caltech101
    private static String TRAINING_IMAGES_PATH = "C:\\Development\\Video Analytics\\images";

    // contains 1 airplane image from caltech101
    private static String TEST_IMAGE = "C:\\Development\\Video Analytics\\testImages\\falseImages\\image_0001.jpg";

    private static String TRAINER_DATA_FILE_PATH = "C:\\Development\\Video Analytics\\out\\trainer.dat";

    public static void main(String[] args) throws Exception {
        LiblinearAnnotator<FImage, String> trainer = null;
        File inputDataFile = new File(TRAINER_DATA_FILE_PATH);
        if (inputDataFile.isFile()) {
            trainer = IOUtils.readFromFile(inputDataFile);
        } else {
            VFSGroupDataset<FImage> allData = null;
            allData = new VFSGroupDataset<FImage>(
                    TRAINING_IMAGES_PATH,
                    ImageUtilities.FIMAGE_READER);

            GroupedDataset<String, ListDataset<FImage>, FImage> data =
                    GroupSampler.sample(allData, 1, false);

            GroupedRandomSplitter<String, FImage> splits =
                    new GroupedRandomSplitter<String, FImage>(data, 15, 0, 15); // 15 training, 15 testing


            DenseSIFT denseSIFT = new DenseSIFT(5, 7);
            PyramidDenseSIFT<FImage> pyramidDenseSIFT = new PyramidDenseSIFT<FImage>(denseSIFT, 6f, 7);

            GroupedDataset<String, ListDataset<FImage>, FImage> sample =
                    GroupedUniformRandomisedSampler.sample(splits.getTrainingDataset(), 15);

            HardAssigner<byte[], float[], IntFloatPair> assigner = trainQuantiser(sample, pyramidDenseSIFT);

            FeatureExtractor<DoubleFV, FImage> extractor = new PHOWExtractor(pyramidDenseSIFT, assigner);

            //
            // Now we’re ready to construct and train a classifier
            //
            trainer = new LiblinearAnnotator<FImage, String>(
                    extractor, LiblinearAnnotator.Mode.MULTICLASS, SolverType.L2R_L2LOSS_SVC, 1.0, 0.00001);

            Date start = new Date();
            System.out.println("Classifier training: start");
            trainer.train(splits.getTrainingDataset());
            IOUtils.writeToFile(trainer, new File(TRAINER_DATA_FILE_PATH));
            System.out.println("Classifier training: end");
            Date end = new Date();
            long durationSec = (end.getTime() - start.getTime()) / 1000;
            System.out.println("Classifier training duration: " + durationSec + " seconds");
        }


        FImage query = ImageUtilities.readF(new File(TEST_IMAGE));

        final List<ScoredAnnotation<String>> scoredAnnotations = trainer.annotate(query);
        final ClassificationResult<String> classificationResult = trainer.classify(query);
        System.out.println("scoredAnnotations: " + scoredAnnotations);
        System.out.println("classificationResult: " + classificationResult);
    }

    /**
     * This method extracts the first 10000 dense SIFT features from the images in the dataset, and then clusters them
     * into 300 separate classes. The method then returns a HardAssigner which can be used to assign SIFT features to
     * identifiers
     *
     * @param pyramidDenseSIFT
     * @return
     */
    static HardAssigner<byte[], float[], IntFloatPair> trainQuantiser(
            Dataset<FImage> sample,
            PyramidDenseSIFT<FImage> pyramidDenseSIFT)
    {
        System.out.println("trainQuantiser: start");
        Date start = new Date();
        List<LocalFeatureList<ByteDSIFTKeypoint>> allKeys = new ArrayList<LocalFeatureList<ByteDSIFTKeypoint>>();

        int i = 0;

        int total = sample.numInstances();
        for (FImage rec : sample) {
            i++;
            System.out.println(String.format("Analysing image %d out of %d", i, total));
            FImage img = rec.getImage();

            pyramidDenseSIFT.analyseImage(img);
            allKeys.add(pyramidDenseSIFT.getByteKeypoints(0.005f));
        }
        final int numberOfDenseSiftFeaturesToExtract = 10000;
        final int numberOfClassesInCluster = 300;
        if (allKeys.size() > numberOfDenseSiftFeaturesToExtract)
            allKeys = allKeys.subList(0, numberOfDenseSiftFeaturesToExtract);

        ByteKMeans km = ByteKMeans.createKDTreeEnsemble(numberOfClassesInCluster);
        DataSource<byte[]> dataSource = new LocalFeatureListDataSource<ByteDSIFTKeypoint, byte[]>(allKeys);
        System.out.println(String.format(
                "Clustering %d image features into %d classes...",
                numberOfDenseSiftFeaturesToExtract, numberOfClassesInCluster));
        ByteCentroidsResult result = km.cluster(dataSource);
        Date end = new Date();
        System.out.println("trainQuantiser: end");
        System.out.println("trainQuantiser duration: " + (end.getTime() - start.getTime())/1000 + " seconds");
        return result.defaultHardAssigner();
    }

    static class PHOWExtractor implements FeatureExtractor<DoubleFV, FImage> {
        PyramidDenseSIFT<FImage> pdsift;
        HardAssigner<byte[], float[], IntFloatPair> assigner;

        public PHOWExtractor(PyramidDenseSIFT<FImage> pdsift, HardAssigner<byte[], float[], IntFloatPair> assigner)
        {
            this.pdsift = pdsift;
            this.assigner = assigner;
        }

        public DoubleFV extractFeature(FImage object) {
            FImage image = object.getImage();
            pdsift.analyseImage(image);

            BagOfVisualWords<byte[]> bovw = new BagOfVisualWords<byte[]>(assigner);

            BlockSpatialAggregator<byte[], SparseIntFV> spatial = new BlockSpatialAggregator<byte[], SparseIntFV>(
                    bovw, 2, 2);

            return spatial.aggregate(pdsift.getByteKeypoints(0.015f), image.getBounds()).normaliseFV();
        }
    }
}

1 Ответ

0 голосов
/ 17 октября 2018

Если вы просто хотите классифицировать вещи по модели, которую вы обучили, тогда игнорируйте все вещи ClassificationEvaluator - это просто для точности вычислений и т. Д.

Взгляните на http://openimaj.org/apidocs/org/openimaj/ml/annotation/linear/LiblinearAnnotator.html(тип вашего trainer объекта).Поскольку ваш экземпляр trainer набирается на FImage и String, его методы annotate() и classify() будут принимать FImage, который вы предоставляете в качестве входных данных, и предоставлять результат классификации в качестве выходных данных (в несколько разных формах;вам придется решить, что лучше всего соответствует вашим потребностям).

...