Age estimation (regression) example

This tutorial aims to demonstrate the comparable accuracy of the Akida-compatible model to the traditional Keras model in performing an age estimation task.

It uses the UTKFace dataset, which includes images of faces and age labels, to showcase how well akida compatible model can predict the ages of individuals based on their facial features.

1. Load the UTKFace Dataset

The UTKFace dataset has 20,000+ diverse face images spanning 0 to 116 years. It includes age, gender, ethnicity annotations. This dataset is useful for various tasks like age estimation, face detection, and more.

Load the dataset from Brainchip data server using the load_data helper (decode JPEG images and load the associated labels).

from akida_models.utk_face.preprocessing import load_data

# Load the dataset
x_train, y_train, x_test, y_test = load_data()
Downloading data from https://data.brainchip.com/dataset-mirror/utk_face/UTKFace_preprocessed.tar.gz.

       0/48742400 [..............................] - ETA: 0s
  114688/48742400 [..............................] - ETA: 21s
  614400/48742400 [..............................] - ETA: 7s 
 1204224/48742400 [..............................] - ETA: 5s
 1769472/48742400 [>.............................] - ETA: 5s
 2367488/48742400 [>.............................] - ETA: 4s
 2957312/48742400 [>.............................] - ETA: 4s
 3547136/48742400 [=>............................] - ETA: 4s
 4128768/48742400 [=>............................] - ETA: 4s
 4718592/48742400 [=>............................] - ETA: 4s
 5308416/48742400 [==>...........................] - ETA: 4s
 5906432/48742400 [==>...........................] - ETA: 4s
 6504448/48742400 [===>..........................] - ETA: 3s
 7086080/48742400 [===>..........................] - ETA: 3s
 7675904/48742400 [===>..........................] - ETA: 3s
 8265728/48742400 [====>.........................] - ETA: 3s
 8839168/48742400 [====>.........................] - ETA: 3s
 9420800/48742400 [====>.........................] - ETA: 3s
10010624/48742400 [=====>........................] - ETA: 3s
10600448/48742400 [=====>........................] - ETA: 3s
11173888/48742400 [=====>........................] - ETA: 3s
11763712/48742400 [======>.......................] - ETA: 3s
12353536/48742400 [======>.......................] - ETA: 3s
12943360/48742400 [======>.......................] - ETA: 3s
13533184/48742400 [=======>......................] - ETA: 3s
14123008/48742400 [=======>......................] - ETA: 3s
14712832/48742400 [========>.....................] - ETA: 3s
15302656/48742400 [========>.....................] - ETA: 2s
15876096/48742400 [========>.....................] - ETA: 2s
16367616/48742400 [=========>....................] - ETA: 2s
16826368/48742400 [=========>....................] - ETA: 2s
17301504/48742400 [=========>....................] - ETA: 2s
17842176/48742400 [=========>....................] - ETA: 2s
18399232/48742400 [==========>...................] - ETA: 2s
18939904/48742400 [==========>...................] - ETA: 2s
19496960/48742400 [===========>..................] - ETA: 2s
20054016/48742400 [===========>..................] - ETA: 2s
20611072/48742400 [===========>..................] - ETA: 2s
21168128/48742400 [============>.................] - ETA: 2s
21725184/48742400 [============>.................] - ETA: 2s
22282240/48742400 [============>.................] - ETA: 2s
22839296/48742400 [=============>................] - ETA: 2s
23396352/48742400 [=============>................] - ETA: 2s
23977984/48742400 [=============>................] - ETA: 2s
24559616/48742400 [==============>...............] - ETA: 2s
25149440/48742400 [==============>...............] - ETA: 2s
25739264/48742400 [==============>...............] - ETA: 2s
26329088/48742400 [===============>..............] - ETA: 2s
26918912/48742400 [===============>..............] - ETA: 1s
27508736/48742400 [===============>..............] - ETA: 1s
28098560/48742400 [================>.............] - ETA: 1s
28688384/48742400 [================>.............] - ETA: 1s
29278208/48742400 [=================>............] - ETA: 1s
29310976/48742400 [=================>............] - ETA: 1s
30195712/48742400 [=================>............] - ETA: 1s
31096832/48742400 [==================>...........] - ETA: 1s
32882688/48742400 [===================>..........] - ETA: 1s
33472512/48742400 [===================>..........] - ETA: 1s
34062336/48742400 [===================>..........] - ETA: 1s
34652160/48742400 [====================>.........] - ETA: 1s
35241984/48742400 [====================>.........] - ETA: 1s
35831808/48742400 [=====================>........] - ETA: 1s
36421632/48742400 [=====================>........] - ETA: 1s
36995072/48742400 [=====================>........] - ETA: 1s
37584896/48742400 [======================>.......] - ETA: 0s
38174720/48742400 [======================>.......] - ETA: 0s
38764544/48742400 [======================>.......] - ETA: 0s
39354368/48742400 [=======================>......] - ETA: 0s
39944192/48742400 [=======================>......] - ETA: 0s
40517632/48742400 [=======================>......] - ETA: 0s
41107456/48742400 [========================>.....] - ETA: 0s
41697280/48742400 [========================>.....] - ETA: 0s
42287104/48742400 [=========================>....] - ETA: 0s
42876928/48742400 [=========================>....] - ETA: 0s
43466752/48742400 [=========================>....] - ETA: 0s
44056576/48742400 [==========================>...] - ETA: 0s
44646400/48742400 [==========================>...] - ETA: 0s
45236224/48742400 [==========================>...] - ETA: 0s
45826048/48742400 [===========================>..] - ETA: 0s
46415872/48742400 [===========================>..] - ETA: 0s
47005696/48742400 [===========================>..] - ETA: 0s
47595520/48742400 [============================>.] - ETA: 0s
48185344/48742400 [============================>.] - ETA: 0s
48742400/48742400 [==============================] - 4s 0us/step

Akida models accept only uint8 tensors as inputs. Use uint8 raw data for Akida performance evaluation.

# For Akida inference, use uint8 raw data
x_test_akida = x_test.astype('uint8')

2. Load a pre-trained native Keras model

The model is a simplified version inspired from VGG architecture. It consists of a succession of convolutional and pooling layers and ends with two dense layers that outputs a single value corresponding to the estimated age.

The performance of the model is evaluated using the “Mean Absolute Error” (MAE). The MAE, used as a metric in regression problem, is calculated as an average of absolute differences between the target values and the predictions. The MAE is a linear score, i.e. all the individual differences are equally weighted in the average.

from akida_models import fetch_file
from tensorflow.keras.models import load_model

# Retrieve the model file from the BrainChip data server
model_file = fetch_file(fname="vgg_utk_face.h5",
                        origin="https://data.brainchip.com/models/AkidaV2/vgg/vgg_utk_face.h5",
                        cache_subdir='models')

# Load the native Keras pre-trained model
model_keras = load_model(model_file)
model_keras.summary()
Downloading data from https://data.brainchip.com/models/AkidaV2/vgg/vgg_utk_face.h5.

      0/1912544 [..............................] - ETA: 0s
  98304/1912544 [>.............................] - ETA: 0s
 589824/1912544 [========>.....................] - ETA: 0s
1171456/1912544 [=================>............] - ETA: 0s
1761280/1912544 [==========================>...] - ETA: 0s
1912544/1912544 [==============================] - 0s 0us/step
Model: "vgg_utk_face"
_________________________________________________________________
 Layer (type)                Output Shape              Param #
=================================================================
 input (InputLayer)          [(None, 32, 32, 3)]       0

 rescaling (Rescaling)       (None, 32, 32, 3)         0

 conv_0 (Conv2D)             (None, 30, 30, 32)        864

 conv_0/BN (BatchNormalizati  (None, 30, 30, 32)       128
 on)

 conv_0/relu (ReLU)          (None, 30, 30, 32)        0

 conv_1 (Conv2D)             (None, 30, 30, 32)        9216

 conv_1/maxpool (MaxPooling2  (None, 15, 15, 32)       0
 D)

 conv_1/BN (BatchNormalizati  (None, 15, 15, 32)       128
 on)

 conv_1/relu (ReLU)          (None, 15, 15, 32)        0

 dropout_3 (Dropout)         (None, 15, 15, 32)        0

 conv_2 (Conv2D)             (None, 15, 15, 64)        18432

 conv_2/BN (BatchNormalizati  (None, 15, 15, 64)       256
 on)

 conv_2/relu (ReLU)          (None, 15, 15, 64)        0

 conv_3 (Conv2D)             (None, 15, 15, 64)        36864

 conv_3/maxpool (MaxPooling2  (None, 8, 8, 64)         0
 D)

 conv_3/BN (BatchNormalizati  (None, 8, 8, 64)         256
 on)

 conv_3/relu (ReLU)          (None, 8, 8, 64)          0

 dropout_4 (Dropout)         (None, 8, 8, 64)          0

 conv_4 (Conv2D)             (None, 8, 8, 84)          48384

 conv_4/BN (BatchNormalizati  (None, 8, 8, 84)         336
 on)

 conv_4/relu (ReLU)          (None, 8, 8, 84)          0

 dropout_5 (Dropout)         (None, 8, 8, 84)          0

 flatten (Flatten)           (None, 5376)              0

 dense_1 (Dense)             (None, 64)                344064

 dense_1/BN (BatchNormalizat  (None, 64)               256
 ion)

 dense_1/relu (ReLU)         (None, 64)                0

 dense_2 (Dense)             (None, 1)                 65

=================================================================
Total params: 459,249
Trainable params: 458,569
Non-trainable params: 680
_________________________________________________________________
# Compile the native Keras model (required to evaluate the MAE)
model_keras.compile(optimizer='Adam', loss='mae')

# Check Keras model performance
mae_keras = model_keras.evaluate(x_test, y_test, verbose=0)

print("Keras MAE: {0:.4f}".format(mae_keras))
Keras MAE: 5.8671

3. Load a pre-trained quantized Keras model

The above native Keras model is quantized and fine-tuned (QAT). The first convolutional layer of our model uses 8-bit weights, other layers are quantized using 4-bit weights, all activations are 4-bit.

from akida_models import vgg_utk_face_pretrained

# Load the pre-trained quantized model
model_quantized_keras = vgg_utk_face_pretrained()
model_quantized_keras.summary()
Downloading data from https://data.brainchip.com/models/AkidaV2/vgg/vgg_utk_face_i8_w4_a4.h5.

      0/1908680 [..............................] - ETA: 0s
 106496/1908680 [>.............................] - ETA: 0s
 614400/1908680 [========>.....................] - ETA: 0s
1196032/1908680 [=================>............] - ETA: 0s
1777664/1908680 [==========================>...] - ETA: 0s
1908680/1908680 [==============================] - 0s 0us/step
Model: "vgg_utk_face"
_________________________________________________________________
 Layer (type)                Output Shape              Param #
=================================================================
 input (InputLayer)          [(None, 32, 32, 3)]       0

 rescaling (QuantizedRescali  (None, 32, 32, 3)        0
 ng)

 conv_0 (QuantizedConv2D)    (None, 30, 30, 32)        896

 conv_0/relu (QuantizedReLU)  (None, 30, 30, 32)       64

 conv_1 (QuantizedConv2D)    (None, 30, 30, 32)        9248

 conv_1/maxpool (QuantizedMa  (None, 15, 15, 32)       0
 xPool2D)

 conv_1/relu (QuantizedReLU)  (None, 15, 15, 32)       64

 dropout_3 (QuantizedDropout  (None, 15, 15, 32)       0
 )

 conv_2 (QuantizedConv2D)    (None, 15, 15, 64)        18496

 conv_2/relu (QuantizedReLU)  (None, 15, 15, 64)       128

 conv_3 (QuantizedConv2D)    (None, 15, 15, 64)        36928

 conv_3/maxpool (QuantizedMa  (None, 8, 8, 64)         0
 xPool2D)

 conv_3/relu (QuantizedReLU)  (None, 8, 8, 64)         128

 dropout_4 (QuantizedDropout  (None, 8, 8, 64)         0
 )

 conv_4 (QuantizedConv2D)    (None, 8, 8, 84)          48468

 conv_4/relu (QuantizedReLU)  (None, 8, 8, 84)         168

 dropout_5 (QuantizedDropout  (None, 8, 8, 84)         0
 )

 flatten (QuantizedFlatten)  (None, 5376)              0

 dense_1 (QuantizedDense)    (None, 64)                344128

 dense_1/relu (QuantizedReLU  (None, 64)               2
 )

 dense_2 (QuantizedDense)    (None, 1)                 65

 dequantizer (Dequantizer)   (None, 1)                 0

=================================================================
Total params: 458,783
Trainable params: 458,229
Non-trainable params: 554
_________________________________________________________________
# Compile the quantized Keras model (required to evaluate the MAE)
model_quantized_keras.compile(optimizer='Adam', loss='mae')

# Check Keras model performance
mae_quant = model_quantized_keras.evaluate(x_test, y_test, verbose=0)

print("Keras MAE: {0:.4f}".format(mae_quant))
Keras MAE: 5.8975

4. Conversion to Akida

The quantized Keras model is now converted into an Akida model. After conversion, we evaluate the performance on the UTKFace dataset.

from cnn2snn import convert

# Convert the model
model_akida = convert(model_quantized_keras)
model_akida.summary()
                Model Summary
______________________________________________
Input shape  Output shape  Sequences  Layers
==============================================
[32, 32, 3]  [1, 1, 1]     1          8
______________________________________________

_________________________________________________________
Layer (type)               Output shape  Kernel shape

============ SW/conv_0-dequantizer (Software) ===========

conv_0 (InputConv2D)       [30, 30, 32]  (3, 3, 3, 32)
_________________________________________________________
conv_1 (Conv2D)            [15, 15, 32]  (3, 3, 32, 32)
_________________________________________________________
conv_2 (Conv2D)            [15, 15, 64]  (3, 3, 32, 64)
_________________________________________________________
conv_3 (Conv2D)            [8, 8, 64]    (3, 3, 64, 64)
_________________________________________________________
conv_4 (Conv2D)            [8, 8, 84]    (3, 3, 64, 84)
_________________________________________________________
dense_1 (Dense2D)          [1, 1, 64]    (5376, 64)
_________________________________________________________
dense_2 (Dense2D)          [1, 1, 1]     (64, 1)
_________________________________________________________
dequantizer (Dequantizer)  [1, 1, 1]     N/A
_________________________________________________________
import numpy as np

# Check Akida model performance
y_akida = model_akida.predict(x_test_akida)

# Compute and display the MAE
mae_akida = np.sum(np.abs(y_test.squeeze() - y_akida.squeeze())) / len(y_test)
print("Akida MAE: {0:.4f}".format(mae_akida))

# For non-regression purposes
assert abs(mae_keras - mae_akida) < 0.5
Akida MAE: 6.0405

5. Estimate age on a single image

Select a random image from the test set for age estimation. Print the Keras model’s age prediction using the model_keras.predict() function. Print the Akida model’s estimated age and the actual age associated with the image.

import matplotlib.pyplot as plt

# Estimate age on a random single image and display Keras and Akida outputs
id = np.random.randint(0, len(y_test) + 1)
age_keras = model_keras.predict(x_test[id:id + 1])

plt.imshow(x_test_akida[id], interpolation='bicubic')
plt.xticks([]), plt.yticks([])
plt.show()

print("Keras estimated age: {0:.1f}".format(age_keras.squeeze()))
print("Akida estimated age: {0:.1f}".format(y_akida[id].squeeze()))
print(f"Actual age: {y_test[id].squeeze()}")
plot 3 regression
1/1 [==============================] - ETA: 0s
1/1 [==============================] - 0s 97ms/step
Keras estimated age: 15.4
Akida estimated age: 14.7
Actual age: 3

Total running time of the script: (0 minutes 26.184 seconds)

Gallery generated by Sphinx-Gallery