1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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 }