3.9. Face RecognitionΒΆ

In [1]:
# set up pose recognizer network

from conx import *

net = Network("Pose recognizer")
Using Theano backend.
conx, version 3.5.9
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()

Dataset name: CMU Faces, full-size

Original source: http://archive.ics.uci.edu/ml/datasets/cmu+face+images

Dataset Split: * training : 375 * testing : 249 * total : 624

Input Summary: * shape : [(120, 128)] * range : [(0.0, 1.0)]

Target Summary: * 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 [7]:
net["output"].colormap = "Greys_r"
net["output"].minmax = (0, 1)
net["input"].colormap = "Greys_r"
net["input"].minmax = (0, 1)
In [8]:
net.dashboard()
In [9]:
net.reset()
net.evaluate()
Out[9]:
{'acc': 0.0,
 'loss': 0.29462089133262637,
 'val_acc': 0.0,
 'val_loss': 0.29715675737963143}
In [10]:
net.test()
========================================================
Testing validation dataset with tolerance 0.1...
Total count: 375
      correct: 0
      incorrect: 375
Total percentage correct: 0.0
In [11]:
if net.saved():
    net.load()
    net.plot_loss_acc()
else:
    net.train(200, accuracy=1.0, tolerance=0.25, report_rate=5, plot=True)
    net.save()
_images/Face_recognition_11_0.png
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.01761 |   0.94385 |   0.02809 |   0.88353
#  203 |   0.03053 |   0.85561 |   0.03633 |   0.76400
#  206 |   0.02527 |   0.89840 |   0.02863 |   0.86800
#  209 |   0.02383 |   0.88235 |   0.02733 |   0.86800
========================================================================
#  210 |   0.02345 |   0.89840 |   0.03496 |   0.78400
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.02345 |   0.89840 |   0.03457 |   0.78715
#  215 |   0.02085 |   0.91176 |   0.02616 |   0.86800
#  220 |   0.01954 |   0.92246 |   0.02534 |   0.87200
========================================================================
#  220 |   0.01954 |   0.92246 |   0.02534 |   0.87200
In [14]:
net.plot_loss_acc()
_images/Face_recognition_14_0.png
In [15]:
net.plot(['acc', 'val_acc'])
_images/Face_recognition_15_0.png
In [16]:
net.plot("all", title="All available metrics")
_images/Face_recognition_16_0.png
In [17]:
net.plot(['loss', 'val_loss'], ymax=0.1, start=50, end=150)
_images/Face_recognition_17_0.png
In [23]:
net.plot(['.*loss'], ymax=0.1, start=50, end=150)
_images/Face_recognition_18_0.png
In [24]:
net.plot_layer_weights('hidden', vshape=net.dataset.inputs.shape[0])
net.plot_layer_weights('output', colormap='viridis')
_images/Face_recognition_19_0.png
_images/Face_recognition_19_1.png
In [25]:
# colorbar colors or values can be easily changed
net.plot_layer_weights('hidden', vshape=net.dataset.inputs.shape[0],
                       units=(2,1,0), colormap="RdBu", wrange=(-0.25, 0.25))
net.plot_layer_weights('output', units=0, wmin=0, colormap="copper", ticks=10)
_images/Face_recognition_20_0.png
_images/Face_recognition_20_1.png
In [26]:
net.show_unit_weights('output', 0)
 3.19 -0.84 -3.78
In [27]:
net.get_weights("output")
Out[27]:
[[[3.1901586055755615,
   -3.335933208465576,
   -3.033083200454712,
   2.358121871948242],
  [-0.8359531760215759,
   -0.8237844705581665,
   1.106552004814148,
   1.6112408638000488],
  [-3.7805275917053223,
   3.049931764602661,
   -2.796229362487793,
   2.2683372497558594]],
 [-1.223926305770874,
  -1.3004560470581055,
  0.8036181330680847,
  -4.329024314880371]]