DS-CNN CIFAR10 inference

This tutorial uses the CIFAR-10 dataset (60k training images distributed in 10 object classes) for a classic object classification task with a network built around the Depthwise Separable Convolutional Neural Network (DS-CNN) which is originated from Zhang et al (2018).

The goal of the tutorial is to provide users with an example of a complex model that can be converted to an Akida model and that can be run on Akida NSoC with an accuracy similar to a standard Keras floating point model.

1. Dataset preparation

from tensorflow.keras.datasets import cifar10

# Load CIFAR10 dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# Reshape x-data
x_train = x_train.reshape(50000, 32, 32, 3)
x_test = x_test.reshape(10000, 32, 32, 3)
input_shape = (32, 32, 3)

Out:

Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz

    16384/170498071 [..............................] - ETA: 1s
    40960/170498071 [..............................] - ETA: 6:31
    90112/170498071 [..............................] - ETA: 5:54
   188416/170498071 [..............................] - ETA: 4:14
   368640/170498071 [..............................] - ETA: 2:57
   385024/170498071 [..............................] - ETA: 3:28
   860160/170498071 [..............................] - ETA: 1:51
  1138688/170498071 [..............................] - ETA: 1:38
  1400832/170498071 [..............................] - ETA: 1:26
  1679360/170498071 [..............................] - ETA: 1:17
  1695744/170498071 [..............................] - ETA: 1:24
  1974272/170498071 [..............................] - ETA: 1:17
  2269184/170498071 [..............................] - ETA: 1:16
  2564096/170498071 [..............................] - ETA: 1:11
  2859008/170498071 [..............................] - ETA: 1:07
  2875392/170498071 [..............................] - ETA: 1:11
  3186688/170498071 [..............................] - ETA: 1:09
  3497984/170498071 [..............................] - ETA: 1:07
  3809280/170498071 [..............................] - ETA: 1:05
  4120576/170498071 [..............................] - ETA: 1:03
  4431872/170498071 [..............................] - ETA: 1:00
  4464640/170498071 [..............................] - ETA: 1:03
  4775936/170498071 [..............................] - ETA: 1:00
  5103616/170498071 [..............................] - ETA: 59s 
  5431296/170498071 [..............................] - ETA: 57s
  5447680/170498071 [..............................] - ETA: 59s
  5775360/170498071 [>.............................] - ETA: 58s
  6103040/170498071 [>.............................] - ETA: 56s
  6119424/170498071 [>.............................] - ETA: 58s
  6463488/170498071 [>.............................] - ETA: 57s
  6807552/170498071 [>.............................] - ETA: 55s
  7135232/170498071 [>.............................] - ETA: 54s
  7168000/170498071 [>.............................] - ETA: 55s
  7512064/170498071 [>.............................] - ETA: 55s
  7856128/170498071 [>.............................] - ETA: 53s
  8200192/170498071 [>.............................] - ETA: 52s
  8232960/170498071 [>.............................] - ETA: 53s
  8560640/170498071 [>.............................] - ETA: 52s
  8921088/170498071 [>.............................] - ETA: 52s
  9281536/170498071 [>.............................] - ETA: 51s
  9641984/170498071 [>.............................] - ETA: 50s
  9674752/170498071 [>.............................] - ETA: 51s
 10018816/170498071 [>.............................] - ETA: 50s
 10362880/170498071 [>.............................] - ETA: 49s
 10412032/170498071 [>.............................] - ETA: 50s
 10756096/170498071 [>.............................] - ETA: 49s
 11116544/170498071 [>.............................] - ETA: 49s
 11476992/170498071 [=>............................] - ETA: 48s
 11509760/170498071 [=>............................] - ETA: 49s
 11870208/170498071 [=>............................] - ETA: 48s
 12230656/170498071 [=>............................] - ETA: 47s
 12263424/170498071 [=>............................] - ETA: 48s
 12607488/170498071 [=>............................] - ETA: 47s
 12984320/170498071 [=>............................] - ETA: 47s
 13361152/170498071 [=>............................] - ETA: 47s
 13737984/170498071 [=>............................] - ETA: 46s
 13787136/170498071 [=>............................] - ETA: 47s
 14131200/170498071 [=>............................] - ETA: 46s
 14508032/170498071 [=>............................] - ETA: 46s
 14868480/170498071 [=>............................] - ETA: 45s
 14934016/170498071 [=>............................] - ETA: 46s
 15278080/170498071 [=>............................] - ETA: 45s
 15638528/170498071 [=>............................] - ETA: 44s
 15687680/170498071 [=>............................] - ETA: 45s
 16048128/170498071 [=>............................] - ETA: 44s
 16408576/170498071 [=>............................] - ETA: 44s
 16457728/170498071 [=>............................] - ETA: 44s
 16818176/170498071 [=>............................] - ETA: 44s
 17145856/170498071 [==>...........................] - ETA: 43s
 17227776/170498071 [==>...........................] - ETA: 44s
 17588224/170498071 [==>...........................] - ETA: 43s
 17915904/170498071 [==>...........................] - ETA: 43s
 17997824/170498071 [==>...........................] - ETA: 43s
 18358272/170498071 [==>...........................] - ETA: 43s
 18669568/170498071 [==>...........................] - ETA: 43s
 18784256/170498071 [==>...........................] - ETA: 43s
 19128320/170498071 [==>...........................] - ETA: 42s
 19439616/170498071 [==>...........................] - ETA: 42s
 19554304/170498071 [==>...........................] - ETA: 42s
 19898368/170498071 [==>...........................] - ETA: 42s
 20201472/170498071 [==>...........................] - ETA: 42s
 20324352/170498071 [==>...........................] - ETA: 42s
 20668416/170498071 [==>...........................] - ETA: 42s
 20963328/170498071 [==>...........................] - ETA: 41s
 21094400/170498071 [==>...........................] - ETA: 42s
 21438464/170498071 [==>...........................] - ETA: 41s
 21716992/170498071 [==>...........................] - ETA: 41s
 21864448/170498071 [==>...........................] - ETA: 41s
 22208512/170498071 [==>...........................] - ETA: 41s
 22478848/170498071 [==>...........................] - ETA: 41s
 22634496/170498071 [==>...........................] - ETA: 41s
 22978560/170498071 [===>..........................] - ETA: 40s
 23248896/170498071 [===>..........................] - ETA: 40s
 23404544/170498071 [===>..........................] - ETA: 40s
 23748608/170498071 [===>..........................] - ETA: 40s
 23994368/170498071 [===>..........................] - ETA: 40s
 24174592/170498071 [===>..........................] - ETA: 40s
 24518656/170498071 [===>..........................] - ETA: 40s
 24764416/170498071 [===>..........................] - ETA: 39s
 24944640/170498071 [===>..........................] - ETA: 40s
 25288704/170498071 [===>..........................] - ETA: 39s
 25550848/170498071 [===>..........................] - ETA: 39s
 25714688/170498071 [===>..........................] - ETA: 39s
 26058752/170498071 [===>..........................] - ETA: 39s
 26320896/170498071 [===>..........................] - ETA: 39s
 26484736/170498071 [===>..........................] - ETA: 39s
 26828800/170498071 [===>..........................] - ETA: 39s
 27082752/170498071 [===>..........................] - ETA: 38s
 27254784/170498071 [===>..........................] - ETA: 39s
 27598848/170498071 [===>..........................] - ETA: 38s
 27852800/170498071 [===>..........................] - ETA: 38s
 28024832/170498071 [===>..........................] - ETA: 38s
 28368896/170498071 [===>..........................] - ETA: 38s
 28614656/170498071 [====>.........................] - ETA: 38s
 28794880/170498071 [====>.........................] - ETA: 38s
 29138944/170498071 [====>.........................] - ETA: 38s
 29384704/170498071 [====>.........................] - ETA: 38s
 29564928/170498071 [====>.........................] - ETA: 38s
 29925376/170498071 [====>.........................] - ETA: 37s
 30171136/170498071 [====>.........................] - ETA: 37s
 30351360/170498071 [====>.........................] - ETA: 37s
 30695424/170498071 [====>.........................] - ETA: 37s
 30924800/170498071 [====>.........................] - ETA: 37s
 31121408/170498071 [====>.........................] - ETA: 37s
 31481856/170498071 [====>.........................] - ETA: 37s
 31711232/170498071 [====>.........................] - ETA: 37s
 31907840/170498071 [====>.........................] - ETA: 37s
 32251904/170498071 [====>.........................] - ETA: 36s
 32481280/170498071 [====>.........................] - ETA: 36s
 32694272/170498071 [====>.........................] - ETA: 36s
 33038336/170498071 [====>.........................] - ETA: 36s
 33267712/170498071 [====>.........................] - ETA: 36s
 33464320/170498071 [====>.........................] - ETA: 36s
 33824768/170498071 [====>.........................] - ETA: 36s
 34054144/170498071 [====>.........................] - ETA: 36s
 34267136/170498071 [=====>........................] - ETA: 36s
 34627584/170498071 [=====>........................] - ETA: 36s
 34856960/170498071 [=====>........................] - ETA: 35s
 35053568/170498071 [=====>........................] - ETA: 36s
 35414016/170498071 [=====>........................] - ETA: 35s
 35643392/170498071 [=====>........................] - ETA: 35s
 35856384/170498071 [=====>........................] - ETA: 35s
 36216832/170498071 [=====>........................] - ETA: 35s
 36454400/170498071 [=====>........................] - ETA: 35s
 36659200/170498071 [=====>........................] - ETA: 35s
 37036032/170498071 [=====>........................] - ETA: 35s
 37265408/170498071 [=====>........................] - ETA: 35s
 37478400/170498071 [=====>........................] - ETA: 35s
 37838848/170498071 [=====>........................] - ETA: 34s
 38068224/170498071 [=====>........................] - ETA: 34s
 38281216/170498071 [=====>........................] - ETA: 34s
 38658048/170498071 [=====>........................] - ETA: 34s
 38895616/170498071 [=====>........................] - ETA: 34s
 39116800/170498071 [=====>........................] - ETA: 34s
 39477248/170498071 [=====>........................] - ETA: 34s
 39706624/170498071 [=====>........................] - ETA: 34s
 39952384/170498071 [======>.......................] - ETA: 34s
 40312832/170498071 [======>.......................] - ETA: 33s
 40542208/170498071 [======>.......................] - ETA: 33s
 40787968/170498071 [======>.......................] - ETA: 33s
 41164800/170498071 [======>.......................] - ETA: 33s
 41410560/170498071 [======>.......................] - ETA: 33s
 41639936/170498071 [======>.......................] - ETA: 33s
 42016768/170498071 [======>.......................] - ETA: 33s
 42246144/170498071 [======>.......................] - ETA: 33s
 42508288/170498071 [======>.......................] - ETA: 33s
 42885120/170498071 [======>.......................] - ETA: 32s
 43130880/170498071 [======>.......................] - ETA: 32s
 43393024/170498071 [======>.......................] - ETA: 32s
 43769856/170498071 [======>.......................] - ETA: 32s
 43999232/170498071 [======>.......................] - ETA: 32s
 44277760/170498071 [======>.......................] - ETA: 32s
 44670976/170498071 [======>.......................] - ETA: 32s
 44900352/170498071 [======>.......................] - ETA: 32s
 45178880/170498071 [======>.......................] - ETA: 32s
 45572096/170498071 [=======>......................] - ETA: 31s
 45817856/170498071 [=======>......................] - ETA: 31s
 46096384/170498071 [=======>......................] - ETA: 31s
 46505984/170498071 [=======>......................] - ETA: 31s
 46759936/170498071 [=======>......................] - ETA: 31s
 47030272/170498071 [=======>......................] - ETA: 31s
 47439872/170498071 [=======>......................] - ETA: 31s
 47669248/170498071 [=======>......................] - ETA: 31s
 47996928/170498071 [=======>......................] - ETA: 31s
 48406528/170498071 [=======>......................] - ETA: 30s
 48635904/170498071 [=======>......................] - ETA: 30s
 48963584/170498071 [=======>......................] - ETA: 30s
 49389568/170498071 [=======>......................] - ETA: 30s
 49627136/170498071 [=======>......................] - ETA: 30s
 49963008/170498071 [=======>......................] - ETA: 30s
 50388992/170498071 [=======>......................] - ETA: 29s
 50642944/170498071 [=======>......................] - ETA: 29s
 50978816/170498071 [=======>......................] - ETA: 29s
 51404800/170498071 [========>.....................] - ETA: 29s
 51675136/170498071 [========>.....................] - ETA: 29s
 52027392/170498071 [========>.....................] - ETA: 29s
 52453376/170498071 [========>.....................] - ETA: 29s
 52731904/170498071 [========>.....................] - ETA: 29s
 53092352/170498071 [========>.....................] - ETA: 28s
 53534720/170498071 [========>.....................] - ETA: 28s
 53821440/170498071 [========>.....................] - ETA: 28s
 54190080/170498071 [========>.....................] - ETA: 28s
 54632448/170498071 [========>.....................] - ETA: 28s
 54927360/170498071 [========>.....................] - ETA: 28s
 55304192/170498071 [========>.....................] - ETA: 28s
 55779328/170498071 [========>.....................] - ETA: 27s
 56090624/170498071 [========>.....................] - ETA: 27s
 56451072/170498071 [========>.....................] - ETA: 27s
 56942592/170498071 [=========>....................] - ETA: 27s
 57253888/170498071 [=========>....................] - ETA: 27s
 57630720/170498071 [=========>....................] - ETA: 27s
 58122240/170498071 [=========>....................] - ETA: 26s
 58449920/170498071 [=========>....................] - ETA: 26s
 58843136/170498071 [=========>....................] - ETA: 26s
 59351040/170498071 [=========>....................] - ETA: 26s
 59662336/170498071 [=========>....................] - ETA: 26s
 60071936/170498071 [=========>....................] - ETA: 26s
 60448768/170498071 [=========>....................] - ETA: 25s
 60727296/170498071 [=========>....................] - ETA: 25s
 61251584/170498071 [=========>....................] - ETA: 25s
 61595648/170498071 [=========>....................] - ETA: 25s
 62005248/170498071 [=========>....................] - ETA: 25s
 62431232/170498071 [=========>....................] - ETA: 25s
 62693376/170498071 [==========>...................] - ETA: 25s
 63250432/170498071 [==========>...................] - ETA: 24s
 63594496/170498071 [==========>...................] - ETA: 24s
 64004096/170498071 [==========>...................] - ETA: 24s
 64462848/170498071 [==========>...................] - ETA: 24s
 64757760/170498071 [==========>...................] - ETA: 24s
 65331200/170498071 [==========>...................] - ETA: 24s
 65708032/170498071 [==========>...................] - ETA: 23s
 66117632/170498071 [==========>...................] - ETA: 23s
 66543616/170498071 [==========>...................] - ETA: 23s
 66625536/170498071 [==========>...................] - ETA: 23s
 67543040/170498071 [==========>...................] - ETA: 23s
 67969024/170498071 [==========>...................] - ETA: 23s
 68280320/170498071 [===========>..................] - ETA: 23s
 68575232/170498071 [===========>..................] - ETA: 22s
 69017600/170498071 [===========>..................] - ETA: 22s
 69263360/170498071 [===========>..................] - ETA: 22s
 69623808/170498071 [===========>..................] - ETA: 22s
 69918720/170498071 [===========>..................] - ETA: 22s
 70213632/170498071 [===========>..................] - ETA: 22s
 70672384/170498071 [===========>..................] - ETA: 22s
 70934528/170498071 [===========>..................] - ETA: 22s
 71278592/170498071 [===========>..................] - ETA: 22s
 71614464/170498071 [===========>..................] - ETA: 22s
 71917568/170498071 [===========>..................] - ETA: 21s
 72376320/170498071 [===========>..................] - ETA: 21s
 72654848/170498071 [===========>..................] - ETA: 21s
 72998912/170498071 [===========>..................] - ETA: 21s
 73359360/170498071 [===========>..................] - ETA: 21s
 73670656/170498071 [===========>..................] - ETA: 21s
 74162176/170498071 [============>.................] - ETA: 21s
 74506240/170498071 [============>.................] - ETA: 21s
 74850304/170498071 [============>.................] - ETA: 21s
 75210752/170498071 [============>.................] - ETA: 20s
 75489280/170498071 [============>.................] - ETA: 20s
 75980800/170498071 [============>.................] - ETA: 20s
 76341248/170498071 [============>.................] - ETA: 20s
 76668928/170498071 [============>.................] - ETA: 20s
 77053952/170498071 [============>.................] - ETA: 20s
 77357056/170498071 [============>.................] - ETA: 20s
 77881344/170498071 [============>.................] - ETA: 20s
 78249984/170498071 [============>.................] - ETA: 20s
 78635008/170498071 [============>.................] - ETA: 19s
 79159296/170498071 [============>.................] - ETA: 19s
 79536128/170498071 [============>.................] - ETA: 19s
 79929344/170498071 [=============>................] - ETA: 19s
 80453632/170498071 [=============>................] - ETA: 19s
 80781312/170498071 [=============>................] - ETA: 19s
 81190912/170498071 [=============>................] - ETA: 19s
 81600512/170498071 [=============>................] - ETA: 19s
 81879040/170498071 [=============>................] - ETA: 18s
 82436096/170498071 [=============>................] - ETA: 18s
 82763776/170498071 [=============>................] - ETA: 18s
 83181568/170498071 [=============>................] - ETA: 18s
 83615744/170498071 [=============>................] - ETA: 18s
 83894272/170498071 [=============>................] - ETA: 18s
 84451328/170498071 [=============>................] - ETA: 18s
 84779008/170498071 [=============>................] - ETA: 18s
 85188608/170498071 [=============>................] - ETA: 17s
 85598208/170498071 [==============>...............] - ETA: 17s
 85925888/170498071 [==============>...............] - ETA: 17s
 86499328/170498071 [==============>...............] - ETA: 17s
 86835200/170498071 [==============>...............] - ETA: 17s
 87244800/170498071 [==============>...............] - ETA: 17s
 87629824/170498071 [==============>...............] - ETA: 17s
 87990272/170498071 [==============>...............] - ETA: 17s
 88580096/170498071 [==============>...............] - ETA: 17s
 88907776/170498071 [==============>...............] - ETA: 16s
 89325568/170498071 [==============>...............] - ETA: 16s
 89743360/170498071 [==============>...............] - ETA: 16s
 90087424/170498071 [==============>...............] - ETA: 16s
 90677248/170498071 [==============>...............] - ETA: 16s
 90988544/170498071 [===============>..............] - ETA: 16s
 91430912/170498071 [===============>..............] - ETA: 16s
 91889664/170498071 [===============>..............] - ETA: 16s
 92200960/170498071 [===============>..............] - ETA: 16s
 92807168/170498071 [===============>..............] - ETA: 15s
 93118464/170498071 [===============>..............] - ETA: 15s
 93577216/170498071 [===============>..............] - ETA: 15s
 94035968/170498071 [===============>..............] - ETA: 15s
 94347264/170498071 [===============>..............] - ETA: 15s
 94953472/170498071 [===============>..............] - ETA: 15s
 95264768/170498071 [===============>..............] - ETA: 15s
 95723520/170498071 [===============>..............] - ETA: 15s
 96206848/170498071 [===============>..............] - ETA: 15s
 96509952/170498071 [===============>..............] - ETA: 14s
 97099776/170498071 [================>.............] - ETA: 14s
 97402880/170498071 [================>.............] - ETA: 14s
 97853440/170498071 [================>.............] - ETA: 14s
 98271232/170498071 [================>.............] - ETA: 14s
 98672640/170498071 [================>.............] - ETA: 14s
 99278848/170498071 [================>.............] - ETA: 14s
 99565568/170498071 [================>.............] - ETA: 14s
100032512/170498071 [================>.............] - ETA: 14s
100433920/170498071 [================>.............] - ETA: 13s
100851712/170498071 [================>.............] - ETA: 13s
101457920/170498071 [================>.............] - ETA: 13s
101752832/170498071 [================>.............] - ETA: 13s
102227968/170498071 [================>.............] - ETA: 13s
102588416/170498071 [=================>............] - ETA: 13s
103047168/170498071 [=================>............] - ETA: 13s
103669760/170498071 [=================>............] - ETA: 13s
104013824/170498071 [=================>............] - ETA: 13s
104448000/170498071 [=================>............] - ETA: 12s
104833024/170498071 [=================>............] - ETA: 12s
105242624/170498071 [=================>............] - ETA: 12s
105865216/170498071 [=================>............] - ETA: 12s
106209280/170498071 [=================>............] - ETA: 12s
106651648/170498071 [=================>............] - ETA: 12s
107028480/170498071 [=================>............] - ETA: 12s
107438080/170498071 [=================>............] - ETA: 12s
108077056/170498071 [==================>...........] - ETA: 12s
108421120/170498071 [==================>...........] - ETA: 12s
108863488/170498071 [==================>...........] - ETA: 11s
109240320/170498071 [==================>...........] - ETA: 11s
109649920/170498071 [==================>...........] - ETA: 11s
110256128/170498071 [==================>...........] - ETA: 11s
111091712/170498071 [==================>...........] - ETA: 11s
111517696/170498071 [==================>...........] - ETA: 11s
111730688/170498071 [==================>...........] - ETA: 11s
112074752/170498071 [==================>...........] - ETA: 11s
112353280/170498071 [==================>...........] - ETA: 11s
112664576/170498071 [==================>...........] - ETA: 11s
113123328/170498071 [==================>...........] - ETA: 11s
113369088/170498071 [==================>...........] - ETA: 10s
113762304/170498071 [===================>..........] - ETA: 10s
114221056/170498071 [===================>..........] - ETA: 10s
114450432/170498071 [===================>..........] - ETA: 10s
114827264/170498071 [===================>..........] - ETA: 10s
115122176/170498071 [===================>..........] - ETA: 10s
115449856/170498071 [===================>..........] - ETA: 10s
115941376/170498071 [===================>..........] - ETA: 10s
116203520/170498071 [===================>..........] - ETA: 10s
116563968/170498071 [===================>..........] - ETA: 10s
116875264/170498071 [===================>..........] - ETA: 10s
117202944/170498071 [===================>..........] - ETA: 10s
117710848/170498071 [===================>..........] - ETA: 10s
117972992/170498071 [===================>..........] - ETA: 10s
118349824/170498071 [===================>..........] - ETA: 9s 
118677504/170498071 [===================>..........] - ETA: 9s
119005184/170498071 [===================>..........] - ETA: 9s
119545856/170498071 [====================>.........] - ETA: 9s
119824384/170498071 [====================>.........] - ETA: 9s
120201216/170498071 [====================>.........] - ETA: 9s
120528896/170498071 [====================>.........] - ETA: 9s
120889344/170498071 [====================>.........] - ETA: 9s
121413632/170498071 [====================>.........] - ETA: 9s
121708544/170498071 [====================>.........] - ETA: 9s
122101760/170498071 [====================>.........] - ETA: 9s
122429440/170498071 [====================>.........] - ETA: 9s
122789888/170498071 [====================>.........] - ETA: 9s
123346944/170498071 [====================>.........] - ETA: 8s
123641856/170498071 [====================>.........] - ETA: 8s
124035072/170498071 [====================>.........] - ETA: 8s
124379136/170498071 [====================>.........] - ETA: 8s
124755968/170498071 [====================>.........] - ETA: 8s
125313024/170498071 [=====================>........] - ETA: 8s
125607936/170498071 [=====================>........] - ETA: 8s
126001152/170498071 [=====================>........] - ETA: 8s
126361600/170498071 [=====================>........] - ETA: 8s
126738432/170498071 [=====================>........] - ETA: 8s
127311872/170498071 [=====================>........] - ETA: 8s
127614976/170498071 [=====================>........] - ETA: 8s
128016384/170498071 [=====================>........] - ETA: 7s
128393216/170498071 [=====================>........] - ETA: 7s
128770048/170498071 [=====================>........] - ETA: 7s
129343488/170498071 [=====================>........] - ETA: 7s
129646592/170498071 [=====================>........] - ETA: 7s
130064384/170498071 [=====================>........] - ETA: 7s
130441216/170498071 [=====================>........] - ETA: 7s
130818048/170498071 [======================>.......] - ETA: 7s
131407872/170498071 [======================>.......] - ETA: 7s
131719168/170498071 [======================>.......] - ETA: 7s
132145152/170498071 [======================>.......] - ETA: 7s
132521984/170498071 [======================>.......] - ETA: 7s
132898816/170498071 [======================>.......] - ETA: 6s
133488640/170498071 [======================>.......] - ETA: 6s
133808128/170498071 [======================>.......] - ETA: 6s
134242304/170498071 [======================>.......] - ETA: 6s
134619136/170498071 [======================>.......] - ETA: 6s
134995968/170498071 [======================>.......] - ETA: 6s
135618560/170498071 [======================>.......] - ETA: 6s
135929856/170498071 [======================>.......] - ETA: 6s
136355840/170498071 [======================>.......] - ETA: 6s
136749056/170498071 [=======================>......] - ETA: 6s
137125888/170498071 [=======================>......] - ETA: 6s
137748480/170498071 [=======================>......] - ETA: 6s
138043392/170498071 [=======================>......] - ETA: 5s
138502144/170498071 [=======================>......] - ETA: 5s
138895360/170498071 [=======================>......] - ETA: 5s
139272192/170498071 [=======================>......] - ETA: 5s
139894784/170498071 [=======================>......] - ETA: 5s
140206080/170498071 [=======================>......] - ETA: 5s
140664832/170498071 [=======================>......] - ETA: 5s
141058048/170498071 [=======================>......] - ETA: 5s
141434880/170498071 [=======================>......] - ETA: 5s
142073856/170498071 [=======================>......] - ETA: 5s
142385152/170498071 [========================>.....] - ETA: 5s
142827520/170498071 [========================>.....] - ETA: 5s
143220736/170498071 [========================>.....] - ETA: 4s
143613952/170498071 [========================>.....] - ETA: 4s
144236544/170498071 [========================>.....] - ETA: 4s
144531456/170498071 [========================>.....] - ETA: 4s
145006592/170498071 [========================>.....] - ETA: 4s
145399808/170498071 [========================>.....] - ETA: 4s
145793024/170498071 [========================>.....] - ETA: 4s
146432000/170498071 [========================>.....] - ETA: 4s
146710528/170498071 [========================>.....] - ETA: 4s
147202048/170498071 [========================>.....] - ETA: 4s
147595264/170498071 [========================>.....] - ETA: 4s
147988480/170498071 [=========================>....] - ETA: 4s
148627456/170498071 [=========================>....] - ETA: 3s
148889600/170498071 [=========================>....] - ETA: 3s
149397504/170498071 [=========================>....] - ETA: 3s
149757952/170498071 [=========================>....] - ETA: 3s
150183936/170498071 [=========================>....] - ETA: 3s
150790144/170498071 [=========================>....] - ETA: 3s
150822912/170498071 [=========================>....] - ETA: 3s
151658496/170498071 [=========================>....] - ETA: 3s
152018944/170498071 [=========================>....] - ETA: 3s
152297472/170498071 [=========================>....] - ETA: 3s
152576000/170498071 [=========================>....] - ETA: 3s
153001984/170498071 [=========================>....] - ETA: 3s
153133056/170498071 [=========================>....] - ETA: 3s
153559040/170498071 [==========================>...] - ETA: 3s
153755648/170498071 [==========================>...] - ETA: 2s
154124288/170498071 [==========================>...] - ETA: 2s
154329088/170498071 [==========================>...] - ETA: 2s
154689536/170498071 [==========================>...] - ETA: 2s
154968064/170498071 [==========================>...] - ETA: 2s
155262976/170498071 [==========================>...] - ETA: 2s
155738112/170498071 [==========================>...] - ETA: 2s
155918336/170498071 [==========================>...] - ETA: 2s
156311552/170498071 [==========================>...] - ETA: 2s
156606464/170498071 [==========================>...] - ETA: 2s
156917760/170498071 [==========================>...] - ETA: 2s
157409280/170498071 [==========================>...] - ETA: 2s
157630464/170498071 [==========================>...] - ETA: 2s
157999104/170498071 [==========================>...] - ETA: 2s
158310400/170498071 [==========================>...] - ETA: 2s
158605312/170498071 [==========================>...] - ETA: 2s
159031296/170498071 [==========================>...] - ETA: 2s
159211520/170498071 [===========================>..] - ETA: 2s
159670272/170498071 [===========================>..] - ETA: 1s
159817728/170498071 [===========================>..] - ETA: 1s
160260096/170498071 [===========================>..] - ETA: 1s
160489472/170498071 [===========================>..] - ETA: 1s
160866304/170498071 [===========================>..] - ETA: 1s
161112064/170498071 [===========================>..] - ETA: 1s
161488896/170498071 [===========================>..] - ETA: 1s
161947648/170498071 [===========================>..] - ETA: 1s
162127872/170498071 [===========================>..] - ETA: 1s
162603008/170498071 [===========================>..] - ETA: 1s
162832384/170498071 [===========================>..] - ETA: 1s
163209216/170498071 [===========================>..] - ETA: 1s
163471360/170498071 [===========================>..] - ETA: 1s
163848192/170498071 [===========================>..] - ETA: 1s
164257792/170498071 [===========================>..] - ETA: 1s
164487168/170498071 [===========================>..] - ETA: 1s
164978688/170498071 [============================>.] - ETA: 0s
165175296/170498071 [============================>.] - ETA: 0s
165601280/170498071 [============================>.] - ETA: 0s
165830656/170498071 [============================>.] - ETA: 0s
166240256/170498071 [============================>.] - ETA: 0s
166633472/170498071 [============================>.] - ETA: 0s
166895616/170498071 [============================>.] - ETA: 0s
167370752/170498071 [============================>.] - ETA: 0s
167534592/170498071 [============================>.] - ETA: 0s
168017920/170498071 [============================>.] - ETA: 0s
168173568/170498071 [============================>.] - ETA: 0s
168648704/170498071 [============================>.] - ETA: 0s
168894464/170498071 [============================>.] - ETA: 0s
169287680/170498071 [============================>.] - ETA: 0s
169713664/170498071 [============================>.] - ETA: 0s
169943040/170498071 [============================>.] - ETA: 0s
170418176/170498071 [============================>.] - ETA: 0s
170500096/170498071 [==============================] - 30s 0us/step

170508288/170498071 [==============================] - 30s 0us/step

2. Create a Keras DS-CNN model

The DS-CNN architecture is available in the Akida models zoo along with pretrained weights.

Note

The pre-trained weights were obtained after training the model with unconstrained float weights and activations for 1000 epochs

from tensorflow.keras.utils import get_file
from tensorflow.keras.models import load_model

# Retrieve the float model with pretrained weights and load it
model_file = get_file(
    "ds_cnn_cifar10.h5",
    "http://data.brainchip.com/models/ds_cnn/ds_cnn_cifar10.h5",
    cache_subdir='models/ds_cnn_cifar10')
model_keras = load_model(model_file)
model_keras.summary()

Out:

Downloading data from http://data.brainchip.com/models/ds_cnn/ds_cnn_cifar10.h5

   16384/10837696 [..............................] - ETA: 8s
  139264/10837696 [..............................] - ETA: 5s
  401408/10837696 [>.............................] - ETA: 3s
  663552/10837696 [>.............................] - ETA: 2s
  925696/10837696 [=>............................] - ETA: 2s
 1187840/10837696 [==>...........................] - ETA: 2s
 1449984/10837696 [===>..........................] - ETA: 2s
 1712128/10837696 [===>..........................] - ETA: 2s
 1974272/10837696 [====>.........................] - ETA: 2s
 2236416/10837696 [=====>........................] - ETA: 2s
 2498560/10837696 [=====>........................] - ETA: 1s
 2760704/10837696 [======>.......................] - ETA: 1s
 3022848/10837696 [=======>......................] - ETA: 1s
 3284992/10837696 [========>.....................] - ETA: 1s
 3547136/10837696 [========>.....................] - ETA: 1s
 3809280/10837696 [=========>....................] - ETA: 1s
 4071424/10837696 [==========>...................] - ETA: 1s
 4333568/10837696 [==========>...................] - ETA: 1s
 4595712/10837696 [===========>..................] - ETA: 1s
 4857856/10837696 [============>.................] - ETA: 1s
 5120000/10837696 [=============>................] - ETA: 1s
 5382144/10837696 [=============>................] - ETA: 1s
 5644288/10837696 [==============>...............] - ETA: 1s
 5906432/10837696 [===============>..............] - ETA: 1s
 6168576/10837696 [================>.............] - ETA: 1s
 6430720/10837696 [================>.............] - ETA: 0s
 6692864/10837696 [=================>............] - ETA: 0s
 6955008/10837696 [==================>...........] - ETA: 0s
 7217152/10837696 [==================>...........] - ETA: 0s
 7479296/10837696 [===================>..........] - ETA: 0s
 7741440/10837696 [====================>.........] - ETA: 0s
 8003584/10837696 [=====================>........] - ETA: 0s
 8265728/10837696 [=====================>........] - ETA: 0s
 8527872/10837696 [======================>.......] - ETA: 0s
 8790016/10837696 [=======================>......] - ETA: 0s
 9052160/10837696 [========================>.....] - ETA: 0s
 9314304/10837696 [========================>.....] - ETA: 0s
 9576448/10837696 [=========================>....] - ETA: 0s
 9838592/10837696 [==========================>...] - ETA: 0s
10100736/10837696 [==========================>...] - ETA: 0s
10362880/10837696 [===========================>..] - ETA: 0s
10625024/10837696 [============================>.] - ETA: 0s
10838016/10837696 [==============================] - 2s 0us/step

10846208/10837696 [==============================] - 2s 0us/step
Model: "ds_cnn_cifar10"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         [(None, 32, 32, 3)]       0
_________________________________________________________________
rescaling (Rescaling)        (None, 32, 32, 3)         0
_________________________________________________________________
conv_0 (Conv2D)              (None, 32, 32, 128)       3456
_________________________________________________________________
conv_0_BN (BatchNormalizatio (None, 32, 32, 128)       512
_________________________________________________________________
conv_0_relu (ReLU)           (None, 32, 32, 128)       0
_________________________________________________________________
separable_1 (SeparableConv2D (None, 32, 32, 128)       17536
_________________________________________________________________
separable_1_BN (BatchNormali (None, 32, 32, 128)       512
_________________________________________________________________
separable_1_relu (ReLU)      (None, 32, 32, 128)       0
_________________________________________________________________
separable_2 (SeparableConv2D (None, 32, 32, 256)       33920
_________________________________________________________________
separable_2_BN (BatchNormali (None, 32, 32, 256)       1024
_________________________________________________________________
separable_2_relu (ReLU)      (None, 32, 32, 256)       0
_________________________________________________________________
separable_3 (SeparableConv2D (None, 32, 32, 256)       67840
_________________________________________________________________
separable_3_maxpool (MaxPool (None, 16, 16, 256)       0
_________________________________________________________________
separable_3_BN (BatchNormali (None, 16, 16, 256)       1024
_________________________________________________________________
separable_3_relu (ReLU)      (None, 16, 16, 256)       0
_________________________________________________________________
separable_4 (SeparableConv2D (None, 16, 16, 512)       133376
_________________________________________________________________
separable_4_BN (BatchNormali (None, 16, 16, 512)       2048
_________________________________________________________________
separable_4_relu (ReLU)      (None, 16, 16, 512)       0
_________________________________________________________________
separable_5 (SeparableConv2D (None, 16, 16, 512)       266752
_________________________________________________________________
separable_5_maxpool (MaxPool (None, 8, 8, 512)         0
_________________________________________________________________
separable_5_BN (BatchNormali (None, 8, 8, 512)         2048
_________________________________________________________________
separable_5_relu (ReLU)      (None, 8, 8, 512)         0
_________________________________________________________________
separable_6 (SeparableConv2D (None, 8, 8, 512)         266752
_________________________________________________________________
separable_6_BN (BatchNormali (None, 8, 8, 512)         2048
_________________________________________________________________
separable_6_relu (ReLU)      (None, 8, 8, 512)         0
_________________________________________________________________
separable_7 (SeparableConv2D (None, 8, 8, 512)         266752
_________________________________________________________________
separable_7_maxpool (MaxPool (None, 4, 4, 512)         0
_________________________________________________________________
separable_7_BN (BatchNormali (None, 4, 4, 512)         2048
_________________________________________________________________
separable_7_relu (ReLU)      (None, 4, 4, 512)         0
_________________________________________________________________
separable_8 (SeparableConv2D (None, 4, 4, 1024)        528896
_________________________________________________________________
separable_8_BN (BatchNormali (None, 4, 4, 1024)        4096
_________________________________________________________________
separable_8_relu (ReLU)      (None, 4, 4, 1024)        0
_________________________________________________________________
separable_9 (SeparableConv2D (None, 4, 4, 1024)        1057792
_________________________________________________________________
separable_9_BN (BatchNormali (None, 4, 4, 1024)        4096
_________________________________________________________________
separable_9_relu (ReLU)      (None, 4, 4, 1024)        0
_________________________________________________________________
separable_10 (SeparableConv2 (None, 4, 4, 10)          19456
_________________________________________________________________
separable_10_global_avg (Glo (None, 10)                0
=================================================================
Total params: 2,681,984
Trainable params: 2,672,256
Non-trainable params: 9,728
_________________________________________________________________

Keras model accuracy is checked against the first n images of the test set.

The table below summarizes the expected results:

#Images

Accuracy

100

96.00 %

1000

94.30 %

10000

93.60 %

Note

Depending on your hardware setup, the processing time may vary.

import numpy as np

from sklearn.metrics import accuracy_score
from timeit import default_timer as timer


# Check Model performance
def check_model_performances(model, x_test, num_images=1000):
    start = timer()
    potentials_keras = model.predict(x_test[:num_images])
    preds_keras = np.squeeze(np.argmax(potentials_keras, 1))

    accuracy = accuracy_score(y_test[:num_images], preds_keras)
    print("Accuracy: " + "{0:.2f}".format(100 * accuracy) + "%")
    end = timer()
    print(f'Keras inference on {num_images} images took {end-start:.2f} s.\n')


check_model_performances(model_keras, x_test)

Out:

Accuracy: 94.30%
Keras inference on 1000 images took 0.73 s.

3. Quantized model

Quantizing a model is done using cnn2snn.quantize. After the call, all the layers will have 4-bit weights and 4-bit activations.

This model will therefore satisfy the Akida NSoC requirements but will suffer from a drop in accuracy due to quantization as shown in the table below:

#Images

Float accuracy

Quantized accuracy

100

96.00 %

96.00 %

1000

94.30 %

92.60 %

10000

93.66 %

92.58 %

from cnn2snn import quantize

# Quantize the model to 4-bit weights and activations
model_keras_quantized = quantize(model_keras, 4, 4)

# Check Model performance
check_model_performances(model_keras_quantized, x_test)

Out:

Accuracy: 92.60%
Keras inference on 1000 images took 0.73 s.

4. Pretrained quantized model

The Akida models zoo also contains a pretrained quantized helper that was obtained using the tune action of akida_models CLI on the quantized model for 100 epochs.

Tuning the model, that is training with a lowered learning rate, allows to recover performances up to the initial floating point accuracy.

#Images

Float accuracy

Quantized accuracy

After tuning

100

96.00 %

96.00 %

97.00 %

1000

94.30 %

92.60 %

94.20 %

10000

93.66 %

92.58 %

93.08 %

from akida_models import ds_cnn_cifar10_pretrained

# Use a quantized model with pretrained quantized weights
model_keras_quantized_pretrained = ds_cnn_cifar10_pretrained()

# Check Model performance
check_model_performances(model_keras_quantized_pretrained, x_test)

Out:

Downloading data from http://data.brainchip.com/models/ds_cnn/ds_cnn_cifar10_iq4_wq4_aq4.h5

   16384/10744080 [..............................] - ETA: 8s
  204800/10744080 [..............................] - ETA: 3s
  466944/10744080 [>.............................] - ETA: 2s
  729088/10744080 [=>............................] - ETA: 2s
  991232/10744080 [=>............................] - ETA: 2s
 1253376/10744080 [==>...........................] - ETA: 2s
 1515520/10744080 [===>..........................] - ETA: 2s
 1777664/10744080 [===>..........................] - ETA: 2s
 2039808/10744080 [====>.........................] - ETA: 2s
 2301952/10744080 [=====>........................] - ETA: 1s
 2564096/10744080 [======>.......................] - ETA: 1s
 2826240/10744080 [======>.......................] - ETA: 1s
 3088384/10744080 [=======>......................] - ETA: 1s
 3350528/10744080 [========>.....................] - ETA: 1s
 3612672/10744080 [=========>....................] - ETA: 1s
 3874816/10744080 [=========>....................] - ETA: 1s
 4136960/10744080 [==========>...................] - ETA: 1s
 4399104/10744080 [===========>..................] - ETA: 1s
 4661248/10744080 [============>.................] - ETA: 1s
 4923392/10744080 [============>.................] - ETA: 1s
 5185536/10744080 [=============>................] - ETA: 1s
 5447680/10744080 [==============>...............] - ETA: 1s
 5709824/10744080 [==============>...............] - ETA: 1s
 5971968/10744080 [===============>..............] - ETA: 1s
 6234112/10744080 [================>.............] - ETA: 1s
 6496256/10744080 [=================>............] - ETA: 0s
 6758400/10744080 [=================>............] - ETA: 0s
 7020544/10744080 [==================>...........] - ETA: 0s
 7282688/10744080 [===================>..........] - ETA: 0s
 7544832/10744080 [====================>.........] - ETA: 0s
 7806976/10744080 [====================>.........] - ETA: 0s
 8069120/10744080 [=====================>........] - ETA: 0s
 8331264/10744080 [======================>.......] - ETA: 0s
 8593408/10744080 [======================>.......] - ETA: 0s
 8855552/10744080 [=======================>......] - ETA: 0s
 9117696/10744080 [========================>.....] - ETA: 0s
 9379840/10744080 [=========================>....] - ETA: 0s
 9641984/10744080 [=========================>....] - ETA: 0s
 9904128/10744080 [==========================>...] - ETA: 0s
10166272/10744080 [===========================>..] - ETA: 0s
10428416/10744080 [============================>.] - ETA: 0s
10690560/10744080 [============================>.] - ETA: 0s
10747904/10744080 [==============================] - 2s 0us/step

10756096/10744080 [==============================] - 2s 0us/step
Accuracy: 94.20%
Keras inference on 1000 images took 0.75 s.

5. Conversion to Akida

5.1 Convert to Akida model

When converting to an Akida model, we just need to pass the Keras model and the input scaling that was used during training to cnn2snn.convert.

from cnn2snn import convert

model_akida = convert(model_keras_quantized_pretrained)

5.2 Check hardware compliancy

The Model.summary method provides a detailed description of the Model layers.

model_akida.summary()

Out:

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

              SW/conv_0-separable_10 (Software)
_____________________________________________________________
Layer (type)              Output shape   Kernel shape
=============================================================
conv_0 (InputConv.)       [32, 32, 128]  (3, 3, 3, 128)
_____________________________________________________________
separable_1 (Sep.Conv.)   [32, 32, 128]  (3, 3, 128, 1)
_____________________________________________________________
                                         (1, 1, 128, 128)
_____________________________________________________________
separable_2 (Sep.Conv.)   [32, 32, 256]  (3, 3, 128, 1)
_____________________________________________________________
                                         (1, 1, 128, 256)
_____________________________________________________________
separable_3 (Sep.Conv.)   [16, 16, 256]  (3, 3, 256, 1)
_____________________________________________________________
                                         (1, 1, 256, 256)
_____________________________________________________________
separable_4 (Sep.Conv.)   [16, 16, 512]  (3, 3, 256, 1)
_____________________________________________________________
                                         (1, 1, 256, 512)
_____________________________________________________________
separable_5 (Sep.Conv.)   [8, 8, 512]    (3, 3, 512, 1)
_____________________________________________________________
                                         (1, 1, 512, 512)
_____________________________________________________________
separable_6 (Sep.Conv.)   [8, 8, 512]    (3, 3, 512, 1)
_____________________________________________________________
                                         (1, 1, 512, 512)
_____________________________________________________________
separable_7 (Sep.Conv.)   [4, 4, 512]    (3, 3, 512, 1)
_____________________________________________________________
                                         (1, 1, 512, 512)
_____________________________________________________________
separable_8 (Sep.Conv.)   [4, 4, 1024]   (3, 3, 512, 1)
_____________________________________________________________
                                         (1, 1, 512, 1024)
_____________________________________________________________
separable_9 (Sep.Conv.)   [4, 4, 1024]   (3, 3, 1024, 1)
_____________________________________________________________
                                         (1, 1, 1024, 1024)
_____________________________________________________________
separable_10 (Sep.Conv.)  [1, 1, 10]     (3, 3, 1024, 1)
_____________________________________________________________
                                         (1, 1, 1024, 10)
_____________________________________________________________

5.3 Check performance

We check the Akida model accuracy on the first n images of the test set.

The table below summarizes the expected results:

#Images

Keras accuracy

Akida accuracy

100

96.00 %

97.00 %

1000

94.30 %

94.00 %

10000

93.66 %

93.04 %

Due to the conversion process, the predictions may be slightly different between the original Keras model and Akida on some specific images.

This explains why when testing on a limited number of images the accuracy numbers between Keras and Akida may be quite different. On the full test set however, the two models accuracies are very close.

num_images = 1000

# Check Model performance
start = timer()
results = model_akida.predict(x_test[:num_images])
accuracy = accuracy_score(y_test[:num_images], results)

print("Accuracy: " + "{0:.2f}".format(100 * accuracy) + "%")
end = timer()
print(f'Akida inference on {num_images} images took {end-start:.2f} s.\n')

# For non-regression purpose
if num_images == 1000:
    assert accuracy == 0.94

Out:

Accuracy: 94.00%
Akida inference on 1000 images took 18.88 s.

Activations sparsity has a great impact on akida inference time. One can have a look at the average input and output sparsity of each layer using evaluate_sparsity

5.4 Show predictions for a random image

import matplotlib.pyplot as plt
import matplotlib.lines as lines
import matplotlib.patches as patches

label_names = [
    'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
    'ship', 'truck'
]

# prepare plot
barWidth = 0.75
pause_time = 1

fig = plt.figure(num='CIFAR10 Classification by Akida runtime', figsize=(8, 4))
ax0 = plt.subplot(1, 3, 1)
imgobj = ax0.imshow(np.zeros((32, 32, 3), dtype=np.uint8))
ax0.set_axis_off()
# Results subplots
ax1 = plt.subplot(1, 2, 2)
ax1.xaxis.set_visible(False)
ax0.text(0, 34, 'Actual class:')
actual_class = ax0.text(16, 34, 'None')
ax0.text(0, 37, 'Predicted class:')
predicted_class = ax0.text(20, 37, 'None')

# Take a random test image
i = np.random.randint(y_test.shape[0])

true_idx = int(y_test[i])
pot = model_akida.evaluate(np.expand_dims(x_test[i], axis=0)).squeeze()

rpot = np.arange(len(pot))
ax1.barh(rpot, pot, height=barWidth)
ax1.set_yticks(rpot - 0.07 * barWidth)
ax1.set_yticklabels(label_names)
predicted_idx = pot.argmax()
imgobj.set_data(x_test[i])
if predicted_idx == true_idx:
    ax1.get_children()[predicted_idx].set_color('g')
else:
    ax1.get_children()[predicted_idx].set_color('r')
actual_class.set_text(label_names[true_idx])
predicted_class.set_text(label_names[predicted_idx])
ax1.set_title('Akida\'s predictions')
plt.show()
Akida's predictions

Total running time of the script: ( 1 minutes 2.209 seconds)

Gallery generated by Sphinx-Gallery