Face RecognitionΒΆ

In [1]:
# set up pose recognizer network

from conx import *

net = Network("Pose recognizer")
conx, version 3.4.0
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 [4]:
net.dataset.inputs.shape
Out[4]:
[(120, 128)]
In [5]:
array2image(net.dataset.inputs[0])
Out[5]:
_images/Face_recognition_4_0.png
In [6]:
net.dataset.targets[0]
Out[6]:
[1, 0, 0, 0]
In [11]:
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 [12]:
net.dashboard()
In [13]:
net.reset()
net.evaluate()
Out[13]:
{'acc': 0.0,
 'loss': 0.2563292597134908,
 'val_acc': 0.0,
 'val_loss': 0.25538724409528524}
In [14]:
net.train(200, accuracy=1.0, tolerance=0.25, report_rate=5, plot='notebook')
_images/Face_recognition_9_0.svg
========================================================================
       |  Training |  Training |  Validate |  Validate
Epochs |     Error |  Accuracy |     Error |  Accuracy
------ | --------- | --------- | --------- | ---------
#  200 |   0.01760 |   0.96791 |   0.03906 |   0.83200
In [15]:
net.train(10, accuracy=1.0, tolerance=0.25, report_rate=3)
Training...
       |  Training |  Training |  Validate |  Validate
Epochs |     Error |  Accuracy |     Error |  Accuracy
------ | --------- | --------- | --------- | ---------
#  200 |   0.01760 |   0.96791 |   0.03906 |   0.83200
#  203 |   0.01731 |   0.97594 |   0.03872 |   0.85200
#  206 |   0.01696 |   0.97326 |   0.03936 |   0.83600
#  209 |   0.01673 |   0.97059 |   0.03817 |   0.84800
========================================================================
#  210 |   0.01663 |   0.97594 |   0.03842 |   0.84000
In [16]:
net.train(10, accuracy=1.0, tolerance=0.25, report_rate=5)
Training...
       |  Training |  Training |  Validate |  Validate
Epochs |     Error |  Accuracy |     Error |  Accuracy
------ | --------- | --------- | --------- | ---------
#  210 |   0.01663 |   0.97594 |   0.03842 |   0.84000
#  215 |   0.01619 |   0.97326 |   0.03833 |   0.84800
#  220 |   0.01571 |   0.98128 |   0.03782 |   0.84400
========================================================================
#  220 |   0.01571 |   0.98128 |   0.03782 |   0.84400
In [17]:
net.plot()
_images/Face_recognition_12_0.png
In [18]:
net.plot(['acc', 'val_acc'])
_images/Face_recognition_13_0.png
In [19]:
net.plot(net.get_metrics(), title="All available metrics")
_images/Face_recognition_14_0.png
In [20]:
net.plot(['loss', 'val_loss'], ymax=0.1, start=50, end=150)
_images/Face_recognition_15_0.png
In [23]:
net.plot_layer_weights('hidden', vshape=net.dataset.inputs.shape[0])
net.plot_layer_weights('output', cmap='viridis')
_images/Face_recognition_16_0.png
_images/Face_recognition_16_1.png
In [24]:
# 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_17_0.png
_images/Face_recognition_17_1.png
In [25]:
net.show_unit_weights('output', 0)
-3.80 -2.43  1.98