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.ArrayList;
35 import java.util.Collections;
36 import java.util.List;
37
38 import org.openimaj.feature.local.list.MemoryLocalFeatureList;
39 import org.openimaj.image.feature.local.aggregate.FisherVector;
40 import org.openimaj.image.feature.local.keypoints.FloatKeypoint;
41 import org.openimaj.image.feature.local.keypoints.Keypoint;
42 import org.openimaj.math.matrix.algorithm.pca.ThinSvdPrincipalComponentAnalysis;
43 import org.openimaj.math.statistics.distribution.MixtureOfGaussians;
44 import org.openimaj.ml.gmm.GaussianMixtureModelEM;
45 import org.openimaj.ml.gmm.GaussianMixtureModelEM.CovarianceType;
46 import org.openimaj.util.array.ArrayUtils;
47
48 import Jama.Matrix;
49
50 public class FisherTesting {
51 public static void main(String[] args) throws IOException {
52 final List<MemoryLocalFeatureList<FloatKeypoint>> data = new ArrayList<MemoryLocalFeatureList<FloatKeypoint>>();
53 final List<FloatKeypoint> allKeys = new ArrayList<FloatKeypoint>();
54
55 for (int i = 0; i < 100; i++) {
56 final MemoryLocalFeatureList<FloatKeypoint> tmp = FloatKeypoint.convert(MemoryLocalFeatureList.read(
57 new File(String.format("/Users/jsh2/Data/ukbench/sift/ukbench%05d.jpg", i)), Keypoint.class));
58 data.add(tmp);
59 allKeys.addAll(tmp);
60 }
61
62 Collections.shuffle(allKeys);
63
64 final double[][] sample128 = new double[1000][];
65 for (int i = 0; i < sample128.length; i++) {
66 sample128[i] = ArrayUtils.convertToDouble(allKeys.get(i).vector);
67 }
68
69 System.out.println("Performing PCA " + sample128.length);
70 final ThinSvdPrincipalComponentAnalysis pca = new ThinSvdPrincipalComponentAnalysis(64);
71 pca.learnBasis(sample128);
72 final double[][] sample64 = pca.project(new Matrix(sample128)).getArray();
73
74 System.out.println("Projecting features");
75 for (final MemoryLocalFeatureList<FloatKeypoint> kpl : data) {
76 for (final FloatKeypoint kp : kpl) {
77 kp.vector = ArrayUtils.convertToFloat(pca.project(ArrayUtils.convertToDouble(kp.vector)));
78 }
79 }
80
81 System.out.println("Learning GMM " + sample64.length);
82 final GaussianMixtureModelEM gmmem = new GaussianMixtureModelEM(512, CovarianceType.Diagonal);
83 final MixtureOfGaussians gmm = gmmem.estimate(sample64);
84
85 final double[][] v1 = gmm.logProbability(new double[][] { sample64[0] });
86 final double[][] v2 = MixtureOfGaussians.logProbability(new double[][] { sample64[0] }, gmm.gaussians);
87
88 System.out.println("Done");
89
90
91
92
93
94
95 final FisherVector<float[]> fisher = new FisherVector<float[]>(gmm, true, true);
96
97 final List<FloatKeypoint> kpl = allKeys.subList(0, 26000);
98 final long t1 = System.currentTimeMillis();
99 fisher.aggregate(kpl).asDoubleVector();
100 final long t2 = System.currentTimeMillis();
101 System.out.println(t2 - t1);
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 }
119 }