You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by ko...@apache.org on 2012/12/29 06:24:40 UTC

svn commit: r1426708 - in /labs/alike/trunk/src/java/org/apache/alike: ClusterDumpReader.java DistanceCalculator.java EuclideanDistanceCalculator.java EuclideanDistanceHistogramMatcher.java HistogramMatcher.java QuantizeVectors.java

Author: koji
Date: Sat Dec 29 05:24:40 2012
New Revision: 1426708

URL: http://svn.apache.org/viewvc?rev=1426708&view=rev
Log:
1. add missing sources for previous commit   2. separate ClusterDumpReader

Added:
    labs/alike/trunk/src/java/org/apache/alike/ClusterDumpReader.java
    labs/alike/trunk/src/java/org/apache/alike/DistanceCalculator.java
    labs/alike/trunk/src/java/org/apache/alike/EuclideanDistanceCalculator.java
Removed:
    labs/alike/trunk/src/java/org/apache/alike/EuclideanDistanceHistogramMatcher.java
    labs/alike/trunk/src/java/org/apache/alike/HistogramMatcher.java
Modified:
    labs/alike/trunk/src/java/org/apache/alike/QuantizeVectors.java

Added: labs/alike/trunk/src/java/org/apache/alike/ClusterDumpReader.java
URL: http://svn.apache.org/viewvc/labs/alike/trunk/src/java/org/apache/alike/ClusterDumpReader.java?rev=1426708&view=auto
==============================================================================
--- labs/alike/trunk/src/java/org/apache/alike/ClusterDumpReader.java (added)
+++ labs/alike/trunk/src/java/org/apache/alike/ClusterDumpReader.java Sat Dec 29 05:24:40 2012
@@ -0,0 +1,79 @@
+/*
+ * 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.FileReader;
+import java.io.IOException;
+
+import org.apache.commons.io.IOUtils;
+
+final class ClusterDumpReader {
+
+  private final int k, d;
+
+  /**
+   * 
+   * @param k number of clusters
+   * @param d number of dimensions
+   */
+  public ClusterDumpReader(int k, int d){
+    this.k = k;
+    this.d = d;
+  }
+  
+  public double[][] getCentroids(String ifile) throws IOException {
+    double[][] centroids = new double[k][d];
+
+    FileReader fr = null;
+    BufferedReader br = null;
+
+    try{
+      fr = new FileReader(ifile);
+      br = new BufferedReader(fr);
+      String line = null;
+      int i = 0;
+      while((line = br.readLine()) != null){
+        int sp = line.indexOf("c=[") + "c=[".length();
+        int ep = line.indexOf("] r=[");
+        //System.out.printf("\"%s\"\n", line.substring(sp, ep));
+        String[] strValues = line.substring(sp, ep).trim().split(",\\s*");
+        if(strValues.length < d){
+          // may be sparse vector representation is used...
+          for(String sv : strValues){
+            int col = sv.indexOf(':');
+            int j = Integer.parseInt(sv.substring(0, col));
+            centroids[i][j] = Double.parseDouble(sv.substring(col + 1));
+          }
+        }
+        else{
+          for(int j = 0; j < d; j++){
+            centroids[i][j] = Double.parseDouble(strValues[j]);
+          }
+        }
+        i++;
+      }
+    }
+    finally{
+      IOUtils.closeQuietly(br);
+      IOUtils.closeQuietly(fr);
+    }
+    
+    return centroids;
+  }
+}

Added: labs/alike/trunk/src/java/org/apache/alike/DistanceCalculator.java
URL: http://svn.apache.org/viewvc/labs/alike/trunk/src/java/org/apache/alike/DistanceCalculator.java?rev=1426708&view=auto
==============================================================================
--- labs/alike/trunk/src/java/org/apache/alike/DistanceCalculator.java (added)
+++ labs/alike/trunk/src/java/org/apache/alike/DistanceCalculator.java Sat Dec 29 05:24:40 2012
@@ -0,0 +1,23 @@
+/*
+ * 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;
+
+public abstract class DistanceCalculator {
+
+  public abstract double compute(double[] centroid, double[] desc);
+}

Added: labs/alike/trunk/src/java/org/apache/alike/EuclideanDistanceCalculator.java
URL: http://svn.apache.org/viewvc/labs/alike/trunk/src/java/org/apache/alike/EuclideanDistanceCalculator.java?rev=1426708&view=auto
==============================================================================
--- labs/alike/trunk/src/java/org/apache/alike/EuclideanDistanceCalculator.java (added)
+++ labs/alike/trunk/src/java/org/apache/alike/EuclideanDistanceCalculator.java Sat Dec 29 05:24:40 2012
@@ -0,0 +1,30 @@
+/*
+ * 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;
+
+public class EuclideanDistanceCalculator extends DistanceCalculator {
+
+  public double compute(double[] centroid, double[] desc) {
+    int D = centroid.length;
+    double sum = 0;
+    for(int i = 0; i < D; i++){
+      sum += (centroid[i] - desc[i]) * (centroid[i] - desc[i]);
+    }
+    return Math.sqrt(sum);
+  }
+}

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=1426708&r1=1426707&r2=1426708&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 05:24:40 2012
@@ -33,6 +33,13 @@ import org.apache.commons.io.IOUtils;
 
 /**
  * This program quantize clustered visual descriptors.
+ * Executing {@link KMeansLauncher} and {@link ClusterDumperLauncher} multiple times,
+ * then {@link FindOptimalCluster} to choose the best suited cluster is recommended
+ * before using this program.
+ * 
+ * @see KMeansLauncher
+ * @see ClusterDumperLauncher
+ * @see FindOptimalCluster
  *
  */
 public class QuantizeVectors {
@@ -44,7 +51,7 @@ public class QuantizeVectors {
    * The main program that takes the path to alikeconfig.xml as an argument.
    * 
    * @param args file path to alikeconfig.xml
-   * @throws IOException 
+   * @throws Exception 
    * 
    * @see {@link AlikeConfig}
    */
@@ -57,10 +64,11 @@ public class QuantizeVectors {
     
     K = config.getNumOfClusters();
     D = config.getNumOfDimensions();
+    ClusterDumpReader clusterDumpReader = new ClusterDumpReader(K, D);
     LuceneIndexerBase indexer = config.getLuceneIndexer();
 
     // read cluster centroids
-    double[][] centroids = getCentroids(config.getClusterDumpFile());
+    double[][] centroids = clusterDumpReader.getCentroids(config.getClusterDumpFile());
     
     // make histograms
     HistogramExecutor executor =
@@ -81,46 +89,6 @@ public class QuantizeVectors {
     }
   }
   
-  static double[][] getCentroids(String ifile) throws IOException {
-    double[][] centroids = new double[K][D];
-
-    FileReader fr = null;
-    BufferedReader br = null;
-
-    try{
-      fr = new FileReader(ifile);
-      br = new BufferedReader(fr);
-      String line = null;
-      int i = 0;
-      while((line = br.readLine()) != null){
-        int sp = line.indexOf("c=[") + "c=[".length();
-        int ep = line.indexOf("] r=[");
-        //System.out.printf("\"%s\"\n", line.substring(sp, ep));
-        String[] strValues = line.substring(sp, ep).trim().split(",\\s*");
-        if(strValues.length < D){
-          // may be sparse vector representation is used...
-          for(String sv : strValues){
-            int col = sv.indexOf(':');
-            int j = Integer.parseInt(sv.substring(0, col));
-            centroids[i][j] = Double.parseDouble(sv.substring(col + 1));
-          }
-        }
-        else{
-          for(int j = 0; j < D; j++){
-            centroids[i][j] = Double.parseDouble(strValues[j]);
-          }
-        }
-        i++;
-      }
-    }
-    finally{
-      IOUtils.closeQuietly(br);
-      IOUtils.closeQuietly(fr);
-    }
-    
-    return centroids;
-  }
-  
   static class HistogramExecutor extends Executor {
     
     private double[][] centroids;



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org