Note
Click here to download the full example code
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')
Downloading data from http://data.brainchip.com/dataset-mirror/utk_face/UTKFace_preprocessed.tar.gz.
0/48742400 [..............................] - ETA: 0s
90112/48742400 [..............................] - ETA: 30s
188416/48742400 [..............................] - ETA: 27s
311296/48742400 [..............................] - ETA: 24s
450560/48742400 [..............................] - ETA: 22s
614400/48742400 [..............................] - ETA: 20s
802816/48742400 [..............................] - ETA: 18s
1024000/48742400 [..............................] - ETA: 16s
1245184/48742400 [..............................] - ETA: 15s
1490944/48742400 [..............................] - ETA: 14s
1761280/48742400 [>.............................] - ETA: 13s
2064384/48742400 [>.............................] - ETA: 12s
2392064/48742400 [>.............................] - ETA: 11s
2760704/48742400 [>.............................] - ETA: 11s
3104768/48742400 [>.............................] - ETA: 10s
3473408/48742400 [=>............................] - ETA: 10s
3686400/48742400 [=>............................] - ETA: 10s
3956736/48742400 [=>............................] - ETA: 9s
4194304/48742400 [=>............................] - ETA: 9s
4431872/48742400 [=>............................] - ETA: 9s
4546560/48742400 [=>............................] - ETA: 10s
4710400/48742400 [=>............................] - ETA: 10s
4825088/48742400 [=>............................] - ETA: 10s
4956160/48742400 [==>...........................] - ETA: 10s
5079040/48742400 [==>...........................] - ETA: 10s
5193728/48742400 [==>...........................] - ETA: 10s
5300224/48742400 [==>...........................] - ETA: 11s
5357568/48742400 [==>...........................] - ETA: 11s
5423104/48742400 [==>...........................] - ETA: 11s
5480448/48742400 [==>...........................] - ETA: 11s
5554176/48742400 [==>...........................] - ETA: 12s
5636096/48742400 [==>...........................] - ETA: 12s
5734400/48742400 [==>...........................] - ETA: 12s
5840896/48742400 [==>...........................] - ETA: 12s
5955584/48742400 [==>...........................] - ETA: 12s
6070272/48742400 [==>...........................] - ETA: 12s
6193152/48742400 [==>...........................] - ETA: 12s
6316032/48742400 [==>...........................] - ETA: 13s
6447104/48742400 [==>...........................] - ETA: 13s
6602752/48742400 [===>..........................] - ETA: 13s
6766592/48742400 [===>..........................] - ETA: 13s
6922240/48742400 [===>..........................] - ETA: 12s
7094272/48742400 [===>..........................] - ETA: 12s
7274496/48742400 [===>..........................] - ETA: 12s
7462912/48742400 [===>..........................] - ETA: 12s
7651328/48742400 [===>..........................] - ETA: 12s
7856128/48742400 [===>..........................] - ETA: 12s
7979008/48742400 [===>..........................] - ETA: 12s
8134656/48742400 [====>.........................] - ETA: 12s
8241152/48742400 [====>.........................] - ETA: 12s
8372224/48742400 [====>.........................] - ETA: 12s
8495104/48742400 [====>.........................] - ETA: 12s
8634368/48742400 [====>.........................] - ETA: 12s
8790016/48742400 [====>.........................] - ETA: 12s
8945664/48742400 [====>.........................] - ETA: 12s
9117696/48742400 [====>.........................] - ETA: 12s
9289728/48742400 [====>.........................] - ETA: 12s
9478144/48742400 [====>.........................] - ETA: 12s
9658368/48742400 [====>.........................] - ETA: 12s
9846784/48742400 [=====>........................] - ETA: 12s
10051584/48742400 [=====>........................] - ETA: 12s
10248192/48742400 [=====>........................] - ETA: 11s
10461184/48742400 [=====>........................] - ETA: 11s
10682368/48742400 [=====>........................] - ETA: 11s
10919936/48742400 [=====>........................] - ETA: 11s
11149312/48742400 [=====>........................] - ETA: 11s
11370496/48742400 [=====>........................] - ETA: 11s
11591680/48742400 [======>.......................] - ETA: 11s
11837440/48742400 [======>.......................] - ETA: 11s
12075008/48742400 [======>.......................] - ETA: 10s
12320768/48742400 [======>.......................] - ETA: 10s
12574720/48742400 [======>.......................] - ETA: 10s
12812288/48742400 [======>.......................] - ETA: 10s
13090816/48742400 [=======>......................] - ETA: 10s
13369344/48742400 [=======>......................] - ETA: 10s
13647872/48742400 [=======>......................] - ETA: 10s
13926400/48742400 [=======>......................] - ETA: 9s
14082048/48742400 [=======>......................] - ETA: 9s
14303232/48742400 [=======>......................] - ETA: 9s
14442496/48742400 [=======>......................] - ETA: 9s
14606336/48742400 [=======>......................] - ETA: 9s
14794752/48742400 [========>.....................] - ETA: 9s
14983168/48742400 [========>.....................] - ETA: 9s
15187968/48742400 [========>.....................] - ETA: 9s
15392768/48742400 [========>.....................] - ETA: 9s
15589376/48742400 [========>.....................] - ETA: 9s
15794176/48742400 [========>.....................] - ETA: 9s
16007168/48742400 [========>.....................] - ETA: 9s
16236544/48742400 [========>.....................] - ETA: 9s
16465920/48742400 [=========>....................] - ETA: 9s
16711680/48742400 [=========>....................] - ETA: 8s
16957440/48742400 [=========>....................] - ETA: 8s
17178624/48742400 [=========>....................] - ETA: 8s
17440768/48742400 [=========>....................] - ETA: 8s
17686528/48742400 [=========>....................] - ETA: 8s
17956864/48742400 [==========>...................] - ETA: 8s
18194432/48742400 [==========>...................] - ETA: 8s
18432000/48742400 [==========>...................] - ETA: 8s
18702336/48742400 [==========>...................] - ETA: 8s
18972672/48742400 [==========>...................] - ETA: 8s
19251200/48742400 [==========>...................] - ETA: 7s
19513344/48742400 [===========>..................] - ETA: 7s
19800064/48742400 [===========>..................] - ETA: 7s
20103168/48742400 [===========>..................] - ETA: 7s
20414464/48742400 [===========>..................] - ETA: 7s
20668416/48742400 [===========>..................] - ETA: 7s
20971520/48742400 [===========>..................] - ETA: 7s
21258240/48742400 [============>.................] - ETA: 7s
21544960/48742400 [============>.................] - ETA: 7s
21839872/48742400 [============>.................] - ETA: 6s
22118400/48742400 [============>.................] - ETA: 6s
22429696/48742400 [============>.................] - ETA: 6s
22765568/48742400 [=============>................] - ETA: 6s
23093248/48742400 [=============>................] - ETA: 6s
23404544/48742400 [=============>................] - ETA: 6s
23732224/48742400 [=============>................] - ETA: 6s
23928832/48742400 [=============>................] - ETA: 6s
24150016/48742400 [=============>................] - ETA: 6s
24412160/48742400 [==============>...............] - ETA: 6s
24666112/48742400 [==============>...............] - ETA: 6s
24780800/48742400 [==============>...............] - ETA: 6s
25067520/48742400 [==============>...............] - ETA: 5s
25255936/48742400 [==============>...............] - ETA: 5s
25370624/48742400 [==============>...............] - ETA: 5s
25550848/48742400 [==============>...............] - ETA: 5s
25722880/48742400 [==============>...............] - ETA: 5s
25903104/48742400 [==============>...............] - ETA: 5s
26083328/48742400 [===============>..............] - ETA: 5s
26271744/48742400 [===============>..............] - ETA: 5s
26468352/48742400 [===============>..............] - ETA: 5s
26681344/48742400 [===============>..............] - ETA: 5s
26902528/48742400 [===============>..............] - ETA: 5s
27115520/48742400 [===============>..............] - ETA: 5s
27279360/48742400 [===============>..............] - ETA: 5s
27492352/48742400 [===============>..............] - ETA: 5s
27729920/48742400 [================>.............] - ETA: 5s
27975680/48742400 [================>.............] - ETA: 5s
28188672/48742400 [================>.............] - ETA: 5s
28319744/48742400 [================>.............] - ETA: 5s
28475392/48742400 [================>.............] - ETA: 5s
28606464/48742400 [================>.............] - ETA: 5s
28737536/48742400 [================>.............] - ETA: 5s
28884992/48742400 [================>.............] - ETA: 5s
29048832/48742400 [================>.............] - ETA: 5s
29220864/48742400 [================>.............] - ETA: 4s
29392896/48742400 [=================>............] - ETA: 4s
29573120/48742400 [=================>............] - ETA: 4s
29753344/48742400 [=================>............] - ETA: 4s
29941760/48742400 [=================>............] - ETA: 4s
30130176/48742400 [=================>............] - ETA: 4s
30310400/48742400 [=================>............] - ETA: 4s
30515200/48742400 [=================>............] - ETA: 4s
30744576/48742400 [=================>............] - ETA: 4s
30973952/48742400 [==================>...........] - ETA: 4s
31178752/48742400 [==================>...........] - ETA: 4s
31358976/48742400 [==================>...........] - ETA: 4s
31514624/48742400 [==================>...........] - ETA: 4s
31670272/48742400 [==================>...........] - ETA: 4s
31801344/48742400 [==================>...........] - ETA: 4s
31899648/48742400 [==================>...........] - ETA: 4s
31989760/48742400 [==================>...........] - ETA: 4s
32063488/48742400 [==================>...........] - ETA: 4s
32129024/48742400 [==================>...........] - ETA: 4s
32227328/48742400 [==================>...........] - ETA: 4s
32333824/48742400 [==================>...........] - ETA: 4s
32448512/48742400 [==================>...........] - ETA: 4s
32571392/48742400 [===================>..........] - ETA: 4s
32694272/48742400 [===================>..........] - ETA: 4s
32833536/48742400 [===================>..........] - ETA: 4s
32997376/48742400 [===================>..........] - ETA: 4s
33153024/48742400 [===================>..........] - ETA: 4s
33325056/48742400 [===================>..........] - ETA: 4s
33497088/48742400 [===================>..........] - ETA: 4s
33669120/48742400 [===================>..........] - ETA: 4s
33857536/48742400 [===================>..........] - ETA: 3s
34062336/48742400 [===================>..........] - ETA: 3s
34267136/48742400 [====================>.........] - ETA: 3s
34480128/48742400 [====================>.........] - ETA: 3s
34693120/48742400 [====================>.........] - ETA: 3s
34906112/48742400 [====================>.........] - ETA: 3s
35127296/48742400 [====================>.........] - ETA: 3s
35356672/48742400 [====================>.........] - ETA: 3s
35586048/48742400 [====================>.........] - ETA: 3s
35815424/48742400 [=====================>........] - ETA: 3s
36069376/48742400 [=====================>........] - ETA: 3s
36306944/48742400 [=====================>........] - ETA: 3s
36569088/48742400 [=====================>........] - ETA: 3s
36831232/48742400 [=====================>........] - ETA: 3s
37093376/48742400 [=====================>........] - ETA: 3s
37347328/48742400 [=====================>........] - ETA: 2s
37617664/48742400 [======================>.......] - ETA: 2s
37888000/48742400 [======================>.......] - ETA: 2s
38166528/48742400 [======================>.......] - ETA: 2s
38461440/48742400 [======================>.......] - ETA: 2s
38748160/48742400 [======================>.......] - ETA: 2s
39034880/48742400 [=======================>......] - ETA: 2s
39256064/48742400 [=======================>......] - ETA: 2s
39378944/48742400 [=======================>......] - ETA: 2s
39534592/48742400 [=======================>......] - ETA: 2s
39698432/48742400 [=======================>......] - ETA: 2s
39878656/48742400 [=======================>......] - ETA: 2s
40075264/48742400 [=======================>......] - ETA: 2s
40271872/48742400 [=======================>......] - ETA: 2s
40476672/48742400 [=======================>......] - ETA: 2s
40689664/48742400 [========================>.....] - ETA: 2s
40902656/48742400 [========================>.....] - ETA: 2s
41115648/48742400 [========================>.....] - ETA: 1s
41312256/48742400 [========================>.....] - ETA: 1s
41541632/48742400 [========================>.....] - ETA: 1s
41738240/48742400 [========================>.....] - ETA: 1s
41934848/48742400 [========================>.....] - ETA: 1s
42172416/48742400 [========================>.....] - ETA: 1s
42393600/48742400 [=========================>....] - ETA: 1s
42655744/48742400 [=========================>....] - ETA: 1s
42885120/48742400 [=========================>....] - ETA: 1s
43114496/48742400 [=========================>....] - ETA: 1s
43368448/48742400 [=========================>....] - ETA: 1s
43622400/48742400 [=========================>....] - ETA: 1s
43761664/48742400 [=========================>....] - ETA: 1s
43966464/48742400 [==========================>...] - ETA: 1s
44138496/48742400 [==========================>...] - ETA: 1s
44302336/48742400 [==========================>...] - ETA: 1s
44474368/48742400 [==========================>...] - ETA: 1s
44646400/48742400 [==========================>...] - ETA: 1s
44818432/48742400 [==========================>...] - ETA: 1s
45006848/48742400 [==========================>...] - ETA: 0s
45195264/48742400 [==========================>...] - ETA: 0s
45391872/48742400 [==========================>...] - ETA: 0s
45572096/48742400 [===========================>..] - ETA: 0s
45752320/48742400 [===========================>..] - ETA: 0s
45981696/48742400 [===========================>..] - ETA: 0s
46243840/48742400 [===========================>..] - ETA: 0s
46473216/48742400 [===========================>..] - ETA: 0s
46669824/48742400 [===========================>..] - ETA: 0s
46915584/48742400 [===========================>..] - ETA: 0s
47161344/48742400 [============================>.] - ETA: 0s
47423488/48742400 [============================>.] - ETA: 0s
47669248/48742400 [============================>.] - ETA: 0s
47931392/48742400 [============================>.] - ETA: 0s
48168960/48742400 [============================>.] - ETA: 0s
48381952/48742400 [============================>.] - ETA: 0s
48562176/48742400 [============================>.] - ETA: 0s
48685056/48742400 [============================>.] - ETA: 0s
48742400/48742400 [==============================] - 13s 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()
Downloading data from http://data.brainchip.com/models/vgg/vgg_utk_face.h5
8192/1913096 [..............................] - ETA: 0s
172032/1913096 [=>............................] - ETA: 0s
458752/1913096 [======>.......................] - ETA: 0s
778240/1913096 [===========>..................] - ETA: 0s
1048576/1913096 [===============>..............] - ETA: 0s
1376256/1913096 [====================>.........] - ETA: 0s
1597440/1913096 [========================>.....] - ETA: 0s
1835008/1913096 [===========================>..] - ETA: 0s
1913096/1913096 [==============================] - 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 (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_1 (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_2 (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.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()
Downloading data from http://data.brainchip.com/models/vgg/vgg_utk_face_iq8_wq2_aq2.h5.
0/1877320 [..............................] - ETA: 0s
73728/1877320 [>.............................] - ETA: 1s
237568/1877320 [==>...........................] - ETA: 0s
368640/1877320 [====>.........................] - ETA: 0s
499712/1877320 [======>.......................] - ETA: 0s
630784/1877320 [=========>....................] - ETA: 0s
761856/1877320 [===========>..................] - ETA: 0s
901120/1877320 [=============>................] - ETA: 0s
1007616/1877320 [===============>..............] - ETA: 0s
1171456/1877320 [=================>............] - ETA: 0s
1294336/1877320 [===================>..........] - ETA: 0s
1409024/1877320 [=====================>........] - ETA: 0s
1531904/1877320 [=======================>......] - ETA: 0s
1654784/1877320 [=========================>....] - ETA: 0s
1785856/1877320 [===========================>..] - ETA: 0s
1877320/1877320 [==============================] - 1s 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 (QuantizedConv2D) (None, 30, 30, 32) 896
conv_0/relu (ActivationDisc (None, 30, 30, 32) 0
reteRelu)
conv_1 (QuantizedConv2D) (None, 30, 30, 32) 9248
conv_1/maxpool (MaxPooling2 (None, 15, 15, 32) 0
D)
conv_1/relu (ActivationDisc (None, 15, 15, 32) 0
reteRelu)
dropout_3 (Dropout) (None, 15, 15, 32) 0
conv_2 (QuantizedConv2D) (None, 15, 15, 64) 18496
conv_2/relu (ActivationDisc (None, 15, 15, 64) 0
reteRelu)
conv_3 (QuantizedConv2D) (None, 15, 15, 64) 36928
conv_3/maxpool (MaxPooling2 (None, 8, 8, 64) 0
D)
conv_3/relu (ActivationDisc (None, 8, 8, 64) 0
reteRelu)
dropout_4 (Dropout) (None, 8, 8, 64) 0
conv_4 (QuantizedConv2D) (None, 8, 8, 84) 48468
conv_4/relu (ActivationDisc (None, 8, 8, 84) 0
reteRelu)
dropout_5 (Dropout) (None, 8, 8, 84) 0
flatten (Flatten) (None, 5376) 0
dense_1 (QuantizedDense) (None, 64) 344128
dense_1/relu (ActivationDis (None, 64) 0
creteRelu)
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))
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()
Model Summary
______________________________________________
Input shape Output shape Sequences Layers
==============================================
[32, 32, 3] [1, 1, 1] 1 7
______________________________________________
_____________________________________________________
Layer (type) Output shape Kernel shape
============ SW/conv_0-dense_2 (Software) ===========
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.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 purpose
assert abs(mae_keras - mae_akida) < 0.5
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()}")
1/1 [==============================] - ETA: 0s
1/1 [==============================] - 0s 133ms/step
Keras estimated age: 31.1
Akida estimated age: 25.8
Actual age: 21
Total running time of the script: ( 0 minutes 32.341 seconds)