Skip to content

Commit

Permalink
Example of feature extraction and PQ
Browse files Browse the repository at this point in the history
  • Loading branch information
manosetro committed Jan 14, 2014
1 parent b1f708e commit ccd9654
Showing 1 changed file with 70 additions and 49 deletions.
119 changes: 70 additions & 49 deletions src/main/java/gr/iti/mklab/visual/examples/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import gr.iti.mklab.visual.dimreduction.PCA;
import gr.iti.mklab.visual.extraction.AbstractFeatureExtractor;
import gr.iti.mklab.visual.extraction.SURFExtractor;
import gr.iti.mklab.visual.utilities.Answer;
import gr.iti.mklab.visual.utilities.Normalization;
import gr.iti.mklab.visual.utilities.Result;
import gr.iti.mklab.visual.vectorization.ImageVectorization;
Expand All @@ -29,32 +30,36 @@ public static void main(String[] args) throws Exception {

int targetLengthMax = 1024;

int targetLength1 = 256;
//int targetLength2 = 1024;
//int targetLength1 = 256;
int targetLength2 = 1024;
//int targetLength3 = 1024;

int maximumNumVectors = 1000000;

String indexFolder = "/home/manos/git/multimedia-indexing/indices/";
//String indexFolder = "/home/manos/git/multimedia-indexing/indices/";

String fullVectorIndexFolder = indexFolder + "main";
//String fullVectorIndexFolder = indexFolder + "main";


int m1 = 16; // num subvectors
String ivfpqIndex1Folder = indexFolder + "cheap";
//int m1 = 16; // num subvectors
//String ivfpqIndex1Folder = indexFolder + "cheap";

//int m2 = 64;
int m2 = 64;
//String ivfpqIndex2Folder = indexFolder + "medium";

// int m3 = 128;
// String ivfpqIndex3Folder = "best";

String linearIndexFolder = "/disk2_data/VisualIndex/data/prototype/linear";
String ivfpqIndexFolder = "/disk2_data/VisualIndex/data/prototype/ivfpq";


int k_c = 256;
int numCoarseCentroids = 8192;

String learningFolder = "/home/manos/git/multimedia-indexing/learning_files/";

String learningFolder = "/home/manosetro/git/multimedia-indexing/learning_files/";

/*
String[] codebookFiles = {
learningFolder + "surf_l2_128c_0.csv",
learningFolder + "surf_l2_128c_1.csv",
Expand All @@ -63,40 +68,56 @@ public static void main(String[] args) throws Exception {
};
String pcaFile = learningFolder + "pca_surf_4x128_32768to1024.txt";
*/


String coarseQuantizerFile1 = learningFolder + "qcoarse_256d_8192k.csv";
//String coarseQuantizerFile2 = learningFolder + "qcoarse_1024d_8192k.csv";
//String coarseQuantizerFile1 = learningFolder + "qcoarse_256d_8192k.csv";
String coarseQuantizerFile2 = learningFolder + "qcoarse_1024d_8192k.csv";
//String coarseQuantizerFile3 = learningFolder + "qcoarse_1024d_8192k.csv";


String productQuantizerFile1 = learningFolder + "pq_256_16x8_rp_ivf_k8192.csv";
//String productQuantizerFile2 = learningFolder + "pq_1024_64x8_rp_ivf_8192k.csv";
//String productQuantizerFile1 = learningFolder + "pq_256_16x8_rp_ivf_k8192.csv";
String productQuantizerFile2 = learningFolder + "pq_1024_64x8_rp_ivf_8192k.csv";
//String productQuantizerFile3 = learningFolder + "pq_1024_128x8_rp_ivf_8192k.csv";

/*
ImageVectorization.setFeatureExtractor(new SURFExtractor());
ImageVectorization.setVladAggregator(new VladAggregatorMultipleVocabularies(codebookFiles,
numCentroids, AbstractFeatureExtractor.SURFLength));
if (targetLengthMax < initialLength) {
PCA pca = new PCA(targetLengthMax, 1, initialLength, true);
pca.loadPCAFromFile(pcaFile);
ImageVectorization.setPcaProjector(pca);
}
System.out.println("PCA loaded!");
*/

//System.out.println("PCA loaded!");

// creating/loading the indices
// this linear index contains plain 1024d vectors and is not loaded in memory
Linear linear = new Linear(targetLengthMax, targetLengthMax, false, fullVectorIndexFolder, true, true, 0); // false by default
Linear linear = new Linear(targetLengthMax, targetLengthMax, false, linearIndexFolder, true, true, 0); // false by default

// this is the cheaper IVFPQ index and is loaded in memory
IVFPQ ivfpq_1 = new IVFPQ(targetLength1, maximumNumVectors, false, ivfpqIndex1Folder, m1, k_c, PQ.TransformationType.RandomPermutation, numCoarseCentroids, true, 0);
ivfpq_1.loadCoarseQuantizer(coarseQuantizerFile1);
ivfpq_1.loadProductQuantizer(productQuantizerFile1);
IVFPQ ivfpq_1 = new IVFPQ(targetLength2, maximumNumVectors, false, ivfpqIndexFolder, m2, k_c, PQ.TransformationType.RandomPermutation, numCoarseCentroids, true, 0);
ivfpq_1.loadCoarseQuantizer(coarseQuantizerFile2);
ivfpq_1.loadProductQuantizer(productQuantizerFile2);
int w = 64; // larger values will improve results/increase seach time
ivfpq_1.setW(w); // how many (out of 8192) lists should be visited during search.

System.out.println("Indices created!");

for(int i=0;i<linear.getLoadCounter();i++) {

String id = linear.getId(i);

System.out.println(i + " => " + id);

double[] vector = linear.getVector(i);
System.out.println(vector.length);

Answer r = ivfpq_1.computeNearestNeighbors(100, vector);
System.out.println(r.getResults().length);
}
// IVFPQ ivfpq_2 = new IVFPQ(targetLength, maximumNumVectors, false, ivfpqIndex1Folder, m2, k_c,
// PQ.TransformationType.RandomPermutation, numCoarseCentroids, true, 0);
// ivfpq_2.loadCoarseQuantizer(coarseQuantizerFile2);
Expand All @@ -107,36 +128,36 @@ public static void main(String[] args) throws Exception {
// ivfpq_3.loadProductQuantizer(productQuantizerFile3);


File imageFolder = new File("/media/manos/Data/Pictures/Sofia 2013");


// setting up the vectorizer and extracting the vector of a single image
for(String imageFilename : imageFolder.list()) {
if(!imageFilename.endsWith("JPG"))
continue;

System.out.println(imageFilename);
ImageVectorization imvec = new ImageVectorization(imageFolder.toString()+"/", imageFilename, targetLengthMax, maxNumPixels);

ImageVectorizationResult imvr = imvec.call();
double[] vector = imvr.getImageVector();
System.out.println(Arrays.toString(vector));


// indexing a vector
String id = imageFilename;

// the full vector is indexed in the disk-based index
//linear.indexVector(id, vector);

// the vector is truncated to the correct dimension and renormalized before sending to the ram-based index
double[] newVector = Arrays.copyOf(vector, targetLength1);
if (newVector.length < vector.length) {
Normalization.normalizeL2(newVector);
}
ivfpq_1.indexVector(id, newVector);

}
//File imageFolder = new File("/media/manos/Data/Pictures/Sofia 2013");


// // setting up the vectorizer and extracting the vector of a single image
// for(String imageFilename : imageFolder.list()) {
// if(!imageFilename.endsWith("JPG"))
// continue;
//
// System.out.println(imageFilename);
// ImageVectorization imvec = new ImageVectorization(imageFolder.toString()+"/", imageFilename, targetLengthMax, maxNumPixels);
//
// ImageVectorizationResult imvr = imvec.call();
// double[] vector = imvr.getImageVector();
// System.out.println(Arrays.toString(vector));
//
//
// // indexing a vector
// String id = imageFilename;
//
// // the full vector is indexed in the disk-based index
// //linear.indexVector(id, vector);
//
// // the vector is truncated to the correct dimension and renormalized before sending to the ram-based index
// double[] newVector = Arrays.copyOf(vector, targetLength1);
// if (newVector.length < vector.length) {
// Normalization.normalizeL2(newVector);
// }
// ivfpq_1.indexVector(id, newVector);
//
// }

/*
int p=0, n=0;
Expand Down

0 comments on commit ccd9654

Please sign in to comment.