Example on face classification |
This introductory example shows how to do face recognition using the nearest neighbor rule based on eigenfaces. We will make use of the ATT face database (formerly the ORL database). It is heavily used in publications as an example of an easy set of images as the pictures are taken under very similar conditions.
The database in stored in prdatafiles. Like in the previous example, it should be verified first whether this repository is available or should be downloaded.
prdatafiles
Now the data itself can be read from disc. If not available it is downloaded from the PRTools
website first. For historical reasons the command is still called orl.
a = orl
We now have a datafile. It points to a set of 400 images (faces) stored on disk. It is constructed in such a way that the labels of the images for the 40 classes are stored in the datafile construct. A class is here a set of 10 faces belonging to the same person.
% Convert to dataset and show a = dataset(a); a = setprior(a,0); show(a,20); pause
With these statements the datafile is converted to a dataset, using every pixel as a feature. As the images have a size of 112 × 92 the feature vectors have a length of 10304.
% Compute eigenfaces w = pca(a); % show first 24 eigenfaces figure; show(w(:,1:24),6)
The PCA (Principal components) mapping is computed and the first 24 components, called eigenfaces, are shown as images.
%project eigenfaces b = a*w; figure; scatterd(b); title('Scatterplot of 10 face images of 40 subjects') xlabel('Projection on eigenface 1') ylabel('Projection on eigenface 2') fontsize(16)
This is a scatterplot of all 400 images as points in a 2-dimensional space constructed by the first two eigenfaces.
% Compute error curve as function of number of eigenfaces e = zeros(1,16); featsizes = [1 2 3 5 7 10 15 20 30 50 70 100 150 200 300 399]; for n = 1:length(featsizes) e(n) = testk(b(:,1:featsizes(n))); end figure semilogx(featsizes,e) axis([1 400 0 1])
The classification error using the nearest neighbor rule is shown as a function of the number of eigenfaces used. It shows that about 40 components is sufficient. This corresponds to the number of classes to be distinguished. This is caused by the fact that inter-class differences (between the classes) are much larger than intra-class differences (between the images in the same class).
% Annotate error curve title('1NN classification error of the ATT face database') xlabel('Number of Eigenfaces') ylabel('1NN error') fontsize(16) linewidth(2)
Proper annotation of figures is needed for documentation and presentations.
showfigs
The showfigs
command is a command that comes with PRTools
and that makes an attempt to show all images on the screen. Its counterpart delfigs
, deletes all images.
R.P.W. Duin
, January 28, 2013Example on face classification |