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.workinprogress.featlearn; 031 032import java.io.File; 033import java.io.IOException; 034import java.util.List; 035 036import org.openimaj.image.DisplayUtilities; 037import org.openimaj.image.FImage; 038import org.openimaj.image.processing.resize.ResizeProcessor; 039import org.openimaj.math.matrix.algorithm.whitening.WhiteningTransform; 040import org.openimaj.math.matrix.algorithm.whitening.ZCAWhitening; 041import org.openimaj.math.statistics.normalisation.PerExampleMeanCenterVar; 042import org.openimaj.ml.clustering.kmeans.SphericalKMeans; 043import org.openimaj.ml.clustering.kmeans.SphericalKMeansResult; 044 045public class Test { 046 public static void main(String[] args) throws IOException { 047 final File patchesFile = new File("patches.bin"); 048 049 // final RandomPatchSampler sampler = 050 // new 051 // RandomPatchSampler(Caltech101.getImages(ImageUtilities.FIMAGE_READER), 052 // 8, 8, 100000); 053 // sampler.save(patchesFile); 054 final List<FImage> patches = RandomPatchSampler.loadPatches(patchesFile); 055 056 final double[][] data = new double[patches.size()][]; 057 for (int i = 0; i < data.length; i++) 058 data[i] = patches.get(i).getDoublePixelVector(); 059 060 // final PCAWhitening whitening = new PCAWhitening(); 061 final WhiteningTransform whitening = new ZCAWhitening(0.1, new PerExampleMeanCenterVar(10f / 255f)); 062 whitening.train(data); 063 final double[][] wd = whitening.whiten(data); 064 065 // final double[][] comps = 066 // whitening.getTransform().transpose().getArray(); 067 // for (int i = 0; i < comps.length; i++) 068 // DisplayUtilities.di play(ResizeProcessor.resample(new 069 // FImage(comps[i], 8, 8).normalise(), 128, 128)); 070 071 // final FImage tmp1 = new FImage(100 * 8, 100 * 8); 072 // final FImage tmp2 = new FImage(100 * 8, 100 * 8); 073 // final FImage tmp3 = new FImage(100 * 8, 100 * 8); 074 // for (int i = 0; i < 100; i++) { 075 // for (int j = 0; j < 100; j++) { 076 // final double[] d = new PerExampleMeanCenterVar(10f / 077 // 255f).normalise(patches.get(i * 100 + j) 078 // .getDoublePixelVector()); 079 // FImage patch = new FImage(d, 8, 8); 080 // patch.divideInplace(2 * Math.max(patch.min(), patch.max())); 081 // patch.addInplace(0.5f); 082 // tmp2.drawImage(patch, i * 8, j * 8); 083 // 084 // tmp3.drawImage(patches.get(i * 100 + j), i * 8, j * 8); 085 // 086 // patch = new FImage(wd[i * 100 + j], 8, 8); 087 // patch.divideInplace(2 * Math.max(patch.min(), patch.max())); 088 // patch.addInplace(0.5f); 089 // tmp1.drawImage(patch, i * 8, j * 8); 090 // } 091 // } 092 // DisplayUtilities.display(tmp3); 093 // DisplayUtilities.display(tmp2); 094 // DisplayUtilities.display(tmp1); 095 096 final SphericalKMeans skm = new SphericalKMeans(2500, 10); 097 final SphericalKMeansResult res = skm.cluster(wd); 098 final FImage tmp = new FImage(50 * (8 + 1) + 1, 50 * (8 + 1) + 1); 099 tmp.fill(1f); 100 for (int i = 0; i < 50; i++) { 101 for (int j = 0; j < 50; j++) { 102 final FImage patch = ResizeProcessor 103 .resample( 104 new FImage(res.centroids[i * 50 + j], 8, 8), 105 8, 8); 106 patch.divideInplace(2 * Math.max(Math.abs(patch.min()), 107 Math.abs(patch.max()))); 108 patch.addInplace(0.5f); 109 tmp.drawImage(patch, i * (8 + 1) + 1, j * (8 + 1) + 1); 110 } 111 } 112 DisplayUtilities.display(tmp); 113 } 114}