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.demos;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.util.Arrays;
35  
36  import org.openimaj.feature.FloatFV;
37  import org.openimaj.feature.local.list.MemoryLocalFeatureList;
38  import org.openimaj.image.feature.dense.gradient.dsift.FloatDSIFTKeypoint;
39  import org.openimaj.image.feature.local.aggregate.FisherVector;
40  import org.openimaj.math.statistics.distribution.DiagonalMultivariateGaussian;
41  import org.openimaj.math.statistics.distribution.MixtureOfGaussians;
42  import org.openimaj.math.statistics.distribution.MultivariateGaussian;
43  
44  import com.jmatio.io.MatFileReader;
45  import com.jmatio.io.MatFileWriter;
46  import com.jmatio.types.MLArray;
47  import com.jmatio.types.MLDouble;
48  import com.jmatio.types.MLSingle;
49  import com.jmatio.types.MLStructure;
50  
51  /**
52   * 
53   * @author Sina Samangooei (ss@ecs.soton.ac.uk)
54   */
55  public class FVFWCheckGMM {
56  
57  	private static final String GMM_MATLAB_FILE = "/Users/ss/Experiments/FVFW/data/gmm_512.mat";
58  	private static final String[] FACE_DSIFTS_PCA = new String[] {
59  			"/Users/ss/Experiments/FVFW/data/aaron-pcadsiftaug.mat"
60  	};
61  
62  	public static void main(String[] args) throws IOException {
63  		final MixtureOfGaussians mog = loadMoG();
64  		final FisherVector<float[]> fisher = new FisherVector<float[]>(mog, true, true);
65  		for (final String faceFile : FACE_DSIFTS_PCA) {
66  			final MemoryLocalFeatureList<FloatDSIFTKeypoint> loadDSIFTPCA = loadDSIFTPCA(faceFile);
67  
68  			final FloatFV fvec = fisher.aggregate(loadDSIFTPCA);
69  			System.out.println(String.format("%s: %s", faceFile, fvec));
70  			System.out.println("Writing...");
71  
72  			final File out = new File(faceFile + ".fisher.mat");
73  			final MLArray data = toMLArray(fvec);
74  			new MatFileWriter(out, Arrays.asList(data));
75  		}
76  	}
77  
78  	private static MemoryLocalFeatureList<FloatDSIFTKeypoint> loadDSIFTPCA(String faceFile) throws IOException {
79  		final File f = new File(faceFile);
80  		final MatFileReader reader = new MatFileReader(f);
81  		final MLSingle feats = (MLSingle) reader.getContent().get("feats");
82  		final int nfeats = feats.getN();
83  		final MemoryLocalFeatureList<FloatDSIFTKeypoint> ret = new MemoryLocalFeatureList<FloatDSIFTKeypoint>();
84  		for (int i = 0; i < nfeats; i++) {
85  			final FloatDSIFTKeypoint feature = new FloatDSIFTKeypoint();
86  			feature.descriptor = new float[feats.getM()];
87  			for (int j = 0; j < feature.descriptor.length; j++) {
88  				feature.descriptor[j] = feats.get(j, i);
89  			}
90  			ret.add(feature);
91  		}
92  
93  		return ret;
94  	}
95  
96  	private static MLArray toMLArray(FloatFV fvec) {
97  		final MLDouble data = new MLDouble("fisherface", new int[] { fvec.values.length, 1 });
98  		for (int i = 0; i < fvec.values.length; i++) {
99  			data.set((double) fvec.values[i], i, 0);
100 		}
101 		return data;
102 	}
103 
104 	private static MixtureOfGaussians loadMoG() throws IOException {
105 		final File f = new File(GMM_MATLAB_FILE);
106 		final MatFileReader reader = new MatFileReader(f);
107 		final MLStructure codebook = (MLStructure) reader.getContent().get("codebook");
108 
109 		final MLSingle mean = (MLSingle) codebook.getField("mean");
110 		final MLSingle variance = (MLSingle) codebook.getField("variance");
111 		final MLSingle coef = (MLSingle) codebook.getField("coef");
112 
113 		final int n_gaussians = mean.getN();
114 		final int n_dims = mean.getM();
115 
116 		final MultivariateGaussian[] ret = new MultivariateGaussian[n_gaussians];
117 		final double[] weights = new double[n_gaussians];
118 		for (int i = 0; i < n_gaussians; i++) {
119 			weights[i] = coef.get(i, 0);
120 			final DiagonalMultivariateGaussian d = new DiagonalMultivariateGaussian(n_dims);
121 			for (int j = 0; j < n_dims; j++) {
122 				d.mean.set(0, j, mean.get(j, i));
123 				d.variance[j] = variance.get(j, i);
124 			}
125 			ret[i] = d;
126 		}
127 
128 		return new MixtureOfGaussians(ret, weights);
129 	}
130 
131 }