/** * There was an error that images with the same score but different documents in the index were * not included in the result list. Here's the test for that. */ public void testDuplicatesInIndex() throws IOException { indexFiles("src\\test\\resources\\images", "index-large-new", 0, true); indexFiles("src\\test\\resources\\images", "index-large-new", 0, false); indexFiles("src\\test\\resources\\images", "index-large-new", 0, false); ImageSearcher s = searchers[0]; IndexReader reader = DirectoryReader.open(FSDirectory.open(new File("index-large-new"))); Document query = reader.document(0); ImageSearchHits hits = s.search(query, reader); FileUtils.saveImageResultsToPng( "duplicate_", hits, query.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[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); }
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); } } } } }
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(); }