Пример #1
0
        public LDA(double[][] trainingSet, List<string> labels,int numOfComponents)
        {
            int n = trainingSet.Length; // sample size
            HashSet<String> tempSet = new HashSet<String>(labels);
            int c = tempSet.Count; // class size

            // process in PCA
            PCA pca = new PCA(trainingSet, labels, n - c);

            // classify
            double[][] meanTotal = new double[n - c][];
            for (int i = 0; i < n - c; i++)
            {
            meanTotal[i] = new double[1];
            }
            Dictionary<String, List<double[]>> dict = new Dictionary<String, List<double[]>>();
            List<projectedTrainingMatrix> pcaTrain = pca.getProjectedTrainingSet();
            for (int i = 0; i < pcaTrain.Count; i++) {
            String key = pcaTrain[i].label;
               meanTotal= meanTotal.Add(pcaTrain[i].matrix);
            if (!dict.ContainsKey(key)) {
                List<double[]> temp = new List<double[]>();

               temp.Add(pcaTrain[i].matrix.Transpose()[0]);
                dict.Add(key, temp);
            } else {
                List<double[]> temp = dict[key];
                temp.Add(pcaTrain[i].matrix.Transpose()[0]);
                dict[key]= temp;
            }
            }
            meanTotal.ToMatrix().Multiply((double) 1 / n);

            // calculate Sw, Sb
            double[][] Sw = new double[n - c][];
            double[][] Sb = new double[n - c][];
            for (int i = 0; i < n - c; i++)
            {
            Sw[i] = new double[n-c];
            Sb[i] = new double[n-c];
            }
            List<String> labelSet = dict.Keys.ToList();
            foreach(string label in labelSet)
               {
               List<double[]> tempMatrix = dict[label];
               double[][] matrixWithinThatClass = tempMatrix.ToArray();
               double[] meanOfCurrentClass = Accord.Statistics.Tools.Mean(matrixWithinThatClass);
            for (int i = 0; i < matrixWithinThatClass.Length; i++) {
                double[][] temp1 = matrixWithinThatClass[i].ToArray().Subtract(meanOfCurrentClass.ToArray());
                temp1 = temp1.Multiply(temp1.Transpose());
                Sw =Sw.Add(temp1);
            }

            double[][] temp = meanOfCurrentClass.ToArray().Subtract(meanTotal);
            temp = temp.Multiply(temp.Transpose()).ToMatrix().Multiply((double)matrixWithinThatClass.Length).ToArray();
            Sb=Sb.Add(temp);
            }

            // calculate the eigenvalues and vectors of Sw^-1 * Sb
            double [][] targetForEigen = Sw.Inverse().Multiply(Sb);
            var feature = new EigenvalueDecomposition(targetForEigen.ToMatrix());

            double[] d = feature.RealEigenvalues;
            int[] indexes;
            d.StableSort(out indexes);
            indexes = indexes.Reverse().ToArray();

            indexes = indexes.Submatrix(0, c - 1);

               //int[] indexes = getIndexesOfKEigenvalues(d, c - 1);

            double[][] eigenVectors = feature.Eigenvectors.ToArray();
            double[][] selectedEigenVectors = eigenVectors.Submatrix(0,	eigenVectors.Length - 1, indexes);

            this.W = pca.getW().Multiply(selectedEigenVectors);

            // Construct projectedTrainingMatrix
            this.projectedTrainingSet = new List<projectedTrainingMatrix>();
             for (int i = 0; i < trainingSet.Length; i++) {
            projectedTrainingMatrix ptm = new projectedTrainingMatrix(this.W.Transpose().Multiply(trainingSet[i].Subtract(pca.meanMatrix).ToArray()), labels[i]);
            this.projectedTrainingSet.Add(ptm);
            }
            this.meanMatrix= pca.meanMatrix;
            GC.Collect();
        }
        void start()
        {
            DirectoryInfo dirInfo = new DirectoryInfo(@"E:\Work\Work apps\Antony\IFI_DB");

            info = dirInfo.GetFiles("*.jpg");
            //info.Length
            numberofimages = info.Length;

            numOfClasses = numberofimages / ImagePerClass;
            mymat = new List<double[]>();
            List<string> filenames = new List<string>();
            for (int i = 0; i < numberofimages; i++)
            {
                if (info[i].Name.Substring(3, 2) != "07")
                {

                    filenames.Add(info[i].Name.Substring(0, 3));
                    var img = BitmapFactory.New(1, 1).FromStream(info[i].OpenRead());
                    if (img.PixelWidth != 100 || img.PixelHeight != 150)
                    {
                        img = img.Resize(100, 150, WriteableBitmapExtensions.Interpolation.Bilinear);
                    }
                    mymat.Add(helper.imgtomat(img.ToByteArray()));

                }
            }
            GC.Collect();
              if (decide == 0)
             {
                 pca = new PCA(mymat.ToArray(), filenames, filenames.Count);
             }
             else
             {
                 if (File.Exists(@"E:\Work\Work apps\lda pca face rec\outputFile.txt"))
                 {
                     ObjectToSerialize objectToSerialize = Serializer.DeSerializeObject(@"E:\Work\Work apps\lda pca face rec\outputFile.txt");
                     lda = objectToSerialize.Lda;

                 }
                 else
                 {
                     lda = new LDA(mymat.ToArray(), filenames, filenames.Count);

                     ObjectToSerialize objectToSerialize = new ObjectToSerialize();
                     objectToSerialize.Lda = lda ;

                     Serializer serializer = new Serializer();
                     serializer.SerializeObject(@"E:\Work\Work apps\lda pca face rec\outputFile.txt", objectToSerialize);
                 }
             }
        }
Пример #3
0
        public LDA(double[][] trainingSet, List <string> labels, int numOfComponents)
        {
            int n = trainingSet.Length; // sample size
            HashSet <String> tempSet = new HashSet <String>(labels);
            int c = tempSet.Count;      // class size

            // process in PCA
            PCA pca = new PCA(trainingSet, labels, n - c);

            // classify
            double[][] meanTotal = new double[n - c][];
            for (int i = 0; i < n - c; i++)
            {
                meanTotal[i] = new double[1];
            }
            Dictionary <String, List <double[]> > dict     = new Dictionary <String, List <double[]> >();
            List <projectedTrainingMatrix>        pcaTrain = pca.getProjectedTrainingSet();

            for (int i = 0; i < pcaTrain.Count; i++)
            {
                String key = pcaTrain[i].label;
                meanTotal = meanTotal.Add(pcaTrain[i].matrix);
                if (!dict.ContainsKey(key))
                {
                    List <double[]> temp = new List <double[]>();

                    temp.Add(pcaTrain[i].matrix.Transpose()[0]);
                    dict.Add(key, temp);
                }
                else
                {
                    List <double[]> temp = dict[key];
                    temp.Add(pcaTrain[i].matrix.Transpose()[0]);
                    dict[key] = temp;
                }
            }
            meanTotal.ToMatrix().Multiply((double)1 / n);

            // calculate Sw, Sb
            double[][] Sw = new double[n - c][];
            double[][] Sb = new double[n - c][];
            for (int i = 0; i < n - c; i++)
            {
                Sw[i] = new double[n - c];
                Sb[i] = new double[n - c];
            }
            List <String> labelSet = dict.Keys.ToList();

            foreach (string label in labelSet)
            {
                List <double[]> tempMatrix            = dict[label];
                double[][]      matrixWithinThatClass = tempMatrix.ToArray();
                double[]        meanOfCurrentClass    = Accord.Statistics.Tools.Mean(matrixWithinThatClass);
                for (int i = 0; i < matrixWithinThatClass.Length; i++)
                {
                    double[][] temp1 = matrixWithinThatClass[i].ToArray().Subtract(meanOfCurrentClass.ToArray());
                    temp1 = temp1.Multiply(temp1.Transpose());
                    Sw    = Sw.Add(temp1);
                }

                double[][] temp = meanOfCurrentClass.ToArray().Subtract(meanTotal);
                temp = temp.Multiply(temp.Transpose()).ToMatrix().Multiply((double)matrixWithinThatClass.Length).ToArray();
                Sb   = Sb.Add(temp);
            }

            // calculate the eigenvalues and vectors of Sw^-1 * Sb
            double [][] targetForEigen = Sw.Inverse().Multiply(Sb);
            var         feature        = new EigenvalueDecomposition(targetForEigen.ToMatrix());

            double[] d = feature.RealEigenvalues;
            int[]    indexes;
            d.StableSort(out indexes);
            indexes = indexes.Reverse().ToArray();

            indexes = indexes.Submatrix(0, c - 1);

            //int[] indexes = getIndexesOfKEigenvalues(d, c - 1);

            double[][] eigenVectors         = feature.Eigenvectors.ToArray();
            double[][] selectedEigenVectors = eigenVectors.Submatrix(0, eigenVectors.Length - 1, indexes);

            this.W = pca.getW().Multiply(selectedEigenVectors);

            // Construct projectedTrainingMatrix
            this.projectedTrainingSet = new List <projectedTrainingMatrix>();
            for (int i = 0; i < trainingSet.Length; i++)
            {
                projectedTrainingMatrix ptm = new projectedTrainingMatrix(this.W.Transpose().Multiply(trainingSet[i].Subtract(pca.meanMatrix).ToArray()), labels[i]);
                this.projectedTrainingSet.Add(ptm);
            }
            this.meanMatrix = pca.meanMatrix;
            GC.Collect();
        }