/** * 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 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(); } } });
@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(); } } });
/** * 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)); }
/** * 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)); }
@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(); } } });
@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(); } } });
@Override public List<ImageSearchResult<METADATA>> search(ImageProvider<MBFImage> query) { final List<IntFloatPair> res = nn.searchKNN(indexerData.extractPcaVlad(query.getImage()), DEFAULT_MAX_RESULTS); final List<ImageSearchResult<METADATA>> results = new ArrayList<ImageSearchResult<METADATA>>(res.size()); for (int i = 0; i < res.size(); i++) { final IntFloatPair r = res.get(i); results.add(new ImageSearchResult<METADATA>(metaStore.get(r.first), r.second)); } return results; } }
@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); return; } final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final DataOutputStream dos = new DataOutputStream(baos); for (final float f : vladData) dos.writeFloat(f); context.write(key, new BytesWritable(baos.toByteArray())); context.getCounter(COUNTERS.EMIT).increment(1L); } }
@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); }