1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
51
52
53
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
85
86
87
88
89
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 }