Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.lang.*;
- import Jama.*;
- public class pcafaces{
- public static void main(String args[]){
- double[][] xmatrix = new double[644][280];
- double sum = 0;
- //int pixels=644;
- //int photos=280;
- double diagSum = 0;
- double dimension = 0;
- int r = 0;
- double[][] amatrix = new double[644][280];
- //file handlers
- Matrix Y;
- Matrix zDash;
- if(args.length!=4){
- throw new NullPointerException();
- }
- String trainPath = args[0];
- String testPath = args[1];
- String varth = args[2];
- String k = args[3];
- int realK =(Integer.parseInt(k));
- for(int i=0;i<40;i++){
- for(int j=0;j<7;j++){
- try{
- BufferedReader in = new BufferedReader(new FileReader(trainPath+"/s"+i+"_"+j+".pgm"));
- in.readLine(); // This reads 'P2'.
- in.readLine(); // This reads the comment.
- in.readLine(); // This reads the image dimensions.
- in.readLine(); // This reads the intensity range.
- for(int p = 0;p<644;p++){
- xmatrix[p][i*7+j]= Double.parseDouble(in.readLine());
- }
- //System.out.println(xmatrix[300][170]);
- in.close();
- }catch(IOException e){}
- }
- }
- double[] xMean = new double[644];
- double[][] xBar = new double[644][280];
- for(int i=0;i<644;i++){
- sum = 0;
- for(int j=0;j<280;j++){
- sum = sum +xmatrix[i][j];
- }
- xMean[i] = sum/(280);
- for(int j=0;j<280;j++){
- xBar[i][j] = xmatrix[i][j] - xMean[i];
- }
- }
- //Initialises matrix A with the values in the double array vals.
- Matrix A = new Matrix(xBar);
- // Invokes SVD from the Jama package.
- SingularValueDecomposition s = A.svd();
- Matrix D = s.getS();//get sigma diagonal matrix and add values to sum
- for(int i=0;i<280;i++){
- diagSum+=D.get(i,i);
- }
- for(int i=0;i<280;i++){
- dimension += D.get(i,i); //(dimension/diagSum)*100
- if(((dimension/diagSum)*100)>=Double.parseDouble(varth)){
- r = i;
- break;
- }
- }
- //Matrix B = new Matrix(xMean);
- Matrix U = s.getU();
- Matrix Ur = U.getMatrix(0, U.getRowDimension()-1, 0, r);
- Y = ((Ur).transpose()).times(A);
- for(int i1=0;i1<40;i1++){
- for(int j1=0;j1<3;j1++){
- try{
- BufferedReader br = new BufferedReader(new FileReader(testPath+"/s"+i1+"_"+j1+".pgm"));
- br.readLine(); // This reads 'P2'.
- br.readLine(); // This reads the comment.
- br.readLine(); // This reads the image dimensions.
- br.readLine(); // This reads the intensity range.
- double[][] zmatrix = new double[23*28][1];
- for(int p =0;p<644;p++){
- zmatrix[p][0]= Double.parseDouble(br.readLine());
- zmatrix[p][0] = zmatrix[p][0]-xMean[p];
- }
- //maybe put above line in seperate for loop.....
- Matrix Z = new Matrix(zmatrix);
- zDash = Ur.transpose().times(Z);
- //perform comparison with test files and train files
- /*8. Perform classification using Z' and Y , e.g., find the K nearest
- neighbours of Z' among Y .*/
- double distance;
- int[] label = new int[realK];
- double[] nndis = new double[realK];
- for(int i=0;i<280;i++){
- distance=0;
- for(int j=0;j<r;j++){
- distance +=Math.pow((zDash.get(j,0)-(Y.get(j,i))),2);
- }
- for(int j=0;j<realK;j++){
- if(distance<nndis[j]){
- for(int l=realK-2;l>=j;l--){
- nndis[l+1]=nndis[l];
- label[l + 1] = label[l];//nnlab[k]=total photos/photosper person
- }
- nndis[realK] = distance;
- label[realK] = i/7;
- break;
- }
- }
- }
- //return majority value of label
- double max=0;
- int index=0;
- boolean check = true;
- int[] result = new int[40];
- double maxResult=0;
- for (int i = 0; i <40; ++i) {
- result[i]= 0;
- }
- for (int i = 0; i<realK; i++) {
- result[label[i]]++;
- }
- for(int i=0;i<40;i++){
- if (result[i] > maxResult) {
- maxResult = result[i];
- index = i;
- check = false;
- }
- else if(result[i]==maxResult){
- maxResult = result[i];
- index = i;
- check = true;
- }
- }
- if(check){
- System.out.println(-1);
- }else{
- System.out.println(result[index]);
- }
- br.close();
- }catch(IOException e){}
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement