-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine-learning-for-facial-recognition-3.html
472 lines (398 loc) · 47.2 KB
/
machine-learning-for-facial-recognition-3.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="Damien RJ" />
<meta property="og:type" content="article" />
<meta name="twitter:card" content="summary">
<meta name="keywords" content="machine learning, Case studies, " />
<meta property="og:title" content="Machine learning for facial recognition "/>
<meta property="og:url" content="./machine-learning-for-facial-recognition-3" />
<meta property="og:description" content="A guest post, contributed by Damien Ramunno-Johnson (LinkedIn, bio-sketch) Introduction The ability to identify faces is a skill that people develop very early in life and can apply almost effortlessly. One reason for this is that our brains are very well adapted for pattern recognition. In contrast, facial recognition can …" />
<meta property="og:site_name" content="EFAVDB" />
<meta property="og:article:author" content="Damien RJ" />
<meta property="og:article:published_time" content="2015-01-21T17:12:00-08:00" />
<meta name="twitter:title" content="Machine learning for facial recognition ">
<meta name="twitter:description" content="A guest post, contributed by Damien Ramunno-Johnson (LinkedIn, bio-sketch) Introduction The ability to identify faces is a skill that people develop very early in life and can apply almost effortlessly. One reason for this is that our brains are very well adapted for pattern recognition. In contrast, facial recognition can …">
<title>Machine learning for facial recognition · EFAVDB
</title>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Fira+Sans:300,400,700" rel="stylesheet" type='text/css' />
<link href="https://fonts.googleapis.com/css?family=Cardo:400,700" rel="stylesheet" type='text/css' />
<link rel="stylesheet" type="text/css" href="./theme/css/elegant.prod.css" media="screen">
<link rel="stylesheet" type="text/css" href="./theme/css/custom.css" media="screen">
<link rel="apple-touch-icon" sizes="180x180" href="./images/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="./images/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="./images/favicon-16x16.png">
<link rel="manifest" href="./images/site.webmanifest">
<link rel="mask-icon" href="./images/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
</head>
<body>
<div id="content">
<div class="navbar navbar-static-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="./"><span class=site-name>EFAVDB</span></a>
<div class="nav-collapse collapse">
<ul class="nav pull-right top-menu">
<li >
<a href=
.
>Home</a>
</li>
<li ><a href="./pages/authors.html">Authors</a></li>
<li ><a href="./categories.html">Categories</a></li>
<li ><a href="./tags.html">Tags</a></li>
<li ><a href="./archives.html">Archives</a></li>
<li><form class="navbar-search" action="./search.html" onsubmit="return validateForm(this.elements['q'].value);"> <input type="text" class="search-query" placeholder="Search" name="q" id="tipue_search_input"></form></li>
</ul>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span1"></div>
<div class="span10">
<article itemscope>
<div class="row-fluid">
<header class="page-header span8 offset2">
<h1>
<a href="./machine-learning-for-facial-recognition-3">
Machine learning for facial recognition
</a>
</h1>
</header>
<div class="span2"></div>
</div>
<div class="row-fluid">
<div class="span8 offset2 article-content">
<p>A guest post, contributed by Damien Ramunno-Johnson (<a href="https://www.linkedin.com/profile/view?id=60223336&authType=NAME_SEARCH&authToken=LOV_&locale=en_US&trk=tyah2&trkInfo=tarId%3A1420748440448%2Ctas%3Adamien%2Cidx%3A1-1-1">LinkedIn</a>, <a href="https://efavdb.github.io/pages/authors.html">bio-sketch</a>)</p>
<h4><strong>Introduction</strong></h4>
<p>The ability to identify faces is a skill that people develop very early in life and can apply almost effortlessly. One reason for this is that our brains are very well adapted for pattern recognition. In contrast, facial recognition can be a somewhat difficult problem for computers. Today, given a full frontal image of a face, computer facial recognition software works well. However, problems can arise given large camera angles, poor lighting, or exaggerated facial expressions: Computers have a ways to go before they catch up with us in this arena.</p>
<p>Although facial recognition algorithms remain imperfect, the methods that exist now are already quite useful and are being applied by many different companies. Two examples, first up Facebook: When you upload pictures to their website, it will now automatically suggest names for the people in your photos. This application is well-suited for machine learning for two reasons. First, every tagged photo already uploaded to the site provides labeled examples on which to train an algorithm, and second, people often post full face images in decent lighting. A second example is provided by Google’s Android phone <span class="caps">OS</span>, which has a face unlock mode. To get this to work, you first have to train your phone by taking images of your face in different lighting conditions and from different angles. After training, the phone can attempt to recognize you. This is another cool application that also often works well.</p>
<p>In this post, we’re going to develop our own basic facial learning algorithm. We’ll find that it is actually pretty straightforward to set one up that is reasonably accurate. Our post follows and expands upon the tutorial found <a href="http://scikit-learn.org/stable/auto_examples/applications/face_recognition.html">here</a>.</p>
<h4><strong>Loading packages and data</strong></h4>
<div class="highlight"><pre><span></span><code><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="kn">from</span> <span class="nn">sklearn.cross_validation</span> <span class="kn">import</span> <span class="n">train_test_split</span>
<span class="kn">from</span> <span class="nn">sklearn.datasets</span> <span class="kn">import</span> <span class="n">fetch_lfw_people</span>
<span class="kn">from</span> <span class="nn">sklearn.grid_search</span> <span class="kn">import</span> <span class="n">GridSearchCV</span>
<span class="kn">from</span> <span class="nn">sklearn.metrics</span> <span class="kn">import</span> <span class="n">classification_report</span>
<span class="kn">from</span> <span class="nn">sklearn.metrics</span> <span class="kn">import</span> <span class="n">confusion_matrix</span>
<span class="kn">from</span> <span class="nn">sklearn.decomposition</span> <span class="kn">import</span> <span class="n">RandomizedPCA</span>
<span class="kn">from</span> <span class="nn">sklearn.svm</span> <span class="kn">import</span> <span class="n">SVC</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="o">%</span><span class="n">matplotlib</span> <span class="n">inline</span>
</code></pre></div>
<p>The sklearn function <a href="http://scikit-learn.org/stable/modules/generated/sklearn.datasets.fetch_lfw_people.html">fetch_lfw_people</a>, imported above, will download the data that we need, if not already present in the faces folder. The dataset we are downloading consists of a set of preprocessed images from <a href="http://vis-www.cs.umass.edu/lfw/">Labeled Faces in the Wild (<span class="caps">LFW</span>)</a>, a database designed for studying unconstrained face recognition. The data set contains more than 13,000 images of faces collected from the web, each labeled with the name of the person pictured. 1680 of the people pictured have two or more distinct photos in the data set.</p>
<p>In our analysis here, we will impose two conditions.</p>
<ol>
<li>First, we will only consider folks that have a minimum of 70 pictures in the data set.</li>
<li>We will resize the images so that they each have a 0.4 aspect ratio.</li>
</ol>
<div class="highlight"><pre><span></span><code>print('Loading Data')
people = fetch_lfw_people(
'./faces', min_faces_per_person=70, resize=0.4)
print('Done!')
&gt;&gt;
Loading Data
Done!
</code></pre></div>
<p>The object <strong>people</strong> contains the following data.</p>
<ol>
<li>people.data: a numpy array with the shape(n_samples, h*w), each row corresponds to a unravelled face.</li>
<li>people.images: a numpy array with the shape(n_samples, h, w), where each row corresponds to a face. The remaining indices here contain gray-scale values for the pixels of each image.</li>
<li>people.target: a numpy array with the shape(n_samples), where each row is the label for the face.</li>
<li>people.target_name: a numpy array with the shape(n_labels), where each row is the name for the label.</li>
</ol>
<p>For the algorithm we will be using, we don’t need the relative position data, so we will use the unraveled people.data.</p>
<div class="highlight"><pre><span></span><code><span class="p">#</span><span class="nn">Find</span><span class="w"> </span><span class="nt">out</span><span class="w"> </span><span class="nt">how</span><span class="w"> </span><span class="nt">many</span><span class="w"> </span><span class="nt">faces</span><span class="w"> </span><span class="nt">we</span><span class="w"> </span><span class="nt">have</span><span class="o">,</span><span class="w"> </span><span class="nt">and</span>
<span class="p">#</span><span class="nn">the</span><span class="w"> </span><span class="nt">size</span><span class="w"> </span><span class="nt">of</span><span class="w"> </span><span class="nt">each</span><span class="w"> </span><span class="nt">picture</span><span class="w"> </span><span class="nt">from</span><span class="o">.</span>
<span class="nt">n_samples</span><span class="o">,</span><span class="w"> </span><span class="nt">h</span><span class="o">,</span><span class="w"> </span><span class="nt">w</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nt">people</span><span class="p">.</span><span class="nc">images</span><span class="p">.</span><span class="nc">shape</span>
<span class="nt">X</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nt">people</span><span class="p">.</span><span class="nc">data</span>
<span class="nt">n_features</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nt">X</span><span class="p">.</span><span class="nc">shape</span><span class="cp">[</span><span class="mi">1</span><span class="cp">]</span>
<span class="nt">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nt">people</span><span class="p">.</span><span class="nc">target</span>
<span class="nt">target_names</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nt">people</span><span class="p">.</span><span class="nc">target_names</span>
<span class="nt">n_classes</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nt">target_names</span><span class="p">.</span><span class="nc">shape</span><span class="cp">[</span><span class="mi">0</span><span class="cp">]</span>
<span class="nt">print</span><span class="o">(&</span><span class="nt">quot</span><span class="o">;</span><span class="nt">Total</span><span class="w"> </span><span class="nt">dataset</span><span class="w"> </span><span class="nt">size</span><span class="o">:&</span><span class="nt">quot</span><span class="o">;)</span>
<span class="nt">print</span><span class="o">(&</span><span class="nt">quot</span><span class="o">;</span><span class="nt">n_images</span><span class="o">:</span><span class="w"> </span><span class="o">%</span><span class="nt">d</span><span class="o">&</span><span class="nt">quot</span><span class="o">;</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="nt">n_samples</span><span class="o">)</span>
<span class="nt">print</span><span class="o">(&</span><span class="nt">quot</span><span class="o">;</span><span class="nt">n_features</span><span class="o">:</span><span class="w"> </span><span class="o">%</span><span class="nt">d</span><span class="o">&</span><span class="nt">quot</span><span class="o">;</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="nt">n_features</span><span class="o">)</span>
<span class="nt">print</span><span class="o">(&</span><span class="nt">quot</span><span class="o">;</span><span class="nt">n_classes</span><span class="o">:</span><span class="w"> </span><span class="o">%</span><span class="nt">d</span><span class="o">&</span><span class="nt">quot</span><span class="o">;</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="nt">n_classes</span><span class="o">)</span>
<span class="o">&</span><span class="nt">gt</span><span class="o">;&</span><span class="nt">gt</span><span class="o">;</span>
<span class="nt">Total</span><span class="w"> </span><span class="nt">dataset</span><span class="w"> </span><span class="nt">size</span><span class="o">:</span>
<span class="nt">n_images</span><span class="o">:</span><span class="w"> </span><span class="nt">1288</span>
<span class="nt">n_features</span><span class="o">:</span><span class="w"> </span><span class="nt">1850</span>
<span class="nt">n_classes</span><span class="o">:</span><span class="w"> </span><span class="nt">7</span>
</code></pre></div>
<p>Looking above we see that our dataset currently has 1288 images. Each image has 1850 pixels, or features. We also have 7 classes, meaning images of 7 different people.</p>
<h4><strong>Data segmentation and dimensional reduction</strong></h4>
<p>At this point we need to segment our data. We are going to use <a href="http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.train_test_split.html">train_test_split</a>, which will take care of splitting our data into random training and testing data sets. Next, we note that we have a lot of features and that there are advantages to having fewer: First, the computational cost is reduced. Second, having fewer features reduces the data’s dimension which can also reduce the complexity of the model and help avoid overfitting. Instead of dropping individual pixels outright, we will carry out a dimensional reduction via a Principle Component Analysis <a href="http://en.wikipedia.org/wiki/Principal_component_analysis"><span class="caps">PCA</span></a>. <span class="caps">PCA</span> works by attempting to represent the variance in the training data with as few dimensions as possible. So instead of dropping features, as we did in our <a href="http://efavdb.github.io/machine-learning-with-wearable-sensors">wearable sensor example</a> analysis, here we will compress features together, and then use only the most important feature combinations. When this is done to images, the features returned by <span class="caps">PCA</span> are commonly called eigenfaces (some examples are given below).</p>
<p>The function we are going to use to carry out our <span class="caps">PCA</span> is <a href="http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.RandomizedPCA.html">RandomizedPCA</a>. We’ll keep the top 150 eigenfaces, and we’ll also whiten the data — ie normalize our new, principal component feature set. The goal of whitening is to make the input less redundant. Whitening is performed by rotating into the coordinate space of the principal components, dividing each dimension by square root of variance in that direction (giving the feature unit variance), and then rotating back to pixel space.</p>
<div class="highlight"><pre><span></span><code># split into a training and testing set
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25)
# Compute the PCA (eigenfaces) on the face dataset
n_components = 150
pca = RandomizedPCA(
n_components=n_components, whiten=True).fit(X_train)
eigenfaces = pca.components_.reshape((n_components, h, w))
X_train_pca = pca.transform(X_train)
</code></pre></div>
<h4><strong>Visualizing the eigenfaces</strong></h4>
<p>Let’s now take a moment to examine the dataset’s principal eigenfaces: the set of images that we will project each example onto to obtain independent features. To do that we will use the following helper function to make life easier — visual follows.</p>
<div class="highlight"><pre><span></span><code><span class="err">#</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="n">helper</span><span class="w"> </span><span class="k">function</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">make</span><span class="w"> </span><span class="n">plots</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">faces</span>
<span class="n">def</span><span class="w"> </span><span class="n">plot_gallery</span><span class="p">(</span><span class="n">images</span><span class="p">,</span><span class="w"> </span><span class="n">titles</span><span class="p">,</span><span class="w"> </span><span class="n">h</span><span class="p">,</span><span class="w"> </span><span class="n">w</span><span class="p">,</span><span class="w"> </span><span class="n">n_row</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="n">n_col</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span><span class="err">:</span>
<span class="n">plt</span><span class="p">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mf">1.8</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">n_col</span><span class="p">,</span><span class="w"> </span><span class="mf">2.4</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">n_row</span><span class="p">))</span>
<span class="n">plt</span><span class="p">.</span><span class="n">subplots_adjust</span><span class="p">(</span><span class="n">bottom</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="nf">left</span><span class="o">=</span><span class="mf">.01</span><span class="p">,</span><span class="w"> </span><span class="nf">right</span><span class="o">=</span><span class="mf">.99</span><span class="p">,</span>
<span class="k">top</span><span class="o">=</span><span class="mf">.90</span><span class="p">,</span><span class="w"> </span><span class="n">hspace</span><span class="o">=</span><span class="mf">.35</span><span class="p">)</span>
<span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="k">range</span><span class="p">(</span><span class="n">n_row</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">n_col</span><span class="p">)</span><span class="err">:</span>
<span class="n">plt</span><span class="p">.</span><span class="n">subplot</span><span class="p">(</span><span class="n">n_row</span><span class="p">,</span><span class="w"> </span><span class="n">n_col</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">imshow</span><span class="p">(</span><span class="n">images</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">.</span><span class="n">reshape</span><span class="p">((</span><span class="n">h</span><span class="p">,</span><span class="w"> </span><span class="n">w</span><span class="p">)),</span><span class="w"> </span><span class="n">cmap</span><span class="o">=</span><span class="n">plt</span><span class="p">.</span><span class="n">cm</span><span class="p">.</span><span class="n">gray</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">title</span><span class="p">(</span><span class="n">titles</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">,</span><span class="w"> </span><span class="k">size</span><span class="o">=</span><span class="mi">12</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">xticks</span><span class="p">(())</span>
<span class="n">plt</span><span class="p">.</span><span class="n">yticks</span><span class="p">(())</span>
<span class="err">#</span><span class="w"> </span><span class="n">Plot</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">gallery</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">most</span><span class="w"> </span><span class="n">significative</span><span class="w"> </span><span class="n">eigenfaces</span>
<span class="n">eigenface_titles</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span>
<span class="n">&quot;eigenface %d&quot; % i for i in range(eigenfaces.shape[0</span><span class="o">]</span><span class="p">)</span><span class="err">]</span>
<span class="n">plot_gallery</span><span class="p">(</span><span class="n">eigenfaces</span><span class="p">,</span><span class="w"> </span><span class="n">eigenface_titles</span><span class="p">,</span><span class="w"> </span><span class="n">h</span><span class="p">,</span><span class="w"> </span><span class="n">w</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div>
<p><a href="./wp-content/uploads/2015/01/Screen-Shot-2015-01-21-at-12.36.09-PM.png"><img alt="Screen Shot 2015-01-21 at 12.36.09 PM" src="./wp-content/uploads/2015/01/Screen-Shot-2015-01-21-at-12.36.09-PM.png"></a></p>
<h4><strong>Training a model</strong></h4>
<p>Now that we have reduced the dimensionality of the data it is time to go ahead and train a model. I am going to use the same <span class="caps">SVM</span> and GridSearchCV method I explained in my previous <a href="http://efavdb.github.io/machine-learning-with-wearable-sensors">post</a>. However, instead of using a linear kernel, as we did last time, I’ll use instead a <a href="http://scikit-learn.org/stable/auto_examples/svm/plot_rbf_parameters.html">radial basis function (<span class="caps">RBF</span>)</a> kernel. The <span class="caps">RBF</span> kernel is a good choice here since we’d like to have non-linear decision boundaries — in general, it’s a reasonable idea to try this out whenever the number of training examples outnumbers the number of features characterizing those examples. The parameter C here acts as a regularization term: Small C values give you smooth decision boundaries, while large C values give complicated boundaries that attempt to fit/accommodate all training data. The gamma parameter defines how far the influence of a single point example extends (the width of the <span class="caps">RBF</span> kernel).</p>
<div class="highlight"><pre><span></span><code><span class="p">#</span><span class="nn">Train</span><span class="w"> </span><span class="nt">a</span><span class="w"> </span><span class="nt">SVM</span><span class="w"> </span><span class="nt">classification</span><span class="w"> </span><span class="nt">model</span>
<span class="nt">print</span><span class="o">(&</span><span class="nt">quot</span><span class="o">;</span><span class="nt">Fitting</span><span class="w"> </span><span class="nt">the</span><span class="w"> </span><span class="nt">classifier</span><span class="w"> </span><span class="nt">to</span><span class="w"> </span><span class="nt">the</span><span class="w"> </span><span class="nt">training</span><span class="w"> </span><span class="nt">set</span><span class="o">&</span><span class="nt">quot</span><span class="o">;)</span>
<span class="nt">t0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nt">time</span><span class="o">()</span>
<span class="nt">param_grid</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="err">'C':</span><span class="w"> </span><span class="cp">[</span><span class="mi">1</span><span class="nx">e3</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="nx">e3</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="nx">e4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="nx">e4</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="nx">e5</span><span class="cp">]</span><span class="err">,</span>
<span class="err">'gamma':</span><span class="w"> </span><span class="cp">[</span><span class="mf">0.0001</span><span class="p">,</span><span class="w"> </span><span class="mf">0.0005</span><span class="p">,</span><span class="w"> </span><span class="mf">0.001</span><span class="p">,</span><span class="w"> </span><span class="mf">0.005</span><span class="p">,</span><span class="w"> </span><span class="mf">0.01</span><span class="p">,</span><span class="w"> </span><span class="mf">0.1</span><span class="cp">]</span><span class="err">,</span><span class="w"> </span><span class="p">}</span>
<span class="nt">clf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nt">GridSearchCV</span><span class="o">(</span>
<span class="nt">SVC</span><span class="o">(</span><span class="nt">kernel</span><span class="o">=</span><span class="s1">'rbf'</span><span class="o">,</span><span class="w"> </span><span class="nt">class_weight</span><span class="o">=</span><span class="s1">'auto'</span><span class="o">),</span><span class="w"> </span><span class="nt">param_grid</span><span class="o">)</span>
<span class="nt">clf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nt">clf</span><span class="p">.</span><span class="nc">fit</span><span class="o">(</span><span class="nt">X_train_pca</span><span class="o">,</span><span class="w"> </span><span class="nt">y_train</span><span class="o">)</span>
<span class="nt">print</span><span class="o">(&</span><span class="nt">quot</span><span class="o">;</span><span class="nt">done</span><span class="w"> </span><span class="nt">in</span><span class="w"> </span><span class="o">%</span><span class="nt">0</span><span class="p">.</span><span class="nc">3fs</span><span class="o">&</span><span class="nt">quot</span><span class="o">;</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="o">(</span><span class="nt">time</span><span class="o">()</span><span class="w"> </span><span class="nt">-</span><span class="w"> </span><span class="nt">t0</span><span class="o">))</span>
<span class="nt">print</span><span class="o">(&</span><span class="nt">quot</span><span class="o">;</span><span class="nt">Best</span><span class="w"> </span><span class="nt">estimator</span><span class="w"> </span><span class="nt">found</span><span class="w"> </span><span class="nt">by</span><span class="w"> </span><span class="nt">grid</span><span class="w"> </span><span class="nt">search</span><span class="o">:&</span><span class="nt">quot</span><span class="o">;)</span>
<span class="nt">print</span><span class="o">(</span><span class="nt">clf</span><span class="p">.</span><span class="nc">best_estimator_</span><span class="o">)</span>
<span class="o">&</span><span class="nt">gt</span><span class="o">;&</span><span class="nt">gt</span><span class="o">;</span>
<span class="nt">Fitting</span><span class="w"> </span><span class="nt">the</span><span class="w"> </span><span class="nt">classifier</span><span class="w"> </span><span class="nt">to</span><span class="w"> </span><span class="nt">the</span><span class="w"> </span><span class="nt">training</span><span class="w"> </span><span class="nt">set</span>
<span class="nt">done</span><span class="w"> </span><span class="nt">in</span><span class="w"> </span><span class="nt">16</span><span class="p">.</span><span class="nc">056s</span>
<span class="nt">Best</span><span class="w"> </span><span class="nt">estimator</span><span class="w"> </span><span class="nt">found</span><span class="w"> </span><span class="nt">by</span><span class="w"> </span><span class="nt">grid</span><span class="w"> </span><span class="nt">search</span><span class="o">:</span>
<span class="nt">SVC</span><span class="o">(</span><span class="nt">C</span><span class="o">=</span><span class="nt">1000</span><span class="p">.</span><span class="nc">0</span><span class="o">,</span><span class="w"> </span><span class="nt">cache_size</span><span class="o">=</span><span class="nt">200</span><span class="o">,</span><span class="w"> </span><span class="nt">class_weight</span><span class="o">=</span><span class="s1">'auto'</span><span class="o">,</span><span class="w"> </span><span class="nt">coef0</span><span class="o">=</span><span class="nt">0</span><span class="p">.</span><span class="nc">0</span><span class="o">,</span>
<span class="nt">degree</span><span class="o">=</span><span class="nt">3</span><span class="o">,</span><span class="w"> </span><span class="nt">gamma</span><span class="o">=</span><span class="nt">0</span><span class="p">.</span><span class="nc">001</span><span class="o">,</span><span class="w"> </span><span class="nt">kernel</span><span class="o">=</span><span class="s1">'rbf'</span><span class="o">,</span><span class="w"> </span><span class="nt">max_iter</span><span class="o">=</span><span class="nt">-1</span><span class="o">,</span>
<span class="nt">probability</span><span class="o">=</span><span class="nt">False</span><span class="o">,</span><span class="w"> </span><span class="nt">random_state</span><span class="o">=</span><span class="nt">None</span><span class="o">,</span><span class="w"> </span><span class="nt">shrinking</span><span class="o">=</span><span class="nt">True</span><span class="o">,</span>
<span class="nt">tol</span><span class="o">=</span><span class="nt">0</span><span class="p">.</span><span class="nc">001</span><span class="o">,</span><span class="w"> </span><span class="nt">verbose</span><span class="o">=</span><span class="nt">False</span><span class="o">)</span>
</code></pre></div>
<h4><strong>Model validation</strong></h4>
<p>That’s it for training! Next we’ll validate our model on the testing data set. Below, we first use our <span class="caps">PCA</span> model to transform the testing data into our current feature space. Then, we apply our model to make predictions on this set. To get a feel for how well the model is doing, we print a <a href="http://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html">classification_report</a> and a <a href="http://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html">confusion matrix</a>.</p>
<div class="highlight"><pre><span></span><code>#Quantitative evaluation of the model quality on the test set
#Validate the data
X_test_pca = pca.transform(X_test)
y_pred = clf.predict(X_test_pca)
print(classification_report(
y_test, y_pred, target_names=target_names))
print('Confusion Matrix')
#Make a data frame so we can have some nice labels
cm = confusion_matrix(y_test, y_pred, labels=range(n_classes))
df = pd.DataFrame(cm, columns = target_names, index = target_names)
print(df)
&gt;&gt;
precision recall f1-score support
Ariel Sharon 0.81 0.85 0.83 20
Colin Powell 0.82 0.78 0.80 54
Donald Rumsfeld 0.78 0.67 0.72 27
George W Bush 0.87 0.95 0.91 139
Gerhard Schroeder 0.86 0.73 0.79 26
Hugo Chavez 1.00 0.75 0.86 20
Tony Blair 0.84 0.89 0.86 36
avg / total 0.85 0.85 0.85 322
Confusion Matrix
Ariel Sharon Colin Powell Donald Rumsfeld George W Bush \
Ariel Sharon 17 3 0 0
Colin Powell 1 42 1 7
Donald Rumsfeld 3 1 18 3
George W Bush 0 3 3 132
Gerhard Schroeder 0 1 1 3
Hugo Chavez 0 0 0 4
Tony Blair 0 1 0 3
Gerhard Schroeder Hugo Chavez Tony Blair
Ariel Sharon 0 0 0
Colin Powell 1 0 2
Donald Rumsfeld 1 0 1
George W Bush 0 0 1
Gerhard Schroeder 19 0 2
Hugo Chavez 1 15 0
Tony Blair 0 0 32
</code></pre></div>
<p>As a quick reminder, lets define what the terms above are:</p>
<ol>
<li>precision is the ratio Tp / (Tp + Fp) where Tp is the number of true positives and Fp the number of false positives.</li>
<li>recall is the ration of Tp / (Tp + Fn) where Fn is the number of false negatives.</li>
<li>f1-score is (precision * recall) / (precision + recall)</li>
<li>support is the total number of occurrences of each face.</li>
</ol>
<p>In our second table here, we have printed a confusion matrix, which provides a nice summary visualization of our results: Each row is the actual class, and the columns are the predicted class. For example, in row 1 there are 17 correct identifications of Arial Sharon, and 5 wrong ones. Using our previously defined helper plotting function, we show some examples of predicted vs true names below. Our simple algorithm’s accuracy is imperfect, yet satisfying!</p>
<div class="highlight"><pre><span></span><code><span class="n">#Plot</span><span class="w"> </span><span class="n">predictions</span><span class="w"> </span><span class="k">on</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">portion</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">test</span><span class="w"> </span><span class="k">set</span>
<span class="n">def</span><span class="w"> </span><span class="n">title</span><span class="p">(</span><span class="n">y_pred</span><span class="p">,</span><span class="w"> </span><span class="n">y_test</span><span class="p">,</span><span class="w"> </span><span class="n">target_names</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">)</span><span class="err">:</span>
<span class="n">pred_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">target_names</span><span class="o">[</span><span class="n">y_pred[i</span><span class="o">]</span><span class="err">]</span><span class="p">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s1">' '</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="o">[</span><span class="n">-1</span><span class="o">]</span>
<span class="n">true_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">target_names</span><span class="o">[</span><span class="n">y_test[i</span><span class="o">]</span><span class="err">]</span><span class="p">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s1">' '</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="o">[</span><span class="n">-1</span><span class="o">]</span>
<span class="k">return</span><span class="w"> </span><span class="s1">'predicted: %s\ntrue: %s'</span><span class="o">%</span><span class="p">(</span><span class="n">pred_name</span><span class="p">,</span><span class="w"> </span><span class="n">true_name</span><span class="p">)</span>
<span class="n">prediction_titles</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span><span class="n">title(y_pred, y_test, target_names, i)</span>
<span class="n">for i in range(y_pred.shape[0</span><span class="o">]</span><span class="p">)</span><span class="err">]</span>
<span class="n">plot_gallery</span><span class="p">(</span><span class="n">X_test</span><span class="p">,</span><span class="w"> </span><span class="n">prediction_titles</span><span class="p">,</span><span class="w"> </span><span class="n">h</span><span class="p">,</span><span class="w"> </span><span class="n">w</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span>
</code></pre></div>
<p><a href="./wp-content/uploads/2015/01/download.png"><img alt="download" src="./wp-content/uploads/2015/01/download.png"></a></p>
<h4><strong>Discussion</strong></h4>
<p>85% average accuracy shows that <span class="caps">PCA</span> (Eigenface analysis) can provide accurate face recognition results, given just a modest amount of training data. There are pros and cons to eigenfaces however:</p>
<ul>
<li>Pros<ol>
<li>Training can be automated.</li>
<li>Once the the eigenfaces are calculated, face recognition can be performed in real time.</li>
<li>Eigenfaces can handle large databases.</li>
</ol>
</li>
<li>Cons<ol>
<li>Sensitive to lighting conditions.</li>
<li>Expression changes are not handled well.</li>
<li>Has trouble when the face angle changes.</li>
<li>Difficult to interpret eigenfaces: Eg, one can’t easily read off from these eye separation distance, etc.</li>
</ol>
</li>
</ul>
<p>There are more advanced facial recognition methods that take advantage of features special to faces. One example is provided by the <a href="http://en.wikipedia.org/wiki/Active_appearance_model">Active Appearance Model (<span class="caps">AAM</span>)</a>, which finds facial features (nose, mouth, etc.), and then identifies relationships between these to carry out identifications. Whatever the approach, the overall methodology is the same for all facial recognition algorithms:</p>
<ol>
<li>Take a labeled set of faces.</li>
<li>Extract features from those faces using some method of choice (eg eigenfaces).</li>
<li>Train a machine learning model on those features.</li>
<li>Extract features from a new face, and predict the identity.</li>
</ol>
<p>The story doesn’t end with finding faces in photos. Facial recognition is just a subset of machine vision, which is currently being applied widely in industry. For example, Intel and other semiconductor manufactures use machine vision to detect defects in the chips being produced — one application where by-hand (human) analysis is not possible and computers have the upper hand.</p>
<p id="post-share-links">
Like this post? Share on:
<a href="https://twitter.com/intent/tweet?text=Machine%20learning%20for%20facial%C2%A0recognition&url=/machine-learning-for-facial-recognition-3&hashtags=machine-learning" target="_blank" rel="nofollow noopener noreferrer" title="Share on Twitter">Twitter</a>
|
<a href="https://www.facebook.com/sharer/sharer.php?u=/machine-learning-for-facial-recognition-3" target="_blank" rel="nofollow noopener noreferrer" title="Share on Facebook">Facebook</a>
|
<a href="mailto:?subject=Machine%20learning%20for%20facial%C2%A0recognition&body=/machine-learning-for-facial-recognition-3" target="_blank" rel="nofollow noopener noreferrer" title="Share via Email">Email</a>
</p>
<hr />
<div class="author_blurb">
<a href="" target="_blank" rel="nofollow noopener noreferrer">
<img src=/wp-content/uploads/2014/12/headshot.jpg alt="Damien RJ Avatar" title="Damien RJ">
<span class="author_name">Damien RJ</span>
</a>
Damien is a highly experienced researcher with a background in clinical and applied research. Like JSL, he got his PhD at UCLA. He has many years of experience working with imaging, and has a particularly strong background in image segmentation, registration, detection, data analysis, and more recently machine learning. He now works as a data-scientist at Square in San Francisco.
</div>
<hr/>
<aside>
<nav>
<ul class="articles-timeline">
<li class="previous-article">« <a href="./quick-tutorial-on-mysql" title="Previous: Quick tutorial on MySQL">Quick tutorial on MySQL</a></li>
<li class="next-article"><a href="./nba-week-11-summary-week-12-predictions" title="Next: NBA week 11 summary, week 12 predictions">NBA week 11 summary, week 12 predictions</a> »</li>
</ul>
</nav>
</aside>
</div>
<section id="article-sidebar" class="span2">
<h4>Published</h4>
<time itemprop="dateCreated" datetime="2015-01-21T17:12:00-08:00">Jan 21, 2015</time>
<h4>Category</h4>
<a class="category-link" href="./categories.html#case-studies-ref">Case studies</a>
<h4>Tags</h4>
<ul class="list-of-tags tags-in-article">
<li><a href="./tags.html#machine-learning-ref">machine learning
<span>10</span>
</a></li>
</ul>
<h4>Contact</h4>
<div id="sidebar-social-link">
<a href="https://twitter.com/efavdb" title="" target="_blank" rel="nofollow noopener noreferrer">
<svg xmlns="http://www.w3.org/2000/svg" aria-label="Twitter" role="img" viewBox="0 0 512 512"><rect width="512" height="512" rx="15%" fill="#1da1f3"/><path fill="#fff" d="M437 152a72 72 0 0 1-40 12 72 72 0 0 0 32-40 72 72 0 0 1-45 17 72 72 0 0 0-122 65 200 200 0 0 1-145-74 72 72 0 0 0 22 94 72 72 0 0 1-32-7 72 72 0 0 0 56 69 72 72 0 0 1-32 1 72 72 0 0 0 67 50 200 200 0 0 1-105 29 200 200 0 0 0 309-179 200 200 0 0 0 35-37"/></svg>
</a>
<a href="https://github.com/efavdb" title="" target="_blank" rel="nofollow noopener noreferrer">
<svg xmlns="http://www.w3.org/2000/svg" aria-label="GitHub" role="img" viewBox="0 0 512 512"><rect width="512" height="512" rx="15%" fill="#1B1817"/><path fill="#fff" d="M335 499c14 0 12 17 12 17H165s-2-17 12-17c13 0 16-6 16-12l-1-50c-71 16-86-28-86-28-12-30-28-37-28-37-24-16 1-16 1-16 26 2 40 26 40 26 22 39 59 28 74 22 2-17 9-28 16-35-57-6-116-28-116-126 0-28 10-51 26-69-3-6-11-32 3-67 0 0 21-7 70 26 42-12 86-12 128 0 49-33 70-26 70-26 14 35 6 61 3 67 16 18 26 41 26 69 0 98-60 120-117 126 10 8 18 24 18 48l-1 70c0 6 3 12 16 12z"/></svg>
</a>
<a href="https://www.youtube.com/channel/UClfvjoSiu0VvWOh5OpnuusA" title="" target="_blank" rel="nofollow noopener noreferrer">
<svg xmlns="http://www.w3.org/2000/svg" aria-label="YouTube" role="img" viewBox="0 0 512 512" fill="#ed1d24"><rect width="512" height="512" rx="15%"/><path d="m427 169c-4-15-17-27-32-31-34-9-239-10-278 0-15 4-28 16-32 31-9 38-10 135 0 174 4 15 17 27 32 31 36 10 241 10 278 0 15-4 28-16 32-31 9-36 9-137 0-174" fill="#fff"/><path d="m220 203v106l93-53"/></svg>
</a>
</div>
</section>
</div>
</article>
<button onclick="topFunction()" id="myBtn" title="Go to top">▲</button>
</div>
<div class="span1"></div>
</div>
</div>
</div>
<footer>
<div>
<span class="site-name"><a href= .>EFAVDB</span> - Everybody's Favorite Data Blog</a>
</div>
<!-- <div id="fpowered"> -->
<!-- Powered by: <a href="http://getpelican.com/" title="Pelican Home Page" target="_blank" rel="nofollow noopener noreferrer">Pelican</a> -->
<!-- Theme: <a href="https://elegant.oncrashreboot.com/" title="Theme Elegant Home Page" target="_blank" rel="nofollow noopener noreferrer">Elegant</a> -->
<!-- -->
<!-- </div> -->
</footer> <script src="//code.jquery.com/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
<script>
function validateForm(query)
{
return (query.length > 0);
}
</script>
<script>
//** Scroll to top button **
//Get the button:
mybutton = document.getElementById("myBtn");
// When the user scrolls down 30px from the top of the document, show the button
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 30 || document.documentElement.scrollTop > 30) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
// When the user clicks on the button, scroll to the top of the document
function topFunction() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}
</script>
<script>
(function () {
if (window.location.hash.match(/^#comment-\d+$/)) {
$('#comment_thread').collapse('show');
}
})();
window.onhashchange=function(){
if (window.location.hash.match(/^#comment-\d+$/))
window.location.reload(true);
}
$('#comment_thread').on('shown', function () {
var link = document.getElementById('comment-accordion-toggle');
var old_innerHTML = link.innerHTML;
$(link).fadeOut(200, function() {
$(this).text('Click here to hide comments').fadeIn(200);
});
$('#comment_thread').on('hidden', function () {
$(link).fadeOut(200, function() {
$(this).text(old_innerHTML).fadeIn(200);
});
})
})
</script>
</body>
<!-- Theme: Elegant built for Pelican
License : MIT -->
</html>