hi guys I have a set of face images with which i want to do face recognition using Petland's PCA method.I gathered these steps from their docs
1.represent matrix of face images data 2.find the adjusted matrix by substracting the mean face 3.calculate covariance matrix (cov=A* A_transpose) where A is from step2 4.find eigenvectors and select those with highest eigenvalues 5.calculate facespace=eigenvectors*A
when it comes to implementation i have doubts as to how i should represent the matrix of face images? using PIL image.getdata() i can make an array of each greyscale image. Should the matrix be like each row contains an array representing an image? That will make a matrix with rows=numimages and columns=numpixels
cavariancematrix =A *A_transpose will create a square matrix of shape(numimages,numimages) Using numpy.linalg.eigh(covariancematrix) will give eigenvectors of same shape as the covariance matrix.
I would like to know if this is the correct way to do this..I have no big expertise in linear algebra so i would be grateful if someone can confirm the right way of doing this
RoyG
RoyG,
The timing of your question couldn't be better, I just did an blog post on this (I also plugged scipy and the EPD):
http://www.datawrangling.com/python-montage-code-for-displaying-arrays.html
The code basically replicates the matlab montage() function and approach to handling grayscale images using matplotlib.
-Pete
On Fri, Feb 29, 2008 at 2:15 PM, devnew@gmail.com devnew@gmail.com wrote:
hi guys I have a set of face images with which i want to do face recognition using Petland's PCA method.I gathered these steps from their docs
1.represent matrix of face images data 2.find the adjusted matrix by substracting the mean face 3.calculate covariance matrix (cov=A* A_transpose) where A is from step2 4.find eigenvectors and select those with highest eigenvalues 5.calculate facespace=eigenvectors*A
when it comes to implementation i have doubts as to how i should represent the matrix of face images? using PIL image.getdata() i can make an array of each greyscale image. Should the matrix be like each row contains an array representing an image? That will make a matrix with rows=numimages and columns=numpixels
cavariancematrix =A *A_transpose will create a square matrix of shape(numimages,numimages) Using numpy.linalg.eigh(covariancematrix) will give eigenvectors of same shape as the covariance matrix.
I would like to know if this is the correct way to do this..I have no big expertise in linear algebra so i would be grateful if someone can confirm the right way of doing this
RoyG _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Here is the page I referenced for the octave version ... it includes examples very similar to what you want. I will be posting a very similar example in Python later this month.
I don't have any Python code on hand for the Petland paper, but I think matlab example should be easy to translate to scipy/matplotlib using the montage function:
load faces.mat %Form covariance matrix C=cov(faces'); %build eigenvectors and eigenvalues [E,D] = eig(C); %sort based on eigenvalue [B,index] = sortrows(D'); E2=E'; E2(index,:)'; eigensorted=E2(index,:)'; %show eigenfaces clear Z; for i=1:length(eigensorted) Z(:,:,1,i)=reshape(eigensorted(:,i)-1.5*min(min(min(eigensorted))), 19,19); end montage(Z) %show top 16 eigenfaces
clear Z; for i=1:16 Z(:,:,1,i)=reshape(eigensorted(:,i)-min(min(min(eigensorted))), 19,19); end montage(Z)
On Fri, Feb 29, 2008 at 2:50 PM, Peter Skomoroch peter.skomoroch@gmail.com wrote:
RoyG,
The timing of your question couldn't be better, I just did an blog post on this (I also plugged scipy and the EPD):
http://www.datawrangling.com/python-montage-code-for-displaying-arrays.html
The code basically replicates the matlab montage() function and approach to handling grayscale images using matplotlib.
-Pete
On Fri, Feb 29, 2008 at 2:15 PM, devnew@gmail.com devnew@gmail.com wrote:
hi guys I have a set of face images with which i want to do face recognition using Petland's PCA method.I gathered these steps from their docs
1.represent matrix of face images data 2.find the adjusted matrix by substracting the mean face 3.calculate covariance matrix (cov=A* A_transpose) where A is from step2 4.find eigenvectors and select those with highest eigenvalues 5.calculate facespace=eigenvectors*A
when it comes to implementation i have doubts as to how i should represent the matrix of face images? using PIL image.getdata() i can make an array of each greyscale image. Should the matrix be like each row contains an array representing an image? That will make a matrix with rows=numimages and columns=numpixels
cavariancematrix =A *A_transpose will create a square matrix of shape(numimages,numimages) Using numpy.linalg.eigh(covariancematrix) will give eigenvectors of same shape as the covariance matrix.
I would like to know if this is the correct way to do this..I have no big expertise in linear algebra so i would be grateful if someone can confirm the right way of doing this
RoyG _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- Peter N. Skomoroch peter.skomoroch@gmail.com http://www.datawrangling.com
Forgot the url:
http://www.cis.hut.fi/Opinnot/T-61.2010/harjoitustyo_en07.shtml
On Fri, Feb 29, 2008 at 2:56 PM, Peter Skomoroch peter.skomoroch@gmail.com wrote:
Here is the page I referenced for the octave version ... it includes examples very similar to what you want. I will be posting a very similar example in Python later this month.
I don't have any Python code on hand for the Petland paper, but I think matlab example should be easy to translate to scipy/matplotlib using the montage function:
load faces.mat %Form covariance matrix C=cov(faces'); %build eigenvectors and eigenvalues [E,D] = eig(C); %sort based on eigenvalue [B,index] = sortrows(D'); E2=E'; E2(index,:)'; eigensorted=E2(index,:)'; %show eigenfaces clear Z; for i=1:length(eigensorted) Z(:,:,1,i)=reshape(eigensorted(:,i)-1.5*min(min(min(eigensorted))), 19,19); end montage(Z) %show top 16 eigenfaces
clear Z; for i=1:16 Z(:,:,1,i)=reshape(eigensorted(:,i)-min(min(min(eigensorted))), 19,19); end montage(Z)
On Fri, Feb 29, 2008 at 2:50 PM, Peter Skomoroch < peter.skomoroch@gmail.com> wrote:
RoyG,
The timing of your question couldn't be better, I just did an blog post on this (I also plugged scipy and the EPD):
http://www.datawrangling.com/python-montage-code-for-displaying-arrays.html
The code basically replicates the matlab montage() function and approach to handling grayscale images using matplotlib.
-Pete
On Fri, Feb 29, 2008 at 2:15 PM, devnew@gmail.com devnew@gmail.com wrote:
hi guys I have a set of face images with which i want to do face recognition using Petland's PCA method.I gathered these steps from their docs
1.represent matrix of face images data 2.find the adjusted matrix by substracting the mean face 3.calculate covariance matrix (cov=A* A_transpose) where A is from step2 4.find eigenvectors and select those with highest eigenvalues 5.calculate facespace=eigenvectors*A
when it comes to implementation i have doubts as to how i should represent the matrix of face images? using PIL image.getdata() i can make an array of each greyscale image. Should the matrix be like each row contains an array representing an image? That will make a matrix with rows=numimages and columns=numpixels
cavariancematrix =A *A_transpose will create a square matrix of shape(numimages,numimages) Using numpy.linalg.eigh(covariancematrix) will give eigenvectors of same shape as the covariance matrix.
I would like to know if this is the correct way to do this..I have no big expertise in linear algebra so i would be grateful if someone can confirm the right way of doing this
RoyG _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- Peter N. Skomoroch peter.skomoroch@gmail.com http://www.datawrangling.com
-- Peter N. Skomoroch peter.skomoroch@gmail.com http://www.datawrangling.com
On Mar 1, 12:57 am, "Peter Skomoroch" wrote: I think
matlab example should be easy to translate to scipy/matplotlib using the montage function:
load faces.mat %Form covariance matrix C=cov(faces'); %build eigenvectors and eigenvalues [E,D] = eig(C);
hi Peter, nice code..ran the examples.. however couldn't follow the matlab code since i have no exposure to matlab..was using numpy etc for calcs could you confirm the layout for the face images data? i assumed that the initial face matrix should be faces=a numpy matrix with N rows ie N=numofimages
row1=image1pixels as a sequence row2=image2pixels as a sequence ... rowN=imageNpixels as a sequence
and covariancematrix=faces*faces_transpose
is this the right way? thanks
I think that is correct...
Here is what the final result should look like:
http://www.datawrangling.com/media/images/first_16.png
If the dimensions for the sample faces don't work out to ( 361 x 361 ) in the end, then you are likely to be missing a transpose somewhere. Also, be aware that the scipy linalg.eig by default returns a vector of eigenvalues and a matrix, but the Matlab eig(), returns 2 matrices ( the eigenvalues are multiplied by an identity matrix to get a diagonal matrix). You can check out the mathesaurus reference sheet for help translating the example into python, but hopefully this will point you in the right direction:
see:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/eig.html
vs:
help(linalg.eig)
Help on function eig in module scipy.linalg.decomp:
eig(a, b=None, left=False, right=True, overwrite_a=False, overwrite_b=False) Solve ordinary and generalized eigenvalue problem of a square matrix.
Inputs: a -- An N x N matrix. b -- An N x N matrix [default is identity(N)]. left -- Return left eigenvectors [disabled]. right -- Return right eigenvectors [enabled]. overwrite_a, overwrite_b -- save space by overwriting the a and/or b matrices (both False by default) Outputs: w -- eigenvalues [left==right==False]. w,vr -- w and right eigenvectors [left==False,right=True]. w,vl -- w and left eigenvectors [left==True,right==False]. w,vl,vr -- [left==right==True]. Definitions: a * vr[:,i] = w[i] * b * vr[:,i] a^H * vl[:,i] = conjugate(w[i]) * b^H * vl[:,i] where a^H denotes transpose(conjugate(a)).
On Sat, Mar 1, 2008 at 12:41 AM, devnew@gmail.com devnew@gmail.com wrote:
On Mar 1, 12:57 am, "Peter Skomoroch" wrote: I think
matlab example should be easy to translate to scipy/matplotlib using
the
montage function:
load faces.mat %Form covariance matrix C=cov(faces'); %build eigenvectors and eigenvalues [E,D] = eig(C);
hi Peter, nice code..ran the examples.. however couldn't follow the matlab code since i have no exposure to matlab..was using numpy etc for calcs could you confirm the layout for the face images data? i assumed that the initial face matrix should be faces=a numpy matrix with N rows ie N=numofimages
row1=image1pixels as a sequence row2=image2pixels as a sequence ... rowN=imageNpixels as a sequence
and covariancematrix=faces*faces_transpose
is this the right way? thanks _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
The steps you describe here are correct. I am putting together an open source computer vision library based on numpy/scipy. It will include an automatic PCA algorithm with face detection, eye detection, PCA dimensionally reduction, and distance measurement. If you are interested let me know and I will redouble my efforts to release the code soon.
Dave
On Feb 29, 2008, at 12:15 PM, devnew@gmail.com wrote:
1.represent matrix of face images data 2.find the adjusted matrix by substracting the mean face 3.calculate covariance matrix (cov=A* A_transpose) where A is from step2 4.find eigenvectors and select those with highest eigenvalues 5.calculate facespace=eigenvectors*A
Am Dienstag, 11. März 2008 00:24:04 schrieb David Bolme:
The steps you describe here are correct. I am putting together an open source computer vision library based on numpy/scipy. It will include an automatic PCA algorithm with face detection, eye detection, PCA dimensionally reduction, and distance measurement. If you are interested let me know and I will redouble my efforts to release the code soon.
That's interesting, we're also working on a computer vision module using NumPy (actually, a VIGRA <-> NumPy binding sort of), and there's scipy.ndimage, too. Maybe (part of) your code could be integrated into the latter? I am looking forward to it anyway.
If you are interested I now have the code on source forge. It still needs some critical documentation. I am planing to get this documented and a beta release sometime this summer. Currently it ties together PIL, OpenCV, numpy/scipy, LibSVM, and some of own code with an emphases on face recognition since that is my research area.
http://pyvision.sourceforge.net
On Apr 7, 2008, at 3:40 AM, Hans Meine wrote:
Am Dienstag, 11. März 2008 00:24:04 schrieb David Bolme:
The steps you describe here are correct. I am putting together an open source computer vision library based on numpy/scipy. It will include an automatic PCA algorithm with face detection, eye detection, PCA dimensionally reduction, and distance measurement. If you are interested let me know and I will redouble my efforts to release the code soon.
That's interesting, we're also working on a computer vision module using NumPy (actually, a VIGRA <-> NumPy binding sort of), and there's scipy.ndimage, too. Maybe (part of) your code could be integrated into the latter? I am looking forward to it anyway.
-- Ciao, / / /--/ / / ANS _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion