Author: koji
Date: Sat Dec 29 08:35:25 2012
New Revision: 1426715
URL: http://svn.apache.org/viewvc?rev=1426715&view=rev
Log:
add VisualDescriptorsExecutorBase
Added:
labs/alike/trunk/src/java/org/apache/alike/VisualDescriptorsExecutorBase.java
Modified:
labs/alike/trunk/src/java/org/apache/alike/QuantizeVectors.java
Modified: labs/alike/trunk/src/java/org/apache/alike/QuantizeVectors.java
URL:
http://svn.apache.org/viewvc/labs/alike/trunk/src/java/org/apache/alike/QuantizeVectors.java?rev=1426715&r1=1426714&r2=1426715&view=diff
==============================================================================
--- labs/alike/trunk/src/java/org/apache/alike/QuantizeVectors.java (original)
+++ labs/alike/trunk/src/java/org/apache/alike/QuantizeVectors.java Sat Dec 29
08:35:25 2012
@@ -17,10 +17,6 @@
package org.apache.alike;
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@@ -28,9 +24,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import org.apache.alike.FileUtil.Executor;
-import org.apache.commons.io.IOUtils;
-
/**
* This program quantize clustered visual descriptors.
* Executing {@link KMeansLauncher} and {@link ClusterDumperLauncher} multiple
times,
@@ -71,8 +64,8 @@ public class QuantizeVectors {
double[][] centroids =
clusterDumpReader.getCentroids(config.getClusterDumpFile());
// make histograms
- HistogramExecutor executor =
- new HistogramExecutor(centroids, config.getDistanceCalculator());
+ HistogramMaker executor =
+ new HistogramMaker(K, centroids, config.getDistanceCalculator());
FileUtil.executeRecursively(executor, config.getDescNormalFSDir());
Map<String, int[]> histograms = executor.getHistograms();
@@ -89,61 +82,27 @@ public class QuantizeVectors {
}
}
- static class HistogramExecutor extends Executor {
+ static class HistogramMaker extends VisualDescriptorsExecutorBase {
- private double[][] centroids;
- Map<String, int[]> histogramMap;
- private DistanceCalculator distanceCalculator;
+ private int[] histgram = null;
+ private Map<String, int[]> histogramMap;
- public HistogramExecutor(double[][] centroids, DistanceCalculator
distanceCalculator){
- this.centroids = centroids;
+ public HistogramMaker(int k, double[][] centroids, DistanceCalculator
distanceCalculator){
+ super(k, centroids, distanceCalculator);
histogramMap = new HashMap<String, int[]>();
- this.distanceCalculator = distanceCalculator;
}
@Override
- public boolean isExecutable(File theFile){
- return theFile.getName().endsWith(".txt");
+ protected void preProcess(String key){
+ histgram = new int[k];
}
- public void execute(File theFile) {
- try {
- makeHistgram(theFile);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- void makeHistgram(File theFile) throws IOException {
- int[] histgram = new int[K];
-
- BufferedReader br = new BufferedReader(new FileReader(theFile));
- String key = br.readLine();
- String line = br.readLine(); // skip number of lines count
- while((line = br.readLine()) != null){
- String[] strValues = line.trim().split("\\s+");
- double[] desc = new double[strValues.length];
- for(int i = 0; i < strValues.length; i++){
- desc[i] = Double.parseDouble(strValues[i]);
- }
-
- voteForVisualWord(histgram, desc);
- }
- br.close();
+ @Override
+ protected void postProcess(String key){
histogramMap.put(key, histgram);
}
- void voteForVisualWord(int[] histgram, double[] desc){
- double minDistance = Double.MAX_VALUE;
- int pos = Integer.MAX_VALUE;
- for(int i = 0; i < K; i++){
- double distance = distanceCalculator.compute(centroids[i], desc);
- if(minDistance > distance){
- minDistance = distance;
- pos = i;
- }
- }
-
+ protected void minFoundProcess(String key, int pos, double minDistance){
// vote for minimum distance
histgram[pos]++;
}
Added:
labs/alike/trunk/src/java/org/apache/alike/VisualDescriptorsExecutorBase.java
URL:
http://svn.apache.org/viewvc/labs/alike/trunk/src/java/org/apache/alike/VisualDescriptorsExecutorBase.java?rev=1426715&view=auto
==============================================================================
---
labs/alike/trunk/src/java/org/apache/alike/VisualDescriptorsExecutorBase.java
(added)
+++
labs/alike/trunk/src/java/org/apache/alike/VisualDescriptorsExecutorBase.java
Sat Dec 29 08:35:25 2012
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.alike;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+
+import org.apache.alike.FileUtil.Executor;
+import org.apache.commons.io.IOUtils;
+
+/**
+ * An {@link Executor} that reads visual descriptors to process them.
+ * Sub classes can override {@link #preProcess(String)},
+ * {@link VisualDescriptorsExecutorBase#postProcess(String)} and
+ * {@link #minFoundProcess(String)} to change the default behavior.
+ *
+ */
+public abstract class VisualDescriptorsExecutorBase extends Executor {
+
+ protected int k;
+ protected double[][] centroids;
+ protected DistanceCalculator distanceCalculator;
+
+ public VisualDescriptorsExecutorBase(int k, double[][] centroids,
DistanceCalculator distanceCalculator){
+ this.k = k;
+ this.centroids = centroids;
+ this.distanceCalculator = distanceCalculator;
+ }
+
+ protected void preProcess(String key){
+ // do nothing for the default
+ }
+
+ protected void postProcess(String key){
+ // do nothing for the default
+ }
+
+ protected void minFoundProcess(String key, int pos, double minDistance){
+ // do nothing for the default
+ }
+
+ @Override
+ public boolean isExecutable(File theFile){
+ return theFile.getName().endsWith(".txt");
+ }
+
+ public final void execute(File theFile) {
+ FileReader fr = null;
+ BufferedReader br = null;
+ try {
+ fr = new FileReader(theFile);
+ br = new BufferedReader(fr);
+ String key = br.readLine();
+ preProcess(key);
+ String line = br.readLine(); // skip number of lines count
+ while((line = br.readLine()) != null){
+ String[] strValues = line.trim().split("\\s+");
+ double[] desc = new double[strValues.length];
+ for(int i = 0; i < strValues.length; i++){
+ desc[i] = Double.parseDouble(strValues[i]);
+ }
+
+ double minDistance = Double.MAX_VALUE;
+ int pos = Integer.MAX_VALUE;
+ for(int i = 0; i < k; i++){
+ double distance = distanceCalculator.compute(centroids[i], desc);
+ if(minDistance > distance){
+ minDistance = distance;
+ pos = i;
+ }
+ }
+
+ minFoundProcess(key, pos, minDistance);
+ }
+ postProcess(key);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ finally{
+ IOUtils.closeQuietly(br);
+ IOUtils.closeQuietly(fr);
+ }
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]