001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.demos.sandbox.vlad; 031 032import java.io.File; 033import java.io.IOException; 034import java.util.ArrayList; 035import java.util.Arrays; 036import java.util.Collections; 037import java.util.Comparator; 038import java.util.List; 039 040import org.apache.commons.math.stat.descriptive.DescriptiveStatistics; 041import org.openimaj.feature.local.list.MemoryLocalFeatureList; 042import org.openimaj.image.feature.local.keypoints.Keypoint; 043import org.openimaj.image.indexing.vlad.VLADIndexerData; 044import org.openimaj.io.IOUtils; 045import org.openimaj.knn.pq.FloatADCNearestNeighbours; 046import org.openimaj.util.function.Operation; 047import org.openimaj.util.pair.IntObjectPair; 048import org.openimaj.util.parallel.Parallel; 049 050public class UKBenchTest { 051 public static void index() throws IOException { 052 final VLADIndexerData indexer = VLADIndexerData.read(new File("/Users/jsh2/vlad-indexer-ukbench-2x-nohell.dat")); 053 054 final List<IntObjectPair<float[]>> index = new ArrayList<IntObjectPair<float[]>>(); 055 final List<IntObjectPair<float[]>> syncList = Collections.synchronizedList(index); 056 057 Parallel.forEach(Arrays.asList(new File("/Users/jsh2/Data/ukbench/sift/").listFiles()), new 058 Operation<File>() 059 { 060 061 @Override 062 public void perform(File f) { 063 try { 064 System.out.println(f); 065 066 final int id = Integer.parseInt(f.getName().replace("ukbench", 067 "").replace(".jpg", "")); 068 069 final MemoryLocalFeatureList<Keypoint> keys = 070 MemoryLocalFeatureList.read(f, Keypoint.class); 071 072 syncList.add(new IntObjectPair<float[]>(id, indexer.extractPcaVlad(keys))); 073 } catch (final Exception e) { 074 e.printStackTrace(); 075 } 076 } 077 }); 078 079 IOUtils.writeToFile(index, new 080 File("/Users/jsh2/Desktop/ukb-nohell.idx")); 081 } 082 083 public static void search() throws IOException { 084 final VLADIndexerData indexer = VLADIndexerData.read(new 085 File("/Users/jsh2/vlad-indexer-ukbench-2x-nohell.dat")); 086 final List<IntObjectPair<float[]>> index = IOUtils.readFromFile(new 087 File("/Users/jsh2/Desktop/ukb-nohell.idx")); 088 089 Collections.sort(index, new Comparator<IntObjectPair<float[]>>() { 090 @Override 091 public int compare(IntObjectPair<float[]> o1, IntObjectPair<float[]> o2) 092 { 093 return o1.first == o2.first ? 0 : o1.first < o2.first ? -1 : 1; 094 } 095 }); 096 097 final List<float[]> data = IntObjectPair.getSecond(index); 098 099 final FloatADCNearestNeighbours nn = new 100 FloatADCNearestNeighbours(indexer.getProductQuantiser(), 101 data.toArray(new float[data.size()][])); 102 // final FloatNearestNeighboursExact nn = new 103 // FloatNearestNeighboursExact(data.toArray(new float[data.size()][])); 104 105 final DescriptiveStatistics stats = new DescriptiveStatistics(); 106 for (int i = 0; i < 10200; i++) { 107 final int start = (i / 4) * 4; 108 final int stop = start + 4; 109 110 final int[][] argmins = new int[1][4]; 111 final float[][] mins = new float[1][4]; 112 nn.searchKNN(new float[][] { index.get(i).second }, 4, argmins, mins); 113 final int score = score(argmins[0], start, stop); 114 115 stats.addValue(score); 116 System.out.format("Query %d, Score: %d, Mean: %f\n", i, score, 117 stats.getMean()); 118 } 119 System.out.println("Total: " + stats.getMean()); 120 } 121 122 private static int score(int[] docs, int start, int stop) { 123 int score = 0; 124 for (final int i : docs) { 125 if (i >= start && i < stop) 126 score++; 127 } 128 return score; 129 } 130}