001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.tools.clusterquantiser.fastkmeans; 031 032import java.io.File; 033import java.io.IOException; 034 035import org.kohsuke.args4j.CmdLineOptionsProvider; 036import org.kohsuke.args4j.Option; 037import org.openimaj.data.DataSource; 038import org.openimaj.io.IOUtils; 039import org.openimaj.ml.clustering.ByteCentroidsResult; 040import org.openimaj.ml.clustering.kmeans.ByteKMeans; 041import org.openimaj.ml.clustering.kmeans.ByteKMeansInit; 042 043/** 044 * Initialisation options for k-means. 045 * 046 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 047 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 048 * 049 */ 050public enum ByteKMeansInitialisers implements CmdLineOptionsProvider { 051 /** 052 * Randomly sampled points to start 053 */ 054 RANDOM { 055 @Override 056 public Options getOptions() { 057 return new RandomOptions(); 058 } 059 }, 060 /** 061 * Start from provided centroids 062 */ 063 RANDOMSETCLUSTER { 064 @Override 065 public Options getOptions() { 066 return new RandomSetClusterOptions(); 067 } 068 }; 069 070 @Override 071 public abstract Options getOptions(); 072 073 /** 074 * Base options for FastByteKMeansInitialisers types 075 * 076 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 077 * 078 */ 079 public abstract class Options { 080 /** 081 * Initialise the clusterer 082 * 083 * @param fkmb 084 * @throws Exception 085 */ 086 public abstract void setClusterInit(ByteKMeans fkmb) throws Exception; 087 } 088 089 class RandomOptions extends Options { 090 @Override 091 public void setClusterInit(ByteKMeans fkmb) { 092 fkmb.setInit(new ByteKMeansInit.RANDOM()); 093 } 094 } 095 096 class RandomSetClusterOptions extends Options { 097 @Option(name = "--random-set-source", aliases = "-rss", required = true, usage = "Specify the random set source") 098 private File randomSetSource = null; 099 100 @Override 101 public void setClusterInit(ByteKMeans fkmb) throws IOException { 102 class RANDOMSETINIT extends ByteKMeansInit { 103 private File f; 104 105 public RANDOMSETINIT(File f) { 106 this.f = f; 107 } 108 109 @Override 110 public void initKMeans(DataSource<byte[]> bds, byte[][] clusters) throws IOException { 111 System.out.println("...Loading RANDOMSET cluster for FASTKMEANS init"); 112 final ByteCentroidsResult rsbc = IOUtils.read(f, ByteCentroidsResult.class); 113 final byte[][] toBeCopied = rsbc.getCentroids(); 114 for (int i = 0; i < clusters.length; i++) { 115 // If the random set cluster is too small for this 116 // cluster, pad the remaining space with random entries 117 // from the data source 118 if (i > toBeCopied.length) { 119 final int remaining = clusters.length - i; 120 final byte[][] padding = new byte[remaining][bds.numDimensions()]; 121 bds.getRandomRows(padding); 122 for (int j = 0; j < padding.length; j++) { 123 System.arraycopy(padding[j], 0, clusters[i + j], 0, padding[j].length); 124 } 125 break; 126 } 127 128 System.arraycopy(toBeCopied[i], 0, clusters[i], 0, toBeCopied[i].length); 129 } 130 System.out.println("...Done"); 131 } 132 } 133 134 fkmb.setInit(new RANDOMSETINIT(randomSetSource)); 135 } 136 } 137}