3.11. Face RecognitionΒΆ

In [1]:
# set up pose recognizer network

from conx import *

net = Network("Pose recognizer")
conx, version 3.4.3
Using Theano backend.
In [2]:
# uncomment one of the following lines:

datafile = 'cmu_faces_full_size'
#datafile = 'cmu_faces_half_size'
#datafile = 'cmu_faces_quarter_size'

net.dataset.get(datafile)
net.dataset.split(0.40)  # use 40% of the dataset for validation
net.dataset.shuffle()
net.dataset.summary()
Input Summary:
   count  : 624 (375 for training, 249 for testing)
   shape  : [(120, 128)]
   range  : (0.0, 1.0)
Target Summary:
   count  : 624 (375 for training, 249 for testing)
   shape  : [(4,)]
   range  : (0, 1)
In [3]:
net.dataset.inputs.shape
Out[3]:
[(120, 128)]
In [4]:
array2image(net.dataset.inputs[0])
Out[4]:
_images/Face_recognition_4_0.png
In [5]:
net.dataset.targets[0]
Out[5]:
[0, 0, 0, 1]
In [6]:
net.add(Layer('input', net.dataset.inputs.shape[0]))
net.add(FlattenLayer("flatten"))
net.add(Layer('hidden', 3, activation='sigmoid'))
net.add(Layer('output', 4, activation='sigmoid'))
net.connect()
net.compile(loss='mean_squared_error', optimizer=SGD(lr=0.3, momentum=0.1))
In [8]:
net["output"].colormap = "Greys_r"
net["output"].minmax = (0, 1)
In [9]:
net.dashboard()
In [10]:
net.reset()
net.evaluate()
Out[10]:
{'acc': 0.0,
 'loss': 0.23963996656735739,
 'val_acc': 0.0,
 'val_loss': 0.23864392912292098}
In [11]:
net.train(200, accuracy=1.0, tolerance=0.25, report_rate=5, plot='notebook')
_images/Face_recognition_10_0.svg
========================================================================
       |  Training |  Training |  Validate |  Validate
Epochs |     Error |  Accuracy |     Error |  Accuracy
------ | --------- | --------- | --------- | ---------
#  200 |   0.02065 |   0.70321 |   0.04646 |   0.54800
In [12]:
net.train(10, accuracy=1.0, tolerance=0.25, report_rate=3)
Evaluating initial validation metrics...
Training...
       |  Training |  Training |  Validate |  Validate
Epochs |     Error |  Accuracy |     Error |  Accuracy
------ | --------- | --------- | --------- | ---------
#  200 |   0.02065 |   0.70321 |   0.04656 |   0.55020
#  203 |   0.02035 |   0.70856 |   0.04617 |   0.54800
#  206 |   0.02003 |   0.71658 |   0.04647 |   0.57200
#  209 |   0.01976 |   0.78075 |   0.04635 |   0.60400
========================================================================
#  210 |   0.01965 |   0.77005 |   0.04562 |   0.61200
In [13]:
net.train(10, accuracy=1.0, tolerance=0.25, report_rate=5)
Evaluating initial validation metrics...
Training...
       |  Training |  Training |  Validate |  Validate
Epochs |     Error |  Accuracy |     Error |  Accuracy
------ | --------- | --------- | --------- | ---------
#  210 |   0.01965 |   0.77005 |   0.04571 |   0.61446
#  215 |   0.01918 |   0.81016 |   0.04547 |   0.63600
#  220 |   0.01874 |   0.83690 |   0.04506 |   0.67200
========================================================================
#  220 |   0.01874 |   0.83690 |   0.04506 |   0.67200
In [14]:
net.plot()
_images/Face_recognition_13_0.png
In [15]:
net.plot(['acc', 'val_acc'])
_images/Face_recognition_14_0.png
In [16]:
net.plot(net.get_metrics(), title="All available metrics")
_images/Face_recognition_15_0.png
In [17]:
net.plot(['loss', 'val_loss'], ymax=0.1, start=50, end=150)
_images/Face_recognition_16_0.png
In [18]:
net.plot_layer_weights('hidden', vshape=net.dataset.inputs.shape[0])
net.plot_layer_weights('output', cmap='viridis')
_images/Face_recognition_17_0.png
_images/Face_recognition_17_1.png
In [19]:
# colorbar colors or values can be easily changed
net.plot_layer_weights('hidden', vshape=net.dataset.inputs.shape[0],
                       units=(2,1,0), cmap="RdBu", wrange=(-0.25, 0.25))
net.plot_layer_weights('output', units=0, wmin=0, cmap="copper", ticks=10)
_images/Face_recognition_18_0.png
_images/Face_recognition_18_1.png
In [20]:
net.show_unit_weights('output', 0)
-2.29 -4.08  1.34