PCATutorial.cpp
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Principal Component Analysis Tutorial Sample Code
6  *
7  * This file is part of the "Principal Component Analysis" tutorial.
8  * The tutorial requires that you download the Cambridge Face Database
9  * from http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html
10  * and adjust the facedirectory path to the directory containing the faces
11  * in PGM format.
12  *
13  * You need the libraries boost_serialization, boost_system, and
14  * boost_filesystem for this example.
15  *
16  *
17  *
18  * \author C. Igel
19  * \date 2011
20  *
21  *
22  * \par Copyright 1995-2017 Shark Development Team
23  *
24  * <BR><HR>
25  * This file is part of Shark.
26  * <http://shark-ml.org/>
27  *
28  * Shark is free software: you can redistribute it and/or modify
29  * it under the terms of the GNU Lesser General Public License as published
30  * by the Free Software Foundation, either version 3 of the License, or
31  * (at your option) any later version.
32  *
33  * Shark is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36  * GNU Lesser General Public License for more details.
37  *
38  * You should have received a copy of the GNU Lesser General Public License
39  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
40  *
41  */
42 //===========================================================================
43 
45 #include <shark/Data/Pgm.h>
46 
47 #include <fstream>
48 #include <boost/format.hpp>
49 
50 using namespace std;
51 using namespace shark;
52 
53 
54 int main(){
55  // read image data
56  const char *facedirectory = "Cambridge_FaceDB"; //< set this to the directory containing the face database
58  cout << "Read images ... " << flush;
59  try {
60  importPGMSet(facedirectory, images);
61  } catch(...) {
62  cerr << "[PCATutorial] could not open face database directory\n\nThis file is part of the \"Principal Component Analysis\" tutorial.\nThe tutorial requires that you download the Cambridge Face Database\nfrom http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html\nand adjust the facedirectory path in the source code to the directory\ncontaining the faces in PGM format." << endl;
63  return 1;
64  }
65  cout << "done." << endl;
66 
67  unsigned l = images.numberOfElements(); // number of samples
68  unsigned x = images.shape()[1]; // width of images
69  unsigned y = images.shape()[0]; // height of images
70 
71  cout << "Eigenvalue decomposition ... " << flush;
72  PCA pca(images);
73  cout << "done." << endl;
74 
75  cout << "Writing mean face and eigenvalues... " << flush;
76  ofstream ofs("facesEigenvalues.csv");
77  for(unsigned i=0; i<l; i++)
78  ofs << pca.eigenvalue(i) << endl;
79  exportPGM("facesMean.pgm", pca.mean(), x, y);
80  cout << "done. " << endl;
81 
82  cout << "Encoding ... " << flush;
83  unsigned m = 299;
84  LinearModel<> enc;
85  pca.encoder(enc, m);
86  Data<RealVector> encodedImages = enc(images);
87  cout << "done. " << endl;
88 
89  unsigned sampleImage = 0;
90  cout << "Reconstructing face " << sampleImage << " ... " << flush;
91  boost::format fmterTrue("face%d.pgm");
92  exportPGM((fmterTrue % sampleImage).str().c_str(), images.element(sampleImage), x, y);
93  LinearModel<> dec;
94  pca.decoder(dec, m);
95  boost::format fmterRec("facesReconstruction%d-%d.pgm");
96  exportPGM((fmterRec % sampleImage % m).str().c_str(), dec(encodedImages.element(sampleImage)), x, y);
97  cout << "done." << endl;
98 }