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.demos;
031
032import java.io.File;
033import java.io.IOException;
034import java.util.Arrays;
035
036import org.openimaj.feature.FloatFV;
037import org.openimaj.feature.local.list.MemoryLocalFeatureList;
038import org.openimaj.image.feature.dense.gradient.dsift.FloatDSIFTKeypoint;
039import org.openimaj.image.feature.local.aggregate.FisherVector;
040import org.openimaj.math.statistics.distribution.DiagonalMultivariateGaussian;
041import org.openimaj.math.statistics.distribution.MixtureOfGaussians;
042import org.openimaj.math.statistics.distribution.MultivariateGaussian;
043
044import com.jmatio.io.MatFileReader;
045import com.jmatio.io.MatFileWriter;
046import com.jmatio.types.MLArray;
047import com.jmatio.types.MLDouble;
048import com.jmatio.types.MLSingle;
049import com.jmatio.types.MLStructure;
050
051/**
052 * 
053 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
054 */
055public class FVFWCheckGMM {
056
057        private static final String GMM_MATLAB_FILE = "/Users/ss/Experiments/FVFW/data/gmm_512.mat";
058        private static final String[] FACE_DSIFTS_PCA = new String[] {
059                        "/Users/ss/Experiments/FVFW/data/aaron-pcadsiftaug.mat"
060        };
061
062        public static void main(String[] args) throws IOException {
063                final MixtureOfGaussians mog = loadMoG();
064                final FisherVector<float[]> fisher = new FisherVector<float[]>(mog, true, true);
065                for (final String faceFile : FACE_DSIFTS_PCA) {
066                        final MemoryLocalFeatureList<FloatDSIFTKeypoint> loadDSIFTPCA = loadDSIFTPCA(faceFile);
067
068                        final FloatFV fvec = fisher.aggregate(loadDSIFTPCA);
069                        System.out.println(String.format("%s: %s", faceFile, fvec));
070                        System.out.println("Writing...");
071
072                        final File out = new File(faceFile + ".fisher.mat");
073                        final MLArray data = toMLArray(fvec);
074                        new MatFileWriter(out, Arrays.asList(data));
075                }
076        }
077
078        private static MemoryLocalFeatureList<FloatDSIFTKeypoint> loadDSIFTPCA(String faceFile) throws IOException {
079                final File f = new File(faceFile);
080                final MatFileReader reader = new MatFileReader(f);
081                final MLSingle feats = (MLSingle) reader.getContent().get("feats");
082                final int nfeats = feats.getN();
083                final MemoryLocalFeatureList<FloatDSIFTKeypoint> ret = new MemoryLocalFeatureList<FloatDSIFTKeypoint>();
084                for (int i = 0; i < nfeats; i++) {
085                        final FloatDSIFTKeypoint feature = new FloatDSIFTKeypoint();
086                        feature.descriptor = new float[feats.getM()];
087                        for (int j = 0; j < feature.descriptor.length; j++) {
088                                feature.descriptor[j] = feats.get(j, i);
089                        }
090                        ret.add(feature);
091                }
092
093                return ret;
094        }
095
096        private static MLArray toMLArray(FloatFV fvec) {
097                final MLDouble data = new MLDouble("fisherface", new int[] { fvec.values.length, 1 });
098                for (int i = 0; i < fvec.values.length; i++) {
099                        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}