final ThinSvdPrincipalComponentAnalysis pca = new ThinSvdPrincipalComponentAnalysis(64); pca.learnBasis(sample128); final double[][] sample64 = pca.project(new Matrix(sample128)).getArray(); for (final MemoryLocalFeatureList<FloatKeypoint> kpl : data) { for (final FloatKeypoint kp : kpl) { kp.vector = ArrayUtils.convertToFloat(pca.project(ArrayUtils.convertToDouble(kp.vector)));
final ThinSvdPrincipalComponentAnalysis pca = new ThinSvdPrincipalComponentAnalysis(ndims); pca.learnBasis(data); final double[] evs = pca.getEigenValues(); final double[] invStdDev = new double[ndims]; for (int i = 0; i < ndims; i++) invStdDev[i] = 1.0 / Math.sqrt(evs[i]); W = MatrixUtils.diag(invStdDev).times(pca.getBasis().transpose());
/** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { final File indir = new File("/Volumes/Raid/face_databases/lfw-centre-affine-pdsift/"); final Matrix samples = sample(indir, 100000); final ThinSvdPrincipalComponentAnalysis pca = new ThinSvdPrincipalComponentAnalysis(64); System.out.println("Performing PCA"); pca.learnBasis(samples); IOUtils.writeToFile(pca, new File("/Volumes/Raid/face_databases/lfw-centre-affine-pdsift-pca64.bin")); // final ThinSvdPrincipalComponentAnalysis pca = // IOUtils.readFromFile(new File( // "/Volumes/Raid/face_databases/lfw-centre-affine-pdsift-pca64.bin")); processFiles(pca, indir, new File("/Volumes/Raid/face_databases/lfw-centre-affine-pdsift-pca64" + "/")); } }
/** * Construct with the given number of principal components. * * @param numComponents * the number of PCs */ public EigenImages(int numComponents) { this.numComponents = numComponents; pca = new FeatureVectorPCA(new ThinSvdPrincipalComponentAnalysis(numComponents)); }
@Override public void perform(File f) { final Matrix basis = pca.getBasis(); final Matrix tmp = new Matrix(1, 128); if (f.getName().endsWith(".bin")) { try { System.out.println("Processing " + f); final MemoryLocalFeatureList<FloatDSIFTKeypoint> feats = MemoryLocalFeatureList.read(f, FloatDSIFTKeypoint.class); final File outfile = new File(outdir, f.getAbsolutePath().replace( indir.getAbsolutePath(), "")); outfile.getParentFile().mkdirs(); for (final FloatDSIFTKeypoint kpt : feats) { tmp.getArray()[0] = ArrayUtils.convertToDouble(kpt.descriptor); final double[] proj = tmp.times(basis).getArray()[0]; kpt.descriptor = ArrayUtils.convertToFloat(proj); kpt.descriptor = Arrays.copyOf(kpt.descriptor, kpt.descriptor.length + 2); kpt.descriptor[kpt.descriptor.length - 2] = (kpt.x / 125f) - 0.5f; kpt.descriptor[kpt.descriptor.length - 1] = (kpt.y / 160f) - 0.5f; } IOUtils.writeBinary(outfile, feats); f.delete(); } catch (final Exception e) { } } } });
/** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { final File indir = new File("/Volumes/Raid/face_databases/lfw-centre-affine-pdsift/"); final Matrix samples = sample(indir, 100000); final ThinSvdPrincipalComponentAnalysis pca = new ThinSvdPrincipalComponentAnalysis(64); System.out.println("Performing PCA"); pca.learnBasis(samples); IOUtils.writeToFile(pca, new File("/Volumes/Raid/face_databases/lfw-centre-affine-pdsift-pca64.bin")); // final ThinSvdPrincipalComponentAnalysis pca = // IOUtils.readFromFile(new File( // "/Volumes/Raid/face_databases/lfw-centre-affine-pdsift-pca64.bin")); processFiles(pca, indir, new File("/Volumes/Raid/face_databases/lfw-centre-affine-pdsift-pca64" + "/")); } }
/** * Construct with the given number of principal components. * * @param numComponents * the number of PCs */ public EigenImages(int numComponents) { this.numComponents = numComponents; pca = new FeatureVectorPCA(new ThinSvdPrincipalComponentAnalysis(numComponents)); }
@Override public void perform(File f) { final Matrix basis = pca.getBasis(); final Matrix tmp = new Matrix(1, 128); if (f.getName().endsWith(".bin")) { try { System.out.println("Processing " + f); final MemoryLocalFeatureList<FloatDSIFTKeypoint> feats = MemoryLocalFeatureList.read(f, FloatDSIFTKeypoint.class); final File outfile = new File(outdir, f.getAbsolutePath().replace( indir.getAbsolutePath(), "")); outfile.getParentFile().mkdirs(); for (final FloatDSIFTKeypoint kpt : feats) { tmp.getArray()[0] = ArrayUtils.convertToDouble(kpt.descriptor); final double[] proj = tmp.times(basis).getArray()[0]; kpt.descriptor = ArrayUtils.convertToFloat(proj); kpt.descriptor = Arrays.copyOf(kpt.descriptor, kpt.descriptor.length + 2); kpt.descriptor[kpt.descriptor.length - 2] = (kpt.x / 125f) - 0.5f; kpt.descriptor[kpt.descriptor.length - 1] = (kpt.y / 160f) - 0.5f; } IOUtils.writeBinary(outfile, feats); f.delete(); } catch (final Exception e) { } } } });
final ThinSvdPrincipalComponentAnalysis pca = new ThinSvdPrincipalComponentAnalysis(64); pca.learnBasis(sample128); final double[][] sample64 = pca.project(new Matrix(sample128)).getArray(); kp.vector = ArrayUtils.convertToFloat(pca.project(ArrayUtils.convertToDouble(kp.vector)));
@Override public void train(List<? extends IndependentPair<?, FImage>> data) { width = data.get(0).secondObject().width; height = data.get(0).secondObject().height; final Map<Object, List<double[]>> mapData = new HashMap<Object, List<double[]>>(); final List<double[]> listData = new ArrayList<double[]>(); for (final IndependentPair<?, FImage> item : data) { List<double[]> fvs = mapData.get(item.firstObject()); if (fvs == null) mapData.put(item.firstObject(), fvs = new ArrayList<double[]>()); final double[] fv = FImage2DoubleFV.INSTANCE.extractFeature(item.getSecondObject()).values; fvs.add(fv); listData.add(fv); } final PrincipalComponentAnalysis pca = new ThinSvdPrincipalComponentAnalysis(numComponents); pca.learnBasis(listData); final List<double[][]> ldaData = new ArrayList<double[][]>(mapData.size()); for (final Entry<?, List<double[]>> e : mapData.entrySet()) { final List<double[]> vecs = e.getValue(); final double[][] classData = new double[vecs.size()][]; for (int i = 0; i < classData.length; i++) { classData[i] = pca.project(vecs.get(i)); } ldaData.add(classData); } final LinearDiscriminantAnalysis lda = new LinearDiscriminantAnalysis(numComponents); lda.learnBasis(ldaData); basis = pca.getBasis().times(lda.getBasis()); mean = pca.getMean(); }
final ThinSvdPrincipalComponentAnalysis pca = new ThinSvdPrincipalComponentAnalysis(64); pca.learnBasis(sample128); final double[][] sample64 = pca.project(new Matrix(sample128)).getArray(); kp.vector = ArrayUtils.convertToFloat(pca.project(ArrayUtils.convertToDouble(kp.vector)));
@Override public void train(List<? extends IndependentPair<?, FImage>> data) { width = data.get(0).secondObject().width; height = data.get(0).secondObject().height; final Map<Object, List<double[]>> mapData = new HashMap<Object, List<double[]>>(); final List<double[]> listData = new ArrayList<double[]>(); for (final IndependentPair<?, FImage> item : data) { List<double[]> fvs = mapData.get(item.firstObject()); if (fvs == null) mapData.put(item.firstObject(), fvs = new ArrayList<double[]>()); final double[] fv = FImage2DoubleFV.INSTANCE.extractFeature(item.getSecondObject()).values; fvs.add(fv); listData.add(fv); } final PrincipalComponentAnalysis pca = new ThinSvdPrincipalComponentAnalysis(numComponents); pca.learnBasis(listData); final List<double[][]> ldaData = new ArrayList<double[][]>(mapData.size()); for (final Entry<?, List<double[]>> e : mapData.entrySet()) { final List<double[]> vecs = e.getValue(); final double[][] classData = new double[vecs.size()][]; for (int i = 0; i < classData.length; i++) { classData[i] = pca.project(vecs.get(i)); } ldaData.add(classData); } final LinearDiscriminantAnalysis lda = new LinearDiscriminantAnalysis(numComponents); lda.learnBasis(ldaData); basis = pca.getBasis().times(lda.getBasis()); mean = pca.getMean(); }
final FeatureVectorPCA pca = new FeatureVectorPCA(new ThinSvdPrincipalComponentAnalysis(numPcaDims)); pca.learnBasis(vlads);