Regression tutorial

This tutorial demonstrates that hardware-compatible Akida models can perform regression tasks at the same accuracy level as a native CNN network.

This is illustrated through an age estimation problem using the UTKFace dataset.

1. Load the dataset

from akida_models.utk_face.preprocessing import load_data

# Load the dataset using akida_models preprocessing tool
x_train, y_train, x_test, y_test = load_data()

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

Out:

Downloading data from http://data.brainchip.com/dataset-mirror/utk_face/UTKFace_preprocessed.tar.gz

   16384/48742400 [..............................] - ETA: 38s
  139264/48742400 [..............................] - ETA: 23s
  401408/48742400 [..............................] - ETA: 15s
  663552/48742400 [..............................] - ETA: 13s
  925696/48742400 [..............................] - ETA: 12s
 1187840/48742400 [..............................] - ETA: 12s
 1449984/48742400 [..............................] - ETA: 11s
 1712128/48742400 [>.............................] - ETA: 11s
 1974272/48742400 [>.............................] - ETA: 11s
 2236416/48742400 [>.............................] - ETA: 11s
 2498560/48742400 [>.............................] - ETA: 10s
 2760704/48742400 [>.............................] - ETA: 10s
 3022848/48742400 [>.............................] - ETA: 10s
 3284992/48742400 [=>............................] - ETA: 10s
 3547136/48742400 [=>............................] - ETA: 10s
 3809280/48742400 [=>............................] - ETA: 10s
 4071424/48742400 [=>............................] - ETA: 10s
 4333568/48742400 [=>............................] - ETA: 10s
 4595712/48742400 [=>............................] - ETA: 10s
 4857856/48742400 [=>............................] - ETA: 9s 
 5120000/48742400 [==>...........................] - ETA: 9s
 5382144/48742400 [==>...........................] - ETA: 9s
 5644288/48742400 [==>...........................] - ETA: 9s
 5906432/48742400 [==>...........................] - ETA: 9s
 6168576/48742400 [==>...........................] - ETA: 9s
 6430720/48742400 [==>...........................] - ETA: 9s
 6692864/48742400 [===>..........................] - ETA: 9s
 6955008/48742400 [===>..........................] - ETA: 9s
 7217152/48742400 [===>..........................] - ETA: 9s
 7479296/48742400 [===>..........................] - ETA: 9s
 7741440/48742400 [===>..........................] - ETA: 9s
 8003584/48742400 [===>..........................] - ETA: 9s
 8265728/48742400 [====>.........................] - ETA: 9s
 8527872/48742400 [====>.........................] - ETA: 9s
 8790016/48742400 [====>.........................] - ETA: 8s
 9052160/48742400 [====>.........................] - ETA: 8s
 9314304/48742400 [====>.........................] - ETA: 8s
 9576448/48742400 [====>.........................] - ETA: 8s
 9838592/48742400 [=====>........................] - ETA: 8s
10100736/48742400 [=====>........................] - ETA: 8s
10362880/48742400 [=====>........................] - ETA: 8s
10625024/48742400 [=====>........................] - ETA: 8s
10887168/48742400 [=====>........................] - ETA: 8s
11149312/48742400 [=====>........................] - ETA: 8s
11411456/48742400 [======>.......................] - ETA: 8s
11673600/48742400 [======>.......................] - ETA: 8s
11935744/48742400 [======>.......................] - ETA: 8s
12197888/48742400 [======>.......................] - ETA: 8s
12460032/48742400 [======>.......................] - ETA: 8s
12722176/48742400 [======>.......................] - ETA: 8s
12984320/48742400 [======>.......................] - ETA: 7s
13246464/48742400 [=======>......................] - ETA: 7s
13508608/48742400 [=======>......................] - ETA: 7s
13770752/48742400 [=======>......................] - ETA: 7s
14032896/48742400 [=======>......................] - ETA: 7s
14295040/48742400 [=======>......................] - ETA: 7s
14557184/48742400 [=======>......................] - ETA: 7s
14819328/48742400 [========>.....................] - ETA: 7s
15081472/48742400 [========>.....................] - ETA: 7s
15343616/48742400 [========>.....................] - ETA: 7s
15605760/48742400 [========>.....................] - ETA: 7s
15867904/48742400 [========>.....................] - ETA: 7s
16130048/48742400 [========>.....................] - ETA: 7s
16392192/48742400 [=========>....................] - ETA: 7s
16654336/48742400 [=========>....................] - ETA: 7s
16916480/48742400 [=========>....................] - ETA: 7s
17178624/48742400 [=========>....................] - ETA: 7s
17440768/48742400 [=========>....................] - ETA: 6s
17702912/48742400 [=========>....................] - ETA: 6s
17965056/48742400 [==========>...................] - ETA: 6s
18227200/48742400 [==========>...................] - ETA: 6s
18489344/48742400 [==========>...................] - ETA: 6s
18751488/48742400 [==========>...................] - ETA: 6s
19013632/48742400 [==========>...................] - ETA: 6s
19275776/48742400 [==========>...................] - ETA: 6s
19537920/48742400 [===========>..................] - ETA: 6s
19800064/48742400 [===========>..................] - ETA: 6s
20062208/48742400 [===========>..................] - ETA: 6s
20324352/48742400 [===========>..................] - ETA: 6s
20586496/48742400 [===========>..................] - ETA: 6s
20848640/48742400 [===========>..................] - ETA: 6s
21110784/48742400 [===========>..................] - ETA: 6s
21372928/48742400 [============>.................] - ETA: 6s
21635072/48742400 [============>.................] - ETA: 6s
21897216/48742400 [============>.................] - ETA: 5s
22159360/48742400 [============>.................] - ETA: 5s
22421504/48742400 [============>.................] - ETA: 5s
22683648/48742400 [============>.................] - ETA: 5s
22945792/48742400 [=============>................] - ETA: 5s
23207936/48742400 [=============>................] - ETA: 5s
23470080/48742400 [=============>................] - ETA: 5s
23732224/48742400 [=============>................] - ETA: 5s
23994368/48742400 [=============>................] - ETA: 5s
24256512/48742400 [=============>................] - ETA: 5s
24518656/48742400 [==============>...............] - ETA: 5s
24780800/48742400 [==============>...............] - ETA: 5s
25042944/48742400 [==============>...............] - ETA: 5s
25305088/48742400 [==============>...............] - ETA: 5s
25567232/48742400 [==============>...............] - ETA: 5s
25829376/48742400 [==============>...............] - ETA: 5s
26091520/48742400 [===============>..............] - ETA: 5s
26353664/48742400 [===============>..............] - ETA: 4s
26615808/48742400 [===============>..............] - ETA: 4s
26877952/48742400 [===============>..............] - ETA: 4s
27140096/48742400 [===============>..............] - ETA: 4s
27402240/48742400 [===============>..............] - ETA: 4s
27664384/48742400 [================>.............] - ETA: 4s
27926528/48742400 [================>.............] - ETA: 4s
28114944/48742400 [================>.............] - ETA: 4s
28319744/48742400 [================>.............] - ETA: 4s
28581888/48742400 [================>.............] - ETA: 4s
28844032/48742400 [================>.............] - ETA: 4s
29106176/48742400 [================>.............] - ETA: 4s
29368320/48742400 [=================>............] - ETA: 4s
29630464/48742400 [=================>............] - ETA: 4s
29892608/48742400 [=================>............] - ETA: 4s
30154752/48742400 [=================>............] - ETA: 4s
30416896/48742400 [=================>............] - ETA: 4s
30679040/48742400 [=================>............] - ETA: 4s
30941184/48742400 [==================>...........] - ETA: 3s
31203328/48742400 [==================>...........] - ETA: 3s
31465472/48742400 [==================>...........] - ETA: 3s
31727616/48742400 [==================>...........] - ETA: 3s
31989760/48742400 [==================>...........] - ETA: 3s
32251904/48742400 [==================>...........] - ETA: 3s
32514048/48742400 [===================>..........] - ETA: 3s
32776192/48742400 [===================>..........] - ETA: 3s
33038336/48742400 [===================>..........] - ETA: 3s
33300480/48742400 [===================>..........] - ETA: 3s
33562624/48742400 [===================>..........] - ETA: 3s
33824768/48742400 [===================>..........] - ETA: 3s
34086912/48742400 [===================>..........] - ETA: 3s
34349056/48742400 [====================>.........] - ETA: 3s
34611200/48742400 [====================>.........] - ETA: 3s
34873344/48742400 [====================>.........] - ETA: 3s
35135488/48742400 [====================>.........] - ETA: 3s
35397632/48742400 [====================>.........] - ETA: 2s
35659776/48742400 [====================>.........] - ETA: 2s
35921920/48742400 [=====================>........] - ETA: 2s
36184064/48742400 [=====================>........] - ETA: 2s
36446208/48742400 [=====================>........] - ETA: 2s
36708352/48742400 [=====================>........] - ETA: 2s
36970496/48742400 [=====================>........] - ETA: 2s
37232640/48742400 [=====================>........] - ETA: 2s
37494784/48742400 [======================>.......] - ETA: 2s
37756928/48742400 [======================>.......] - ETA: 2s
38019072/48742400 [======================>.......] - ETA: 2s
38281216/48742400 [======================>.......] - ETA: 2s
38543360/48742400 [======================>.......] - ETA: 2s
38805504/48742400 [======================>.......] - ETA: 2s
39067648/48742400 [=======================>......] - ETA: 2s
39329792/48742400 [=======================>......] - ETA: 2s
39591936/48742400 [=======================>......] - ETA: 2s
39854080/48742400 [=======================>......] - ETA: 1s
40116224/48742400 [=======================>......] - ETA: 1s
40378368/48742400 [=======================>......] - ETA: 1s
40640512/48742400 [========================>.....] - ETA: 1s
40902656/48742400 [========================>.....] - ETA: 1s
41164800/48742400 [========================>.....] - ETA: 1s
41426944/48742400 [========================>.....] - ETA: 1s
41689088/48742400 [========================>.....] - ETA: 1s
41951232/48742400 [========================>.....] - ETA: 1s
42213376/48742400 [========================>.....] - ETA: 1s
42475520/48742400 [=========================>....] - ETA: 1s
42737664/48742400 [=========================>....] - ETA: 1s
42999808/48742400 [=========================>....] - ETA: 1s
43261952/48742400 [=========================>....] - ETA: 1s
43524096/48742400 [=========================>....] - ETA: 1s
43786240/48742400 [=========================>....] - ETA: 1s
44048384/48742400 [==========================>...] - ETA: 1s
44310528/48742400 [==========================>...] - ETA: 0s
44572672/48742400 [==========================>...] - ETA: 0s
44834816/48742400 [==========================>...] - ETA: 0s
45096960/48742400 [==========================>...] - ETA: 0s
45359104/48742400 [==========================>...] - ETA: 0s
45621248/48742400 [===========================>..] - ETA: 0s
45883392/48742400 [===========================>..] - ETA: 0s
46145536/48742400 [===========================>..] - ETA: 0s
46407680/48742400 [===========================>..] - ETA: 0s
46669824/48742400 [===========================>..] - ETA: 0s
46931968/48742400 [===========================>..] - ETA: 0s
47194112/48742400 [============================>.] - ETA: 0s
47456256/48742400 [============================>.] - ETA: 0s
47718400/48742400 [============================>.] - ETA: 0s
47980544/48742400 [============================>.] - ETA: 0s
48242688/48742400 [============================>.] - ETA: 0s
48504832/48742400 [============================>.] - ETA: 0s
48742400/48742400 [==============================] - 11s 0us/step

48750592/48742400 [==============================] - 11s 0us/step

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 fully connected layers that outputs a single value corresponding to the estimated age. This model architecture is compatible with the design constraints before quantization. It is the starting point for a model runnable on the Akida NSoC.

The pre-trained native Keras model loaded below was trained on 300 epochs. The model file is available on the BrainChip data server.

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 tensorflow.keras.utils import get_file
from tensorflow.keras.models import load_model

# Retrieve the model file from the BrainChip data server
model_file = get_file("vgg_utk_face.h5",
                      "http://data.brainchip.com/models/vgg/vgg_utk_face.h5",
                      cache_subdir='models')

# Load the native Keras pre-trained model
model_keras = load_model(model_file)
model_keras.summary()

Out:

Downloading data from http://data.brainchip.com/models/vgg/vgg_utk_face.h5

  16384/1907648 [..............................] - ETA: 1s
 204800/1907648 [==>...........................] - ETA: 0s
 466944/1907648 [======>.......................] - ETA: 0s
 729088/1907648 [==========>...................] - ETA: 0s
 991232/1907648 [==============>...............] - ETA: 0s
1253376/1907648 [==================>...........] - ETA: 0s
1515520/1907648 [======================>.......] - ETA: 0s
1777664/1907648 [==========================>...] - ETA: 0s
1908736/1907648 [==============================] - 0s 0us/step

1916928/1907648 [==============================] - 0s 0us/step
Model: "vgg_utk_face"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_37 (InputLayer)        [(None, 32, 32, 3)]       0
_________________________________________________________________
rescaling_36 (Rescaling)     (None, 32, 32, 3)         0
_________________________________________________________________
conv_0 (Conv2D)              (None, 30, 30, 32)        864
_________________________________________________________________
conv_0_BN (BatchNormalizatio (None, 30, 30, 32)        128
_________________________________________________________________
conv_0_relu (ReLU)           (None, 30, 30, 32)        0
_________________________________________________________________
conv_1 (Conv2D)              (None, 30, 30, 32)        9216
_________________________________________________________________
conv_1_maxpool (MaxPooling2D (None, 15, 15, 32)        0
_________________________________________________________________
conv_1_BN (BatchNormalizatio (None, 15, 15, 32)        128
_________________________________________________________________
conv_1_relu (ReLU)           (None, 15, 15, 32)        0
_________________________________________________________________
dropout (Dropout)            (None, 15, 15, 32)        0
_________________________________________________________________
conv_2 (Conv2D)              (None, 15, 15, 64)        18432
_________________________________________________________________
conv_2_BN (BatchNormalizatio (None, 15, 15, 64)        256
_________________________________________________________________
conv_2_relu (ReLU)           (None, 15, 15, 64)        0
_________________________________________________________________
conv_3 (Conv2D)              (None, 15, 15, 64)        36864
_________________________________________________________________
conv_3_maxpool (MaxPooling2D (None, 8, 8, 64)          0
_________________________________________________________________
conv_3_BN (BatchNormalizatio (None, 8, 8, 64)          256
_________________________________________________________________
conv_3_relu (ReLU)           (None, 8, 8, 64)          0
_________________________________________________________________
dropout_1 (Dropout)          (None, 8, 8, 64)          0
_________________________________________________________________
conv_4 (Conv2D)              (None, 8, 8, 84)          48384
_________________________________________________________________
conv_4_BN (BatchNormalizatio (None, 8, 8, 84)          336
_________________________________________________________________
conv_4_relu (ReLU)           (None, 8, 8, 84)          0
_________________________________________________________________
dropout_2 (Dropout)          (None, 8, 8, 84)          0
_________________________________________________________________
flatten_3 (Flatten)          (None, 5376)              0
_________________________________________________________________
dense_1 (Dense)              (None, 64)                344064
_________________________________________________________________
dense_1_BN (BatchNormalizati (None, 64)                256
_________________________________________________________________
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))

Out:

Keras MAE: 5.8023

3. Load a pre-trained quantized Keras model satisfying Akida NSoC requirements

The above native Keras model is quantized and fine-tuned to get a quantized Keras model satisfying the Akida NSoC requirements. The first convolutional layer of our model uses 8-bit weights and other layers are quantized using 2-bit weights. All activations are 2 bits.

The pre-trained model was obtained after two fine-tuning episodes:

  • the model is first quantized and fine-tuned with 4-bit weights and activations (first convolutional weights are 8 bits)

  • the model is then quantized and fine-tuned with 2-bit weights and activations (first convolutional weights are still 8 bits).

The table below summarizes the “Mean Absolute Error” (MAE) results obtained after every training episode.

Episode

Weights Quant.

Activ. Quant.

MAE

Epochs

1

N/A

N/A

5.80

300

2

8/4 bits

4 bits

5.79

30

3

8/2 bits

2 bits

6.15

30

Here, we directly load the pre-trained quantized Keras model using the akida_models helper.

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()

Out:

Downloading data from http://data.brainchip.com/models/vgg/vgg_utk_face_iq8_wq2_aq2.h5

  16384/1877128 [..............................] - ETA: 1s
 204800/1877128 [==>...........................] - ETA: 0s
 466944/1877128 [======>.......................] - ETA: 0s
 729088/1877128 [==========>...................] - ETA: 0s
 991232/1877128 [==============>...............] - ETA: 0s
1253376/1877128 [===================>..........] - ETA: 0s
1515520/1877128 [=======================>......] - ETA: 0s
1777664/1877128 [===========================>..] - ETA: 0s
1884160/1877128 [==============================] - 0s 0us/step

1892352/1877128 [==============================] - 0s 0us/step
Model: "vgg_utk_face"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_38 (InputLayer)        [(None, 32, 32, 3)]       0
_________________________________________________________________
rescaling_37 (Rescaling)     (None, 32, 32, 3)         0
_________________________________________________________________
conv_0 (QuantizedConv2D)     (None, 30, 30, 32)        896
_________________________________________________________________
conv_0_relu (ActivationDiscr (None, 30, 30, 32)        0
_________________________________________________________________
conv_1 (QuantizedConv2D)     (None, 30, 30, 32)        9248
_________________________________________________________________
conv_1_maxpool (MaxPooling2D (None, 15, 15, 32)        0
_________________________________________________________________
conv_1_relu (ActivationDiscr (None, 15, 15, 32)        0
_________________________________________________________________
dropout_3 (Dropout)          (None, 15, 15, 32)        0
_________________________________________________________________
conv_2 (QuantizedConv2D)     (None, 15, 15, 64)        18496
_________________________________________________________________
conv_2_relu (ActivationDiscr (None, 15, 15, 64)        0
_________________________________________________________________
conv_3 (QuantizedConv2D)     (None, 15, 15, 64)        36928
_________________________________________________________________
conv_3_maxpool (MaxPooling2D (None, 8, 8, 64)          0
_________________________________________________________________
conv_3_relu (ActivationDiscr (None, 8, 8, 64)          0
_________________________________________________________________
dropout_4 (Dropout)          (None, 8, 8, 64)          0
_________________________________________________________________
conv_4 (QuantizedConv2D)     (None, 8, 8, 84)          48468
_________________________________________________________________
conv_4_relu (ActivationDiscr (None, 8, 8, 84)          0
_________________________________________________________________
dropout_5 (Dropout)          (None, 8, 8, 84)          0
_________________________________________________________________
flatten_4 (Flatten)          (None, 5376)              0
_________________________________________________________________
dense_1 (QuantizedDense)     (None, 64)                344128
_________________________________________________________________
dense_1_relu (ActivationDisc (None, 64)                0
_________________________________________________________________
dense_2 (QuantizedDense)     (None, 1)                 65
=================================================================
Total params: 458,229
Trainable params: 458,229
Non-trainable params: 0
_________________________________________________________________
# 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))

Out:

Keras MAE: 6.1465

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.

Since activations sparsity has a great impact on Akida inference time, we also have a look at the average input and output sparsity of each layer on a subset of the dataset.

from cnn2snn import convert

# Convert the model
model_akida = convert(model_quantized_keras)
model_akida.summary()

Out:

                Model Summary
______________________________________________
Input shape  Output shape  Sequences  Layers
==============================================
[32, 32, 3]  [1, 1, 1]     1          7
______________________________________________

            SW/conv_0-dense_2 (Software)
_____________________________________________________
Layer (type)         Output shape  Kernel shape
=====================================================
conv_0 (InputConv.)  [30, 30, 32]  (3, 3, 3, 32)
_____________________________________________________
conv_1 (Conv.)       [15, 15, 32]  (3, 3, 32, 32)
_____________________________________________________
conv_2 (Conv.)       [15, 15, 64]  (3, 3, 32, 64)
_____________________________________________________
conv_3 (Conv.)       [8, 8, 64]    (3, 3, 64, 64)
_____________________________________________________
conv_4 (Conv.)       [8, 8, 84]    (3, 3, 64, 84)
_____________________________________________________
dense_1 (Fully.)     [1, 1, 64]    (1, 1, 5376, 64)
_____________________________________________________
dense_2 (Fully.)     [1, 1, 1]     (1, 1, 64, 1)
_____________________________________________________
import numpy as np

# Check Akida model performance
y_akida = model_akida.evaluate(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 purpose
assert abs(mae_keras - mae_akida) < 0.5

Out:

Akida MAE: 6.1791

Let’s summarize the MAE performance for the native Keras, the quantized Keras and the Akida model.

Model

MAE

native Keras

5.80

quantized Keras

6.15

Akida

6.21

5. Estimate age on a single 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 4 regression

Out:

Keras estimated age: 29.1
Akida estimated age: 27.9
Actual age: 29

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

Gallery generated by Sphinx-Gallery