@Override public Result cluster(DataSource<byte[]> ds) { try { Result result = cluster(ds, conf.K); result.nn = conf.factory.create(result.centroids); return result; } catch (Exception e) { throw new RuntimeException(e); } }
@Override public Result cluster(byte[][] data) { DataSource<byte[]> ds = new ByteArrayBackedDataSource(data, rng); try { Result result = cluster(ds, conf.K); result.nn = conf.factory.create(result.centroids); return result; } catch (Exception e) { throw new RuntimeException(e); } }
/** * Main clustering algorithm. A number of threads as specified are * started each containing an assignment job and a reference to * the same set of ByteNearestNeighbours object (i.e. Exact or KDTree). * Each thread is added to a job pool and started in parallel. * A single accumulator is shared between all threads and locked on update. * <br/> * This methods expects that the initial centroids have already been set in * the <code>result</code> object and as such <strong>ignores</strong> the * init object. <strong>In normal operation you should call one of the other <code>cluster</code> * cluster methods instead of this one.</strong> However, if you wish to resume clustering * iterations from a result that you've already generated this is the method * to use. * * @param data the data to be clustered * @param result the results object to be populated * @throws InterruptedException if interrupted while waiting, in * which case unfinished tasks are cancelled. */ public void cluster(byte[][] data, Result result) throws InterruptedException { DataSource<byte[]> ds = new ByteArrayBackedDataSource(data, rng); cluster(ds, result); }
private static HardAssigner<byte[], float[], IntFloatPair> trainQuantiser( GroupedDataset<String, ListDataset<Record<FImage>>, Record<FImage>> sample, PyramidDenseSIFT<FImage> pdsift) { List<LocalFeatureList<ByteDSIFTKeypoint>> allkeys = new ArrayList<LocalFeatureList<ByteDSIFTKeypoint>>(); for (final Record<FImage> rec : sample) { final FImage img = rec.getImage(); pdsift.analyseImage(img); allkeys.add(pdsift.getByteKeypoints(0.005f)); } if (allkeys.size() > 10000) allkeys = allkeys.subList(0, 10000); final ByteKMeans km = ByteKMeans.createKDTreeEnsemble(300); final DataSource<byte[]> datasource = new LocalFeatureListDataSource<ByteDSIFTKeypoint, byte[]>(allkeys); final ByteCentroidsResult result = km.cluster(datasource); return result.defaultHardAssigner(); } }
/** * Initiate clustering with the given data and number of clusters. * Internally this method constructs the array to hold the centroids * and calls {@link #cluster(DataSource, byte [][])}. * * @param data data source to cluster with * @param K number of clusters to find * @return cluster centroids */ protected Result cluster(DataSource<byte[]> data, int K) throws Exception { int D = data.numDimensions(); Result result = new Result(); result.centroids = new byte[K][D]; init.initKMeans(data, result.centroids); cluster(data, result); return result; }
final ByteNearestNeighboursProvider centroids = (ByteNearestNeighboursProvider) kmeans.cluster(tmp);
@Override public int[][] performClustering(byte[][] data) { ByteCentroidsResult clusters = this.cluster(data); return new IndexClusters(clusters.defaultHardAssigner().assign(data)).clusters(); }
final ByteCentroidsResult result = kmeans.cluster(datasource);
/** * Compute HierarchicalByteKMeans clustering. * * @param data Data to cluster. * @param K Number of clusters for this node. * @param height Tree height. * * @return a new HierarchicalByteKMeans node representing a sub-clustering. **/ private Node trainLevel(final byte[][] data, int K, int height) { Node node = new Node(); node.children = (height == 1) ? null : new Node[K]; ByteKMeans kmeans = newByteKMeans(K); node.result = kmeans.cluster(data); HardAssigner<byte[], float[], IntFloatPair> assigner = node.result.defaultHardAssigner(); if (height > 1) { int[] ids = assigner.assign(data); for (int k = 0; k < K; k++) { byte[][] partition = extractSubset(data, ids, k); int partitionK = Math.min(K, partition.length); node.children[k] = trainLevel(partition, partitionK, height - 1); } } return node; }
final ByteCentroidsResult result = kmeans.cluster(data);
@Override public SpatialClusters<?> create(List<SampleBatch> batches) throws Exception { System.err.println("Constructing a FASTKMEANS cluster"); SpatialClusterer<?, ?> c = null; System.err.println("Constructing a fastkmeans worker: "); if (this.precision == Precision.BYTE) { final SampleBatchByteDataSource ds = new SampleBatchByteDataSource(batches); ds.setSeed(seed); c = new ByteKMeans(confByte(ds.numDimensions())); ((ByteKMeans) c).seed(seed); clusterInitOp.setClusterInit((ByteKMeans) c); return ((ByteKMeans) c).cluster(ds); } else { final SampleBatchIntDataSource ds = new SampleBatchIntDataSource(batches); ds.setSeed(seed); c = new IntKMeans(confInt(ds.numDimensions())); ((IntKMeans) c).seed(seed); return ((IntKMeans) c).cluster(ds); } }
@Override public SpatialClusters<?> create(byte[][] data) throws Exception { SpatialClusterer<?, ?> c = null; if (this.precision == Precision.BYTE) { c = new ByteKMeans(confByte(data[0].length)); ((ByteKMeans) c).seed(seed); if (clusterInitOp == null) clusterInitOp = clusterInit.getOptions(); clusterInitOp.setClusterInit((ByteKMeans) c); return ((ByteKMeans) c).cluster(data); } else { c = new IntKMeans(confInt(data[0].length)); ((IntKMeans) c).seed(seed); return ((IntKMeans) c).cluster(ByteArrayConverter.byteToInt(data)); } }
node.result = kmeans.cluster(data);