/** * Extract the PCA-projected VLAD feature from the given image. The local * features will be post-processed before being aggregated using * {@link VLAD} and projected by the PCA basis. * * @param image * the image to extract from * @return the pca-vlad aggregated representation of the image */ public float[] extractPcaVlad(MBFImage image) { return extractPcaVlad(extractor.extractFeature(image)); }
@Override protected void setup(Context context) throws IOException, InterruptedException { indexer = VLADIndexerData.read(new File("./" + context.getConfiguration().get(VLAD_INDEXER_DATA_PATH_KEY))); }
public VLADIndexer(VLADIndexerData indexerData, IncrementalMetaIndex<DATA, METADATA> metaStore) { this.indexerData = indexerData; this.nn = indexerData.createIncrementalIndex(); this.metaStore = metaStore; }
public static void createPQADCNN() throws IOException { final File input = new File("/Volumes/My Book/flickr46m-vlad64-pca128-pq16x8-indexer-mirflickr25k-sift1x.dat"); final DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(input))); final VLADIndexerData indexer = VLADIndexerData.read(new File( "/Users/jsh2/vlad64-pca128-pq16x8-indexer-mirflickr25k-sift1x.dat")); final IncrementalFloatADCNearestNeighbours nn = new IncrementalFloatADCNearestNeighbours( indexer.getProductQuantiser(), 128, 46000000); final TLongArrayList indexes = new TLongArrayList(46000000); try { final float[] farr = new float[128]; for (int x = 0;; x++) { if (x % 100000 == 0) System.out.println(x); final long id = dis.readLong(); for (int i = 0; i < 128; i++) { farr[i] = dis.readFloat(); } nn.add(farr); indexes.add(id); } } catch (final EOFException e) { dis.close(); } IOUtils.writeBinary(new File( "/Volumes/My Book/flickr46m-vlad64-pca128-pq16x8-indexer-mirflickr25k-sift1x-pqadcnn.dat"), nn); IOUtils.writeToFile(indexes, new File( "/Volumes/My Book/flickr46m-vlad64-pca128-pq16x8-indexer-mirflickr25k-sift1x-pqadcnn-indexes.dat")); }
@Override protected void map(Text key, BytesWritable value, Context context) throws IOException, InterruptedException { final List<Keypoint> keys = MemoryLocalFeatureList.read(new ByteArrayInputStream(value.getBytes()), Keypoint.class); final float[] vladData = indexer.extractPcaVlad(keys); if (vladData == null) { context.getCounter(COUNTERS.NULL).increment(1L); System.out.println("VLAD is null; keys has length " + keys.size()); return; } final byte[] pqVladData = indexer.getProductQuantiser().quantise(vladData); mos.write("pcavlad", key, floatToBytes(vladData)); context.write(key, new BytesWritable(pqVladData)); context.getCounter(COUNTERS.EMIT).increment(1L); }
public static void main(String[] args) throws IOException { final FImage image = ImageUtilities.readF(new File("/Users/jsh2/Data/ukbench/full/ukbench01001.jpg")); final DoGSIFTEngine engine = new DoGSIFTEngine(); engine.getOptions().setDoubleInitialImage(false); final LocalFeatureList<Keypoint> features = engine.findFeatures(image); final List<FloatKeypoint> fkeys = FloatKeypoint.convert(features); for (final FloatKeypoint k : fkeys) HellingerNormaliser.normalise(k.vector, 0); final VLADIndexerData indexer = VLADIndexerData.read(new File("/Users/jsh2/vlad-indexer-ukbench-2x.dat")); // final ByteCentroidsResult centroids = IOUtils.read(new // File("/Users/jsh2/Desktop/ukbench16.voc"), // ByteCentroidsResult.class); // final ExactByteAssigner assigner = new ExactByteAssigner(centroids); // final VLAD<byte[]> vlad = new VLAD<byte[]>(assigner, // centroids.centroids, true); // final MultidimensionalFloatFV agg = vlad.aggregate(features); final MultidimensionalFloatFV agg = indexer.getVLAD().aggregate(fkeys); System.out.println(agg); DisplayUtilities.display(VLAD.drawDescriptor(agg.values, 64, 4, 8)); } }
/** * Extract the product-quantisedPCA-projected VLAD feature from the given * image. The local features will be post-processed before being aggregated * using {@link VLAD} and projected by the PCA basis. * * @param image * the image to extract from * @return the product-quantised pca-vlad aggregated representation of the * image */ public byte[] extractPQPcaVlad(MBFImage image) { return extractPQPcaVlad(extractor.extractFeature(image)); }
@Override public void indexImage(DATA image) { final int id = indexerData.index(image.getImage(), nn); metaStore.put(id, image); }
final FloatProductQuantiser pq = FloatProductQuantiserUtilities.train(pcaVlads, numPqAssigners, numPqIterations); return new VLADIndexerData(vlad, pca, pq, extractor, postProcess);
public static void createPQADCNN() throws IOException { final File input = new File("/Volumes/My Book/flickr46m-vlad64-pca128-pq16x8-indexer-mirflickr25k-sift1x.dat"); final DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(input))); final VLADIndexerData indexer = VLADIndexerData.read(new File( "/Users/jsh2/vlad64-pca128-pq16x8-indexer-mirflickr25k-sift1x.dat")); final IncrementalFloatADCNearestNeighbours nn = new IncrementalFloatADCNearestNeighbours( indexer.getProductQuantiser(), 128, 46000000); final TLongArrayList indexes = new TLongArrayList(46000000); try { final float[] farr = new float[128]; for (int x = 0;; x++) { if (x % 100000 == 0) System.out.println(x); final long id = dis.readLong(); for (int i = 0; i < 128; i++) { farr[i] = dis.readFloat(); } nn.add(farr); indexes.add(id); } } catch (final EOFException e) { dis.close(); } IOUtils.writeBinary(new File( "/Volumes/My Book/flickr46m-vlad64-pca128-pq16x8-indexer-mirflickr25k-sift1x-pqadcnn.dat"), nn); IOUtils.writeToFile(indexes, new File( "/Volumes/My Book/flickr46m-vlad64-pca128-pq16x8-indexer-mirflickr25k-sift1x-pqadcnn-indexes.dat")); }
public static void main(String[] args) throws IOException { final FImage image = ImageUtilities.readF(new File("/Users/jsh2/Data/ukbench/full/ukbench01001.jpg")); final DoGSIFTEngine engine = new DoGSIFTEngine(); engine.getOptions().setDoubleInitialImage(false); final LocalFeatureList<Keypoint> features = engine.findFeatures(image); final List<FloatKeypoint> fkeys = FloatKeypoint.convert(features); for (final FloatKeypoint k : fkeys) HellingerNormaliser.normalise(k.vector, 0); final VLADIndexerData indexer = VLADIndexerData.read(new File("/Users/jsh2/vlad-indexer-ukbench-2x.dat")); // final ByteCentroidsResult centroids = IOUtils.read(new // File("/Users/jsh2/Desktop/ukbench16.voc"), // ByteCentroidsResult.class); // final ExactByteAssigner assigner = new ExactByteAssigner(centroids); // final VLAD<byte[]> vlad = new VLAD<byte[]>(assigner, // centroids.centroids, true); // final MultidimensionalFloatFV agg = vlad.aggregate(features); final MultidimensionalFloatFV agg = indexer.getVLAD().aggregate(fkeys); System.out.println(agg); DisplayUtilities.display(VLAD.drawDescriptor(agg.values, 64, 4, 8)); } }
@Override public void perform(Integer i) { try { System.out.println("Loading " + i); final File file = new File(String.format("/Users/jsh2/Data/ukbench/sift/ukbench%05d.jpg", i)); final List<Keypoint> keys = MemoryLocalFeatureList.read(file, Keypoint.class); vlads[i] = indexer.extractPcaVlad(keys); } catch (final Exception e) { e.printStackTrace(); } } });
public static void search() throws IOException { final VLADIndexerData indexer = VLADIndexerData.read(new File("/Users/jsh2/vlad-indexer-ukbench-2x-nohell.dat")); final List<IntObjectPair<float[]>> index = IOUtils.readFromFile(new FloatADCNearestNeighbours(indexer.getProductQuantiser(), data.toArray(new float[data.size()][]));
@Override protected void setup(Context context) throws IOException, InterruptedException { indexer = VLADIndexerData.read(new File("vlad-data.bin")); mos = new MultipleOutputs<Text, BytesWritable>(context); }
@Override public void perform(Integer i) { try { System.out.println("Loading " + i); final File file = new File(String.format("/Users/jsh2/Data/ukbench/sift/ukbench%05d.jpg", i)); final List<Keypoint> keys = MemoryLocalFeatureList.read(file, Keypoint.class); vlads[i] = indexer.extractPcaVlad(keys); } catch (final Exception e) { e.printStackTrace(); } } });
public static void search() throws IOException { final VLADIndexerData indexer = VLADIndexerData.read(new File("/Users/jsh2/vlad-indexer-ukbench-2x-nohell.dat")); final List<IntObjectPair<float[]>> index = IOUtils.readFromFile(new FloatADCNearestNeighbours(indexer.getProductQuantiser(), data.toArray(new float[data.size()][]));
public static void index() throws IOException { final VLADIndexerData indexer = VLADIndexerData.read(new File("/Users/jsh2/vlad-indexer-ukbench-2x-nohell.dat")); final List<IntObjectPair<float[]>> index = new ArrayList<IntObjectPair<float[]>>(); final List<IntObjectPair<float[]>> syncList = Collections.synchronizedList(index); Parallel.forEach(Arrays.asList(new File("/Users/jsh2/Data/ukbench/sift/").listFiles()), new Operation<File>() { @Override public void perform(File f) { try { System.out.println(f); final int id = Integer.parseInt(f.getName().replace("ukbench", "").replace(".jpg", "")); final MemoryLocalFeatureList<Keypoint> keys = MemoryLocalFeatureList.read(f, Keypoint.class); syncList.add(new IntObjectPair<float[]>(id, indexer.extractPcaVlad(keys))); } catch (final Exception e) { e.printStackTrace(); } } }); IOUtils.writeToFile(index, new File("/Users/jsh2/Desktop/ukb-nohell.idx")); }
/** * Index the given image into the given nearest neighbours object by * extracting the PCA-VLAD representation and then product-quantising. * * @param image * the image to index * @param nn * the nearest neighbours object * @return the index at which the features were added in the nearest * neighbours object */ public int index(MBFImage image, IncrementalFloatADCNearestNeighbours nn) { return nn.add(extractPcaVlad(image)); }
public static void index() throws IOException { final VLADIndexerData indexer = VLADIndexerData.read(new File("/Users/jsh2/vlad-indexer-ukbench-2x-nohell.dat")); final List<IntObjectPair<float[]>> index = new ArrayList<IntObjectPair<float[]>>(); final List<IntObjectPair<float[]>> syncList = Collections.synchronizedList(index); Parallel.forEach(Arrays.asList(new File("/Users/jsh2/Data/ukbench/sift/").listFiles()), new Operation<File>() { @Override public void perform(File f) { try { System.out.println(f); final int id = Integer.parseInt(f.getName().replace("ukbench", "").replace(".jpg", "")); final MemoryLocalFeatureList<Keypoint> keys = MemoryLocalFeatureList.read(f, Keypoint.class); syncList.add(new IntObjectPair<float[]>(id, indexer.extractPcaVlad(keys))); } catch (final Exception e) { e.printStackTrace(); } } }); IOUtils.writeToFile(index, new File("/Users/jsh2/Desktop/ukb-nohell.idx")); }
/** * Index the given features into the given nearest neighbours object by * converting them to the PCA-VLAD representation and then * product-quantising. * * @param features * the features to index * @param nn * the nearest neighbours object * @return the index at which the features were added in the nearest * neighbours object */ public int index(List<? extends LocalFeature<?, ?>> features, IncrementalFloatADCNearestNeighbours nn) { return nn.add(extractPcaVlad(features)); }