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: 3s
    40960/170498071 [..............................] - ETA: 7:40
    90112/170498071 [..............................] - ETA: 6:56
   204800/170498071 [..............................] - ETA: 4:35
   401408/170498071 [..............................] - ETA: 3:07
   548864/170498071 [..............................] - ETA: 2:52
   630784/170498071 [..............................] - ETA: 2:58
  1089536/170498071 [..............................] - ETA: 2:01
  1171456/170498071 [..............................] - ETA: 2:07
  1630208/170498071 [..............................] - ETA: 1:43
  1892352/170498071 [..............................] - ETA: 1:38
  2170880/170498071 [..............................] - ETA: 1:34
  2465792/170498071 [..............................] - ETA: 1:30
  2744320/170498071 [..............................] - ETA: 1:27
  3039232/170498071 [..............................] - ETA: 1:25
  3334144/170498071 [..............................] - ETA: 1:23
  3629056/170498071 [..............................] - ETA: 1:26
  4120576/170498071 [..............................] - ETA: 1:20
  4333568/170498071 [..............................] - ETA: 1:20
  4546560/170498071 [..............................] - ETA: 1:20
  4775936/170498071 [..............................] - ETA: 1:20
  4988928/170498071 [..............................] - ETA: 1:20
  5218304/170498071 [..............................] - ETA: 1:20
  5431296/170498071 [..............................] - ETA: 1:19
  5447680/170498071 [..............................] - ETA: 1:20
  5660672/170498071 [..............................] - ETA: 1:18
  5677056/170498071 [..............................] - ETA: 1:20
  5890048/170498071 [>.............................] - ETA: 1:18
  5906432/170498071 [>.............................] - ETA: 1:20
  6119424/170498071 [>.............................] - ETA: 1:18
  6135808/170498071 [>.............................] - ETA: 1:19
  6365184/170498071 [>.............................] - ETA: 1:19
  6594560/170498071 [>.............................] - ETA: 1:18
  6610944/170498071 [>.............................] - ETA: 1:19
  6840320/170498071 [>.............................] - ETA: 1:19
  7069696/170498071 [>.............................] - ETA: 1:17
  7086080/170498071 [>.............................] - ETA: 1:19
  7315456/170498071 [>.............................] - ETA: 1:17
  7331840/170498071 [>.............................] - ETA: 1:18
  7561216/170498071 [>.............................] - ETA: 1:17
  7577600/170498071 [>.............................] - ETA: 1:18
  7806976/170498071 [>.............................] - ETA: 1:17
  7823360/170498071 [>.............................] - ETA: 1:18
  8052736/170498071 [>.............................] - ETA: 1:16
  8069120/170498071 [>.............................] - ETA: 1:17
  8298496/170498071 [>.............................] - ETA: 1:16
  8314880/170498071 [>.............................] - ETA: 1:17
  8544256/170498071 [>.............................] - ETA: 1:16
  8560640/170498071 [>.............................] - ETA: 1:17
  8790016/170498071 [>.............................] - ETA: 1:16
  8806400/170498071 [>.............................] - ETA: 1:17
  9035776/170498071 [>.............................] - ETA: 1:16
  9052160/170498071 [>.............................] - ETA: 1:16
  9281536/170498071 [>.............................] - ETA: 1:15
  9297920/170498071 [>.............................] - ETA: 1:16
  9543680/170498071 [>.............................] - ETA: 1:15
  9560064/170498071 [>.............................] - ETA: 1:16
  9789440/170498071 [>.............................] - ETA: 1:15
  9805824/170498071 [>.............................] - ETA: 1:16
 10051584/170498071 [>.............................] - ETA: 1:15
 10297344/170498071 [>.............................] - ETA: 1:14
 10313728/170498071 [>.............................] - ETA: 1:15
 10543104/170498071 [>.............................] - ETA: 1:14
 10559488/170498071 [>.............................] - ETA: 1:15
 10805248/170498071 [>.............................] - ETA: 1:14
 10821632/170498071 [>.............................] - ETA: 1:15
 11051008/170498071 [>.............................] - ETA: 1:14
 11067392/170498071 [>.............................] - ETA: 1:14
 11313152/170498071 [>.............................] - ETA: 1:13
 11329536/170498071 [>.............................] - ETA: 1:14
 11550720/170498071 [=>............................] - ETA: 1:13
 11575296/170498071 [=>............................] - ETA: 1:14
 11788288/170498071 [=>............................] - ETA: 1:13
 11837440/170498071 [=>............................] - ETA: 1:14
 12050432/170498071 [=>............................] - ETA: 1:13
 12083200/170498071 [=>............................] - ETA: 1:13
 12296192/170498071 [=>............................] - ETA: 1:13
 12345344/170498071 [=>............................] - ETA: 1:13
 12558336/170498071 [=>............................] - ETA: 1:13
 12591104/170498071 [=>............................] - ETA: 1:13
 12787712/170498071 [=>............................] - ETA: 1:12
 12853248/170498071 [=>............................] - ETA: 1:13
 13066240/170498071 [=>............................] - ETA: 1:12
 13099008/170498071 [=>............................] - ETA: 1:13
 13295616/170498071 [=>............................] - ETA: 1:12
 13361152/170498071 [=>............................] - ETA: 1:12
 13557760/170498071 [=>............................] - ETA: 1:12
 13606912/170498071 [=>............................] - ETA: 1:12
 13803520/170498071 [=>............................] - ETA: 1:12
 13869056/170498071 [=>............................] - ETA: 1:12
 14065664/170498071 [=>............................] - ETA: 1:12
 14114816/170498071 [=>............................] - ETA: 1:12
 14311424/170498071 [=>............................] - ETA: 1:11
 14376960/170498071 [=>............................] - ETA: 1:12
 14573568/170498071 [=>............................] - ETA: 1:11
 14622720/170498071 [=>............................] - ETA: 1:12
 14819328/170498071 [=>............................] - ETA: 1:11
 14884864/170498071 [=>............................] - ETA: 1:11
 15081472/170498071 [=>............................] - ETA: 1:11
 15130624/170498071 [=>............................] - ETA: 1:11
 15327232/170498071 [=>............................] - ETA: 1:11
 15392768/170498071 [=>............................] - ETA: 1:11
 15589376/170498071 [=>............................] - ETA: 1:11
 15638528/170498071 [=>............................] - ETA: 1:11
 15835136/170498071 [=>............................] - ETA: 1:10
 15900672/170498071 [=>............................] - ETA: 1:11
 16097280/170498071 [=>............................] - ETA: 1:10
 16146432/170498071 [=>............................] - ETA: 1:10
 16343040/170498071 [=>............................] - ETA: 1:10
 16408576/170498071 [=>............................] - ETA: 1:10
 16605184/170498071 [=>............................] - ETA: 1:10
 16654336/170498071 [=>............................] - ETA: 1:10
 16850944/170498071 [=>............................] - ETA: 1:10
 16916480/170498071 [=>............................] - ETA: 1:10
 17113088/170498071 [==>...........................] - ETA: 1:10
 17162240/170498071 [==>...........................] - ETA: 1:10
 17358848/170498071 [==>...........................] - ETA: 1:09
 17424384/170498071 [==>...........................] - ETA: 1:10
 17620992/170498071 [==>...........................] - ETA: 1:09
 17686528/170498071 [==>...........................] - ETA: 1:09
 17883136/170498071 [==>...........................] - ETA: 1:09
 17932288/170498071 [==>...........................] - ETA: 1:09
 18128896/170498071 [==>...........................] - ETA: 1:09
 18194432/170498071 [==>...........................] - ETA: 1:09
 18358272/170498071 [==>...........................] - ETA: 1:09
 18456576/170498071 [==>...........................] - ETA: 1:09
 18571264/170498071 [==>...........................] - ETA: 1:09
 18702336/170498071 [==>...........................] - ETA: 1:09
 18833408/170498071 [==>...........................] - ETA: 1:09
 18964480/170498071 [==>...........................] - ETA: 1:09
 19079168/170498071 [==>...........................] - ETA: 1:08
 19226624/170498071 [==>...........................] - ETA: 1:08
 19341312/170498071 [==>...........................] - ETA: 1:08
 19488768/170498071 [==>...........................] - ETA: 1:08
 19603456/170498071 [==>...........................] - ETA: 1:08
 19750912/170498071 [==>...........................] - ETA: 1:08
 19865600/170498071 [==>...........................] - ETA: 1:08
 20013056/170498071 [==>...........................] - ETA: 1:08
 20144128/170498071 [==>...........................] - ETA: 1:08
 20291584/170498071 [==>...........................] - ETA: 1:08
 20406272/170498071 [==>...........................] - ETA: 1:08
 20553728/170498071 [==>...........................] - ETA: 1:07
 20668416/170498071 [==>...........................] - ETA: 1:07
 20815872/170498071 [==>...........................] - ETA: 1:07
 20946944/170498071 [==>...........................] - ETA: 1:07
 21094400/170498071 [==>...........................] - ETA: 1:07
 21209088/170498071 [==>...........................] - ETA: 1:07
 21372928/170498071 [==>...........................] - ETA: 1:07
 21487616/170498071 [==>...........................] - ETA: 1:07
 21635072/170498071 [==>...........................] - ETA: 1:07
 21766144/170498071 [==>...........................] - ETA: 1:06
 21913600/170498071 [==>...........................] - ETA: 1:06
 22044672/170498071 [==>...........................] - ETA: 1:06
 22192128/170498071 [==>...........................] - ETA: 1:06
 22323200/170498071 [==>...........................] - ETA: 1:06
 22470656/170498071 [==>...........................] - ETA: 1:06
 22601728/170498071 [==>...........................] - ETA: 1:06
 22765568/170498071 [===>..........................] - ETA: 1:06
 22896640/170498071 [===>..........................] - ETA: 1:06
 23044096/170498071 [===>..........................] - ETA: 1:05
 23175168/170498071 [===>..........................] - ETA: 1:05
 23339008/170498071 [===>..........................] - ETA: 1:05
 23470080/170498071 [===>..........................] - ETA: 1:05
 23633920/170498071 [===>..........................] - ETA: 1:05
 23764992/170498071 [===>..........................] - ETA: 1:05
 23928832/170498071 [===>..........................] - ETA: 1:05
 24059904/170498071 [===>..........................] - ETA: 1:05
 24223744/170498071 [===>..........................] - ETA: 1:04
 24371200/170498071 [===>..........................] - ETA: 1:04
 24535040/170498071 [===>..........................] - ETA: 1:04
 24682496/170498071 [===>..........................] - ETA: 1:04
 24829952/170498071 [===>..........................] - ETA: 1:04
 24977408/170498071 [===>..........................] - ETA: 1:04
 25141248/170498071 [===>..........................] - ETA: 1:04
 25288704/170498071 [===>..........................] - ETA: 1:03
 25468928/170498071 [===>..........................] - ETA: 1:03
 25616384/170498071 [===>..........................] - ETA: 1:03
 25780224/170498071 [===>..........................] - ETA: 1:03
 25944064/170498071 [===>..........................] - ETA: 1:03
 26107904/170498071 [===>..........................] - ETA: 1:03
 26255360/170498071 [===>..........................] - ETA: 1:02
 26435584/170498071 [===>..........................] - ETA: 1:02
 26599424/170498071 [===>..........................] - ETA: 1:02
 26763264/170498071 [===>..........................] - ETA: 1:02
 26927104/170498071 [===>..........................] - ETA: 1:02
 27107328/170498071 [===>..........................] - ETA: 1:02
 27271168/170498071 [===>..........................] - ETA: 1:01
 27435008/170498071 [===>..........................] - ETA: 1:01
 27598848/170498071 [===>..........................] - ETA: 1:01
 27795456/170498071 [===>..........................] - ETA: 1:01
 27959296/170498071 [===>..........................] - ETA: 1:01
 28139520/170498071 [===>..........................] - ETA: 1:01
 28319744/170498071 [===>..........................] - ETA: 1:00
 28499968/170498071 [====>.........................] - ETA: 1:00
 28680192/170498071 [====>.........................] - ETA: 1:00
 28876800/170498071 [====>.........................] - ETA: 1:00
 29057024/170498071 [====>.........................] - ETA: 1:00
 29237248/170498071 [====>.........................] - ETA: 59s 
 29417472/170498071 [====>.........................] - ETA: 59s
 29630464/170498071 [====>.........................] - ETA: 59s
 29810688/170498071 [====>.........................] - ETA: 59s
 30007296/170498071 [====>.........................] - ETA: 59s
 30187520/170498071 [====>.........................] - ETA: 58s
 30400512/170498071 [====>.........................] - ETA: 58s
 30580736/170498071 [====>.........................] - ETA: 58s
 30810112/170498071 [====>.........................] - ETA: 58s
 30998528/170498071 [====>.........................] - ETA: 58s
 31219712/170498071 [====>.........................] - ETA: 57s
 31408128/170498071 [====>.........................] - ETA: 57s
 31629312/170498071 [====>.........................] - ETA: 57s
 31825920/170498071 [====>.........................] - ETA: 57s
 32055296/170498071 [====>.........................] - ETA: 56s
 32251904/170498071 [====>.........................] - ETA: 56s
 32497664/170498071 [====>.........................] - ETA: 56s
 32710656/170498071 [====>.........................] - ETA: 56s
 32940032/170498071 [====>.........................] - ETA: 55s
 33161216/170498071 [====>.........................] - ETA: 55s
 33382400/170498071 [====>.........................] - ETA: 55s
 33611776/170498071 [====>.........................] - ETA: 55s
 33841152/170498071 [====>.........................] - ETA: 54s
 34062336/170498071 [====>.........................] - ETA: 54s
 34316288/170498071 [=====>........................] - ETA: 54s
 34545664/170498071 [=====>........................] - ETA: 54s
 34807808/170498071 [=====>........................] - ETA: 53s
 35037184/170498071 [=====>........................] - ETA: 53s
 35282944/170498071 [=====>........................] - ETA: 53s
 35520512/170498071 [=====>........................] - ETA: 53s
 35790848/170498071 [=====>........................] - ETA: 52s
 36028416/170498071 [=====>........................] - ETA: 52s
 36298752/170498071 [=====>........................] - ETA: 52s
 36544512/170498071 [=====>........................] - ETA: 52s
 36839424/170498071 [=====>........................] - ETA: 51s
 37085184/170498071 [=====>........................] - ETA: 51s
 37363712/170498071 [=====>........................] - ETA: 51s
 37609472/170498071 [=====>........................] - ETA: 51s
 37920768/170498071 [=====>........................] - ETA: 50s
 38182912/170498071 [=====>........................] - ETA: 50s
 38477824/170498071 [=====>........................] - ETA: 50s
 38739968/170498071 [=====>........................] - ETA: 49s
 39034880/170498071 [=====>........................] - ETA: 49s
 39297024/170498071 [=====>........................] - ETA: 49s
 39624704/170498071 [=====>........................] - ETA: 48s
 39903232/170498071 [======>.......................] - ETA: 48s
 40230912/170498071 [======>.......................] - ETA: 48s
 40525824/170498071 [======>.......................] - ETA: 48s
 40837120/170498071 [======>.......................] - ETA: 47s
 41115648/170498071 [======>.......................] - ETA: 47s
 41459712/170498071 [======>.......................] - ETA: 47s
 41746432/170498071 [======>.......................] - ETA: 46s
 42098688/170498071 [======>.......................] - ETA: 46s
 42393600/170498071 [======>.......................] - ETA: 46s
 42754048/170498071 [======>.......................] - ETA: 45s
 43048960/170498071 [======>.......................] - ETA: 45s
 43425792/170498071 [======>.......................] - ETA: 45s
 43737088/170498071 [======>.......................] - ETA: 45s
 44113920/170498071 [======>.......................] - ETA: 44s
 44425216/170498071 [======>.......................] - ETA: 44s
 44802048/170498071 [======>.......................] - ETA: 44s
 45129728/170498071 [======>.......................] - ETA: 43s
 45522944/170498071 [=======>......................] - ETA: 43s
 45867008/170498071 [=======>......................] - ETA: 43s
 46243840/170498071 [=======>......................] - ETA: 42s
 46587904/170498071 [=======>......................] - ETA: 42s
 46997504/170498071 [=======>......................] - ETA: 42s
 47357952/170498071 [=======>......................] - ETA: 41s
 47767552/170498071 [=======>......................] - ETA: 41s
 48144384/170498071 [=======>......................] - ETA: 41s
 48553984/170498071 [=======>......................] - ETA: 40s
 48947200/170498071 [=======>......................] - ETA: 40s
 49340416/170498071 [=======>......................] - ETA: 40s
 49717248/170498071 [=======>......................] - ETA: 39s
 50159616/170498071 [=======>......................] - ETA: 39s
 50552832/170498071 [=======>......................] - ETA: 39s
 50995200/170498071 [=======>......................] - ETA: 38s
 51388416/170498071 [========>.....................] - ETA: 38s
 51863552/170498071 [========>.....................] - ETA: 38s
 52289536/170498071 [========>.....................] - ETA: 37s
 52731904/170498071 [========>.....................] - ETA: 37s
 53141504/170498071 [========>.....................] - ETA: 37s
 53633024/170498071 [========>.....................] - ETA: 36s
 54042624/170498071 [========>.....................] - ETA: 36s
 54550528/170498071 [========>.....................] - ETA: 36s
 54976512/170498071 [========>.....................] - ETA: 35s
 55484416/170498071 [========>.....................] - ETA: 35s
 55926784/170498071 [========>.....................] - ETA: 35s
 56451072/170498071 [========>.....................] - ETA: 34s
 56909824/170498071 [=========>....................] - ETA: 34s
 57434112/170498071 [=========>....................] - ETA: 34s
 57892864/170498071 [=========>....................] - ETA: 33s
 58433536/170498071 [=========>....................] - ETA: 33s
 58908672/170498071 [=========>....................] - ETA: 33s
 59449344/170498071 [=========>....................] - ETA: 32s
 59940864/170498071 [=========>....................] - ETA: 32s
 60497920/170498071 [=========>....................] - ETA: 32s
 61005824/170498071 [=========>....................] - ETA: 31s
 61579264/170498071 [=========>....................] - ETA: 31s
 62087168/170498071 [=========>....................] - ETA: 31s
 62660608/170498071 [==========>...................] - ETA: 30s
 63184896/170498071 [==========>...................] - ETA: 30s
 63791104/170498071 [==========>...................] - ETA: 30s
 64315392/170498071 [==========>...................] - ETA: 29s
 64937984/170498071 [==========>...................] - ETA: 29s
 65478656/170498071 [==========>...................] - ETA: 29s
 66101248/170498071 [==========>...................] - ETA: 28s
 66658304/170498071 [==========>...................] - ETA: 28s
 67297280/170498071 [==========>...................] - ETA: 28s
 67854336/170498071 [==========>...................] - ETA: 27s
 68526080/170498071 [===========>..................] - ETA: 27s
 69099520/170498071 [===========>..................] - ETA: 27s
 69754880/170498071 [===========>..................] - ETA: 26s
 70361088/170498071 [===========>..................] - ETA: 26s
 71032832/170498071 [===========>..................] - ETA: 26s
 71655424/170498071 [===========>..................] - ETA: 25s
 72343552/170498071 [===========>..................] - ETA: 25s
 72966144/170498071 [===========>..................] - ETA: 25s
 73670656/170498071 [===========>..................] - ETA: 24s
 74309632/170498071 [============>.................] - ETA: 24s
 75030528/170498071 [============>.................] - ETA: 24s
 75702272/170498071 [============>.................] - ETA: 23s
 76406784/170498071 [============>.................] - ETA: 23s
 77094912/170498071 [============>.................] - ETA: 23s
 77832192/170498071 [============>.................] - ETA: 22s
 78520320/170498071 [============>.................] - ETA: 22s
 78749696/170498071 [============>.................] - ETA: 22s
 80535552/170498071 [=============>................] - ETA: 21s
 81010688/170498071 [=============>................] - ETA: 21s
 81518592/170498071 [=============>................] - ETA: 21s
 82042880/170498071 [=============>................] - ETA: 21s
 82567168/170498071 [=============>................] - ETA: 20s
 83091456/170498071 [=============>................] - ETA: 20s
 83632128/170498071 [=============>................] - ETA: 20s
 84172800/170498071 [=============>................] - ETA: 20s
 84713472/170498071 [=============>................] - ETA: 20s
 85254144/170498071 [==============>...............] - ETA: 19s
 85811200/170498071 [==============>...............] - ETA: 19s
 86351872/170498071 [==============>...............] - ETA: 19s
 86908928/170498071 [==============>...............] - ETA: 19s
 87465984/170498071 [==============>...............] - ETA: 19s
 88023040/170498071 [==============>...............] - ETA: 18s
 88498176/170498071 [==============>...............] - ETA: 18s
 89169920/170498071 [==============>...............] - ETA: 18s
 89726976/170498071 [==============>...............] - ETA: 18s
 90316800/170498071 [==============>...............] - ETA: 18s
 90873856/170498071 [==============>...............] - ETA: 17s
 91463680/170498071 [===============>..............] - ETA: 17s
 91987968/170498071 [===============>..............] - ETA: 17s
 92626944/170498071 [===============>..............] - ETA: 17s
 93118464/170498071 [===============>..............] - ETA: 17s
 93790208/170498071 [===============>..............] - ETA: 17s
 94199808/170498071 [===============>..............] - ETA: 16s
 94887936/170498071 [===============>..............] - ETA: 16s
 95346688/170498071 [===============>..............] - ETA: 16s
 95903744/170498071 [===============>..............] - ETA: 16s
 96411648/170498071 [===============>..............] - ETA: 16s
 96903168/170498071 [================>.............] - ETA: 16s
 97214464/170498071 [================>.............] - ETA: 16s
 98754560/170498071 [================>.............] - ETA: 15s
 99115008/170498071 [================>.............] - ETA: 15s
 99540992/170498071 [================>.............] - ETA: 15s
 99917824/170498071 [================>.............] - ETA: 15s
100343808/170498071 [================>.............] - ETA: 15s
100687872/170498071 [================>.............] - ETA: 14s
101179392/170498071 [================>.............] - ETA: 14s
101507072/170498071 [================>.............] - ETA: 14s
102047744/170498071 [================>.............] - ETA: 14s
102391808/170498071 [=================>............] - ETA: 14s
102916096/170498071 [=================>............] - ETA: 14s
103276544/170498071 [=================>............] - ETA: 14s
103817216/170498071 [=================>............] - ETA: 14s
104194048/170498071 [=================>............] - ETA: 13s
104751104/170498071 [=================>............] - ETA: 13s
105095168/170498071 [=================>............] - ETA: 13s
105635840/170498071 [=================>............] - ETA: 13s
106012672/170498071 [=================>............] - ETA: 13s
106586112/170498071 [=================>............] - ETA: 13s
106930176/170498071 [=================>............] - ETA: 13s
107487232/170498071 [=================>............] - ETA: 13s
107864064/170498071 [=================>............] - ETA: 13s
108421120/170498071 [==================>...........] - ETA: 12s
108814336/170498071 [==================>...........] - ETA: 12s
109387776/170498071 [==================>...........] - ETA: 12s
109748224/170498071 [==================>...........] - ETA: 12s
110305280/170498071 [==================>...........] - ETA: 12s
110698496/170498071 [==================>...........] - ETA: 12s
111255552/170498071 [==================>...........] - ETA: 12s
111648768/170498071 [==================>...........] - ETA: 12s
112205824/170498071 [==================>...........] - ETA: 11s
112615424/170498071 [==================>...........] - ETA: 11s
113188864/170498071 [==================>...........] - ETA: 11s
113565696/170498071 [==================>...........] - ETA: 11s
114139136/170498071 [===================>..........] - ETA: 11s
114548736/170498071 [===================>..........] - ETA: 11s
115122176/170498071 [===================>..........] - ETA: 11s
115515392/170498071 [===================>..........] - ETA: 11s
116080640/170498071 [===================>..........] - ETA: 10s
116449280/170498071 [===================>..........] - ETA: 10s
116891648/170498071 [===================>..........] - ETA: 10s
117350400/170498071 [===================>..........] - ETA: 10s
117743616/170498071 [===================>..........] - ETA: 10s
118284288/170498071 [===================>..........] - ETA: 10s
118677504/170498071 [===================>..........] - ETA: 10s
119136256/170498071 [===================>..........] - ETA: 10s
119562240/170498071 [====================>.........] - ETA: 10s
120102912/170498071 [====================>.........] - ETA: 9s 
120446976/170498071 [====================>.........] - ETA: 9s
121053184/170498071 [====================>.........] - ETA: 9s
121438208/170498071 [====================>.........] - ETA: 9s
122003456/170498071 [====================>.........] - ETA: 9s
122413056/170498071 [====================>.........] - ETA: 9s
122855424/170498071 [====================>.........] - ETA: 9s
123346944/170498071 [====================>.........] - ETA: 9s
123756544/170498071 [====================>.........] - ETA: 9s
124313600/170498071 [====================>.........] - ETA: 9s
124739584/170498071 [====================>.........] - ETA: 8s
125296640/170498071 [=====================>........] - ETA: 8s
125706240/170498071 [=====================>........] - ETA: 8s
126181376/170498071 [=====================>........] - ETA: 8s
126640128/170498071 [=====================>........] - ETA: 8s
127180800/170498071 [=====================>........] - ETA: 8s
127590400/170498071 [=====================>........] - ETA: 8s
128163840/170498071 [=====================>........] - ETA: 8s
128507904/170498071 [=====================>........] - ETA: 8s
129130496/170498071 [=====================>........] - ETA: 7s
129523712/170498071 [=====================>........] - ETA: 7s
130146304/170498071 [=====================>........] - ETA: 7s
130555904/170498071 [=====================>........] - ETA: 7s
131178496/170498071 [======================>.......] - ETA: 7s
131588096/170498071 [======================>.......] - ETA: 7s
132194304/170498071 [======================>.......] - ETA: 7s
132603904/170498071 [======================>.......] - ETA: 7s
133210112/170498071 [======================>.......] - ETA: 7s
133619712/170498071 [======================>.......] - ETA: 6s
134160384/170498071 [======================>.......] - ETA: 6s
134619136/170498071 [======================>.......] - ETA: 6s
135045120/170498071 [======================>.......] - ETA: 6s
135618560/170498071 [======================>.......] - ETA: 6s
135995392/170498071 [======================>.......] - ETA: 6s
136503296/170498071 [=======================>......] - ETA: 6s
137011200/170498071 [=======================>......] - ETA: 6s
137469952/170498071 [=======================>......] - ETA: 6s
137928704/170498071 [=======================>......] - ETA: 6s
138452992/170498071 [=======================>......] - ETA: 5s
138878976/170498071 [=======================>......] - ETA: 5s
139468800/170498071 [=======================>......] - ETA: 5s
139862016/170498071 [=======================>......] - ETA: 5s
140451840/170498071 [=======================>......] - ETA: 5s
140877824/170498071 [=======================>......] - ETA: 5s
141361152/170498071 [=======================>......] - ETA: 5s
141860864/170498071 [=======================>......] - ETA: 5s
142254080/170498071 [========================>.....] - ETA: 5s
142811136/170498071 [========================>.....] - ETA: 5s
143237120/170498071 [========================>.....] - ETA: 5s
143712256/170498071 [========================>.....] - ETA: 4s
144236544/170498071 [========================>.....] - ETA: 4s
144711680/170498071 [========================>.....] - ETA: 4s
145104896/170498071 [========================>.....] - ETA: 4s
145711104/170498071 [========================>.....] - ETA: 4s
146104320/170498071 [========================>.....] - ETA: 4s
146628608/170498071 [========================>.....] - ETA: 4s
147103744/170498071 [========================>.....] - ETA: 4s
147562496/170498071 [========================>.....] - ETA: 4s
148103168/170498071 [=========================>....] - ETA: 4s
148512768/170498071 [=========================>....] - ETA: 3s
149069824/170498071 [=========================>....] - ETA: 3s
149479424/170498071 [=========================>....] - ETA: 3s
149938176/170498071 [=========================>....] - ETA: 3s
150462464/170498071 [=========================>....] - ETA: 3s
150953984/170498071 [=========================>....] - ETA: 3s
151379968/170498071 [=========================>....] - ETA: 3s
151977984/170498071 [=========================>....] - ETA: 3s
152379392/170498071 [=========================>....] - ETA: 3s
152838144/170498071 [=========================>....] - ETA: 3s
153346048/170498071 [=========================>....] - ETA: 3s
153804800/170498071 [==========================>...] - ETA: 2s
154329088/170498071 [==========================>...] - ETA: 2s
154755072/170498071 [==========================>...] - ETA: 2s
155328512/170498071 [==========================>...] - ETA: 2s
155721728/170498071 [==========================>...] - ETA: 2s
156246016/170498071 [==========================>...] - ETA: 2s
156753920/170498071 [==========================>...] - ETA: 2s
157229056/170498071 [==========================>...] - ETA: 2s
157720576/170498071 [==========================>...] - ETA: 2s
158228480/170498071 [==========================>...] - ETA: 2s
158670848/170498071 [==========================>...] - ETA: 2s
159277056/170498071 [===========================>..] - ETA: 1s
159719424/170498071 [===========================>..] - ETA: 1s
160309248/170498071 [===========================>..] - ETA: 1s
160751616/170498071 [===========================>..] - ETA: 1s
161341440/170498071 [===========================>..] - ETA: 1s
161800192/170498071 [===========================>..] - ETA: 1s
162390016/170498071 [===========================>..] - ETA: 1s
162832384/170498071 [===========================>..] - ETA: 1s
163422208/170498071 [===========================>..] - ETA: 1s
163864576/170498071 [===========================>..] - ETA: 1s
164438016/170498071 [===========================>..] - ETA: 1s
164847616/170498071 [============================>.] - ETA: 0s
165437440/170498071 [============================>.] - ETA: 0s
165879808/170498071 [============================>.] - ETA: 0s
166486016/170498071 [============================>.] - ETA: 0s
166912000/170498071 [============================>.] - ETA: 0s
167419904/170498071 [============================>.] - ETA: 0s
167911424/170498071 [============================>.] - ETA: 0s
168353792/170498071 [============================>.] - ETA: 0s
168878080/170498071 [============================>.] - ETA: 0s
169328640/170498071 [============================>.] - ETA: 0s
169877504/170498071 [============================>.] - ETA: 0s
170254336/170498071 [============================>.] - ETA: 0s
170500096/170498071 [==============================] - 29s 0us/step

170508288/170498071 [==============================] - 29s 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.74 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
  139264/10744080 [..............................] - ETA: 5s
  401408/10744080 [>.............................] - ETA: 3s
  663552/10744080 [>.............................] - ETA: 2s
  925696/10744080 [=>............................] - ETA: 2s
 1187840/10744080 [==>...........................] - ETA: 2s
 1449984/10744080 [===>..........................] - ETA: 2s
 1712128/10744080 [===>..........................] - ETA: 2s
 1974272/10744080 [====>.........................] - ETA: 2s
 2236416/10744080 [=====>........................] - ETA: 2s
 2498560/10744080 [=====>........................] - ETA: 1s
 2760704/10744080 [======>.......................] - ETA: 1s
 3022848/10744080 [=======>......................] - ETA: 1s
 3284992/10744080 [========>.....................] - ETA: 1s
 3547136/10744080 [========>.....................] - ETA: 1s
 3809280/10744080 [=========>....................] - ETA: 1s
 4071424/10744080 [==========>...................] - ETA: 1s
 4333568/10744080 [===========>..................] - ETA: 1s
 4595712/10744080 [===========>..................] - ETA: 1s
 4857856/10744080 [============>.................] - ETA: 1s
 5120000/10744080 [=============>................] - ETA: 1s
 5382144/10744080 [==============>...............] - ETA: 1s
 5644288/10744080 [==============>...............] - ETA: 1s
 5906432/10744080 [===============>..............] - ETA: 1s
 6168576/10744080 [================>.............] - ETA: 1s
 6430720/10744080 [================>.............] - ETA: 0s
 6692864/10744080 [=================>............] - ETA: 0s
 6955008/10744080 [==================>...........] - ETA: 0s
 7217152/10744080 [===================>..........] - ETA: 0s
 7479296/10744080 [===================>..........] - ETA: 0s
 7741440/10744080 [====================>.........] - ETA: 0s
 8003584/10744080 [=====================>........] - ETA: 0s
 8265728/10744080 [======================>.......] - ETA: 0s
 8527872/10744080 [======================>.......] - ETA: 0s
 8790016/10744080 [=======================>......] - ETA: 0s
 9052160/10744080 [========================>.....] - ETA: 0s
 9314304/10744080 [=========================>....] - ETA: 0s
 9576448/10744080 [=========================>....] - ETA: 0s
 9838592/10744080 [==========================>...] - ETA: 0s
10100736/10744080 [===========================>..] - ETA: 0s
10362880/10744080 [===========================>..] - ETA: 0s
10625024/10744080 [============================>.] - ETA: 0s
10747904/10744080 [==============================] - 2s 0us/step

10756096/10744080 [==============================] - 2s 0us/step
Accuracy: 94.20%
Keras inference on 1000 images took 0.77 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
______________________________________________

/usr/local/lib/python3.6/dist-packages/numpy/core/_asarray.py:83: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  return array(a, dtype, copy=False, order=order)
              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.41 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 Model.statistics

# Print model statistics
print("Model statistics")
print(model_akida.statistics)

Out:

Model statistics

Sequence SW/conv_0-separable_10
Average framerate = 54.33 fps
Layer (type)                  output sparsity
conv_0 (InputConv.)           0.59
Layer (type)                  output sparsity
separable_1 (Sep.Conv.)       0.51
Layer (type)                  output sparsity
separable_2 (Sep.Conv.)       0.54
Layer (type)                  output sparsity
separable_3 (Sep.Conv.)       0.63
Layer (type)                  output sparsity
separable_4 (Sep.Conv.)       0.64
Layer (type)                  output sparsity
separable_5 (Sep.Conv.)       0.71
Layer (type)                  output sparsity
separable_6 (Sep.Conv.)       0.68
Layer (type)                  output sparsity
separable_7 (Sep.Conv.)       0.75
Layer (type)                  output sparsity
separable_8 (Sep.Conv.)       0.84
Layer (type)                  output sparsity
separable_9 (Sep.Conv.)       0.84
Layer (type)                  output sparsity
separable_10 (Sep.Conv.)      N/A

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 Execution Engine',
                 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 1.275 seconds)

Gallery generated by Sphinx-Gallery