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.matrix.algorithm.pca.PrincipalComponentAnalysis;
41 import org.openimaj.math.matrix.algorithm.pca.ThinSvdPrincipalComponentAnalysis;
42 import org.openimaj.math.statistics.distribution.DiagonalMultivariateGaussian;
43 import org.openimaj.math.statistics.distribution.MixtureOfGaussians;
44 import org.openimaj.math.statistics.distribution.MultivariateGaussian;
45 import org.openimaj.util.array.ArrayUtils;
46
47 import Jama.Matrix;
48
49 import com.jmatio.io.MatFileReader;
50 import com.jmatio.io.MatFileWriter;
51 import com.jmatio.types.MLArray;
52 import com.jmatio.types.MLDouble;
53 import com.jmatio.types.MLSingle;
54 import com.jmatio.types.MLStructure;
55
56
57
58
59
60 public class FVFWCheckPCAGMM {
61
62 private static final String GMM_MATLAB_FILE = "/Users/ss/Experiments/FVFW/data/gmm_512.mat";
63 private static final String PCA_MATLAB_FILE = "/Users/ss/Experiments/FVFW/data/PCA_64.mat";
64 private static final String[] FACE_DSIFTS = new String[] {
65 "/Users/ss/Experiments/FVFW/data/Aaron_Eckhart_0001-pdfsift.bin"
66 };
67
68 public static void main(String[] args) throws IOException {
69 final MixtureOfGaussians mog = loadMoG(new File(GMM_MATLAB_FILE));
70 final PrincipalComponentAnalysis pca = loadPCA(new File(PCA_MATLAB_FILE));
71 final FisherVector<float[]> fisher = new FisherVector<float[]>(mog, true, true);
72 for (final String faceFile : FACE_DSIFTS) {
73 final MemoryLocalFeatureList<FloatDSIFTKeypoint> loadDSIFT = MemoryLocalFeatureList.read(new File(faceFile),
74 FloatDSIFTKeypoint.class);
75 projectPCA(loadDSIFT, pca);
76
77 final FloatFV fvec = fisher.aggregate(loadDSIFT);
78 System.out.println(String.format("%s: %s", faceFile, fvec));
79 System.out.println("Writing...");
80
81 final File out = new File(faceFile + ".fisher.mat");
82 final MLArray data = toMLArray(fvec);
83 new MatFileWriter(out, Arrays.asList(data));
84 }
85 }
86
87 private static MLArray toMLArray(FloatFV fvec) {
88 final MLDouble data = new MLDouble("fisherface", new int[] { fvec.values.length, 1 });
89 for (int i = 0; i < fvec.values.length; i++) {
90 data.set((double) fvec.values[i], i, 0);
91 }
92 return data;
93 }
94
95 private static void projectPCA(
96 MemoryLocalFeatureList<FloatDSIFTKeypoint> loadDSIFT,
97 PrincipalComponentAnalysis pca)
98 {
99 for (final FloatDSIFTKeypoint kp : loadDSIFT) {
100 kp.descriptor = ArrayUtils.convertToFloat(pca.project(ArrayUtils.convertToDouble(kp.descriptor)));
101 final int nf = kp.descriptor.length;
102 kp.descriptor = Arrays.copyOf(kp.descriptor, nf + 2);
103 kp.descriptor[nf] = (kp.x / 125f) - 0.5f;
104 kp.descriptor[nf + 1] = (kp.y / 160f) - 0.5f;
105 }
106 loadDSIFT.resetVecLength();
107 }
108
109 static class LoadedPCA extends ThinSvdPrincipalComponentAnalysis {
110
111 public LoadedPCA(Matrix basis, double[] mean) {
112 super(basis.getRowDimension());
113 this.basis = basis;
114 this.mean = mean;
115 }
116
117 }
118
119 public static PrincipalComponentAnalysis loadPCA(File f) throws IOException {
120 final MatFileReader reader = new MatFileReader(f);
121 final MLSingle mean = (MLSingle) reader.getContent().get("mu");
122 final MLSingle eigvec = (MLSingle) reader.getContent().get("proj");
123 final Matrix basis = new Matrix(eigvec.getM(), eigvec.getN());
124 final double[] meand = new double[eigvec.getN()];
125 for (int j = 0; j < eigvec.getN(); j++) {
126
127 meand[j] = 0;
128 for (int i = 0; i < eigvec.getM(); i++) {
129 basis.set(i, j, eigvec.get(i, j));
130 }
131 }
132 final PrincipalComponentAnalysis ret = new LoadedPCA(basis.transpose(), meand);
133 return ret;
134 }
135
136 public static MixtureOfGaussians loadMoG(File f) throws IOException {
137 final MatFileReader reader = new MatFileReader(f);
138 final MLStructure codebook = (MLStructure) reader.getContent().get("codebook");
139
140 final MLSingle mean = (MLSingle) codebook.getField("mean");
141 final MLSingle variance = (MLSingle) codebook.getField("variance");
142 final MLSingle coef = (MLSingle) codebook.getField("coef");
143
144 final int n_gaussians = mean.getN();
145 final int n_dims = mean.getM();
146
147 final MultivariateGaussian[] ret = new MultivariateGaussian[n_gaussians];
148 final double[] weights = new double[n_gaussians];
149 for (int i = 0; i < n_gaussians; i++) {
150 weights[i] = coef.get(i, 0);
151 final DiagonalMultivariateGaussian d = new DiagonalMultivariateGaussian(n_dims);
152 for (int j = 0; j < n_dims; j++) {
153 d.mean.set(0, j, mean.get(j, i));
154 d.variance[j] = variance.get(j, i);
155 }
156 ret[i] = d;
157 }
158
159 return new MixtureOfGaussians(ret, weights);
160 }
161
162 }