GXNOR/MNIST inference

The MNIST dataset is a handwritten digits database. It has a training set of 60,000 samples, and a test set of 10,000 samples. Each sample comprises a 28x28 pixel image and an associated label.

This tutorial illustrates how to use a pre-trained model to process the MNIST dataset.

1. Dataset preparation

import numpy as np

import matplotlib.cm as cm
import matplotlib.pyplot as plt

from tensorflow.keras.datasets import mnist

# Retrieve MNIST dataset
_, (test_set, test_label) = mnist.load_data()

# Add a dimension to images sets as akida expects 4 dimensions inputs
test_set = np.expand_dims(test_set, -1)

# Display a few images from the test set
f, axarr = plt.subplots(1, 4)
for i in range(0, 4):
    axarr[i].imshow(test_set[i].reshape((28, 28)), cmap=cm.Greys_r)
    axarr[i].set_title('Class %d' % test_label[i])
plt.show()
Class 7, Class 2, Class 1, Class 0
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz

    8192/11490434 [..............................] - ETA: 0s
   16384/11490434 [..............................] - ETA: 5:43
   40960/11490434 [..............................] - ETA: 3:59
   57344/11490434 [..............................] - ETA: 3:31
   81920/11490434 [..............................] - ETA: 2:42
   90112/11490434 [..............................] - ETA: 2:39
   98304/11490434 [..............................] - ETA: 3:07
  122880/11490434 [..............................] - ETA: 2:37
  131072/11490434 [..............................] - ETA: 2:39
  139264/11490434 [..............................] - ETA: 2:41
  147456/11490434 [..............................] - ETA: 2:41
  155648/11490434 [..............................] - ETA: 2:53
  172032/11490434 [..............................] - ETA: 3:01
  188416/11490434 [..............................] - ETA: 2:52
  196608/11490434 [..............................] - ETA: 2:53
  204800/11490434 [..............................] - ETA: 3:10
  212992/11490434 [..............................] - ETA: 3:16
  237568/11490434 [..............................] - ETA: 3:03
  245760/11490434 [..............................] - ETA: 3:03
  253952/11490434 [..............................] - ETA: 3:03
  262144/11490434 [..............................] - ETA: 3:03
  270336/11490434 [..............................] - ETA: 3:04
  278528/11490434 [..............................] - ETA: 3:03
  286720/11490434 [..............................] - ETA: 3:03
  294912/11490434 [..............................] - ETA: 3:03
  303104/11490434 [..............................] - ETA: 3:17
  311296/11490434 [..............................] - ETA: 3:15
  327680/11490434 [..............................] - ETA: 3:09
  335872/11490434 [..............................] - ETA: 3:08
  344064/11490434 [..............................] - ETA: 3:08
  352256/11490434 [..............................] - ETA: 3:16
  360448/11490434 [..............................] - ETA: 3:17
  376832/11490434 [..............................] - ETA: 3:19
  393216/11490434 [>.............................] - ETA: 3:30
  417792/11490434 [>.............................] - ETA: 3:24
  425984/11490434 [>.............................] - ETA: 3:29
  442368/11490434 [>.............................] - ETA: 3:25
  450560/11490434 [>.............................] - ETA: 3:25
  458752/11490434 [>.............................] - ETA: 3:30
  466944/11490434 [>.............................] - ETA: 3:33
  483328/11490434 [>.............................] - ETA: 3:30
  491520/11490434 [>.............................] - ETA: 3:28
  499712/11490434 [>.............................] - ETA: 3:26
  507904/11490434 [>.............................] - ETA: 3:30
  524288/11490434 [>.............................] - ETA: 3:30
  540672/11490434 [>.............................] - ETA: 3:26
  548864/11490434 [>.............................] - ETA: 3:28
  557056/11490434 [>.............................] - ETA: 3:28
  565248/11490434 [>.............................] - ETA: 3:26
  573440/11490434 [>.............................] - ETA: 3:25
  581632/11490434 [>.............................] - ETA: 3:25
  589824/11490434 [>.............................] - ETA: 3:24
  598016/11490434 [>.............................] - ETA: 3:24
  606208/11490434 [>.............................] - ETA: 3:26
  614400/11490434 [>.............................] - ETA: 3:27
  622592/11490434 [>.............................] - ETA: 3:26
  638976/11490434 [>.............................] - ETA: 3:35
  647168/11490434 [>.............................] - ETA: 3:34
  655360/11490434 [>.............................] - ETA: 3:34
  679936/11490434 [>.............................] - ETA: 3:28
  688128/11490434 [>.............................] - ETA: 3:31
  704512/11490434 [>.............................] - ETA: 3:28
  712704/11490434 [>.............................] - ETA: 3:31
  729088/11490434 [>.............................] - ETA: 3:29
  737280/11490434 [>.............................] - ETA: 3:28
  745472/11490434 [>.............................] - ETA: 3:27
  753664/11490434 [>.............................] - ETA: 3:26
  761856/11490434 [>.............................] - ETA: 3:26
  770048/11490434 [=>............................] - ETA: 3:25
  778240/11490434 [=>............................] - ETA: 3:25
  786432/11490434 [=>............................] - ETA: 3:27
  794624/11490434 [=>............................] - ETA: 3:27
  802816/11490434 [=>............................] - ETA: 3:26
  811008/11490434 [=>............................] - ETA: 3:25
  819200/11490434 [=>............................] - ETA: 3:30
  835584/11490434 [=>............................] - ETA: 3:27
  843776/11490434 [=>............................] - ETA: 3:28
  868352/11490434 [=>............................] - ETA: 3:27
  884736/11490434 [=>............................] - ETA: 3:23
  901120/11490434 [=>............................] - ETA: 3:25
  925696/11490434 [=>............................] - ETA: 3:20
  933888/11490434 [=>............................] - ETA: 3:20
  942080/11490434 [=>............................] - ETA: 3:19
  950272/11490434 [=>............................] - ETA: 3:25
  966656/11490434 [=>............................] - ETA: 3:23
  983040/11490434 [=>............................] - ETA: 3:24
  991232/11490434 [=>............................] - ETA: 3:25
 1007616/11490434 [=>............................] - ETA: 3:22
 1015808/11490434 [=>............................] - ETA: 3:21
 1024000/11490434 [=>............................] - ETA: 3:25
 1048576/11490434 [=>............................] - ETA: 3:20
 1056768/11490434 [=>............................] - ETA: 3:22
 1073152/11490434 [=>............................] - ETA: 3:20
 1081344/11490434 [=>............................] - ETA: 3:19
 1089536/11490434 [=>............................] - ETA: 3:19
 1097728/11490434 [=>............................] - ETA: 3:21
 1114112/11490434 [=>............................] - ETA: 3:18
 1122304/11490434 [=>............................] - ETA: 3:18
 1130496/11490434 [=>............................] - ETA: 3:17
 1138688/11490434 [=>............................] - ETA: 3:20
 1146880/11490434 [=>............................] - ETA: 3:21
 1155072/11490434 [==>...........................] - ETA: 3:22
 1179648/11490434 [==>...........................] - ETA: 3:22
 1204224/11490434 [==>...........................] - ETA: 3:18
 1212416/11490434 [==>...........................] - ETA: 3:18
 1220608/11490434 [==>...........................] - ETA: 3:23
 1253376/11490434 [==>...........................] - ETA: 3:19
 1261568/11490434 [==>...........................] - ETA: 3:18
 1277952/11490434 [==>...........................] - ETA: 3:16
 1286144/11490434 [==>...........................] - ETA: 3:16
 1294336/11490434 [==>...........................] - ETA: 3:16
 1302528/11490434 [==>...........................] - ETA: 3:15
 1310720/11490434 [==>...........................] - ETA: 3:17
 1327104/11490434 [==>...........................] - ETA: 3:15
 1335296/11490434 [==>...........................] - ETA: 3:16
 1359872/11490434 [==>...........................] - ETA: 3:13
 1368064/11490434 [==>...........................] - ETA: 3:13
 1376256/11490434 [==>...........................] - ETA: 3:13
 1384448/11490434 [==>...........................] - ETA: 3:12
 1392640/11490434 [==>...........................] - ETA: 3:12
 1400832/11490434 [==>...........................] - ETA: 3:12
 1409024/11490434 [==>...........................] - ETA: 3:11
 1417216/11490434 [==>...........................] - ETA: 3:11
 1425408/11490434 [==>...........................] - ETA: 3:12
 1433600/11490434 [==>...........................] - ETA: 3:11
 1449984/11490434 [==>...........................] - ETA: 3:11
 1466368/11490434 [==>...........................] - ETA: 3:11
 1482752/11490434 [==>...........................] - ETA: 3:09
 1490944/11490434 [==>...........................] - ETA: 3:10
 1499136/11490434 [==>...........................] - ETA: 3:10
 1515520/11490434 [==>...........................] - ETA: 3:11
 1540096/11490434 [===>..........................] - ETA: 3:08
 1548288/11490434 [===>..........................] - ETA: 3:08
 1556480/11490434 [===>..........................] - ETA: 3:08
 1564672/11490434 [===>..........................] - ETA: 3:09
 1581056/11490434 [===>..........................] - ETA: 3:07
 1589248/11490434 [===>..........................] - ETA: 3:07
 1597440/11490434 [===>..........................] - ETA: 3:08
 1613824/11490434 [===>..........................] - ETA: 3:06
 1622016/11490434 [===>..........................] - ETA: 3:06
 1630208/11490434 [===>..........................] - ETA: 3:05
 1638400/11490434 [===>..........................] - ETA: 3:05
 1646592/11490434 [===>..........................] - ETA: 3:05
 1654784/11490434 [===>..........................] - ETA: 3:04
 1662976/11490434 [===>..........................] - ETA: 3:04
 1671168/11490434 [===>..........................] - ETA: 3:03
 1679360/11490434 [===>..........................] - ETA: 3:03
 1687552/11490434 [===>..........................] - ETA: 3:03
 1695744/11490434 [===>..........................] - ETA: 3:02
 1703936/11490434 [===>..........................] - ETA: 3:02
 1712128/11490434 [===>..........................] - ETA: 3:02
 1720320/11490434 [===>..........................] - ETA: 3:01
 1728512/11490434 [===>..........................] - ETA: 3:01
 1736704/11490434 [===>..........................] - ETA: 3:00
 1744896/11490434 [===>..........................] - ETA: 3:00
 1753088/11490434 [===>..........................] - ETA: 3:00
 1761280/11490434 [===>..........................] - ETA: 2:59
 1769472/11490434 [===>..........................] - ETA: 3:00
 1794048/11490434 [===>..........................] - ETA: 2:58
 1802240/11490434 [===>..........................] - ETA: 2:57
 1810432/11490434 [===>..........................] - ETA: 2:57
 1818624/11490434 [===>..........................] - ETA: 2:57
 1851392/11490434 [===>..........................] - ETA: 2:54
 1859584/11490434 [===>..........................] - ETA: 2:54
 1867776/11490434 [===>..........................] - ETA: 2:53
 1875968/11490434 [===>..........................] - ETA: 2:53
 1884160/11490434 [===>..........................] - ETA: 2:52
 1892352/11490434 [===>..........................] - ETA: 2:52
 1900544/11490434 [===>..........................] - ETA: 2:51
 1908736/11490434 [===>..........................] - ETA: 2:51
 1916928/11490434 [====>.........................] - ETA: 2:50
 1925120/11490434 [====>.........................] - ETA: 2:50
 1933312/11490434 [====>.........................] - ETA: 2:49
 1941504/11490434 [====>.........................] - ETA: 2:49
 1949696/11490434 [====>.........................] - ETA: 2:48
 1957888/11490434 [====>.........................] - ETA: 2:49
 1982464/11490434 [====>.........................] - ETA: 2:47
 1990656/11490434 [====>.........................] - ETA: 2:46
 1998848/11490434 [====>.........................] - ETA: 2:45
 2007040/11490434 [====>.........................] - ETA: 2:45
 2015232/11490434 [====>.........................] - ETA: 2:45
 2023424/11490434 [====>.........................] - ETA: 2:44
 2031616/11490434 [====>.........................] - ETA: 2:44
 2039808/11490434 [====>.........................] - ETA: 2:43
 2048000/11490434 [====>.........................] - ETA: 2:43
 2056192/11490434 [====>.........................] - ETA: 2:42
 2064384/11490434 [====>.........................] - ETA: 2:42
 2072576/11490434 [====>.........................] - ETA: 2:41
 2080768/11490434 [====>.........................] - ETA: 2:41
 2088960/11490434 [====>.........................] - ETA: 2:40
 2097152/11490434 [====>.........................] - ETA: 2:40
 2105344/11490434 [====>.........................] - ETA: 2:39
 2113536/11490434 [====>.........................] - ETA: 2:39
 2121728/11490434 [====>.........................] - ETA: 2:38
 2129920/11490434 [====>.........................] - ETA: 2:38
 2138112/11490434 [====>.........................] - ETA: 2:37
 2154496/11490434 [====>.........................] - ETA: 2:36
 2162688/11490434 [====>.........................] - ETA: 2:36
 2170880/11490434 [====>.........................] - ETA: 2:35
 2179072/11490434 [====>.........................] - ETA: 2:35
 2187264/11490434 [====>.........................] - ETA: 2:34
 2195456/11490434 [====>.........................] - ETA: 2:34
 2211840/11490434 [====>.........................] - ETA: 2:33
 2220032/11490434 [====>.........................] - ETA: 2:32
 2228224/11490434 [====>.........................] - ETA: 2:32
 2236416/11490434 [====>.........................] - ETA: 2:31
 2252800/11490434 [====>.........................] - ETA: 2:30
 2269184/11490434 [====>.........................] - ETA: 2:29
 2285568/11490434 [====>.........................] - ETA: 2:28
 2301952/11490434 [=====>........................] - ETA: 2:27
 2318336/11490434 [=====>........................] - ETA: 2:27
 2334720/11490434 [=====>........................] - ETA: 2:26
 2342912/11490434 [=====>........................] - ETA: 2:25
 2359296/11490434 [=====>........................] - ETA: 2:24
 2375680/11490434 [=====>........................] - ETA: 2:23
 2383872/11490434 [=====>........................] - ETA: 2:23
 2392064/11490434 [=====>........................] - ETA: 2:22
 2408448/11490434 [=====>........................] - ETA: 2:22
 2424832/11490434 [=====>........................] - ETA: 2:21
 2441216/11490434 [=====>........................] - ETA: 2:20
 2457600/11490434 [=====>........................] - ETA: 2:19
 2473984/11490434 [=====>........................] - ETA: 2:19
 2506752/11490434 [=====>........................] - ETA: 2:17
 2523136/11490434 [=====>........................] - ETA: 2:16
 2539520/11490434 [=====>........................] - ETA: 2:15
 2555904/11490434 [=====>........................] - ETA: 2:14
 2572288/11490434 [=====>........................] - ETA: 2:14
 2588672/11490434 [=====>........................] - ETA: 2:13
 2605056/11490434 [=====>........................] - ETA: 2:12
 2621440/11490434 [=====>........................] - ETA: 2:11
 2637824/11490434 [=====>........................] - ETA: 2:11
 2695168/11490434 [======>.......................] - ETA: 2:08
 2711552/11490434 [======>.......................] - ETA: 2:07
 2768896/11490434 [======>.......................] - ETA: 2:04
 2785280/11490434 [======>.......................] - ETA: 2:04
 2834432/11490434 [======>.......................] - ETA: 2:02
 2899968/11490434 [======>.......................] - ETA: 1:59
 2916352/11490434 [======>.......................] - ETA: 1:58
 2940928/11490434 [======>.......................] - ETA: 1:57
 2965504/11490434 [======>.......................] - ETA: 1:56
 3006464/11490434 [======>.......................] - ETA: 1:54
 3022848/11490434 [======>.......................] - ETA: 1:54
 3039232/11490434 [======>.......................] - ETA: 1:53
 3055616/11490434 [======>.......................] - ETA: 1:52
 3072000/11490434 [=======>......................] - ETA: 1:52
 3088384/11490434 [=======>......................] - ETA: 1:51
 3104768/11490434 [=======>......................] - ETA: 1:50
 3121152/11490434 [=======>......................] - ETA: 1:50
 3137536/11490434 [=======>......................] - ETA: 1:49
 3153920/11490434 [=======>......................] - ETA: 1:48
 3178496/11490434 [=======>......................] - ETA: 1:47
 3194880/11490434 [=======>......................] - ETA: 1:47
 3211264/11490434 [=======>......................] - ETA: 1:46
 3235840/11490434 [=======>......................] - ETA: 1:45
 3260416/11490434 [=======>......................] - ETA: 1:44
 3276800/11490434 [=======>......................] - ETA: 1:44
 3293184/11490434 [=======>......................] - ETA: 1:43
 3309568/11490434 [=======>......................] - ETA: 1:43
 3325952/11490434 [=======>......................] - ETA: 1:42
 3407872/11490434 [=======>......................] - ETA: 1:39
 3432448/11490434 [=======>......................] - ETA: 1:38
 3448832/11490434 [========>.....................] - ETA: 1:38
 3473408/11490434 [========>.....................] - ETA: 1:37
 3497984/11490434 [========>.....................] - ETA: 1:36
 3522560/11490434 [========>.....................] - ETA: 1:35
 3547136/11490434 [========>.....................] - ETA: 1:34
 3571712/11490434 [========>.....................] - ETA: 1:34
 3596288/11490434 [========>.....................] - ETA: 1:33
 3620864/11490434 [========>.....................] - ETA: 1:32
 3645440/11490434 [========>.....................] - ETA: 1:31
 3670016/11490434 [========>.....................] - ETA: 1:30
 3694592/11490434 [========>.....................] - ETA: 1:30
 3710976/11490434 [========>.....................] - ETA: 1:29
 3735552/11490434 [========>.....................] - ETA: 1:28
 3743744/11490434 [========>.....................] - ETA: 1:29
 3792896/11490434 [========>.....................] - ETA: 1:27
 3858432/11490434 [=========>....................] - ETA: 1:25
 3883008/11490434 [=========>....................] - ETA: 1:24
 3907584/11490434 [=========>....................] - ETA: 1:24
 3932160/11490434 [=========>....................] - ETA: 1:23
 3956736/11490434 [=========>....................] - ETA: 1:22
 3981312/11490434 [=========>....................] - ETA: 1:21
 4005888/11490434 [=========>....................] - ETA: 1:21
 4030464/11490434 [=========>....................] - ETA: 1:20
 4063232/11490434 [=========>....................] - ETA: 1:20
 4128768/11490434 [=========>....................] - ETA: 1:18
 4210688/11490434 [=========>....................] - ETA: 1:16
 4243456/11490434 [==========>...................] - ETA: 1:15
 4276224/11490434 [==========>...................] - ETA: 1:14
 4308992/11490434 [==========>...................] - ETA: 1:13
 4341760/11490434 [==========>...................] - ETA: 1:13
 4440064/11490434 [==========>...................] - ETA: 1:10
 4472832/11490434 [==========>...................] - ETA: 1:09
 4505600/11490434 [==========>...................] - ETA: 1:09
 4538368/11490434 [==========>...................] - ETA: 1:08
 4571136/11490434 [==========>...................] - ETA: 1:07
 4603904/11490434 [===========>..................] - ETA: 1:07
 4636672/11490434 [===========>..................] - ETA: 1:06
 4669440/11490434 [===========>..................] - ETA: 1:05
 4718592/11490434 [===========>..................] - ETA: 1:04
 4767744/11490434 [===========>..................] - ETA: 1:03
 4800512/11490434 [===========>..................] - ETA: 1:02
 4849664/11490434 [===========>..................] - ETA: 1:01
 4898816/11490434 [===========>..................] - ETA: 1:00
 4931584/11490434 [===========>..................] - ETA: 1:00
 4964352/11490434 [===========>..................] - ETA: 59s 
 5013504/11490434 [============>.................] - ETA: 58s
 5046272/11490434 [============>.................] - ETA: 58s
 5079040/11490434 [============>.................] - ETA: 57s
 5128192/11490434 [============>.................] - ETA: 56s
 5160960/11490434 [============>.................] - ETA: 56s
 5193728/11490434 [============>.................] - ETA: 55s
 5242880/11490434 [============>.................] - ETA: 54s
 5259264/11490434 [============>.................] - ETA: 54s
 5292032/11490434 [============>.................] - ETA: 53s
 5324800/11490434 [============>.................] - ETA: 53s
 5373952/11490434 [=============>................] - ETA: 52s
 5406720/11490434 [=============>................] - ETA: 51s
 5439488/11490434 [=============>................] - ETA: 51s
 5488640/11490434 [=============>................] - ETA: 50s
 5521408/11490434 [=============>................] - ETA: 50s
 5554176/11490434 [=============>................] - ETA: 49s
 5603328/11490434 [=============>................] - ETA: 48s
 5636096/11490434 [=============>................] - ETA: 48s
 5668864/11490434 [=============>................] - ETA: 47s
 5718016/11490434 [=============>................] - ETA: 47s
 5767168/11490434 [==============>...............] - ETA: 46s
 5816320/11490434 [==============>...............] - ETA: 45s
 5849088/11490434 [==============>...............] - ETA: 45s
 5881856/11490434 [==============>...............] - ETA: 44s
 5931008/11490434 [==============>...............] - ETA: 44s
 5980160/11490434 [==============>...............] - ETA: 43s
 6029312/11490434 [==============>...............] - ETA: 42s
 6062080/11490434 [==============>...............] - ETA: 42s
 6111232/11490434 [==============>...............] - ETA: 41s
 6160384/11490434 [===============>..............] - ETA: 40s
 6209536/11490434 [===============>..............] - ETA: 40s
 6258688/11490434 [===============>..............] - ETA: 39s
 6307840/11490434 [===============>..............] - ETA: 39s
 6356992/11490434 [===============>..............] - ETA: 38s
 6406144/11490434 [===============>..............] - ETA: 37s
 6455296/11490434 [===============>..............] - ETA: 37s
 6504448/11490434 [===============>..............] - ETA: 36s
 6553600/11490434 [================>.............] - ETA: 36s
 6602752/11490434 [================>.............] - ETA: 35s
 6651904/11490434 [================>.............] - ETA: 34s
 6701056/11490434 [================>.............] - ETA: 34s
 6750208/11490434 [================>.............] - ETA: 33s
 6799360/11490434 [================>.............] - ETA: 33s
 6848512/11490434 [================>.............] - ETA: 32s
 6914048/11490434 [=================>............] - ETA: 31s
 6963200/11490434 [=================>............] - ETA: 31s
 7004160/11490434 [=================>............] - ETA: 31s
 7200768/11490434 [=================>............] - ETA: 28s
 7249920/11490434 [=================>............] - ETA: 28s
 7299072/11490434 [==================>...........] - ETA: 27s
 7356416/11490434 [==================>...........] - ETA: 27s
 7405568/11490434 [==================>...........] - ETA: 26s
 7454720/11490434 [==================>...........] - ETA: 26s
 7503872/11490434 [==================>...........] - ETA: 25s
 7553024/11490434 [==================>...........] - ETA: 25s
 7602176/11490434 [==================>...........] - ETA: 25s
 7659520/11490434 [==================>...........] - ETA: 24s
 7708672/11490434 [===================>..........] - ETA: 24s
 7757824/11490434 [===================>..........] - ETA: 23s
 7806976/11490434 [===================>..........] - ETA: 23s
 7856128/11490434 [===================>..........] - ETA: 22s
 7905280/11490434 [===================>..........] - ETA: 22s
 7970816/11490434 [===================>..........] - ETA: 21s
 8036352/11490434 [===================>..........] - ETA: 21s
 8101888/11490434 [====================>.........] - ETA: 20s
 8151040/11490434 [====================>.........] - ETA: 20s
 8200192/11490434 [====================>.........] - ETA: 19s
 8265728/11490434 [====================>.........] - ETA: 19s
 8331264/11490434 [====================>.........] - ETA: 18s
 8396800/11490434 [====================>.........] - ETA: 18s
 8462336/11490434 [=====================>........] - ETA: 17s
 8527872/11490434 [=====================>........] - ETA: 17s
 8593408/11490434 [=====================>........] - ETA: 16s
 8658944/11490434 [=====================>........] - ETA: 16s
 8724480/11490434 [=====================>........] - ETA: 15s
 8790016/11490434 [=====================>........] - ETA: 15s
 8855552/11490434 [======================>.......] - ETA: 14s
 8912896/11490434 [======================>.......] - ETA: 14s
 8986624/11490434 [======================>.......] - ETA: 13s
 9052160/11490434 [======================>.......] - ETA: 13s
 9084928/11490434 [======================>.......] - ETA: 13s
 9265152/11490434 [=======================>......] - ETA: 12s
 9535488/11490434 [=======================>......] - ETA: 10s
 9617408/11490434 [========================>.....] - ETA: 9s 
 9699328/11490434 [========================>.....] - ETA: 9s
 9764864/11490434 [========================>.....] - ETA: 8s
 9822208/11490434 [========================>.....] - ETA: 8s
 9879552/11490434 [========================>.....] - ETA: 8s
10092544/11490434 [=========================>....] - ETA: 7s
10436608/11490434 [==========================>...] - ETA: 5s
10518528/11490434 [==========================>...] - ETA: 4s
10600448/11490434 [==========================>...] - ETA: 4s
10682368/11490434 [==========================>...] - ETA: 3s
10764288/11490434 [===========================>..] - ETA: 3s
10846208/11490434 [===========================>..] - ETA: 3s
10928128/11490434 [===========================>..] - ETA: 2s
11010048/11490434 [===========================>..] - ETA: 2s
11091968/11490434 [===========================>..] - ETA: 1s
11173888/11490434 [============================>.] - ETA: 1s
11272192/11490434 [============================>.] - ETA: 1s
11386880/11490434 [============================>.] - ETA: 0s
11490434/11490434 [==============================] - 52s 5us/step

2. Create a Keras GXNOR model

The GXNOR architecture is available in the Akida models zoo along with pretrained weights.

Note

The pre-trained weights were obtained with knowledge distillation training, using the EfficientNet model from this repository and the Distiller class from the knowledge distillation toolkit.

The float training was done for 30 epochs, the model is then gradually quantized following: 8-4-4 –> 4-4-4 –> 4-4-2 –> 2-2-2 –> 2-2-1 by tuning the model at each step with the same distillation training method for 5 epochs.

from akida_models import gxnor_mnist_pretrained

model_keras = gxnor_mnist_pretrained()
model_keras.summary()
Downloading data from http://data.brainchip.com/models/gxnor/gxnor_mnist_iq2_wq2_aq1.h5.

      0/6557232 [..............................] - ETA: 0s
 188416/6557232 [..............................] - ETA: 1s
 516096/6557232 [=>............................] - ETA: 1s
 933888/6557232 [===>..........................] - ETA: 0s
1294336/6557232 [====>.........................] - ETA: 0s
1695744/6557232 [======>.......................] - ETA: 0s
2007040/6557232 [========>.....................] - ETA: 0s
2318336/6557232 [=========>....................] - ETA: 0s
2654208/6557232 [===========>..................] - ETA: 0s
2981888/6557232 [============>.................] - ETA: 0s
3317760/6557232 [==============>...............] - ETA: 0s
3670016/6557232 [===============>..............] - ETA: 0s
3981312/6557232 [=================>............] - ETA: 0s
4210688/6557232 [==================>...........] - ETA: 0s
4390912/6557232 [===================>..........] - ETA: 0s
4562944/6557232 [===================>..........] - ETA: 0s
4726784/6557232 [====================>.........] - ETA: 0s
4915200/6557232 [=====================>........] - ETA: 0s
5079040/6557232 [======================>.......] - ETA: 0s
5242880/6557232 [======================>.......] - ETA: 0s
5332992/6557232 [=======================>......] - ETA: 0s
5431296/6557232 [=======================>......] - ETA: 0s
5537792/6557232 [========================>.....] - ETA: 0s
5644288/6557232 [========================>.....] - ETA: 0s
5775360/6557232 [=========================>....] - ETA: 0s
5898240/6557232 [=========================>....] - ETA: 0s
6021120/6557232 [==========================>...] - ETA: 0s
6184960/6557232 [===========================>..] - ETA: 0s
6348800/6557232 [============================>.] - ETA: 0s
6496256/6557232 [============================>.] - ETA: 0s
6557232/6557232 [==============================] - 2s 0us/step
WARNING:tensorflow:No training configuration found in the save file, so the model was *not* compiled. Compile it manually.
Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #
=================================================================
 input (InputLayer)          [(None, 28, 28, 1)]       0

 rescaling (Rescaling)       (None, 28, 28, 1)         0

 block_1/conv_1 (QuantizedCo  (None, 28, 28, 32)       832
 nv2D)

 block_1/conv_1/maxpool (Max  (None, 14, 14, 32)       0
 Pooling2D)

 block_1/conv_1/relu (Activa  (None, 14, 14, 32)       0
 tionDiscreteRelu)

 block_2/conv_1 (QuantizedCo  (None, 7, 7, 64)         18496
 nv2D)

 block_2/conv_1/relu (Activa  (None, 7, 7, 64)         0
 tionDiscreteRelu)

 flatten (Flatten)           (None, 3136)              0

 fc_1 (QuantizedDense)       (None, 512)               1606144

 fc_1/relu (ActivationDiscre  (None, 512)              0
 teRelu)

 predictions (QuantizedDense  (None, 10)               5130
 )

=================================================================
Total params: 1,630,602
Trainable params: 1,630,602
Non-trainable params: 0
_________________________________________________________________
# Check Model performances
from tensorflow.keras.losses import SparseCategoricalCrossentropy

model_keras.compile(optimizer='adam',
                    loss=SparseCategoricalCrossentropy(from_logits=True),
                    metrics=['accuracy'])
keras_accuracy = model_keras.evaluate(test_set, test_label, verbose=0)[1]
print(f"Keras accuracy : {keras_accuracy}")
Keras accuracy : 0.9919999837875366

3. Conversion to Akida

3.1 Convert to Akida model

When converting to an Akida model, we just need to pass the Keras model to cnn2snn.convert.

from cnn2snn import convert

model_akida = convert(model_keras)

3.2 Check hardware compliancy

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

model_akida.summary()
                Model Summary
______________________________________________
Input shape  Output shape  Sequences  Layers
==============================================
[28, 28, 1]  [1, 1, 10]    1          4
______________________________________________

______________________________________________________________
Layer (type)                 Output shape  Kernel shape

========== SW/block_1/conv_1-predictions (Software) ==========

block_1/conv_1 (InputConv.)  [14, 14, 32]  (5, 5, 1, 32)
______________________________________________________________
block_2/conv_1 (Conv.)       [7, 7, 64]    (3, 3, 32, 64)
______________________________________________________________
fc_1 (Fully.)                [1, 1, 512]   (1, 1, 3136, 512)
______________________________________________________________
predictions (Fully.)         [1, 1, 10]    (1, 1, 512, 10)
______________________________________________________________

3.3. Check performance

from sklearn.metrics import accuracy_score

# Check performance against num_samples samples
num_samples = 10000

results = model_akida.predict_classes(test_set[:num_samples])
accuracy = accuracy_score(test_label[:num_samples], results[:num_samples])

# For non-regression purpose
assert accuracy > 0.99

# Display results
print("Accuracy: " + "{0:.2f}".format(100 * accuracy) + "%")
Accuracy: 99.20%

Depending on the number of samples you run, you should find a performance of around 99% (99.20% if you run all 10000 samples).

3.4 Show predictions for a single image

Now try processing a single image, say, the first image in the dataset that we looked at above:

# Test a single example
sample_image = 0
image = test_set[sample_image]
outputs = model_akida.predict(image.reshape(1, 28, 28, 1))
print('Input Label: %i' % test_label[sample_image])

f, axarr = plt.subplots(1, 2)
axarr[0].imshow(test_set[sample_image].reshape((28, 28)), cmap=cm.Greys_r)
axarr[0].set_title('Class %d' % test_label[sample_image])
axarr[1].bar(range(10), outputs.squeeze())
axarr[1].set_xticks(range(10))
plt.show()

print(outputs.squeeze())
Class 7
Input Label: 7
[-3.656929  -3.5484793 -2.2506387 -3.48714   -3.563524  -4.3317537
 -3.2740457  2.3640327 -2.9353547 -3.9376156]

Consider the output from the model, printed above. As is typical in backprop trained models, the final layer here comprises a ‘fully-connected or ‘dense’ layer, with one neuron per class in the data (here, 10). The goal of training is to maximize the response of the neuron corresponding to the label of each training sample, while minimizing the responses of the other neurons.

In the bar chart above, you can see the outputs from all 10 neurons. It is easy to see that neuron 7 responds much more strongly than the others. The first sample is indeed a number 7.

Check this for some of the other samples by editing the value of sample_image in the script above (anything from 0 to 9999).

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

Gallery generated by Sphinx-Gallery