Beispiel #1
0
 public void testPerformance() throws IOException {
   System.out.printf(
       "%s\t%s\t%s\t%s\t%s\t%s\n",
       "Test", "recall@5", "recall@10", "recall@20", "recall@30", "recall@50");
   IndexReader reader = DirectoryReader.open(MMapDirectory.open(new File(indexPath)));
   for (int i = 0; i < testFeatures.length; i++) {
     getRecall(
         testFeatures[i].getName().substring(testFeatures[i].getName().lastIndexOf('.') + 1),
         new GenericFastImageSearcher(250, testFeatures[i], true, reader),
         reader);
   }
 }
 public void init(String indexName, RemoteBucketManager remoteBucketManager, boolean recreate)
     throws IOException {
   analyzer = new StandardAnalyzer(Version.LUCENE_40);
   doc = new Document();
   fieldContent = new TextField("content", "", Field.Store.YES);
   doc.add(fieldContent);
   fieldUrl = new StringField("url", "", Field.Store.YES);
   doc.add(fieldUrl);
   bucketManager = remoteBucketManager;
   File indexPath = null;
   if (recreate) {
     indexPath = bucketManager.clearIndexPath(indexName);
   } else {
     indexPath = bucketManager.loadIndexPath(indexName);
   }
   index = MMapDirectory.open(indexPath);
 }
Beispiel #3
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();
    //        }
  }
Beispiel #4
0
  public void testMAP() throws IOException {
    // copy index to ram to be much faster ...
    IndexReader reader = DirectoryReader.open(MMapDirectory.open(new File(indexPath)));

    System.out.println("hashFunctionsFileName\tmap\tp@10\terror rate");
    //        SimpleColorHistogram.DEFAULT_DISTANCE_FUNCTION =
    // SimpleColorHistogram.DistanceFunction.L1;
    //        computeMAP(ImageSearcherFactory.createColorHistogramImageSearcher(1000), "Color
    // Histogram - L1");
    //        SimpleColorHistogram.DEFAULT_DISTANCE_FUNCTION =
    // SimpleColorHistogram.DistanceFunction.L2;
    //        computeMAP(ImageSearcherFactory.createColorHistogramImageSearcher(1000), "Color
    // Histogram - L2");
    //        SimpleColorHistogram.DEFAULT_DISTANCE_FUNCTION =
    // SimpleColorHistogram.DistanceFunction.JSD;
    //        computeMAP(ImageSearcherFactory.createColorHistogramImageSearcher(1000), "Color
    // Histogram - JSD");
    //        SimpleColorHistogram.DEFAULT_DISTANCE_FUNCTION =
    // SimpleColorHistogram.DistanceFunction.TANIMOTO;

    //        computeMAP(new GenericFastImageSearcher(1000, CEDD.class), "CEDD", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, AutoColorCorrelogram.class, true,
    // reader), "Color Correlogram", reader);
    computeMAP(
        new GenericFastImageSearcher(1000, ColorLayout.class, true, reader),
        "Color Layout",
        reader);
    computeMAP(
        new GenericFastImageSearcher(1000, EdgeHistogram.class, true, reader),
        "Edge Histogram",
        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, OpponentHistogram.class, true, reader),
        "OpponentHistogram",
        reader);
    computeMAP(
        new GenericFastImageSearcher(
            1000, PHOG.class, DocumentBuilder.FIELD_NAME_PHOG, true, reader),
        "PHOG",
        reader);
    //        computeMAP(new GenericFastImageSearcher(1000, SimpleColorHistogram.class, true,
    // reader), "RGB Color Histogram", reader);
    computeMAP(
        new GenericFastImageSearcher(1000, ScalableColor.class, true, reader),
        "Scalable Color",
        reader);

    //        computeMAP(ImageSearcherFactory.createTamuraImageSearcher(1000), "Tamura");
    //        computeMAP(ImageSearcherFactory.createGaborImageSearcher(1000), "Gabor");
    //        computeMAP(ImageSearcherFactory.createLuminanceLayoutImageSearcher(1000), "LumLay");
    //        computeMAP(new GenericFastImageSearcher(1000, FuzzyOpponentHistogram.class, "opHist"),
    // "Joint Opponent Histogram - JSD");
    //        computeMAP(new GenericFastImageSearcher(1000, RankAndOpponent.class, "jop"), "JointOp
    // Hist");
    //        computeMAP(new GenericFastImageSearcher(1000, SPCEDD.class, true, reader), "SPCEDD",
    // reader);
    //        computeMAP(new GenericFastImageSearcher(1000, SPFCTH.class, true, reader), "SPFCTH",
    // reader);
    //        computeMAP(new GenericFastImageSearcher(1000, SPJCD.class, true, reader), "SPJCD",
    // reader);
    //        computeMAP(new GenericFastImageSearcher(1000, SPACC.class, true, reader), "SPACC",
    // reader);
    //        computeMAP(new GenericFastImageSearcher(1000, BinaryPatternsPyramid.class, true,
    // reader), "whog", reader);
    //        computeMAP(new GenericFastImageSearcher(1000, LocalBinaryPatternsAndOpponent.class,
    // true, reader), "jhl", reader);
    //        computeMAP(new GenericFastImageSearcher(1000,
    // RotationInvariantLocalBinaryPatterns.class, "rlbp"), "RILBP");
    //        computeMAP(new GenericFastImageSearcher(1000, SPLBP.class, "splbp"), "SPLBP");
    //        computeMAP(ImageSearcherFactory.createJpegCoefficientHistogramImageSearcher(1000),
    // "JPEG Coeffs");
    //        computeMAP(new VisualWordsImageSearcher(1000,
    // DocumentBuilder.FIELD_NAME_SURF_VISUAL_WORDS), "SURF BoVW", reader);
    //        computeMAP(new VisualWordsImageSearcher(1000,
    // DocumentBuilder.FIELD_NAME_MSER_LOCAL_FEATURE_HISTOGRAM_VISUAL_WORDS), "MSER BoVW");
    //        computeMAP(new VisualWordsImageSearcher(1000,
    // DocumentBuilder.FIELD_NAME_SIFT_VISUAL_WORDS), "SIFT BoVW");

  }