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 CNN Keras training and inference, the data is normalized
input_scaling = (127, 127)
x_test_keras = (x_test.astype('float32') - input_scaling[1]) / input_scaling[0]

# 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

    8192/48742400 [..............................] - ETA: 1:51
   73728/48742400 [..............................] - ETA: 50s 
  204800/48742400 [..............................] - ETA: 31s
  401408/48742400 [..............................] - ETA: 23s
  598016/48742400 [..............................] - ETA: 20s
  794624/48742400 [..............................] - ETA: 18s
  991232/48742400 [..............................] - ETA: 17s
 1187840/48742400 [..............................] - ETA: 17s
 1384448/48742400 [..............................] - ETA: 16s
 1581056/48742400 [..............................] - ETA: 16s
 1777664/48742400 [>.............................] - ETA: 16s
 1974272/48742400 [>.............................] - ETA: 15s
 2170880/48742400 [>.............................] - ETA: 15s
 2367488/48742400 [>.............................] - ETA: 15s
 2564096/48742400 [>.............................] - ETA: 15s
 2760704/48742400 [>.............................] - ETA: 15s
 2957312/48742400 [>.............................] - ETA: 15s
 3153920/48742400 [>.............................] - ETA: 14s
 3350528/48742400 [=>............................] - ETA: 14s
 3547136/48742400 [=>............................] - ETA: 14s
 3743744/48742400 [=>............................] - ETA: 14s
 3940352/48742400 [=>............................] - ETA: 14s
 4136960/48742400 [=>............................] - ETA: 14s
 4333568/48742400 [=>............................] - ETA: 14s
 4530176/48742400 [=>............................] - ETA: 14s
 4726784/48742400 [=>............................] - ETA: 14s
 4923392/48742400 [==>...........................] - ETA: 13s
 5120000/48742400 [==>...........................] - ETA: 13s
 5316608/48742400 [==>...........................] - ETA: 13s
 5513216/48742400 [==>...........................] - ETA: 13s
 5709824/48742400 [==>...........................] - ETA: 13s
 5906432/48742400 [==>...........................] - ETA: 13s
 6103040/48742400 [==>...........................] - ETA: 13s
 6299648/48742400 [==>...........................] - ETA: 13s
 6496256/48742400 [==>...........................] - ETA: 13s
 6692864/48742400 [===>..........................] - ETA: 13s
 6889472/48742400 [===>..........................] - ETA: 13s
 7086080/48742400 [===>..........................] - ETA: 13s
 7282688/48742400 [===>..........................] - ETA: 12s
 7479296/48742400 [===>..........................] - ETA: 12s
 7675904/48742400 [===>..........................] - ETA: 12s
 7872512/48742400 [===>..........................] - ETA: 12s
 8069120/48742400 [===>..........................] - ETA: 12s
 8265728/48742400 [====>.........................] - ETA: 12s
 8462336/48742400 [====>.........................] - ETA: 12s
 8658944/48742400 [====>.........................] - ETA: 12s
 8855552/48742400 [====>.........................] - ETA: 12s
 9052160/48742400 [====>.........................] - ETA: 12s
 9248768/48742400 [====>.........................] - ETA: 12s
 9445376/48742400 [====>.........................] - ETA: 12s
 9641984/48742400 [====>.........................] - ETA: 12s
 9838592/48742400 [=====>........................] - ETA: 12s
10035200/48742400 [=====>........................] - ETA: 12s
10231808/48742400 [=====>........................] - ETA: 11s
10428416/48742400 [=====>........................] - ETA: 11s
10625024/48742400 [=====>........................] - ETA: 11s
10821632/48742400 [=====>........................] - ETA: 11s
11018240/48742400 [=====>........................] - ETA: 11s
11214848/48742400 [=====>........................] - ETA: 11s
11411456/48742400 [======>.......................] - ETA: 11s
11608064/48742400 [======>.......................] - ETA: 11s
11804672/48742400 [======>.......................] - ETA: 11s
12001280/48742400 [======>.......................] - ETA: 11s
12197888/48742400 [======>.......................] - ETA: 11s
12394496/48742400 [======>.......................] - ETA: 11s
12591104/48742400 [======>.......................] - ETA: 11s
12787712/48742400 [======>.......................] - ETA: 11s
12984320/48742400 [======>.......................] - ETA: 11s
13180928/48742400 [=======>......................] - ETA: 10s
13377536/48742400 [=======>......................] - ETA: 10s
13574144/48742400 [=======>......................] - ETA: 10s
13770752/48742400 [=======>......................] - ETA: 10s
13967360/48742400 [=======>......................] - ETA: 10s
14163968/48742400 [=======>......................] - ETA: 10s
14360576/48742400 [=======>......................] - ETA: 10s
14557184/48742400 [=======>......................] - ETA: 10s
14753792/48742400 [========>.....................] - ETA: 10s
14950400/48742400 [========>.....................] - ETA: 10s
15147008/48742400 [========>.....................] - ETA: 10s
15343616/48742400 [========>.....................] - ETA: 10s
15540224/48742400 [========>.....................] - ETA: 10s
15736832/48742400 [========>.....................] - ETA: 10s
15933440/48742400 [========>.....................] - ETA: 10s
16130048/48742400 [========>.....................] - ETA: 10s
16326656/48742400 [=========>....................] - ETA: 9s 
16523264/48742400 [=========>....................] - ETA: 9s
16719872/48742400 [=========>....................] - ETA: 9s
16916480/48742400 [=========>....................] - ETA: 9s
17113088/48742400 [=========>....................] - ETA: 9s
17309696/48742400 [=========>....................] - ETA: 9s
17506304/48742400 [=========>....................] - ETA: 9s
17702912/48742400 [=========>....................] - ETA: 9s
17899520/48742400 [==========>...................] - ETA: 9s
18096128/48742400 [==========>...................] - ETA: 9s
18292736/48742400 [==========>...................] - ETA: 9s
18489344/48742400 [==========>...................] - ETA: 9s
18685952/48742400 [==========>...................] - ETA: 9s
18882560/48742400 [==========>...................] - ETA: 9s
19079168/48742400 [==========>...................] - ETA: 9s
19275776/48742400 [==========>...................] - ETA: 9s
19472384/48742400 [==========>...................] - ETA: 8s
19668992/48742400 [===========>..................] - ETA: 8s
19865600/48742400 [===========>..................] - ETA: 8s
20062208/48742400 [===========>..................] - ETA: 8s
20258816/48742400 [===========>..................] - ETA: 8s
20455424/48742400 [===========>..................] - ETA: 8s
20652032/48742400 [===========>..................] - ETA: 8s
20848640/48742400 [===========>..................] - ETA: 8s
21045248/48742400 [===========>..................] - ETA: 8s
21241856/48742400 [============>.................] - ETA: 8s
21438464/48742400 [============>.................] - ETA: 8s
21635072/48742400 [============>.................] - ETA: 8s
21831680/48742400 [============>.................] - ETA: 8s
22028288/48742400 [============>.................] - ETA: 8s
22224896/48742400 [============>.................] - ETA: 8s
22421504/48742400 [============>.................] - ETA: 8s
22618112/48742400 [============>.................] - ETA: 8s
22814720/48742400 [=============>................] - ETA: 7s
23011328/48742400 [=============>................] - ETA: 7s
23207936/48742400 [=============>................] - ETA: 7s
23404544/48742400 [=============>................] - ETA: 7s
23601152/48742400 [=============>................] - ETA: 7s
23797760/48742400 [=============>................] - ETA: 7s
23994368/48742400 [=============>................] - ETA: 7s
24190976/48742400 [=============>................] - ETA: 7s
24387584/48742400 [==============>...............] - ETA: 7s
24584192/48742400 [==============>...............] - ETA: 7s
24780800/48742400 [==============>...............] - ETA: 7s
24977408/48742400 [==============>...............] - ETA: 7s
25174016/48742400 [==============>...............] - ETA: 7s
25370624/48742400 [==============>...............] - ETA: 7s
25567232/48742400 [==============>...............] - ETA: 7s
25763840/48742400 [==============>...............] - ETA: 7s
25960448/48742400 [==============>...............] - ETA: 6s
26157056/48742400 [===============>..............] - ETA: 6s
26353664/48742400 [===============>..............] - ETA: 6s
26550272/48742400 [===============>..............] - ETA: 6s
26746880/48742400 [===============>..............] - ETA: 6s
26943488/48742400 [===============>..............] - ETA: 6s
27140096/48742400 [===============>..............] - ETA: 6s
27336704/48742400 [===============>..............] - ETA: 6s
27533312/48742400 [===============>..............] - ETA: 6s
27729920/48742400 [================>.............] - ETA: 6s
27926528/48742400 [================>.............] - ETA: 6s
28123136/48742400 [================>.............] - ETA: 6s
28319744/48742400 [================>.............] - ETA: 6s
28516352/48742400 [================>.............] - ETA: 6s
28712960/48742400 [================>.............] - ETA: 6s
28909568/48742400 [================>.............] - ETA: 6s
29106176/48742400 [================>.............] - ETA: 6s
29302784/48742400 [=================>............] - ETA: 5s
29499392/48742400 [=================>............] - ETA: 5s
29696000/48742400 [=================>............] - ETA: 5s
29892608/48742400 [=================>............] - ETA: 5s
30089216/48742400 [=================>............] - ETA: 5s
30285824/48742400 [=================>............] - ETA: 5s
30482432/48742400 [=================>............] - ETA: 5s
30679040/48742400 [=================>............] - ETA: 5s
30875648/48742400 [==================>...........] - ETA: 5s
31072256/48742400 [==================>...........] - ETA: 5s
31268864/48742400 [==================>...........] - ETA: 5s
31465472/48742400 [==================>...........] - ETA: 5s
31662080/48742400 [==================>...........] - ETA: 5s
31858688/48742400 [==================>...........] - ETA: 5s
32055296/48742400 [==================>...........] - ETA: 5s
32251904/48742400 [==================>...........] - ETA: 5s
32448512/48742400 [==================>...........] - ETA: 4s
32645120/48742400 [===================>..........] - ETA: 4s
32841728/48742400 [===================>..........] - ETA: 4s
33038336/48742400 [===================>..........] - ETA: 4s
33234944/48742400 [===================>..........] - ETA: 4s
33431552/48742400 [===================>..........] - ETA: 4s
33628160/48742400 [===================>..........] - ETA: 4s
33824768/48742400 [===================>..........] - ETA: 4s
34021376/48742400 [===================>..........] - ETA: 4s
34217984/48742400 [====================>.........] - ETA: 4s
34414592/48742400 [====================>.........] - ETA: 4s
34611200/48742400 [====================>.........] - ETA: 4s
34807808/48742400 [====================>.........] - ETA: 4s
35004416/48742400 [====================>.........] - ETA: 4s
35201024/48742400 [====================>.........] - ETA: 4s
35397632/48742400 [====================>.........] - ETA: 4s
35594240/48742400 [====================>.........] - ETA: 4s
35790848/48742400 [=====================>........] - ETA: 3s
35987456/48742400 [=====================>........] - ETA: 3s
36184064/48742400 [=====================>........] - ETA: 3s
36380672/48742400 [=====================>........] - ETA: 3s
36577280/48742400 [=====================>........] - ETA: 3s
36773888/48742400 [=====================>........] - ETA: 3s
36970496/48742400 [=====================>........] - ETA: 3s
37167104/48742400 [=====================>........] - ETA: 3s
37363712/48742400 [=====================>........] - ETA: 3s
37560320/48742400 [======================>.......] - ETA: 3s
37756928/48742400 [======================>.......] - ETA: 3s
37953536/48742400 [======================>.......] - ETA: 3s
38150144/48742400 [======================>.......] - ETA: 3s
38346752/48742400 [======================>.......] - ETA: 3s
38543360/48742400 [======================>.......] - ETA: 3s
38739968/48742400 [======================>.......] - ETA: 3s
38936576/48742400 [======================>.......] - ETA: 2s
39133184/48742400 [=======================>......] - ETA: 2s
39329792/48742400 [=======================>......] - ETA: 2s
39526400/48742400 [=======================>......] - ETA: 2s
39723008/48742400 [=======================>......] - ETA: 2s
39919616/48742400 [=======================>......] - ETA: 2s
40116224/48742400 [=======================>......] - ETA: 2s
40312832/48742400 [=======================>......] - ETA: 2s
40509440/48742400 [=======================>......] - ETA: 2s
40706048/48742400 [========================>.....] - ETA: 2s
40902656/48742400 [========================>.....] - ETA: 2s
41099264/48742400 [========================>.....] - ETA: 2s
41295872/48742400 [========================>.....] - ETA: 2s
41492480/48742400 [========================>.....] - ETA: 2s
41689088/48742400 [========================>.....] - ETA: 2s
41885696/48742400 [========================>.....] - ETA: 2s
42082304/48742400 [========================>.....] - ETA: 2s
42278912/48742400 [=========================>....] - ETA: 1s
42475520/48742400 [=========================>....] - ETA: 1s
42672128/48742400 [=========================>....] - ETA: 1s
42868736/48742400 [=========================>....] - ETA: 1s
43065344/48742400 [=========================>....] - ETA: 1s
43261952/48742400 [=========================>....] - ETA: 1s
43458560/48742400 [=========================>....] - ETA: 1s
43655168/48742400 [=========================>....] - ETA: 1s
43851776/48742400 [=========================>....] - ETA: 1s
44048384/48742400 [==========================>...] - ETA: 1s
44244992/48742400 [==========================>...] - ETA: 1s
44441600/48742400 [==========================>...] - ETA: 1s
44638208/48742400 [==========================>...] - ETA: 1s
44834816/48742400 [==========================>...] - ETA: 1s
45031424/48742400 [==========================>...] - ETA: 1s
45228032/48742400 [==========================>...] - ETA: 1s
45424640/48742400 [==========================>...] - ETA: 1s
45621248/48742400 [===========================>..] - ETA: 0s
45817856/48742400 [===========================>..] - ETA: 0s
46014464/48742400 [===========================>..] - ETA: 0s
46211072/48742400 [===========================>..] - ETA: 0s
46407680/48742400 [===========================>..] - ETA: 0s
46604288/48742400 [===========================>..] - ETA: 0s
46800896/48742400 [===========================>..] - ETA: 0s
46997504/48742400 [===========================>..] - ETA: 0s
47194112/48742400 [============================>.] - ETA: 0s
47390720/48742400 [============================>.] - ETA: 0s
47587328/48742400 [============================>.] - ETA: 0s
47783936/48742400 [============================>.] - ETA: 0s
47980544/48742400 [============================>.] - ETA: 0s
48177152/48742400 [============================>.] - ETA: 0s
48373760/48742400 [============================>.] - ETA: 0s
48570368/48742400 [============================>.] - ETA: 0s
48742400/48742400 [==============================] - 15s 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

   8192/1906672 [..............................] - ETA: 4s
  73728/1906672 [>.............................] - ETA: 1s
 204800/1906672 [==>...........................] - ETA: 1s
 401408/1906672 [=====>........................] - ETA: 0s
 598016/1906672 [========>.....................] - ETA: 0s
 794624/1906672 [===========>..................] - ETA: 0s
 991232/1906672 [==============>...............] - ETA: 0s
1187840/1906672 [=================>............] - ETA: 0s
1384448/1906672 [====================>.........] - ETA: 0s
1581056/1906672 [=======================>......] - ETA: 0s
1777664/1906672 [==========================>...] - ETA: 0s
1908736/1906672 [==============================] - 1s 0us/step
Model: "vgg_utk_face"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         [(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 (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_keras, 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

   8192/1876744 [..............................] - ETA: 4s
  73728/1876744 [>.............................] - ETA: 1s
 270336/1876744 [===>..........................] - ETA: 0s
 466944/1876744 [======>.......................] - ETA: 0s
 663552/1876744 [=========>....................] - ETA: 0s
 860160/1876744 [============>.................] - ETA: 0s
1056768/1876744 [===============>..............] - ETA: 0s
1253376/1876744 [===================>..........] - ETA: 0s
1449984/1876744 [======================>.......] - ETA: 0s
1646592/1876744 [=========================>....] - ETA: 0s
1843200/1876744 [============================>.] - ETA: 0s
1884160/1876744 [==============================] - 1s 0us/step
Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_5 (InputLayer)         [(None, 32, 32, 3)]       0
_________________________________________________________________
conv_0 (QuantizedConv2D)     (None, 30, 30, 32)        896
_________________________________________________________________
activation_discrete_relu (Ac (None, 30, 30, 32)        0
_________________________________________________________________
conv_1 (QuantizedConv2D)     (None, 30, 30, 32)        9248
_________________________________________________________________
conv_1_maxpool (MaxPooling2D (None, 15, 15, 32)        0
_________________________________________________________________
activation_discrete_relu_1 ( (None, 15, 15, 32)        0
_________________________________________________________________
dropout_3 (Dropout)          (None, 15, 15, 32)        0
_________________________________________________________________
conv_2 (QuantizedConv2D)     (None, 15, 15, 64)        18496
_________________________________________________________________
activation_discrete_relu_2 ( (None, 15, 15, 64)        0
_________________________________________________________________
conv_3 (QuantizedConv2D)     (None, 15, 15, 64)        36928
_________________________________________________________________
conv_3_maxpool (MaxPooling2D (None, 8, 8, 64)          0
_________________________________________________________________
activation_discrete_relu_3 ( (None, 8, 8, 64)          0
_________________________________________________________________
dropout_4 (Dropout)          (None, 8, 8, 64)          0
_________________________________________________________________
conv_4 (QuantizedConv2D)     (None, 8, 8, 84)          48468
_________________________________________________________________
activation_discrete_relu_4 ( (None, 8, 8, 84)          0
_________________________________________________________________
dropout_5 (Dropout)          (None, 8, 8, 84)          0
_________________________________________________________________
flatten_1 (Flatten)          (None, 5376)              0
_________________________________________________________________
dense_1 (QuantizedDense)     (None, 64)                344128
_________________________________________________________________
activation_discrete_relu_5 ( (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_keras, 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, input_scaling=input_scaling)
model_akida.summary()

Out:

                        Model Summary
_____________________________________________________________
Layer (type)                 Output shape  Kernel shape
=============================================================
conv_0 (InputConvolutional)  [30, 30, 32]  (3, 3, 3, 32)
_____________________________________________________________
conv_1 (Convolutional)       [15, 15, 32]  (3, 3, 32, 32)
_____________________________________________________________
conv_2 (Convolutional)       [15, 15, 64]  (3, 3, 32, 64)
_____________________________________________________________
conv_3 (Convolutional)       [8, 8, 64]    (3, 3, 64, 64)
_____________________________________________________________
conv_4 (Convolutional)       [8, 8, 84]    (3, 3, 64, 84)
_____________________________________________________________
dense_1 (FullyConnected)     [1, 1, 64]    (1, 1, 5376, 64)
_____________________________________________________________
dense_2 (FullyConnected)     [1, 1, 1]     (1, 1, 64, 1)
_____________________________________________________________
Input shape: 32, 32, 3
Backend type: Software - 1.8.10
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.2102
# Print model statistics
print("Model statistics")
stats = model_akida.get_statistics()
model_akida.evaluate(x_test_akida[:20])
for _, stat in stats.items():
    print(stat)

Out:

Model statistics
Layer (type)                  output sparsity
conv_0 (InputConvolutional)   0.74
Layer (type)                  input sparsity      output sparsity     ops
conv_1 (Convolutional)        0.74                0.79                2196648
Layer (type)                  input sparsity      output sparsity     ops
conv_2 (Convolutional)        0.79                0.74                851213
Layer (type)                  input sparsity      output sparsity     ops
conv_3 (Convolutional)        0.74                0.74                2150813
Layer (type)                  input sparsity      output sparsity     ops
conv_4 (Convolutional)        0.74                0.84                794329
Layer (type)                  input sparsity      output sparsity     ops
dense_1 (FullyConnected)      0.84                0.79                54054
Layer (type)                  input sparsity      output sparsity     ops
dense_2 (FullyConnected)      0.79                0.00                14

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_keras[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: 23.1
Akida estimated age: 25.8
Actual age: 26

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

Gallery generated by Sphinx-Gallery