View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.workinprogress.featlearn;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.util.List;
35  
36  import org.openimaj.image.DisplayUtilities;
37  import org.openimaj.image.FImage;
38  import org.openimaj.image.processing.resize.ResizeProcessor;
39  import org.openimaj.math.matrix.algorithm.whitening.WhiteningTransform;
40  import org.openimaj.math.matrix.algorithm.whitening.ZCAWhitening;
41  import org.openimaj.math.statistics.normalisation.PerExampleMeanCenterVar;
42  import org.openimaj.ml.clustering.kmeans.SphericalKMeans;
43  import org.openimaj.ml.clustering.kmeans.SphericalKMeansResult;
44  
45  public class Test {
46  	public static void main(String[] args) throws IOException {
47  		final File patchesFile = new File("patches.bin");
48  
49  		// final RandomPatchSampler sampler =
50  		// new
51  		// RandomPatchSampler(Caltech101.getImages(ImageUtilities.FIMAGE_READER),
52  		// 8, 8, 100000);
53  		// sampler.save(patchesFile);
54  		final List<FImage> patches = RandomPatchSampler.loadPatches(patchesFile);
55  
56  		final double[][] data = new double[patches.size()][];
57  		for (int i = 0; i < data.length; i++)
58  			data[i] = patches.get(i).getDoublePixelVector();
59  
60  		// final PCAWhitening whitening = new PCAWhitening();
61  		final WhiteningTransform whitening = new ZCAWhitening(0.1, new PerExampleMeanCenterVar(10f / 255f));
62  		whitening.train(data);
63  		final double[][] wd = whitening.whiten(data);
64  
65  		// final double[][] comps =
66  		// whitening.getTransform().transpose().getArray();
67  		// for (int i = 0; i < comps.length; i++)
68  		// DisplayUtilities.di play(ResizeProcessor.resample(new
69  		// FImage(comps[i], 8, 8).normalise(), 128, 128));
70  
71  		// final FImage tmp1 = new FImage(100 * 8, 100 * 8);
72  		// final FImage tmp2 = new FImage(100 * 8, 100 * 8);
73  		// final FImage tmp3 = new FImage(100 * 8, 100 * 8);
74  		// for (int i = 0; i < 100; i++) {
75  		// for (int j = 0; j < 100; j++) {
76  		// final double[] d = new PerExampleMeanCenterVar(10f /
77  		// 255f).normalise(patches.get(i * 100 + j)
78  		// .getDoublePixelVector());
79  		// FImage patch = new FImage(d, 8, 8);
80  		// patch.divideInplace(2 * Math.max(patch.min(), patch.max()));
81  		// patch.addInplace(0.5f);
82  		// tmp2.drawImage(patch, i * 8, j * 8);
83  		//
84  		// tmp3.drawImage(patches.get(i * 100 + j), i * 8, j * 8);
85  		//
86  		// patch = new FImage(wd[i * 100 + j], 8, 8);
87  		// patch.divideInplace(2 * Math.max(patch.min(), patch.max()));
88  		// patch.addInplace(0.5f);
89  		// tmp1.drawImage(patch, i * 8, j * 8);
90  		// }
91  		// }
92  		// DisplayUtilities.display(tmp3);
93  		// DisplayUtilities.display(tmp2);
94  		// DisplayUtilities.display(tmp1);
95  
96  		final SphericalKMeans skm = new SphericalKMeans(2500, 10);
97  		final SphericalKMeansResult res = skm.cluster(wd);
98  		final FImage tmp = new FImage(50 * (8 + 1) + 1, 50 * (8 + 1) + 1);
99  		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 }