Example usage for org.apache.lucene.index IndexReader maxDoc

List of usage examples for org.apache.lucene.index IndexReader maxDoc

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexReader maxDoc.

Prototype

public abstract int maxDoc();

Source Link

Document

Returns one greater than the largest possible document number.

Usage

From source file:net.conquiris.lucene.search.NegatingFilter.java

License:Apache License

@Override
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
    final int n = reader.maxDoc();
    final FixedBitSet bits = new FixedBitSet(reader.maxDoc());
    final DocIdSet set = filter.getDocIdSet(reader);
    if (set == null || set == DocIdSet.EMPTY_DOCIDSET) {
        bits.set(0, n);/*from ww  w  .  j  ava2s  .  c  om*/
    } else {
        DocIdSetIterator i = set.iterator();
        if (i == null) {
            bits.set(0, n);
        } else {
            bits.or(i);
            bits.flip(0, n);
        }
    }
    return bits;
}

From source file:net.semanticmetadata.lire.benchmarking.TestSimple.java

License:Open Source License

private void doSearch(ImageSearcher searcher, String prefix, IndexReader reader) throws IOException {
    // Needed for check whether the document is deleted.
    Bits liveDocs = MultiFields.getLiveDocs(reader);
    String fileName, fullFileName;
    Document queryDoc;//  w  ww  . jav  a 2s . c  om
    ImageSearchHits hits;
    for (int i = 0; i < reader.maxDoc(); i++) {
        if (reader.hasDeletions() && !liveDocs.get(i))
            continue; // if it is deleted, just ignore it.
        fullFileName = reader.document(i).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
        fileName = getIDfromFileName(fullFileName);
        if (allQueries.contains(fileName)) {
            // ok, we've got a query here for a document ...
            queryDoc = reader.document(i);
            hits = searcher.search(queryDoc, reader);
            FileUtils.browseUri(FileUtils.saveImageResultsToHtml(prefix + "-" + fileName, hits, fullFileName));
        }
    }
    for (int i = 0; i < outsideQueries.size(); i++) {
        fullFileName = outsideQueries.get(i).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
        fileName = getIDfromFileName(fullFileName);
        if (allQueries.contains(fileName)) {
            // ok, we've got a query here for a document ...
            queryDoc = outsideQueries.get(i);
            hits = searcher.search(queryDoc, reader);
            FileUtils.browseUri(FileUtils.saveImageResultsToHtml(prefix + "-" + fileName, hits, fullFileName));
        }
    }
}

From source file:net.semanticmetadata.lire.benchmarking.TestUCID.java

License:Open Source License

private void computeMAP(ImageSearcher searcher, String prefix, IndexReader reader) throws IOException {
    double queryCount = 0d;
    double errorRate = 0;
    double map = 0;
    double p10 = 0;
    int errorCount = 0;
    // Needed for check whether the document is deleted.
    Bits liveDocs = MultiFields.getLiveDocs(reader);
    PrintWriter fw;/* ww w  .  j  av a 2  s  . c  o  m*/
    if (searcher.toString().contains("ImageSearcherUsingWSs")) {
        (new File("eval/" + prefix.replace(' ', '_') + "/" + clusters + "/")).mkdirs();
        fw = new PrintWriter(new File("eval/" + prefix.replace(' ', '_') + "/" + clusters + "/"
                + prefix.replace(' ', '_') + "-" + db + clusters
                + searcher.toString().split("\\s+")[searcher.toString().split("\\s+").length - 1] + ".txt"));
    } else
        fw = new PrintWriter(new File("eval/" + prefix.replace(' ', '_') + "-" + db + clusters + ".txt"));
    //            fw = new PrintWriter(new File("eval/" + prefix.replace(' ', '_') + "-" + db + "Global.txt")); //forGlobal
    Hashtable<Integer, String> evalText = new Hashtable<Integer, String>(260);
    for (int i = 0; i < reader.maxDoc(); i++) {
        if (reader.hasDeletions() && !liveDocs.get(i))
            continue; // if it is deleted, just ignore it.
        String fileName = getIDfromFileName(
                reader.document(i).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
        if (queries.keySet().contains(fileName)) {
            String tmpEval = "";
            queryCount += 1d;
            // ok, we've got a query here for a document ...
            Document queryDoc = reader.document(i);
            ImageSearchHits hits = searcher.search(queryDoc, reader);
            double rank = 0;
            double avgPrecision = 0;
            double found = 0;
            double tmpP10 = 0;
            Locale.setDefault(Locale.US);
            for (int y = 0; y < hits.length(); y++) {
                String hitFile = getIDfromFileName(
                        hits.doc(y).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
                // TODO: Sort by query ID!
                tmpEval += String.format(Locale.US, "%d 1 %s %d %.2f test\n", query2id.get(fileName),
                        hitFile.substring(0, hitFile.lastIndexOf('.')), (int) rank + 1, hits.score(y));
                // if (!hitFile.equals(fileName)) {
                rank++;
                //                    if ((queries.get(fileName).contains(hitFile) || hitFile.equals(fileName))&&(!fileName.equals(hitFile))) { // it's a hit.
                if (queries.get(fileName).contains(hitFile) || hitFile.equals(fileName)) { // it's a hit.
                    found++;
                    // TODO: Compute error rate, etc. here.
                    avgPrecision += found / rank;// * (1d/queries.get(fileName).size());
                    //                        avgPrecision += found / (rank-1);// * (1d/queries.get(fileName).size());
                    //                            if (rank<=60) System.out.print('X');
                    if (rank <= 10)
                        tmpP10++;
                } else { // nothing has been found.
                    if (rank == 1)
                        errorRate += 1d;
                    //                            if (rank<=60) System.out.print('-');
                }
            }
            // }
            //                System.out.println();
            avgPrecision /= (double) (1d + queries.get(fileName).size());
            //                avgPrecision /= (double) (queries.get(fileName).size());

            if (!(found - queries.get(fileName).size() == 1)) {
                // some of the results have not been found. We have to deal with it ...
                errorCount++;
            }

            // assertTrue(found - queries.get(fileName).size() == 0);
            map += avgPrecision;
            p10 += tmpP10;
            evalText.put(query2id.get(fileName), tmpEval);
        }
    }
    for (int i = 0; i < query2id.size(); i++) {
        fw.write(evalText.get(i + 1));
    }
    fw.close();
    errorRate = errorRate / queryCount;
    map = map / queryCount;
    p10 = p10 / (queryCount * 10d);
    //        System.out.print(prefix);
    String s;
    if (searcher.toString().contains("ImageSearcherUsingWSs"))
        s = String.format("%s\t%.4f\t%.4f\t%.4f\t(%s)", prefix, map, p10, errorRate,
                searcher.toString().split("\\s+")[searcher.toString().split("\\s+").length - 1]);
    else
        s = String.format("%s\t%.4f\t%.4f\t%.4f", prefix, map, p10, errorRate);
    if (errorCount > 0) {
        // some of the results have not been found. We have to deal with it ...
        //System.err.println("Did not find result ;(  (" + errorCount + ")");
        s += "\t~~\tDid not find result ;(\t(" + errorCount + ")";
    }
    System.out.println(s);
}

From source file:net.semanticmetadata.lire.benchmarking.TestUCID.java

License:Open Source License

private void testSearchSpeed(ArrayList<String> images, final Class featureClass) throws IOException {
    parallelIndexer = new ParallelIndexer(8, indexPath, testExtensive, true) {
        @Override/*www. j  a va2  s.co  m*/
        public void addBuilders(ChainedDocumentBuilder builder) {
            builder.addBuilder(new GenericDocumentBuilder(featureClass, "feature"));
        }
    };
    parallelIndexer.run();
    IndexReader reader = DirectoryReader
            .open(new RAMDirectory(FSDirectory.open(new File(indexPath)), IOContext.READONCE));
    Bits liveDocs = MultiFields.getLiveDocs(reader);
    double queryCount = 0d;
    ImageSearcher searcher = new GenericFastImageSearcher(100, featureClass, "feature");
    long ms = System.currentTimeMillis();
    for (int i = 0; i < reader.maxDoc(); i++) {
        if (reader.hasDeletions() && !liveDocs.get(i))
            continue; // if it is deleted, just ignore it.
        String fileName = getIDfromFileName(
                reader.document(i).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
        if (queries.keySet().contains(fileName)) {
            queryCount += 1d;
            // ok, we've got a query here for a document ...
            Document queryDoc = reader.document(i);
            ImageSearchHits hits = searcher.search(queryDoc, reader);
        }
    }
    ms = System.currentTimeMillis() - ms;
    System.out.printf("%s \t %3.1f \n",
            featureClass.getName().substring(featureClass.getName().lastIndexOf('.') + 1),
            (double) ms / queryCount);
}

From source file:net.semanticmetadata.lire.benchmarking.TestUniversal.java

License:Open Source License

public void testMAP() throws IOException {
    // INDEXING ...
    ParallelIndexer parallelIndexer = new ParallelIndexer(DocumentBuilder.NUM_OF_THREADS, indexPath,
            testExtensive, numOfClusters, numOfDocsForVocabulary, aggregator);
    //        ParallelIndexer parallelIndexer = new ParallelIndexer(DocumentBuilder.NUM_OF_THREADS, indexPath, testExtensive, false);
    //        ParallelIndexer parallelIndexer = new ParallelIndexer(DocumentBuilder.NUM_OF_THREADS, indexPath, testExtensive);

    //GLOBALS/*from  w  w w  .ja  v a2 s  .c o m*/
    //        parallelIndexer.addExtractor(ACCID.class);
    parallelIndexer.addExtractor(CEDD.class);
    //        parallelIndexer.addExtractor(FCTH.class);
    //        parallelIndexer.addExtractor(JCD.class);
    //        parallelIndexer.addExtractor(AutoColorCorrelogram.class);
    //        parallelIndexer.addExtractor(BinaryPatternsPyramid.class);
    //        parallelIndexer.addExtractor(FuzzyColorHistogram.class);
    //        parallelIndexer.addExtractor(FuzzyOpponentHistogram.class);
    //        parallelIndexer.addExtractor(Gabor.class);
    //        parallelIndexer.addExtractor(JpegCoefficientHistogram.class);
    //        parallelIndexer.addExtractor(LocalBinaryPatterns.class);
    //        parallelIndexer.addExtractor(LuminanceLayout.class);
    //        parallelIndexer.addExtractor(OpponentHistogram.class);
    //        parallelIndexer.addExtractor(PHOG.class);
    //        parallelIndexer.addExtractor(RotationInvariantLocalBinaryPatterns.class);
    //        parallelIndexer.addExtractor(SimpleColorHistogram.class);
    //        parallelIndexer.addExtractor(Tamura.class);
    //        parallelIndexer.addExtractor(JointHistogram.class);
    //        parallelIndexer.addExtractor(LocalBinaryPatternsAndOpponent.class);
    //        parallelIndexer.addExtractor(RankAndOpponent.class);
    //        parallelIndexer.addExtractor(ColorLayout.class);
    //        parallelIndexer.addExtractor(EdgeHistogram.class);
    //        parallelIndexer.addExtractor(ScalableColor.class);
    //        parallelIndexer.addExtractor(SPCEDD.class);
    //        parallelIndexer.addExtractor(SPJCD.class);
    //        parallelIndexer.addExtractor(SPFCTH.class);
    //        parallelIndexer.addExtractor(SPACC.class);
    //        parallelIndexer.addExtractor(SPLBP.class);

    //SIMPLE
    //        parallelIndexer.addExtractor(CEDD.class, SimpleExtractor.KeypointDetector.CVSURF);
    //        parallelIndexer.addExtractor(FCTH.class, SimpleExtractor.KeypointDetector.CVSURF);
    parallelIndexer.addExtractor(JCD.class, SimpleExtractor.KeypointDetector.CVSURF);
    //        parallelIndexer.addExtractor(AutoColorCorrelogram.class, SimpleExtractor.KeypointDetector.CVSURF);
    //        parallelIndexer.addExtractor(OpponentHistogram.class, SimpleExtractor.KeypointDetector.CVSURF);
    //        parallelIndexer.addExtractor(ColorLayout.class, SimpleExtractor.KeypointDetector.CVSURF);
    //        parallelIndexer.addExtractor(EdgeHistogram.class, SimpleExtractor.KeypointDetector.CVSURF);
    //        parallelIndexer.addExtractor(ScalableColor.class, SimpleExtractor.KeypointDetector.CVSURF);

    //LOCAL
    parallelIndexer.addExtractor(CvSurfExtractor.class);
    //        parallelIndexer.addExtractor(CvSiftExtractor.class);
    //        parallelIndexer.addExtractor(SurfExtractor.class);
    //        parallelIndexer.addExtractor(SiftExtractor.class);
    //        parallelIndexer.addExtractor(SelfSimilaritiesExtractor.class);

    parallelIndexer.run();

    // SEARCHING
    IndexReader reader = DirectoryReader
            .open(new RAMDirectory(FSDirectory.open(Paths.get(indexPath)), IOContext.READONCE));
    //        IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexPath)));
    System.out.println("Documents in the reader: " + reader.maxDoc());
    //
    System.out.println("Feature\tMAP\tp@10\tER");
    //
    long start = System.currentTimeMillis();
    //
    //        computeMAP(new GenericFastImageSearcher(1000, ACCID.class, true, reader), "ACCID", reader);
    computeMAP(new GenericFastImageSearcher(1000, CEDD.class, true, reader), "CEDD", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, FCTH.class, true, reader), "FCTH", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, JCD.class, true, reader), "JCD", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, AutoColorCorrelogram.class, true, reader), "AutoColorCorrelogram", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, BinaryPatternsPyramid.class, true, reader), "BinaryPatternsPyramid", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, FuzzyColorHistogram.class, true, reader), "FuzzyColorHistogram", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, FuzzyOpponentHistogram.class, true, reader), "FuzzyOpponentHistogram", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, Gabor.class, true, reader), "Gabor", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, JpegCoefficientHistogram.class, true, reader), "JpegCoefficientHistogram", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, LocalBinaryPatterns.class, true, reader), "LocalBinaryPatterns", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, LuminanceLayout.class, true, reader), "LuminanceLayout", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, OpponentHistogram.class, true, reader), "OpponentHistogram", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, PHOG.class, true, reader), "PHOG", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, RotationInvariantLocalBinaryPatterns.class, true, reader), "RotationInvariantLocalBinaryPatterns", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, SimpleColorHistogram.class, true, reader), "SimpleColorHistogram", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, Tamura.class, true, reader), "Tamura", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, JointHistogram.class, true, reader), "JointHistogram", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, LocalBinaryPatternsAndOpponent.class, true, reader), "LocalBinaryPatternsAndOpponent", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, RankAndOpponent.class, true, reader), "RankAndOpponent", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, ColorLayout.class, true, reader), "ColorLayout", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, EdgeHistogram.class, true, reader), "EdgeHistogram", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, ScalableColor.class, true, reader), "ScalableColor", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, SPCEDD.class, true, reader), "SPCEDD", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, SPJCD.class, true, reader), "SPJCD", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, SPFCTH.class, true, reader), "SPFCTH", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, SPACC.class, true, reader), "SPACC", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, SPLBP.class, true, reader), "SPLBP", reader);

    //BOVW
    for (int i = 0; i < numOfClusters.length; i++) {
        //            computeMAP(new GenericFastImageSearcher(1000, CEDD.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], true, reader, indexPath + ".config"), "Simple BOVW CEDD CVSURF", reader, numOfClusters[i]);
        //            computeMAP(new GenericFastImageSearcher(1000, FCTH.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], true, reader, indexPath + ".config"), "Simple BOVW FCTH CVSURF", reader, numOfClusters[i]);
        computeMAP(
                new GenericFastImageSearcher(1000, JCD.class, SimpleExtractor.KeypointDetector.CVSURF,
                        new BOVW(), numOfClusters[i], true, reader, indexPath + ".config"),
                "Simple BOVW JCD CVSURF", reader, numOfClusters[i]);
        //            computeMAP(new GenericFastImageSearcher(1000, AutoColorCorrelogram.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], true, reader, indexPath + ".config"), "Simple BOVW AutoColorCorrelogram CVSURF", reader, numOfClusters[i]);
        //            computeMAP(new GenericFastImageSearcher(1000, OpponentHistogram.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], true, reader, indexPath + ".config"), "Simple BOVW OpponentHistogram CVSURF", reader, numOfClusters[i]);
        //            computeMAP(new GenericFastImageSearcher(1000, ColorLayout.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], true, reader, indexPath + ".config"), "Simple BOVW ColorLayout CVSURF", reader, numOfClusters[i]);
        //            computeMAP(new GenericFastImageSearcher(1000, EdgeHistogram.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], true, reader, indexPath + ".config"), "Simple BOVW EdgeHistogram CVSURF", reader, numOfClusters[i]);
        //            computeMAP(new GenericFastImageSearcher(1000, ScalableColor.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], true, reader, indexPath + ".config"), "Simple BOVW ScalableColor CVSURF", reader, numOfClusters[i]);

        //            performWSs(CEDD.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], reader, indexPath + ".config", "Simple BOVW CEDD CVSURF");
        //            performWSs(FCTH.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], reader, indexPath + ".config", "Simple BOVW FCTH CVSURF");
        performWSs(JCD.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], reader,
                indexPath + ".config", "Simple BOVW JCD CVSURF");
        //            performWSs(AutoColorCorrelogram.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], reader, indexPath + ".config", "Simple BOVW AutoColorCorrelogram CVSURF");
        //            performWSs(OpponentHistogram.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], reader, indexPath + ".config", "Simple BOVW OpponentHistogram CVSURF");
        //            performWSs(ColorLayout.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], reader, indexPath + ".config", "Simple BOVW ColorLayout CVSURF");
        //            performWSs(EdgeHistogram.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], reader, indexPath + ".config", "Simple BOVW EdgeHistogram CVSURF");
        //            performWSs(ScalableColor.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], reader, indexPath + ".config", "Simple BOVW ScalableColor CVSURF");

        computeMAP(new GenericFastImageSearcher(1000, CvSurfExtractor.class, new BOVW(), numOfClusters[i], true,
                reader, indexPath + ".config"), "CVSURF BOVW", reader, numOfClusters[i]);
        //            computeMAP(new GenericFastImageSearcher(1000, CvSiftExtractor.class, new BOVW(), numOfClusters[i], true, reader, indexPath + ".config"), "CVSIFT BOVW", reader, numOfClusters[i]);
        //            computeMAP(new GenericFastImageSearcher(1000, SurfExtractor.class, new BOVW(), numOfClusters[i], true, reader, indexPath + ".config"), "SURF BOVW", reader, numOfClusters[i]);
        //            computeMAP(new GenericFastImageSearcher(1000, SiftExtractor.class, new BOVW(), numOfClusters[i], true, reader, indexPath + ".config"), "SIFT BOVW", reader, numOfClusters[i]);
        //            computeMAP(new GenericFastImageSearcher(1000, SelfSimilaritiesExtractor.class, new BOVW(), numOfClusters[i], true, reader, indexPath + ".config"), "SelfSimilarities BOVW", reader, numOfClusters[i]);

        performWSs(CvSurfExtractor.class, new BOVW(), numOfClusters[i], reader, indexPath + ".config",
                "CVSURF BOVW");
        //            performWSs(CvSiftExtractor.class, new BOVW(), numOfClusters[i], reader, indexPath + ".config", "CVSIFT BOVW");
        //            performWSs(SurfExtractor.class, new BOVW(), numOfClusters[i], reader, indexPath + ".config", "SURF BOVW");
        //            performWSs(SiftExtractor.class, new BOVW(), numOfClusters[i], reader, indexPath + ".config", "SIFT BOVW");
        //            performWSs(SelfSimilaritiesExtractor.class, new BOVW(), numOfClusters[i], reader, indexPath + ".config", "SelfSimilarities BOVW");
    }

    //VLAD
    //        for (int i = 0; i < numOfClusters.length; i++) {
    //            computeMAP(new GenericFastImageSearcher(1000, CEDD.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, reader, indexPath + ".config"), "Simple VLAD CEDD CVSURF", reader, numOfClusters[i]);
    //            computeMAP(new GenericFastImageSearcher(1000, FCTH.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, reader, indexPath + ".config"), "Simple VLAD FCTH CVSURF", reader, numOfClusters[i]);
    //            computeMAP(new GenericFastImageSearcher(1000, JCD.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, reader, indexPath + ".config"), "Simple VLAD JCD CVSURF", reader, numOfClusters[i]);
    //            computeMAP(new GenericFastImageSearcher(1000, AutoColorCorrelogram.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, reader, indexPath + ".config"), "Simple VLAD AutoColorCorrelogram CVSURF", reader, numOfClusters[i]);
    //            computeMAP(new GenericFastImageSearcher(1000, OpponentHistogram.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, reader, indexPath + ".config"), "Simple VLAD OpponentHistogram CVSURF", reader, numOfClusters[i]);
    //            computeMAP(new GenericFastImageSearcher(1000, ColorLayout.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, reader, indexPath + ".config"), "Simple VLAD ColorLayout CVSURF", reader, numOfClusters[i]);
    //            computeMAP(new GenericFastImageSearcher(1000, EdgeHistogram.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, reader, indexPath + ".config"), "Simple VLAD EdgeHistogram CVSURF", reader, numOfClusters[i]);
    //            computeMAP(new GenericFastImageSearcher(1000, ScalableColor.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, reader, indexPath + ".config"), "Simple VLAD ScalableColor CVSURF", reader, numOfClusters[i]);
    ////
    //            computeMAP(new GenericFastImageSearcher(1000, CvSurfExtractor.class, new VLAD(), numOfClusters[i], true, reader, indexPath + ".config"), "CVSURF VLAD", reader, numOfClusters[i]);
    //            computeMAP(new GenericFastImageSearcher(1000, CvSiftExtractor.class, new VLAD(), numOfClusters[i], true, reader, indexPath + ".config"), "CVSIFT VLAD", reader, numOfClusters[i]);
    //            computeMAP(new GenericFastImageSearcher(1000, SurfExtractor.class, new VLAD(), numOfClusters[i], true, reader, indexPath + ".config"), "SURF VLAD", reader, numOfClusters[i]);
    //            computeMAP(new GenericFastImageSearcher(1000, SiftExtractor.class, new VLAD(), numOfClusters[i], true, reader, indexPath + ".config"), "SIFT VLAD", reader, numOfClusters[i]);
    ////            computeMAP(new GenericFastImageSearcher(1000, SelfSimilaritiesExtractor.class, new VLAD(), numOfClusters[i], true, reader, indexPath + ".config"), "SelfSimilarities VLAD", reader, numOfClusters[i]);
    //        }

    double h = (System.currentTimeMillis() - start) / 3600000.0;
    double m = (h - Math.floor(h)) * 60.0;
    double s = (m - Math.floor(m)) * 60;
    System.out.printf("Total time of searching: %s.\n", String.format("%s%02d:%02d",
            (((int) h > 0) ? String.format("%02d:", (int) h) : ""), (int) m, (int) s));
}

From source file:net.semanticmetadata.lire.benchmarking.TestUniversal.java

License:Open Source License

private void computeMAP(ImageSearcher searcher, String prefix, IndexReader reader, int clusters)
        throws IOException {
    long start = System.currentTimeMillis();
    long timeOfSearch = 0, ms;

    double queryCount = 0d;
    double errorRate = 0;
    double map = 0;
    double p10 = 0;
    int errorCount = 0;
    // Needed for check whether the document is deleted.
    Bits liveDocs = MultiFields.getLiveDocs(reader);
    PrintWriter fw;/*www. j a  v a 2s .c o m*/
    if (searcher.toString().contains("ImageSearcherUsingWSs")) {
        (new File("eval/" + db + "/" + prefix.replace(' ', '_') + "/" + clusters + "/")).mkdirs();
        fw = new PrintWriter(new File("eval/" + db + "/" + prefix.replace(' ', '_') + "/" + clusters + "/"
                + prefix.replace(' ', '_') + "-" + db + clusters
                + searcher.toString().split("\\s+")[searcher.toString().split("\\s+").length - 1] + ".txt"));
    } else {
        //            (new File("eval/#WithMirFlickr/" + db + "/")).mkdirs();
        (new File("eval/" + db + "/")).mkdirs();
        if (clusters > 0)
            fw = new PrintWriter(
                    new File("eval/" + db + "/" + prefix.replace(' ', '_') + "-" + db + clusters + ".txt"));
        else
            //                fw = new PrintWriter(new File("eval/#WithMirFlickr/" + db + "/" + prefix.replace(' ', '_') + "-" + db + "Global.txt")); //forGlobal
            fw = new PrintWriter(
                    new File("eval/" + db + "/" + prefix.replace(' ', '_') + "-" + db + "Global.txt")); //forGlobal
    }
    Hashtable<Integer, String> evalText = new Hashtable<Integer, String>(260);
    for (int i = 0; i < reader.maxDoc(); i++) {
        if (reader.hasDeletions() && !liveDocs.get(i))
            continue; // if it is deleted, just ignore it.
        String fileName = getIDfromFileName(
                reader.document(i).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
        if (queries.keySet().contains(fileName)) {
            String tmpEval = "";
            queryCount += 1d;
            // ok, we've got a query here for a document ...
            Document queryDoc = reader.document(i);
            ms = System.currentTimeMillis();
            ImageSearchHits hits = searcher.search(queryDoc, reader);
            timeOfSearch += System.currentTimeMillis() - ms;
            double rank = 0;
            double avgPrecision = 0;
            double found = 0;
            double tmpP10 = 0;
            Locale.setDefault(Locale.US);
            for (int y = 0; y < hits.length(); y++) {
                //                    String hitFile = getIDfromFileName(hits.doc(y).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
                String hitFile = getIDfromFileName(reader.document(hits.documentID(y))
                        .getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
                //                    String hitFile = getIDfromFileName(hits.path(y));
                // TODO: Sort by query ID!
                tmpEval += String.format(Locale.US, "%d 1 %s %d %.2f test\n", query2id.get(fileName),
                        hitFile.substring(0, hitFile.lastIndexOf('.')), (int) rank + 1, hits.score(y));
                // if (!hitFile.equals(fileName)) {
                rank++;
                //                    if ((queries.get(fileName).contains(hitFile) || hitFile.equals(fileName))&&(!fileName.equals(hitFile))) { // it's a hit.
                if (queries.get(fileName).contains(hitFile) || hitFile.equals(fileName)) { // it's a hit.
                    found++;
                    // TODO: Compute error rate, etc. here.
                    avgPrecision += found / rank;// * (1d/queries.get(fileName).size());
                    //                        avgPrecision += found / (rank-1);// * (1d/queries.get(fileName).size());
                    //                            if (rank<=60) System.out.print('X');
                    if (rank <= 10)
                        tmpP10++;
                } else { // nothing has been found.
                    if (rank == 1)
                        errorRate += 1d;
                    //                            if (rank<=60) System.out.print('-');
                }
            }
            // }
            //                System.out.println();
            avgPrecision /= (double) (1d + queries.get(fileName).size());
            //                avgPrecision /= (double) (queries.get(fileName).size());

            if (!(found - queries.get(fileName).size() == 1)) {
                // some of the results have not been found. We have to deal with it ...
                errorCount++;
            }

            // assertTrue(found - queries.get(fileName).size() == 0);
            map += avgPrecision;
            p10 += tmpP10;
            evalText.put(query2id.get(fileName), tmpEval);
        }
    }

    for (int i = 0; i < query2id.size(); i++) {
        fw.write(evalText.get(i + 1));
    }

    fw.close();
    errorRate = errorRate / queryCount;
    map = map / queryCount;
    p10 = p10 / (queryCount * 10d);

    double h = (System.currentTimeMillis() - start) / 3600000.0;
    double m = (h - Math.floor(h)) * 60.0;
    double s = (m - Math.floor(m)) * 60;
    String str = String.format("%s%02d:%02d", (((int) h > 0) ? String.format("%02d:", (int) h) : ""), (int) m,
            (int) s) + " ~ ";

    if (searcher.toString().contains("ImageSearcherUsingWSs"))
        str += String.format("%s%s\t%.4f\t%.4f\t%.4f\t(%s)", prefix, ((clusters > 0) ? ("\t" + clusters) : ""),
                map, p10, errorRate,
                searcher.toString().split("\\s+")[searcher.toString().split("\\s+").length - 1]);
    else
        str += String.format("%s%s\t%.4f\t%.4f\t%.4f", prefix, ((clusters > 0) ? ("\t" + clusters) : ""), map,
                p10, errorRate);
    if (errorCount > 0) {
        // some of the results have not been found. We have to deal with it ...
        str += "\t~~\tDid not find result ;(\t(" + errorCount + ")";
    }
    h = timeOfSearch / 3600000.0;
    m = (h - Math.floor(h)) * 60.0;
    s = (m - Math.floor(m)) * 60;
    str += " ~ TimeOfsearch: " + String.format("%s%02d:%02d",
            (((int) h > 0) ? String.format("%02d:", (int) h) : ""), (int) m, (int) s);

    System.out.println(str);
}

From source file:net.semanticmetadata.lire.benchmarking.TestUniversal.java

License:Open Source License

private void testSearchSpeed(Class<? extends GlobalFeature> featureClass) throws IOException {
    ParallelIndexer parallelIndexer = new ParallelIndexer(DocumentBuilder.NUM_OF_THREADS, indexPath,
            testExtensive, true);//from w  ww.ja  v a2s  .co m
    parallelIndexer.addExtractor(featureClass);
    parallelIndexer.run();
    IndexReader reader = DirectoryReader
            .open(new RAMDirectory(FSDirectory.open(Paths.get(indexPath)), IOContext.READONCE));
    Bits liveDocs = MultiFields.getLiveDocs(reader);
    double queryCount = 0d;
    ImageSearcher searcher = new GenericFastImageSearcher(100, featureClass);
    long ms = System.currentTimeMillis();
    String fileName;
    Document queryDoc;
    ImageSearchHits hits;
    for (int i = 0; i < reader.maxDoc(); i++) {
        if (reader.hasDeletions() && !liveDocs.get(i))
            continue; // if it is deleted, just ignore it.
        fileName = getIDfromFileName(reader.document(i).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
        if (queries.keySet().contains(fileName)) {
            queryCount += 1d;
            // ok, we've got a query here for a document ...
            queryDoc = reader.document(i);
            hits = searcher.search(queryDoc, reader);
        }
    }
    ms = System.currentTimeMillis() - ms;
    System.out.printf("%s \t %3.1f \n",
            featureClass.getName().substring(featureClass.getName().lastIndexOf('.') + 1),
            (double) ms / queryCount);
}

From source file:net.semanticmetadata.lire.benchmarking.TestZuBuD.java

License:Open Source License

public void testMAP() throws IOException {
    // INDEXING ...
    ParallelIndexer parallelIndexer = new ParallelIndexer(DocumentBuilder.NUM_OF_THREADS, indexPath,
            testExtensive, numOfClusters, numOfDocsForVocabulary, aggregator);

    //GLOBALS//from   w  w w .  j av  a2  s . com
    //        parallelIndexer.addExtractor(ACCID.class);
    parallelIndexer.addExtractor(CEDD.class);
    //        parallelIndexer.addExtractor(FCTH.class);
    //        parallelIndexer.addExtractor(JCD.class);
    //        parallelIndexer.addExtractor(AutoColorCorrelogram.class);
    //        parallelIndexer.addExtractor(BinaryPatternsPyramid.class);
    //        parallelIndexer.addExtractor(FuzzyColorHistogram.class);
    //        parallelIndexer.addExtractor(FuzzyOpponentHistogram.class);
    //        parallelIndexer.addExtractor(Gabor.class);
    //        parallelIndexer.addExtractor(JpegCoefficientHistogram.class);
    //        parallelIndexer.addExtractor(LocalBinaryPatterns.class);
    //        parallelIndexer.addExtractor(LuminanceLayout.class);
    //        parallelIndexer.addExtractor(OpponentHistogram.class);
    //        parallelIndexer.addExtractor(PHOG.class);
    //        parallelIndexer.addExtractor(RotationInvariantLocalBinaryPatterns.class);
    //        parallelIndexer.addExtractor(SimpleColorHistogram.class);
    //        parallelIndexer.addExtractor(Tamura.class);
    //        parallelIndexer.addExtractor(JointHistogram.class);
    //        parallelIndexer.addExtractor(LocalBinaryPatternsAndOpponent.class);
    //        parallelIndexer.addExtractor(RankAndOpponent.class);
    //        parallelIndexer.addExtractor(ColorLayout.class);
    //        parallelIndexer.addExtractor(EdgeHistogram.class);
    //        parallelIndexer.addExtractor(ScalableColor.class);
    //        parallelIndexer.addExtractor(SPCEDD.class);
    //        parallelIndexer.addExtractor(SPJCD.class);
    //        parallelIndexer.addExtractor(SPFCTH.class);
    //        parallelIndexer.addExtractor(SPACC.class);
    //        parallelIndexer.addExtractor(SPLBP.class);

    //SIMPLE
    //        parallelIndexer.addExtractor(CEDD.class, SimpleExtractor.KeypointDetector.CVSURF);
    //        parallelIndexer.addExtractor(FCTH.class, SimpleExtractor.KeypointDetector.CVSURF);
    parallelIndexer.addExtractor(JCD.class, SimpleExtractor.KeypointDetector.CVSURF);
    //        parallelIndexer.addExtractor(AutoColorCorrelogram.class, SimpleExtractor.KeypointDetector.CVSURF);
    //        parallelIndexer.addExtractor(OpponentHistogram.class, SimpleExtractor.KeypointDetector.CVSURF);
    //        parallelIndexer.addExtractor(ColorLayout.class, SimpleExtractor.KeypointDetector.CVSURF);
    //        parallelIndexer.addExtractor(EdgeHistogram.class, SimpleExtractor.KeypointDetector.CVSURF);
    //        parallelIndexer.addExtractor(ScalableColor.class, SimpleExtractor.KeypointDetector.CVSURF);

    //LOCAL
    parallelIndexer.addExtractor(CvSurfExtractor.class);
    //        parallelIndexer.addExtractor(CvSiftExtractor.class);
    //        parallelIndexer.addExtractor(SurfExtractor.class);
    //        parallelIndexer.addExtractor(SiftExtractor.class);
    //        parallelIndexer.addExtractor(SelfSimilaritiesExtractor.class);

    parallelIndexer.run();

    ParallelIndexer parallelIndexerSeparate = new ParallelIndexer(DocumentBuilder.NUM_OF_THREADS,
            indexPathQueries, testExtensiveQueries, indexPath);
    parallelIndexerSeparate.run();

    // SEARCHING
    IndexReader readerIndex = DirectoryReader
            .open(new RAMDirectory(FSDirectory.open(Paths.get(indexPath)), IOContext.READONCE));
    System.out.println("Documents in the reader: " + readerIndex.maxDoc());

    IndexReader readerQueries = DirectoryReader
            .open(new RAMDirectory(FSDirectory.open(Paths.get(indexPathQueries)), IOContext.READONCE));
    System.out.println("Documents in the reader: " + readerQueries.maxDoc());

    System.out.println("Feature\tMAP\tp@10\tER");

    long start = System.currentTimeMillis();
    //
    //        computeMAP(new GenericFastImageSearcher(1000, ACCID.class, true, readerIndex), "ACCID", readerIndex, readerQueries);
    computeMAP(new GenericFastImageSearcher(1000, CEDD.class, true, readerIndex), "CEDD", readerIndex,
            readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, FCTH.class, true, readerIndex), "FCTH", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, JCD.class, true, readerIndex), "JCD", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, AutoColorCorrelogram.class, true, readerIndex), "AutoColorCorrelogram", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, BinaryPatternsPyramid.class, true, readerIndex), "BinaryPatternsPyramid", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, FuzzyColorHistogram.class, true, readerIndex), "FuzzyColorHistogram", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, FuzzyOpponentHistogram.class, true, readerIndex), "FuzzyOpponentHistogram", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, Gabor.class, true, readerIndex), "Gabor", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, JpegCoefficientHistogram.class, true, readerIndex), "JpegCoefficientHistogram", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, LocalBinaryPatterns.class, true, readerIndex), "LocalBinaryPatterns", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, LuminanceLayout.class, true, readerIndex), "LuminanceLayout", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, OpponentHistogram.class, true, readerIndex), "OpponentHistogram", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, PHOG.class, true, readerIndex), "PHOG", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, RotationInvariantLocalBinaryPatterns.class, true, readerIndex), "RotationInvariantLocalBinaryPatterns", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, SimpleColorHistogram.class, true, readerIndex), "SimpleColorHistogram", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, Tamura.class, true, readerIndex), "Tamura", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, JointHistogram.class, true, readerIndex), "JointHistogram", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, LocalBinaryPatternsAndOpponent.class, true, readerIndex), "LocalBinaryPatternsAndOpponent", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, RankAndOpponent.class, true, readerIndex), "RankAndOpponent", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, ColorLayout.class, true, readerIndex), "ColorLayout", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, EdgeHistogram.class, true, readerIndex), "EdgeHistogram", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, ScalableColor.class, true, readerIndex), "ScalableColor", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, SPCEDD.class, true, readerIndex), "SPCEDD", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, SPJCD.class, true, readerIndex), "SPJCD", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, SPFCTH.class, true, readerIndex), "SPFCTH", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, SPACC.class, true, readerIndex), "SPACC", readerIndex, readerQueries);
    //        computeMAP(new GenericFastImageSearcher(1000, SPLBP.class, true, readerIndex), "SPLBP", readerIndex, readerQueries);

    //BOVW
    for (int i = 0; i < numOfClusters.length; i++) {
        //            computeMAP(new GenericFastImageSearcher(1000, CEDD.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "Simple BOVW CEDD CVSURF", readerIndex, numOfClusters[i], readerQueries);
        //            computeMAP(new GenericFastImageSearcher(1000, FCTH.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "Simple BOVW FCTH CVSURF", readerIndex, numOfClusters[i], readerQueries);
        computeMAP(
                new GenericFastImageSearcher(1000, JCD.class, SimpleExtractor.KeypointDetector.CVSURF,
                        new BOVW(), numOfClusters[i], true, readerIndex, indexPath + ".config"),
                "Simple BOVW JCD CVSURF", readerIndex, numOfClusters[i], readerQueries);
        //            computeMAP(new GenericFastImageSearcher(1000, AutoColorCorrelogram.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "Simple BOVW AutoColorCorrelogram CVSURF", readerIndex, numOfClusters[i], readerQueries);
        //            computeMAP(new GenericFastImageSearcher(1000, OpponentHistogram.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "Simple BOVW OpponentHistogram CVSURF", readerIndex, numOfClusters[i], readerQueries);
        //            computeMAP(new GenericFastImageSearcher(1000, ColorLayout.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "Simple BOVW ColorLayout CVSURF", readerIndex, numOfClusters[i], readerQueries);
        //            computeMAP(new GenericFastImageSearcher(1000, EdgeHistogram.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "Simple BOVW EdgeHistogram CVSURF", readerIndex, numOfClusters[i], readerQueries);
        //            computeMAP(new GenericFastImageSearcher(1000, ScalableColor.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "Simple BOVW ScalableColor CVSURF", readerIndex, numOfClusters[i], readerQueries);

        //            performWSs(CEDD.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], readerIndex, indexPath + ".config", "Simple BOVW CEDD CVSURF", readerQueries);
        //            performWSs(FCTH.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], readerIndex, indexPath + ".config", "Simple BOVW FCTH CVSURF", readerQueries);
        performWSs(JCD.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i],
                readerIndex, indexPath + ".config", "Simple BOVW JCD CVSURF", readerQueries);
        //            performWSs(AutoColorCorrelogram.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], readerIndex, indexPath + ".config", "Simple BOVW AutoColorCorrelogram CVSURF", readerQueries);
        //            performWSs(OpponentHistogram.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], readerIndex, indexPath + ".config", "Simple BOVW OpponentHistogram CVSURF", readerQueries);
        //            performWSs(ColorLayout.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], readerIndex, indexPath + ".config", "Simple BOVW ColorLayout CVSURF", readerQueries);
        //            performWSs(EdgeHistogram.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], readerIndex, indexPath + ".config", "Simple BOVW EdgeHistogram CVSURF", readerQueries);
        //            performWSs(ScalableColor.class, SimpleExtractor.KeypointDetector.CVSURF, new BOVW(), numOfClusters[i], readerIndex, indexPath + ".config", "Simple BOVW ScalableColor CVSURF", readerQueries);

        computeMAP(
                new GenericFastImageSearcher(1000, CvSurfExtractor.class, new BOVW(), numOfClusters[i], true,
                        readerIndex, indexPath + ".config"),
                "CVSURF BOVW", readerIndex, numOfClusters[i], readerQueries);
        //            computeMAP(new GenericFastImageSearcher(1000, CvSiftExtractor.class, new BOVW(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "CVSIFT BOVW", readerIndex, numOfClusters[i], readerQueries);
        //            computeMAP(new GenericFastImageSearcher(1000, SurfExtractor.class, new BOVW(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "SURF BOVW", readerIndex, numOfClusters[i], readerQueries);
        //            computeMAP(new GenericFastImageSearcher(1000, SiftExtractor.class, new BOVW(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "SIFT BOVW", readerIndex, numOfClusters[i], readerQueries);
        //            computeMAP(new GenericFastImageSearcher(1000, SelfSimilaritiesExtractor.class, new BOVW(), numOfClusters[i], true, reader, indexPath + ".config"), "SelfSimilarities BOVW", reader, numOfClusters[i], readerQueries);

        performWSs(CvSurfExtractor.class, new BOVW(), numOfClusters[i], readerIndex, indexPath + ".config",
                "CVSURF BOVW", readerQueries);
        //            performWSs(CvSiftExtractor.class, new BOVW(), numOfClusters[i], readerIndex, indexPath + ".config", "CVSIFT BOVW", readerQueries);
        //            performWSs(SurfExtractor.class, new BOVW(), numOfClusters[i], readerIndex, indexPath + ".config", "SURF BOVW", readerQueries);
        //            performWSs(SiftExtractor.class, new BOVW(), numOfClusters[i], readerIndex, indexPath + ".config", "SIFT BOVW", readerQueries);
        //            performWSs(SelfSimilaritiesExtractor.class, new BOVW(), numOfClusters[i], reader, indexPath + ".config", "SelfSimilarities BOVW", readerQueries);
    }

    //VLAD
    //        for (int i = 0; i < numOfClusters.length; i++) {
    //            computeMAP(new GenericFastImageSearcher(1000, CEDD.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "Simple VLAD CEDD CVSURF", readerIndex, numOfClusters[i], readerQueries);
    //            computeMAP(new GenericFastImageSearcher(1000, FCTH.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "Simple VLAD FCTH CVSURF", readerIndex, numOfClusters[i], readerQueries);
    //            computeMAP(new GenericFastImageSearcher(1000, JCD.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "Simple VLAD JCD CVSURF", readerIndex, numOfClusters[i], readerQueries);
    //            computeMAP(new GenericFastImageSearcher(1000, AutoColorCorrelogram.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "Simple VLAD AutoColorCorrelogram CVSURF", readerIndex, numOfClusters[i], readerQueries);
    //            computeMAP(new GenericFastImageSearcher(1000, OpponentHistogram.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "Simple VLAD OpponentHistogram CVSURF", readerIndex, numOfClusters[i], readerQueries);
    //            computeMAP(new GenericFastImageSearcher(1000, ColorLayout.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "Simple VLAD ColorLayout CVSURF", readerIndex, numOfClusters[i], readerQueries);
    //            computeMAP(new GenericFastImageSearcher(1000, EdgeHistogram.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "Simple VLAD EdgeHistogram CVSURF", readerIndex, numOfClusters[i], readerQueries);
    //            computeMAP(new GenericFastImageSearcher(1000, ScalableColor.class, SimpleExtractor.KeypointDetector.CVSURF, new VLAD(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "Simple VLAD ScalableColor CVSURF", readerIndex, numOfClusters[i], readerQueries);
    ////
    //            computeMAP(new GenericFastImageSearcher(1000, CvSurfExtractor.class, new VLAD(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "CVSURF VLAD", readerIndex, numOfClusters[i], readerQueries);
    //            computeMAP(new GenericFastImageSearcher(1000, CvSiftExtractor.class, new VLAD(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "CVSIFT VLAD", readerIndex, numOfClusters[i], readerQueries);
    //            computeMAP(new GenericFastImageSearcher(1000, SurfExtractor.class, new VLAD(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "SURF VLAD", readerIndex, numOfClusters[i], readerQueries);
    //            computeMAP(new GenericFastImageSearcher(1000, SiftExtractor.class, new VLAD(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "SIFT VLAD", readerIndex, numOfClusters[i], readerQueries);
    ////            computeMAP(new GenericFastImageSearcher(1000, SelfSimilaritiesExtractor.class, new VLAD(), numOfClusters[i], true, readerIndex, indexPath + ".config"), "SelfSimilarities VLAD", readerIndex, numOfClusters[i], readerQueries);
    //        }

    double h = (System.currentTimeMillis() - start) / 3600000.0;
    double m = (h - Math.floor(h)) * 60.0;
    double s = (m - Math.floor(m)) * 60;
    System.out.printf("Total time of searching: %s.\n", String.format("%s%02d:%02d",
            (((int) h > 0) ? String.format("%02d:", (int) h) : ""), (int) m, (int) s));
}

From source file:net.semanticmetadata.lire.benchmarking.TestZuBuD.java

License:Open Source License

private void computeMAP(ImageSearcher searcher, String prefix, IndexReader reader, int clusters,
        IndexReader readerQueries) throws IOException {
    long start = System.currentTimeMillis();

    double queryCount = 0d;
    double errorRate = 0;
    double map = 0;
    double p10 = 0;
    int errorCount = 0;
    // Needed for check whether the document is deleted.
    Bits liveDocs = MultiFields.getLiveDocs(readerQueries);
    PrintWriter fw;/* w  ww.ja v a 2 s  .com*/
    if (searcher.toString().contains("ImageSearcherUsingWSs")) {
        (new File("eval/" + db + "/" + prefix.replace(' ', '_') + "/" + clusters + "/")).mkdirs();
        fw = new PrintWriter(new File("eval/" + db + "/" + prefix.replace(' ', '_') + "/" + clusters + "/"
                + prefix.replace(' ', '_') + "-" + db + clusters
                + searcher.toString().split("\\s+")[searcher.toString().split("\\s+").length - 1] + ".txt"));
    } else {
        //            (new File("eval/#WithMirFlickr/" + db + "/")).mkdirs();
        (new File("eval/" + db + "/")).mkdirs();
        if (clusters > 0)
            fw = new PrintWriter(
                    new File("eval/" + db + "/" + prefix.replace(' ', '_') + "-" + db + clusters + ".txt"));
        else
            //                fw = new PrintWriter(new File("eval/#WithMirFlickr/" + db + "/" + prefix.replace(' ', '_') + "-" + db + "Global.txt")); //forGlobal
            fw = new PrintWriter(
                    new File("eval/" + db + "/" + prefix.replace(' ', '_') + "-" + db + "Global.txt")); //forGlobal
    }
    Hashtable<Integer, String> evalText = new Hashtable<Integer, String>(260);
    for (int i = 0; i < readerQueries.maxDoc(); i++) {
        if (readerQueries.hasDeletions() && !liveDocs.get(i))
            continue; // if it is deleted, just ignore it.
        String fileName = getIDfromFileName(
                readerQueries.document(i).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
        if (queries.keySet().contains(fileName)) {
            String tmpEval = "";
            queryCount += 1d;
            // ok, we've got a query here for a document ...
            Document queryDoc = readerQueries.document(i);
            ImageSearchHits hits = searcher.search(queryDoc, reader);
            double rank = 0;
            double avgPrecision = 0;
            double found = 0;
            double tmpP10 = 0;
            Locale.setDefault(Locale.US);
            for (int y = 0; y < hits.length(); y++) {
                String hitFile = getIDfromFileName(reader.document(hits.documentID(y))
                        .getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
                // TODO: Sort by query ID!
                tmpEval += String.format(Locale.US, "%d 1 %s %d %.2f test\n", query2id.get(fileName),
                        hitFile.substring(0, hitFile.lastIndexOf('.')), (int) rank + 1, hits.score(y));
                // if (!hitFile.equals(fileName)) {
                rank++;
                //                    if ((queries.get(fileName).contains(hitFile) || hitFile.equals(fileName))&&(!fileName.equals(hitFile))) { // it's a hit.
                if (queries.get(fileName).contains(hitFile) || hitFile.equals(fileName)) { // it's a hit.
                    found++;
                    // TODO: Compute error rate, etc. here.
                    avgPrecision += found / rank;// * (1d/queries.get(fileName).size());
                    //                        avgPrecision += found / (rank-1);// * (1d/queries.get(fileName).size());
                    //                            if (rank<=60) System.out.print('X');
                    if (rank <= 10)
                        tmpP10++;
                } else { // nothing has been found.
                    if (rank == 1)
                        errorRate += 1d;
                    //                            if (rank<=60) System.out.print('-');
                }
            }
            // }
            //                System.out.println();
            //                avgPrecision /= (double) (1d + queries.get(fileName).size()); // TODO: check!!
            avgPrecision /= (double) (queries.get(fileName).size());

            if (!(found - queries.get(fileName).size() == 0)) {
                // some of the results have not been found. We have to deal with it ...
                errorCount++;
            }

            // assertTrue(found - queries.get(fileName).size() == 0);
            map += avgPrecision;
            p10 += tmpP10;
            evalText.put(query2id.get(fileName), tmpEval);
        }
    }

    for (int i = 0; i < query2id.size(); i++) {
        fw.write(evalText.get(i + 1));
    }

    fw.close();
    errorRate = errorRate / queryCount;
    map = map / queryCount;
    p10 = p10 / (queryCount * 10d);

    double h = (System.currentTimeMillis() - start) / 3600000.0;
    double m = (h - Math.floor(h)) * 60.0;
    double s = (m - Math.floor(m)) * 60;
    String str = String.format("%s%02d:%02d", (((int) h > 0) ? String.format("%02d:", (int) h) : ""), (int) m,
            (int) s) + " ~ ";

    if (searcher.toString().contains("ImageSearcherUsingWSs"))
        str += String.format("%s%s\t%.4f\t%.4f\t%.4f\t(%s)", prefix, ((clusters > 0) ? ("\t" + clusters) : ""),
                map, p10, errorRate,
                searcher.toString().split("\\s+")[searcher.toString().split("\\s+").length - 1]);
    else
        str += String.format("%s%s\t%.4f\t%.4f\t%.4f", prefix, ((clusters > 0) ? ("\t" + clusters) : ""), map,
                p10, errorRate);
    if (errorCount > 0) {
        // some of the results have not been found. We have to deal with it ...
        str += "\t~~\tDid not find result ;(\t(" + errorCount + ")";
    }
    System.out.println(str);
}

From source file:net.semanticmetadata.lire.benchmarking.UCIDBenchmark.java

License:Open Source License

private void computeMAP(ImageSearcher searcher, String prefix, IndexReader reader) throws IOException {
    double queryCount = 0d;
    double errorRate = 0;
    double map = 0;
    double p10 = 0;
    // Needed for check whether the document is deleted.
    Bits liveDocs = MultiFields.getLiveDocs(reader);
    PrintWriter fw = new PrintWriter(new File("eval/" + prefix.replace(' ', '_') + "-eval.txt"));
    Hashtable<Integer, String> evalText = new Hashtable<Integer, String>(260);
    for (int i = 0; i < reader.maxDoc(); i++) {
        if (reader.hasDeletions() && !liveDocs.get(i))
            continue; // if it is deleted, just ignore it.
        String fileName = getIDfromFileName(
                reader.document(i).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
        if (queries.keySet().contains(fileName)) {
            String tmpEval = "";
            queryCount += 1d;//from   w  w  w.  ja  va 2 s. c  o  m
            // ok, we've got a query here for a document ...
            Document queryDoc = reader.document(i);
            ImageSearchHits hits = searcher.search(queryDoc, reader);
            double rank = 0;
            double avgPrecision = 0;
            double found = 0;
            double tmpP10 = 0;
            Locale.setDefault(Locale.US);
            for (int y = 0; y < hits.length(); y++) {
                String hitFile = getIDfromFileName(
                        hits.doc(y).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
                // TODO: Sort by query ID!
                tmpEval += String.format(Locale.US, "%d 1 %s %d %.2f test\n", query2id.get(fileName),
                        hitFile.substring(0, hitFile.lastIndexOf('.')), (int) rank + 1, 100 - hits.score(y));
                // if (!hitFile.equals(fileName)) {
                rank++;
                if (queries.get(fileName).contains(hitFile) || hitFile.equals(fileName)) { // it's a hit.
                    found++;
                    // TODO: Compute error rate, etc. here.
                    avgPrecision += found / rank;// * (1d/queries.get(fileName).size());
                    //                            if (rank<=60) System.out.print('X');
                    if (rank <= 10)
                        tmpP10++;
                } else { // nothing has been found.
                    if (rank == 1)
                        errorRate += 1d;
                    //                            if (rank<=60) System.out.print('-');
                }
            }
            // }
            //                System.out.println();
            if (found - queries.get(fileName).size() == 1)
                avgPrecision /= (double) (1d + queries.get(fileName).size());
            else {
                // some of the results have not been found. We have to deal with it ...
                System.err.println("Did not find result ;(");
            }

            // assertTrue(found - queries.get(fileName).size() == 0);
            map += avgPrecision;
            p10 += tmpP10;
            evalText.put(query2id.get(fileName), tmpEval);
        }
    }
    for (int i = 0; i < query2id.size(); i++) {
        fw.write(evalText.get(i + 1));
    }
    fw.close();
    errorRate = errorRate / queryCount;
    map = map / queryCount;
    p10 = p10 / (queryCount * 10d);
    System.out.print(prefix);
    System.out.format("\t%.5f\t%.5f\t%.5f\n", map, p10, errorRate);

}