View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.tools.cbir;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.util.ArrayList;
35  import java.util.List;
36  import java.util.regex.Pattern;
37  
38  import org.kohsuke.args4j.CmdLineException;
39  import org.kohsuke.args4j.CmdLineParser;
40  import org.kohsuke.args4j.Option;
41  import org.openimaj.feature.local.LocalFeature;
42  import org.openimaj.feature.local.LocalFeatureExtractor;
43  import org.openimaj.image.MBFImage;
44  import org.openimaj.image.indexing.vlad.VLADIndexerData;
45  import org.openimaj.image.indexing.vlad.VLADIndexerDataBuilder;
46  import org.openimaj.image.indexing.vlad.VLADIndexerDataBuilder.StandardPostProcesses;
47  import org.openimaj.io.IOUtils;
48  
49  /**
50   * Tool to build a {@link VLADIndexerData} which can be used to build efficient
51   * Product-quantised PCA-VLAD indexes.
52   * 
53   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
54   * 
55   */
56  public class VLADIndexerBuilder extends VLADBuilder {
57  	@Option(
58  			name = "--num-pca-dims",
59  			aliases = "-npca",
60  			usage = "the number of dimensions to project down to using PCA (~128 for normal SIFT)")
61  	protected int numPcaDims = 128;
62  
63  	@Option(
64  			name = "--pca-sample-proportion",
65  			usage = "the percentage of images to use for computing the PCA basis")
66  	protected float pcaSampleProp = 1.0f;
67  
68  	@Option(
69  			name = "--num-pq-iterations",
70  			aliases = "-npqi",
71  			usage = "the number of iterations for clustering the product quantisers (~100)")
72  	protected int numPqIterations = 100;
73  
74  	@Option(name = "--num-pq-assigners", aliases = "-na", usage = "the number of product quantiser assigners (~16)")
75  	protected int numPqAssigners = 16;
76  
77  	@Option(
78  			name = "--post-process",
79  			aliases = "-pp",
80  			usage = "the post-processing to apply to the raw features before input to VLAD")
81  	protected StandardPostProcesses postProcess = StandardPostProcesses.NONE;
82  
83  	/**
84  	 * Main method
85  	 * 
86  	 * @param args
87  	 *            arguments
88  	 * @throws IOException
89  	 *             if an error occurs during reading or writing
90  	 */
91  	public static void main(String[] args) throws IOException {
92  		final VLADIndexerBuilder builder = new VLADIndexerBuilder();
93  		final CmdLineParser parser = new CmdLineParser(builder);
94  
95  		try {
96  			parser.parseArgument(args);
97  		} catch (final CmdLineException e) {
98  			System.err.println(e.getMessage());
99  			System.err.println("Usage: java -jar CBIRTool.jar VLADBuilder [options]");
100 			parser.printUsage(System.err);
101 			return;
102 		}
103 
104 		final LocalFeatureExtractor<LocalFeature<?, ?>, MBFImage> extractor = IOUtils.readFromFile(builder.extractorFile);
105 
106 		final List<File> localFeatures = new ArrayList<File>();
107 		getInputFiles(localFeatures, builder.localFeaturesDir,
108 				builder.regex == null ? null : Pattern.compile(builder.regex));
109 
110 		final VLADIndexerDataBuilder vladBuilder = new VLADIndexerDataBuilder(extractor, localFeatures,
111 				builder.normalise, builder.numVladCentroids, builder.numIterations, builder.numPcaDims,
112 				builder.numPqIterations, builder.numPqAssigners, builder.sampleProp, builder.pcaSampleProp,
113 				builder.postProcess);
114 
115 		final VLADIndexerData vlad = vladBuilder.buildIndexerData();
116 
117 		IOUtils.writeToFile(vlad, builder.output);
118 	}
119 }