3.10. Face RecognitionΒΆ

In [1]:
# set up pose recognizer network

from conx import *

net = Network("Pose recognizer")
Using Theano backend.
conx, version 3.5.0
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, 1, 0]
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.31998993094762168,
 'val_acc': 0.0,
 'val_loss': 0.31493170744922744}
In [10]:
net.test()
========================================================
Testing validation dataset with tolerance 0.1000...
Total count: 375
      correct: 0
      incorrect: 375
Total percentage correct: 0.0
In [12]:
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 [16]:
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.06698 |   0.52139 |   0.08058 |   0.45382
#  203 |   0.06686 |   0.52139 |   0.08045 |   0.45200
#  206 |   0.06673 |   0.52139 |   0.08051 |   0.45200
#  209 |   0.06661 |   0.52139 |   0.08042 |   0.45200
========================================================================
#  210 |   0.06656 |   0.52139 |   0.08042 |   0.45200
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
------ | --------- | --------- | --------- | ---------
#  200 |   0.06698 |   0.52139 |   0.06829 |   0.53414
#  205 |   0.07455 |   0.47326 |   0.06814 |   0.53600
#  210 |   0.07430 |   0.47594 |   0.06830 |   0.53600
========================================================================
#  210 |   0.07430 |   0.47594 |   0.06830 |   0.53600
In [15]:
net.plot_loss_acc()
_images/Face_recognition_14_0.png
In [16]:
net.plot(['acc', 'val_acc'])
_images/Face_recognition_15_0.png
In [17]:
net.plot("all", title="All available metrics")
_images/Face_recognition_16_0.png
In [18]:
net.plot(['loss', 'val_loss'], ymax=0.1, start=50, end=150)
_images/Face_recognition_17_0.png
In [19]:
net.plot_layer_weights('hidden', vshape=net.dataset.inputs.shape[0])
net.plot_layer_weights('output', cmap='viridis')
_images/Face_recognition_18_0.png
_images/Face_recognition_18_1.png
In [20]:
# 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_19_0.png
_images/Face_recognition_19_1.png
In [21]:
net.show_unit_weights('output', 0)
-4.35 -0.87  0.70
In [22]:
net.get_weights("output")
Out[22]:
[[[-4.349478244781494,
   1.965081810951233,
   2.0686089992523193,
   0.4166243374347687],
  [-0.8687325716018677,
   -2.2529144287109375,
   -2.2216029167175293,
   4.495495796203613],
  [0.7045143842697144,
   -1.0950219631195068,
   -1.5272759199142456,
   -1.837742567062378]],
 [1.153671145439148,
  -0.8855527639389038,
  -0.6830140352249146,
  -1.0286221504211426]]