Beispiel #1
0
  public void testParallelMAP() throws IOException {

    int maxHits = 1000;
    IndexReader reader = IndexReader.open(FSDirectory.open(new File(indexPath)));
    ParallelImageSearcher searcher;
    searcher = new ParallelImageSearcher(maxHits, CEDD.class, DocumentBuilder.FIELD_NAME_CEDD);
    Pattern p = Pattern.compile("([0-9]+).jpg");
    double map = 0;
    double errorRate = 0d;
    for (int i = 0; i < sampleQueries.length; i++) {
      int id = sampleQueries[i];
      //            System.out.println("id = " + id + ": " + "("+i+")");
      String file = testExtensive + "/" + id + ".jpg";
      String[] files = {
        id + ".jpg", (id + 1) + ".jpg", (id + 2) + ".jpg", (id + 3) + ".jpg", (id + 4) + ".jpg"
      };
      ImageSearchHits[] hits = searcher.search(findDocs(reader, files), reader);
      for (int k = 0; k < hits.length; k++) {
        int currentID = id + k;
        ImageSearchHits h = hits[k];
        int goodOnes = 0;
        double avgPrecision = 0;
        for (int j = 0; j < h.length(); j++) {
          Document d = h.doc(j);
          String hitsId = d.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
          Matcher matcher = p.matcher(hitsId);
          if (matcher.find()) hitsId = matcher.group(1);
          else fail("Did not get the number ...");
          int testID = Integer.parseInt(hitsId);
          if ((testID != currentID)
              && ((int) Math.floor(id / 100) == (int) Math.floor(testID / 100))) {
            goodOnes++;
            // Only if there is a change in recall
            avgPrecision += (double) goodOnes / (double) (j + 1);
            //                    System.out.print("x");
          } else {
            if (j == 1) { // error rate
              errorRate++;
            }
          }
          //                System.out.print(" (" + testID + ") ");
        }
        assertTrue(goodOnes > 0);
        avgPrecision = avgPrecision / goodOnes;
        assertTrue(avgPrecision > 0);
        map += avgPrecision;
        //                System.out.println(" " + avgPrecision + " (" + map / (i + 1) + ")");
      }
    }
    assertTrue(sampleQueries.length > 0);
    map = map / sampleQueries.length;
    errorRate = errorRate / sampleQueries.length;
    System.out.println("map = " + map);
    System.out.println("errorRate = " + errorRate);
  }
Beispiel #2
0
 public void testRerankFilters() throws IOException {
   int queryDocID = (int) (Math.random() * 10000);
   IndexReader reader = DirectoryReader.open(FSDirectory.open(new File("index-large")));
   // select one feature for the large index:
   int featureIndex = 4;
   int count = 0;
   long ms = System.currentTimeMillis();
   ImageSearchHits hits = searchers[featureIndex].search(reader.document(queryDocID), reader);
   RerankFilter rerank = new RerankFilter(featureClasses[0], DocumentBuilder.FIELD_NAME_CEDD);
   LsaFilter lsa = new LsaFilter(featureClasses[0], DocumentBuilder.FIELD_NAME_CEDD);
   FileUtils.saveImageResultsToPng(
       "GeneralTest_rerank_0_old",
       hits,
       reader.document(queryDocID).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
   hits = rerank.filter(hits, reader.document(queryDocID));
   FileUtils.saveImageResultsToPng(
       "GeneralTest_rerank_1_new",
       hits,
       reader.document(queryDocID).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
   hits = lsa.filter(hits, reader.document(queryDocID));
   FileUtils.saveImageResultsToPng(
       "GeneralTest_rerank_2_lsa",
       hits,
       reader.document(queryDocID).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
 }
Beispiel #3
0
  public void tttestMAPLocalFeatureHistogram() throws IOException {
    int maxSearches = 200;
    int maxHits = 100;
    IndexReader reader = IndexReader.open(FSDirectory.open(new File(indexPath)));
    IndexSearcher is = new IndexSearcher(reader);
    ImageSearcher searcher;
    //        searcher = new SiftLocalFeatureHistogramImageSearcher(maxHits);
    searcher = ImageSearcherFactory.createColorHistogramImageSearcher(maxHits);
    //        searcher = ImageSearcherFactory.createCEDDImageSearcher(maxHits);
    //        searcher = ImageSearcherFactory.createFCTHImageSearcher(maxHits);
    Pattern p = Pattern.compile("\\\\\\d+\\.jpg");
    double map = 0;
    for (int i = 0; i < sampleQueries.length; i++) {
      int id = sampleQueries[i];
      System.out.print("id = " + id + ": ");
      String file = testExtensive + "/" + id + ".jpg";

      ImageSearchHits hits = searcher.search(findDoc(reader, id + ".jpg"), reader);
      int goodOnes = 0;
      double avgPrecision = 0;
      for (int j = 0; j < hits.length(); j++) {
        Document d = hits.doc(j);
        String hitsId = d.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
        Matcher matcher = p.matcher(hitsId);
        if (matcher.find()) hitsId = hitsId.substring(matcher.start() + 1, hitsId.lastIndexOf("."));
        else fail("Did not get the number ...");
        int testID = Integer.parseInt(hitsId);
        //                System.out.print(". " + hitsId + "/"  +
        // d.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]+ " ");
        if ((int) Math.floor(id / 100) == (int) Math.floor(testID / 100)) {
          goodOnes++;
          System.out.print("x");
        } else {
          System.out.print("o");
        }
        //                System.out.print(" (" + testID + ") ");
        avgPrecision += (double) goodOnes / (double) (j + 1);
      }
      avgPrecision = avgPrecision / hits.length();
      map += avgPrecision;
      System.out.println(" " + avgPrecision + " (" + map / (i + 1) + ")");
    }
    map = map / sampleQueries.length;
    System.out.println("map = " + map);
  }
Beispiel #4
0
  public void testSearchIndexLarge() throws IOException {

    for (int i = 0; i < 10; i++) {
      int queryDocID = (int) (Math.random() * 800);
      //            queryDocID = 877 * (i + 1);
      IndexReader reader = DirectoryReader.open(FSDirectory.open(new File("index-large")));
      // select one feature for the large index:
      int featureIndex = 13;
      int count = 0;
      long ms = System.currentTimeMillis();
      ImageSearchHits hits = searchers[featureIndex].search(reader.document(queryDocID), reader);
      for (int j = 0; j < hits.length(); j++) {
        String fileName = hits.doc(j).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
        System.out.println(hits.score(j) + ": \t" + fileName);
      }
      //        FileUtils.saveImageResultsToHtml("GeneralTest_testSearchIndexLarge_", hits,
      // reader.document(10).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
      FileUtils.saveImageResultsToPng(
          "GeneralTest_testSearchIndexLarge_" + i + "_",
          hits,
          reader.document(queryDocID).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
    }
  }
Beispiel #5
0
 public void tttestGetSampleQueries() {
   for (int i = 0; i < 200; i++) {
     System.out.print((int) (Math.random() * 1000) + ", ");
   }
 }
Beispiel #6
0
 public void computeMAP(ImageSearcher searcher, String prefix, IndexReader reader)
     throws IOException {
   Pattern p = Pattern.compile("([0-9]+).jpg");
   double map = 0;
   double errorRate = 0d;
   double precision10 = 0d;
   double[] pr10cat = new double[10];
   double[] pr10cnt = new double[10];
   for (int i = 0; i < pr10cat.length; i++) {
     pr10cat[i] = 0d;
     pr10cnt[i] = 0d;
   }
   long sum = 0, ms = 0;
   for (int i = 0; i < sampleQueries.length; i++) {
     int id = sampleQueries[i];
     String file = testExtensive + "/" + id + ".jpg";
     ms = System.currentTimeMillis();
     ImageSearchHits hits = searcher.search(findDoc(reader, id + ".jpg"), reader);
     sum += (System.currentTimeMillis() - ms);
     int goodOnes = 0;
     double avgPrecision = 0d;
     double precision10temp = 0d;
     int countResults = 0;
     for (int j = 0; j < hits.length(); j++) {
       Document d = hits.doc(j);
       String hitsId = d.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
       Matcher matcher = p.matcher(hitsId);
       if (matcher.find()) hitsId = matcher.group(1);
       else fail("Did not get the number ...");
       int testID = Integer.parseInt(hitsId);
       if (testID != id) countResults++;
       if ((testID != id) && ((int) Math.floor(id / 100) == (int) Math.floor(testID / 100))) {
         goodOnes++;
         // Only if there is a change in recall
         avgPrecision += (double) goodOnes / (double) countResults;
         //                    System.out.print("x");
         if (j <= 10) {
           precision10temp += 1d;
         }
       } else {
         if (j == 1) { // error rate
           errorRate++;
         }
       }
     } // end for loop iterating results.
     //            if (avgPrecision<=0) {
     //                System.out.println("avgPrecision = " + avgPrecision);
     //                System.out.println("goodOnes = " + goodOnes);
     //            }
     assertTrue("Check if average precision is > 0", avgPrecision > 0);
     assertTrue("Check if goodOnes is > 0", goodOnes > 0);
     avgPrecision = avgPrecision / goodOnes;
     precision10 += precision10temp / 10d;
     // precision @ 10 for each category ...
     pr10cat[(int) Math.floor(id / 100)] += precision10temp / 10d;
     pr10cnt[(int) Math.floor(id / 100)] += 1d;
     map += avgPrecision;
   }
   map = map / sampleQueries.length;
   errorRate = errorRate / sampleQueries.length;
   precision10 = precision10 / sampleQueries.length;
   System.out.print(prefix + "\t");
   System.out.print(String.format("%.5f", map) + '\t');
   System.out.print(String.format("%.5f", precision10) + '\t');
   System.out.print(String.format("%.5f", errorRate) + '\t');
   // precision@10 per category
   for (int i = 0; i < pr10cat.length; i++) {
     double v = 0;
     if (pr10cnt[i] > 0) v = pr10cat[i] / pr10cnt[i];
     //            System.out.print(i + ": ");
     System.out.printf("%.5f\t", v);
   }
   System.out.printf("%2.3f\t", (double) sum / (double) sampleQueries.length);
   System.out.println();
 }