Example #1
0
 public void computeErrorRate(ImageSearcher searcher, String prefix)
     throws IOException, InstantiationException, IllegalAccessException {
   //        int maxHits = 10;
   IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(testIndex)));
   for (Iterator<String> testIterator = testcases.keySet().iterator(); testIterator.hasNext(); ) {
     queryImage = testIterator.next();
     Document query;
     if (cutImages) {
       BufferedImage bimg =
           ImageUtils.cropImage(ImageIO.read(new FileInputStream(queryImage)), 0, 0, 200, 69);
       query = builder.createDocument(new FileInputStream(queryImage), queryImage);
     } else query = builder.createDocument(new FileInputStream(queryImage), queryImage);
     ImageSearchHits hits = searcher.search(query, reader);
     // hits = rerank(hits, query, ColorLayout.class, DocumentBuilder.FIELD_NAME_COLORLAYOUT);
     for (int i = 0; i < hits.length(); i++) {
       if (hits.doc(i)
           .get("descriptorImageIdentifier")
           .toLowerCase()
           .endsWith(testcases.get(queryImage))) {
         System.out.println(
             queryImage.substring(queryImage.lastIndexOf('\\') + 1)
                 + "-"
                 + prefix
                 + " -> Found at rank "
                 + i
                 + " ("
                 + hits.length()
                 + ")");
       }
     }
     // saveToHtml(queryImage.substring(queryImage.lastIndexOf('\\') + 1) + "-" + prefix, hits,
     // queryImage);
   }
 }
Example #2
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);
  }
Example #3
0
 private ImageSearchHits rerank(
     ImageSearchHits hits, Document query, Class descriptorClass, String fieldName)
     throws IllegalAccessException, InstantiationException {
   ArrayList<SimpleResult> results = new ArrayList<SimpleResult>(hits.length());
   LireFeature qf = getFeature(descriptorClass, query.getValues(fieldName)[0]);
   float maxDistance = 0f;
   for (int i = 0; i < hits.length(); i++) {
     LireFeature lf = getFeature(descriptorClass, hits.doc(i).getValues(fieldName)[0]);
     float distance = lf.getDistance(qf);
     SimpleResult sr = new SimpleResult(distance, hits.doc(i), i);
     results.add(sr);
     maxDistance = Math.max(maxDistance, distance);
   }
   Collections.sort(results);
   return new SimpleImageSearchHits(results, maxDistance);
 }
Example #4
0
  public void testSearch() throws IOException {
    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexPath)));
    int numDocs = reader.numDocs();
    System.out.println("numDocs = " + numDocs);
    ImageSearcher searcher = ImageSearcherFactory.createScalableColorImageSearcher(50);
    FileInputStream imageStream = new FileInputStream(testFilesPath + testFiles[0]);
    BufferedImage bimg = ImageIO.read(imageStream);
    ImageSearchHits hits = null;
    long time = System.currentTimeMillis();
    for (int i = 0; i < numsearches; i++) {
      hits = searcher.search(bimg, reader);
    }
    time = System.currentTimeMillis() - time;
    System.out.println(
        ((float) time / (float) numsearches)
            + " ms per search with image, averaged on "
            + numsearches);
    for (int i = 0; i < 5; i++) {
      System.out.println(
          hits.score(i)
              + ": "
              + hits.doc(i).getField(DocumentBuilder.FIELD_NAME_IDENTIFIER).stringValue());
    }
    Document document = hits.doc(0);
    time = System.currentTimeMillis();
    for (int i = 0; i < numsearches; i++) {
      hits = searcher.search(document, reader);
    }
    time = System.currentTimeMillis() - time;
    System.out.println(
        ((float) time / (float) numsearches)
            + " ms per search with document, averaged on "
            + numsearches);
    for (int i = 0; i < 5; i++) {
      System.out.println(
          hits.score(i)
              + ": "
              + hits.doc(i).getField(DocumentBuilder.FIELD_NAME_IDENTIFIER).stringValue());
    }

    document = getDocumentBuilder().createDocument(bimg, testFilesPath + testFiles[0]);
    time = System.currentTimeMillis();
    for (int i = 0; i < numsearches; i++) {
      hits = searcher.search(document, reader);
    }
    time = System.currentTimeMillis() - time;
    System.out.println(
        ((float) time / (float) numsearches)
            + " ms per search with document, averaged on "
            + numsearches);
    for (int i = 0; i < 5; i++) {
      System.out.println(
          hits.score(i)
              + ": "
              + hits.doc(i).getField(DocumentBuilder.FIELD_NAME_IDENTIFIER).stringValue());
    }
  }
Example #5
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);
  }
Example #6
0
 private void saveToHtml(String prefix, ImageSearchHits hits, String queryImage)
     throws IOException {
   BufferedWriter bw = new BufferedWriter(new FileWriter("results - " + prefix + ".html"));
   bw.write(
       "<html>\n"
           + "<head><title>Search Results</title></head>\n"
           + "<body bgcolor=\"#FFFFFF\">\n");
   bw.write("<h3>query</h3>\n");
   bw.write(
       "<a href=\"file://" + queryImage + "\"><img src=\"file://" + queryImage + "\"></a><p>\n");
   bw.write("<h3>results</h3>\n");
   for (int i = 0; i < hits.length(); i++) {
     bw.write(
         "<a href=\"file://"
             + hits.doc(i).get("descriptorImageIdentifier")
             + "\"><img src=\"file://"
             + hits.doc(i).get("descriptorImageIdentifier")
             + "\"></a><p>\n");
   }
   bw.write("</body>\n" + "</html>");
   bw.close();
 }
Example #7
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]);
    }
  }
Example #8
0
  public void testCreateAndSearchSmallIndex() throws IOException {
    for (int i = 0, buildersLength = builders.length; i < buildersLength; i++) {
      DocumentBuilder b = builders[i];
      // create an index with a specific builder:
      IndexWriter iw = LuceneUtils.createIndexWriter(indexPath + "-small", true);
      for (String identifier : testFiles) {
        Document doc =
            b.createDocument(new FileInputStream(testFilesPath + identifier), identifier);
        doc.add(new StoredField("video_file", "surgery1.mp4"));
        doc.add(new StoredField("timestamp", "25"));
        iw.addDocument(doc);
      }
      iw.close();

      ImageSearcher s = searchers[i];
      IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexPath + "-small")));
      for (int k = 0; k < reader.maxDoc(); k++) {
        Document query = reader.document(k);
        ImageSearchHits hits = s.search(query, reader);
        for (int y = 0; y < hits.length(); y++) {
          Document result = hits.doc(y);
          if (y == 0) {
            // check if the first result is the query:
            assertEquals(
                result.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0].equals(
                    query.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]),
                true);
            System.out.println(result.getValues("video_file")[0]);
          } else {
            // check if they are ordered by distance:
            assertEquals(hits.score(y) < hits.score(y - 1), true);
          }
        }
      }
    }
  }
Example #9
0
  public void testClassify() throws IOException {
    boolean weightByRank = true;
    String[] classes = {
      "2012", "beach", "food", "london", "music", "nature", "people", "sky", "travel", "wedding"
    };
    int k = 50;
    // CONFIG
    String fieldName = DocumentBuilder.FIELD_NAME_COLORLAYOUT;
    LireFeature feature = new ColorLayout();
    String indexPath = "E:\\acmgc-cl-idx";
    System.out.println(
        "Tests for feature "
            + fieldName
            + " with k="
            + k
            + " - weighting by rank sum: "
            + weightByRank);
    System.out.println("========================================");
    HashMap<String, Integer> tag2count = new HashMap<String, Integer>(k);
    HashMap<String, Double> tag2weight = new HashMap<String, Double>(k);
    int c = 9; // used for just one class ...
    //        for (int c = 0; c < 10; c++) {
    String classIdentifier = classes[c];
    String listFiles = "D:\\DataSets\\Yahoo-GC\\test\\" + classIdentifier + ".txt";

    // INIT
    int[] confusion = new int[10];
    Arrays.fill(confusion, 0);
    HashMap<String, Integer> class2id = new HashMap<String, Integer>(10);
    for (int i = 0; i < classes.length; i++) class2id.put(classes[i], i);

    BufferedReader br = new BufferedReader(new FileReader(listFiles));
    String line;
    IndexReader ir = DirectoryReader.open(MMapDirectory.open(new File(indexPath)));
    // in-memory linear search
    //            ImageSearcher bis = new GenericFastImageSearcher(k, feature.getClass(), fieldName,
    // true, ir);
    // hashing based searcher
    BitSamplingImageSearcher bis =
        new BitSamplingImageSearcher(k, fieldName, fieldName + "_hash", feature, 1000);
    ImageSearchHits hits;
    int count = 0, countCorrect = 0;
    long ms = System.currentTimeMillis();
    while ((line = br.readLine()) != null) {
      try {
        tag2count.clear();
        tag2weight.clear();
        hits = bis.search(ImageIO.read(new File(line)), ir);
        // set tag weights and counts.
        for (int l = 0; l < k; l++) {
          String tag = getTag(hits.doc(l));
          if (tag2count.get(tag) == null) tag2count.put(tag, 1);
          else tag2count.put(tag, tag2count.get(tag) + 1);
          if (weightByRank) {
            if (tag2weight.get(tag) == null) tag2weight.put(tag, (double) l);
            else tag2weight.put(tag, (double) l + tag2weight.get(tag));
          } else {
            if (tag2weight.get(tag) == null) tag2weight.put(tag, Double.valueOf(hits.score(l)));
            else tag2weight.put(tag, (double) l + hits.score(l));
          }
        }
        // find class:
        int maxCount = 0, maxima = 0;
        String classifiedAs = null;
        for (Iterator<String> tagIterator = tag2count.keySet().iterator();
            tagIterator.hasNext(); ) {
          String tag = tagIterator.next();
          if (tag2count.get(tag) > maxCount) {
            maxCount = tag2count.get(tag);
            maxima = 1;
            classifiedAs = tag;
          } else if (tag2count.get(tag) == maxCount) {
            maxima++;
          }
        }
        // if there are two or more classes with the same number of results, then we take a look at
        // the weights.
        // else the class is alread given in classifiedAs.
        if (maxima > 1) {
          double minWeight = Double.MAX_VALUE;
          for (Iterator<String> tagIterator = tag2count.keySet().iterator();
              tagIterator.hasNext(); ) {
            String tag = tagIterator.next();
            if (tag2weight.get(tag) < minWeight) {
              minWeight = tag2weight.get(tag);
              classifiedAs = tag;
            }
          }
        }
        //                    if (tag2.equals(tag3)) tag1 = tag2;
        count++;
        if (classifiedAs.equals(classIdentifier)) countCorrect++;
        // confusion:
        confusion[class2id.get(classifiedAs)]++;
        //                    System.out.printf("%10s (%4.3f, %10d, %4d)\n", classifiedAs, ((double)
        // countCorrect / (double) count), count, (System.currentTimeMillis() - ms) / count);
      } catch (Exception e) {
        System.err.println(e.getMessage());
      }
    }
    //            System.out.println("Results for class " + classIdentifier);
    System.out.printf("Class\tAvg. Precision\tCount Test Images\tms per test\n");
    System.out.printf(
        "%s\t%4.5f\t%10d\t%4d\n",
        classIdentifier,
        ((double) countCorrect / (double) count),
        count,
        (System.currentTimeMillis() - ms) / count);
    System.out.printf("Confusion\t");
    //            for (int i = 0; i < classes.length; i++) {
    //                System.out.printf("%s\t", classes[i]);
    //            }
    //            System.out.println();
    for (int i = 0; i < classes.length; i++) {
      System.out.printf("%d\t", confusion[i]);
    }
    System.out.println();
    //        }
  }
Example #10
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();
 }