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.image.vlad; 031 032import java.io.File; 033import java.io.IOException; 034import java.util.Comparator; 035import java.util.List; 036 037import org.apache.commons.math.stat.descriptive.DescriptiveStatistics; 038import org.openimaj.feature.FloatFVComparison; 039import org.openimaj.feature.local.list.MemoryLocalFeatureList; 040import org.openimaj.image.feature.local.keypoints.Keypoint; 041import org.openimaj.image.indexing.vlad.VLADIndexerData; 042import org.openimaj.util.function.Operation; 043import org.openimaj.util.pair.FloatIntPair; 044import org.openimaj.util.parallel.Parallel; 045import org.openimaj.util.queue.BoundedPriorityQueue; 046 047public class VLADUKBench { 048 public static void main(String[] args) throws IOException { 049 // final ByteCentroidsResult centroids = IOUtils.read(new 050 // File("/Users/jsh2/Desktop/ukbench64.voc"), 051 // ByteCentroidsResult.class); 052 // 053 // final ExactByteAssigner assigner = new ExactByteAssigner(centroids); 054 // final VLAD<byte[]> vlad = new VLAD<byte[]>(assigner, 055 // centroids.centroids, true); 056 057 final VLADIndexerData indexer = VLADIndexerData.read(new File("/Users/jsh2/vlad-indexer-ukbench-2x.dat")); 058 059 final float[][] vlads = new float[10200][]; 060 Parallel.forIndex(0, 10200, 1, new Operation<Integer>() { 061 @Override 062 public void perform(Integer i) { 063 try { 064 System.out.println("Loading " + i); 065 final File file = new File(String.format("/Users/jsh2/Data/ukbench/sift/ukbench%05d.jpg", i)); 066 final List<Keypoint> keys = MemoryLocalFeatureList.read(file, Keypoint.class); 067 068 vlads[i] = indexer.extractPcaVlad(keys); 069 } catch (final Exception e) { 070 e.printStackTrace(); 071 } 072 } 073 }); 074 075 final DescriptiveStatistics stats = new DescriptiveStatistics(); 076 for (int i = 0; i < 10200; i++) { 077 final int start = (i / 4) * 4; 078 final int stop = start + 4; 079 final int score = score(search(vlads[i], vlads), start, stop); 080 stats.addValue(score); 081 System.out.format("Query %d, Score: %d, Mean: %f\n", i, score, stats.getMean()); 082 } 083 System.out.println("Total: " + stats.getMean()); 084 } 085 086 private static int score(BoundedPriorityQueue<FloatIntPair> search, int start, int stop) { 087 int score = 0; 088 for (final FloatIntPair i : search) { 089 if (i.second >= start && i.second < stop) 090 score++; 091 } 092 return score; 093 } 094 095 private static BoundedPriorityQueue<FloatIntPair> search(float[] query, 096 float[][] vlads) 097 { 098 final BoundedPriorityQueue<FloatIntPair> queue = new BoundedPriorityQueue<FloatIntPair>(4, 099 new Comparator<FloatIntPair>() { 100 101 @Override 102 public int compare(FloatIntPair o1, FloatIntPair o2) { 103 return Float.compare(o1.first, o2.first); 104 } 105 }); 106 for (int i = 0; i < vlads.length; i++) { 107 final float[] that = vlads[i]; 108 final float distance = (float) FloatFVComparison.EUCLIDEAN.compare(query, that); 109 110 queue.add(new FloatIntPair(distance, i)); 111 } 112 return queue; 113 } 114}