Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.25 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.lang.*;
  4. import Jama.*;
  5. public class pcafaces{
  6.     public static void main(String args[]){
  7.         double[][] xmatrix = new double[644][280];
  8.         double sum = 0;
  9.         //int pixels=644;
  10.         //int photos=280;
  11.         double diagSum = 0;
  12.         double dimension = 0;
  13.         int r = 0;
  14.         double[][] amatrix = new double[644][280];
  15.         //file handlers
  16.         Matrix Y;
  17.         Matrix zDash;
  18.         if(args.length!=4){
  19.             throw new NullPointerException();
  20.         }
  21.         String trainPath = args[0];
  22.         String testPath = args[1];
  23.         String varth = args[2];
  24.         String k = args[3];
  25.         int realK =(Integer.parseInt(k));
  26.         for(int i=0;i<40;i++){
  27.             for(int j=0;j<7;j++){
  28.                 try{
  29.                     BufferedReader in = new BufferedReader(new FileReader(trainPath+"/s"+i+"_"+j+".pgm"));
  30.                     in.readLine(); // This reads 'P2'.
  31.                     in.readLine(); // This reads the comment.
  32.                     in.readLine(); // This reads the image dimensions.
  33.                     in.readLine(); // This reads the intensity range.
  34.                     for(int p = 0;p<644;p++){
  35.                         xmatrix[p][i*7+j]= Double.parseDouble(in.readLine());
  36.                     }
  37.                     //System.out.println(xmatrix[300][170]);
  38.                     in.close();
  39.                 }catch(IOException e){}
  40.             }
  41.         }
  42.         double[] xMean = new double[644];
  43.         double[][] xBar = new double[644][280];
  44.         for(int i=0;i<644;i++){
  45.             sum = 0;
  46.             for(int j=0;j<280;j++){
  47.                 sum = sum +xmatrix[i][j];
  48.             }
  49.             xMean[i] = sum/(280);
  50.             for(int j=0;j<280;j++){
  51.                 xBar[i][j] = xmatrix[i][j] - xMean[i];
  52.             }
  53.         }
  54.  
  55.         //Initialises matrix A with the values in the double array vals.
  56.         Matrix A = new Matrix(xBar);
  57.         // Invokes SVD from the Jama package.
  58.         SingularValueDecomposition s = A.svd();
  59.         Matrix D = s.getS();//get sigma diagonal matrix and add values to sum
  60.         for(int i=0;i<280;i++){
  61.             diagSum+=D.get(i,i);
  62.         }
  63.         for(int i=0;i<280;i++){
  64.             dimension += D.get(i,i);            //(dimension/diagSum)*100
  65.             if(((dimension/diagSum)*100)>=Double.parseDouble(varth)){
  66.                 r = i;
  67.                 break;
  68.             }
  69.         }
  70.         //Matrix B = new Matrix(xMean);
  71.         Matrix U = s.getU();
  72.         Matrix Ur = U.getMatrix(0, U.getRowDimension()-1, 0, r);
  73.         Y = ((Ur).transpose()).times(A);
  74.  
  75.        
  76.        
  77.         for(int i1=0;i1<40;i1++){
  78.             for(int j1=0;j1<3;j1++){
  79.                 try{
  80.                     BufferedReader br = new BufferedReader(new FileReader(testPath+"/s"+i1+"_"+j1+".pgm"));
  81.                     br.readLine(); // This reads 'P2'.
  82.                     br.readLine(); // This reads the comment.
  83.                     br.readLine(); // This reads the image dimensions.
  84.                     br.readLine(); // This reads the intensity range.
  85.                    
  86.                    
  87.                     double[][] zmatrix = new double[23*28][1];
  88.                     for(int p =0;p<644;p++){
  89.                         zmatrix[p][0]= Double.parseDouble(br.readLine());
  90.                         zmatrix[p][0] = zmatrix[p][0]-xMean[p];
  91.                     }
  92.                     //maybe put above line in seperate for loop.....
  93.                     Matrix Z = new Matrix(zmatrix);
  94.                     zDash = Ur.transpose().times(Z);
  95.                    
  96.                     //perform comparison with test files and train files
  97.                     /*8. Perform classification using Z' and Y , e.g., find the K nearest
  98.     neighbours of Z' among Y .*/
  99.                     double distance;
  100.                     int[] label = new int[realK];
  101.                     double[] nndis = new double[realK];
  102.                     for(int i=0;i<280;i++){
  103.                         distance=0;
  104.                         for(int j=0;j<r;j++){
  105.                             distance +=Math.pow((zDash.get(j,0)-(Y.get(j,i))),2);
  106.                         }
  107.                         for(int j=0;j<realK;j++){
  108.                             if(distance<nndis[j]){
  109.                                 for(int l=realK-2;l>=j;l--){
  110.                                     nndis[l+1]=nndis[l];
  111.                                     label[l + 1]  = label[l];//nnlab[k]=total photos/photosper person
  112.                                 }
  113.                                 nndis[realK] = distance;
  114.                                 label[realK] = i/7;
  115.                                 break;
  116.                             }
  117.                         }
  118.                     }
  119.                     //return majority value of label
  120.                    
  121.                     double max=0;
  122.                     int index=0;
  123.                     boolean check = true;
  124.                     int[] result = new int[40];
  125.                     double maxResult=0;
  126.                     for (int i = 0; i <40; ++i) {
  127.                         result[i]= 0;
  128.                     }
  129.                     for (int i = 0; i<realK; i++) {
  130.                         result[label[i]]++;
  131.                     }
  132.                     for(int i=0;i<40;i++){
  133.                         if (result[i] > maxResult) {
  134.                             maxResult = result[i];
  135.                             index = i;
  136.                             check = false;
  137.                         }
  138.                         else if(result[i]==maxResult){
  139.                             maxResult = result[i];
  140.                             index = i;
  141.                             check = true;
  142.                         }
  143.                     }
  144.                     if(check){
  145.                         System.out.println(-1);
  146.                     }else{
  147.                         System.out.println(result[index]);
  148.                     }
  149.                     br.close();
  150.                 }catch(IOException e){}
  151.             }
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement