Segmentation tutorial

This example demonstrates image segmentation with an Akida-compatible model as illustrated through person segmentation using the Portrait128 dataset.

Using pre-trained models for quick runtime, this example shows the evolution of model performance for a trained TF-Keras floating-point model, a TF-Keras quantized and Quantization Aware Trained (QAT) model, and an Akida-converted model. Notice that the performance of the original TF-Keras floating-point model is maintained throughout the model conversion flow.

1. Load the dataset

import os
import numpy as np
from akida_models import fetch_file

# Download validation set from Brainchip data server, it contains 10% of the original dataset
data_path = fetch_file(fname="val.tar.gz",
                       origin="https://data.brainchip.com/dataset-mirror/portrait128/val.tar.gz",
                       cache_subdir=os.path.join("datasets", "portrait128"),
                       extract=True)

data_dir = os.path.join(os.path.dirname(data_path), "val")
x_val = np.load(os.path.join(data_dir, "val_img.npy"))
y_val = np.load(os.path.join(data_dir, "val_msk.npy")).astype('uint8')
batch_size = 32
steps = x_val.shape[0] // 32

# Visualize some data
import matplotlib.pyplot as plt

rng = np.random.default_rng()
id = rng.integers(0, x_val.shape[0] - 2)

fig, axs = plt.subplots(3, 3, constrained_layout=True)
for col in range(3):
    axs[0, col].imshow(x_val[id + col] / 255.)
    axs[0, col].axis('off')
    axs[1, col].imshow(1 - y_val[id + col], cmap='Greys')
    axs[1, col].axis('off')
    axs[2, col].imshow(x_val[id + col] / 255. * y_val[id + col])
    axs[2, col].axis('off')

fig.suptitle('Image, mask and masked image', fontsize=10)
plt.show()
Image, mask and masked image
Downloading data from https://data.brainchip.com/dataset-mirror/portrait128/val.tar.gz.

        0/267313385 [..............................] - ETA: 0s
   139264/267313385 [..............................] - ETA: 1:37
   663552/267313385 [..............................] - ETA: 40s 
  1204224/267313385 [..............................] - ETA: 33s
  1794048/267313385 [..............................] - ETA: 30s
  2285568/267313385 [..............................] - ETA: 29s
  2859008/267313385 [..............................] - ETA: 28s
  3399680/267313385 [..............................] - ETA: 27s
  3956736/267313385 [..............................] - ETA: 27s
  4546560/267313385 [..............................] - ETA: 26s
  5136384/267313385 [..............................] - ETA: 26s
  5726208/267313385 [..............................] - ETA: 25s
  6299648/267313385 [..............................] - ETA: 25s
  6889472/267313385 [..............................] - ETA: 24s
  7479296/267313385 [..............................] - ETA: 24s
  8069120/267313385 [..............................] - ETA: 24s
  8658944/267313385 [..............................] - ETA: 24s
  9232384/267313385 [>.............................] - ETA: 24s
  9822208/267313385 [>.............................] - ETA: 23s
 10412032/267313385 [>.............................] - ETA: 23s
 11001856/267313385 [>.............................] - ETA: 23s
 11591680/267313385 [>.............................] - ETA: 23s
 12181504/267313385 [>.............................] - ETA: 23s
 12771328/267313385 [>.............................] - ETA: 23s
 13361152/267313385 [>.............................] - ETA: 23s
 13950976/267313385 [>.............................] - ETA: 23s
 14540800/267313385 [>.............................] - ETA: 22s
 15130624/267313385 [>.............................] - ETA: 22s
 15720448/267313385 [>.............................] - ETA: 22s
 16310272/267313385 [>.............................] - ETA: 22s
 16900096/267313385 [>.............................] - ETA: 22s
 17489920/267313385 [>.............................] - ETA: 22s
 18079744/267313385 [=>............................] - ETA: 22s
 18669568/267313385 [=>............................] - ETA: 22s
 19259392/267313385 [=>............................] - ETA: 22s
 19849216/267313385 [=>............................] - ETA: 22s
 20439040/267313385 [=>............................] - ETA: 22s
 21028864/267313385 [=>............................] - ETA: 21s
 21618688/267313385 [=>............................] - ETA: 21s
 22208512/267313385 [=>............................] - ETA: 21s
 22798336/267313385 [=>............................] - ETA: 21s
 23388160/267313385 [=>............................] - ETA: 21s
 23977984/267313385 [=>............................] - ETA: 21s
 24567808/267313385 [=>............................] - ETA: 21s
 25157632/267313385 [=>............................] - ETA: 21s
 25747456/267313385 [=>............................] - ETA: 21s
 26337280/267313385 [=>............................] - ETA: 21s
 26927104/267313385 [==>...........................] - ETA: 21s
 27500544/267313385 [==>...........................] - ETA: 21s
 28090368/267313385 [==>...........................] - ETA: 21s
 28680192/267313385 [==>...........................] - ETA: 21s
 29270016/267313385 [==>...........................] - ETA: 20s
 29843456/267313385 [==>...........................] - ETA: 20s
 30433280/267313385 [==>...........................] - ETA: 20s
 30941184/267313385 [==>...........................] - ETA: 21s
 31825920/267313385 [==>...........................] - ETA: 21s
 33857536/267313385 [==>...........................] - ETA: 20s
 34447360/267313385 [==>...........................] - ETA: 20s
 35037184/267313385 [==>...........................] - ETA: 20s
 35627008/267313385 [==>...........................] - ETA: 20s
 36216832/267313385 [===>..........................] - ETA: 20s
 36806656/267313385 [===>..........................] - ETA: 20s
 37396480/267313385 [===>..........................] - ETA: 20s
 37986304/267313385 [===>..........................] - ETA: 20s
 38576128/267313385 [===>..........................] - ETA: 20s
 39165952/267313385 [===>..........................] - ETA: 19s
 39755776/267313385 [===>..........................] - ETA: 19s
 40345600/267313385 [===>..........................] - ETA: 19s
 40935424/267313385 [===>..........................] - ETA: 19s
 41525248/267313385 [===>..........................] - ETA: 19s
 42115072/267313385 [===>..........................] - ETA: 19s
 42704896/267313385 [===>..........................] - ETA: 19s
 43294720/267313385 [===>..........................] - ETA: 19s
 43884544/267313385 [===>..........................] - ETA: 19s
 44474368/267313385 [===>..........................] - ETA: 19s
 45064192/267313385 [====>.........................] - ETA: 19s
 45654016/267313385 [====>.........................] - ETA: 19s
 46243840/267313385 [====>.........................] - ETA: 19s
 46833664/267313385 [====>.........................] - ETA: 19s
 47423488/267313385 [====>.........................] - ETA: 19s
 48013312/267313385 [====>.........................] - ETA: 19s
 48603136/267313385 [====>.........................] - ETA: 19s
 49192960/267313385 [====>.........................] - ETA: 19s
 49782784/267313385 [====>.........................] - ETA: 18s
 50372608/267313385 [====>.........................] - ETA: 18s
 50962432/267313385 [====>.........................] - ETA: 18s
 51552256/267313385 [====>.........................] - ETA: 18s
 52142080/267313385 [====>.........................] - ETA: 18s
 52731904/267313385 [====>.........................] - ETA: 18s
 53321728/267313385 [====>.........................] - ETA: 18s
 53911552/267313385 [=====>........................] - ETA: 18s
 54501376/267313385 [=====>........................] - ETA: 18s
 55091200/267313385 [=====>........................] - ETA: 18s
 55681024/267313385 [=====>........................] - ETA: 18s
 56270848/267313385 [=====>........................] - ETA: 18s
 56860672/267313385 [=====>........................] - ETA: 18s
 57450496/267313385 [=====>........................] - ETA: 18s
 58040320/267313385 [=====>........................] - ETA: 18s
 58630144/267313385 [=====>........................] - ETA: 18s
 59219968/267313385 [=====>........................] - ETA: 18s
 59809792/267313385 [=====>........................] - ETA: 18s
 60399616/267313385 [=====>........................] - ETA: 17s
 60989440/267313385 [=====>........................] - ETA: 17s
 61579264/267313385 [=====>........................] - ETA: 17s
 62169088/267313385 [=====>........................] - ETA: 17s
 62758912/267313385 [======>.......................] - ETA: 17s
 63332352/267313385 [======>.......................] - ETA: 17s
 63922176/267313385 [======>.......................] - ETA: 17s
 64495616/267313385 [======>.......................] - ETA: 17s
 65052672/267313385 [======>.......................] - ETA: 17s
 65609728/267313385 [======>.......................] - ETA: 17s
 66166784/267313385 [======>.......................] - ETA: 17s
 66723840/267313385 [======>.......................] - ETA: 17s
 67280896/267313385 [======>.......................] - ETA: 17s
 67837952/267313385 [======>.......................] - ETA: 17s
 68395008/267313385 [======>.......................] - ETA: 17s
 68952064/267313385 [======>.......................] - ETA: 17s
 69525504/267313385 [======>.......................] - ETA: 17s
 70082560/267313385 [======>.......................] - ETA: 17s
 70639616/267313385 [======>.......................] - ETA: 17s
 71196672/267313385 [======>.......................] - ETA: 17s
 71753728/267313385 [=======>......................] - ETA: 17s
 72310784/267313385 [=======>......................] - ETA: 17s
 72867840/267313385 [=======>......................] - ETA: 16s
 73424896/267313385 [=======>......................] - ETA: 16s
 73981952/267313385 [=======>......................] - ETA: 16s
 74539008/267313385 [=======>......................] - ETA: 16s
 75112448/267313385 [=======>......................] - ETA: 16s
 75669504/267313385 [=======>......................] - ETA: 16s
 76226560/267313385 [=======>......................] - ETA: 16s
 76783616/267313385 [=======>......................] - ETA: 16s
 77111296/267313385 [=======>......................] - ETA: 16s
 79126528/267313385 [=======>......................] - ETA: 16s
 79814656/267313385 [=======>......................] - ETA: 16s
 80388096/267313385 [========>.....................] - ETA: 16s
 80961536/267313385 [========>.....................] - ETA: 16s
 81534976/267313385 [========>.....................] - ETA: 16s
 82124800/267313385 [========>.....................] - ETA: 16s
 82714624/267313385 [========>.....................] - ETA: 16s
 83304448/267313385 [========>.....................] - ETA: 16s
 83894272/267313385 [========>.....................] - ETA: 16s
 84484096/267313385 [========>.....................] - ETA: 15s
 85073920/267313385 [========>.....................] - ETA: 15s
 85663744/267313385 [========>.....................] - ETA: 15s
 86253568/267313385 [========>.....................] - ETA: 15s
 86843392/267313385 [========>.....................] - ETA: 15s
 87433216/267313385 [========>.....................] - ETA: 15s
 88023040/267313385 [========>.....................] - ETA: 15s
 88612864/267313385 [========>.....................] - ETA: 15s
 89202688/267313385 [=========>....................] - ETA: 15s
 89792512/267313385 [=========>....................] - ETA: 15s
 90382336/267313385 [=========>....................] - ETA: 15s
 90972160/267313385 [=========>....................] - ETA: 15s
 91561984/267313385 [=========>....................] - ETA: 15s
 92151808/267313385 [=========>....................] - ETA: 15s
 92741632/267313385 [=========>....................] - ETA: 15s
 93315072/267313385 [=========>....................] - ETA: 15s
 93904896/267313385 [=========>....................] - ETA: 15s
 94494720/267313385 [=========>....................] - ETA: 15s
 95084544/267313385 [=========>....................] - ETA: 15s
 95674368/267313385 [=========>....................] - ETA: 14s
 96264192/267313385 [=========>....................] - ETA: 14s
 96854016/267313385 [=========>....................] - ETA: 14s
 97443840/267313385 [=========>....................] - ETA: 14s
 98033664/267313385 [==========>...................] - ETA: 14s
 98623488/267313385 [==========>...................] - ETA: 14s
 99213312/267313385 [==========>...................] - ETA: 14s
 99803136/267313385 [==========>...................] - ETA: 14s
100392960/267313385 [==========>...................] - ETA: 14s
100982784/267313385 [==========>...................] - ETA: 14s
101572608/267313385 [==========>...................] - ETA: 14s
102162432/267313385 [==========>...................] - ETA: 14s
102752256/267313385 [==========>...................] - ETA: 14s
103342080/267313385 [==========>...................] - ETA: 14s
103931904/267313385 [==========>...................] - ETA: 14s
104521728/267313385 [==========>...................] - ETA: 14s
105111552/267313385 [==========>...................] - ETA: 14s
105701376/267313385 [==========>...................] - ETA: 14s
106291200/267313385 [==========>...................] - ETA: 14s
106881024/267313385 [==========>...................] - ETA: 13s
107470848/267313385 [===========>..................] - ETA: 13s
108060672/267313385 [===========>..................] - ETA: 13s
108650496/267313385 [===========>..................] - ETA: 13s
109240320/267313385 [===========>..................] - ETA: 13s
109830144/267313385 [===========>..................] - ETA: 13s
110419968/267313385 [===========>..................] - ETA: 13s
111009792/267313385 [===========>..................] - ETA: 13s
111599616/267313385 [===========>..................] - ETA: 13s
112189440/267313385 [===========>..................] - ETA: 13s
112779264/267313385 [===========>..................] - ETA: 13s
113369088/267313385 [===========>..................] - ETA: 13s
113958912/267313385 [===========>..................] - ETA: 13s
114548736/267313385 [===========>..................] - ETA: 13s
115138560/267313385 [===========>..................] - ETA: 13s
115728384/267313385 [===========>..................] - ETA: 13s
116318208/267313385 [============>.................] - ETA: 13s
116908032/267313385 [============>.................] - ETA: 13s
117497856/267313385 [============>.................] - ETA: 13s
118087680/267313385 [============>.................] - ETA: 12s
118677504/267313385 [============>.................] - ETA: 12s
119267328/267313385 [============>.................] - ETA: 12s
119857152/267313385 [============>.................] - ETA: 12s
120430592/267313385 [============>.................] - ETA: 12s
121053184/267313385 [============>.................] - ETA: 12s
121643008/267313385 [============>.................] - ETA: 12s
122232832/267313385 [============>.................] - ETA: 12s
122822656/267313385 [============>.................] - ETA: 12s
123412480/267313385 [============>.................] - ETA: 12s
124002304/267313385 [============>.................] - ETA: 12s
124592128/267313385 [============>.................] - ETA: 12s
125181952/267313385 [=============>................] - ETA: 12s
125771776/267313385 [=============>................] - ETA: 12s
126361600/267313385 [=============>................] - ETA: 12s
126951424/267313385 [=============>................] - ETA: 12s
127541248/267313385 [=============>................] - ETA: 12s
128131072/267313385 [=============>................] - ETA: 12s
128720896/267313385 [=============>................] - ETA: 12s
129310720/267313385 [=============>................] - ETA: 11s
129900544/267313385 [=============>................] - ETA: 11s
130490368/267313385 [=============>................] - ETA: 11s
131080192/267313385 [=============>................] - ETA: 11s
131670016/267313385 [=============>................] - ETA: 11s
132259840/267313385 [=============>................] - ETA: 11s
132849664/267313385 [=============>................] - ETA: 11s
133439488/267313385 [=============>................] - ETA: 11s
134029312/267313385 [==============>...............] - ETA: 11s
134619136/267313385 [==============>...............] - ETA: 11s
135208960/267313385 [==============>...............] - ETA: 11s
135798784/267313385 [==============>...............] - ETA: 11s
136388608/267313385 [==============>...............] - ETA: 11s
136978432/267313385 [==============>...............] - ETA: 11s
137568256/267313385 [==============>...............] - ETA: 11s
138158080/267313385 [==============>...............] - ETA: 11s
138747904/267313385 [==============>...............] - ETA: 11s
139337728/267313385 [==============>...............] - ETA: 11s
139927552/267313385 [==============>...............] - ETA: 11s
140517376/267313385 [==============>...............] - ETA: 10s
141107200/267313385 [==============>...............] - ETA: 10s
141697024/267313385 [==============>...............] - ETA: 10s
142286848/267313385 [==============>...............] - ETA: 10s
142876672/267313385 [===============>..............] - ETA: 10s
143466496/267313385 [===============>..............] - ETA: 10s
144056320/267313385 [===============>..............] - ETA: 10s
144646144/267313385 [===============>..............] - ETA: 10s
145235968/267313385 [===============>..............] - ETA: 10s
145833984/267313385 [===============>..............] - ETA: 10s
146415616/267313385 [===============>..............] - ETA: 10s
147005440/267313385 [===============>..............] - ETA: 10s
147595264/267313385 [===============>..............] - ETA: 10s
148185088/267313385 [===============>..............] - ETA: 10s
148774912/267313385 [===============>..............] - ETA: 10s
149364736/267313385 [===============>..............] - ETA: 10s
149954560/267313385 [===============>..............] - ETA: 10s
150544384/267313385 [===============>..............] - ETA: 10s
151134208/267313385 [===============>..............] - ETA: 10s
151724032/267313385 [================>.............] - ETA: 10s
152313856/267313385 [================>.............] - ETA: 9s 
152903680/267313385 [================>.............] - ETA: 9s
153493504/267313385 [================>.............] - ETA: 9s
154083328/267313385 [================>.............] - ETA: 9s
154673152/267313385 [================>.............] - ETA: 9s
155262976/267313385 [================>.............] - ETA: 9s
155852800/267313385 [================>.............] - ETA: 9s
156442624/267313385 [================>.............] - ETA: 9s
157032448/267313385 [================>.............] - ETA: 9s
157622272/267313385 [================>.............] - ETA: 9s
158212096/267313385 [================>.............] - ETA: 9s
158785536/267313385 [================>.............] - ETA: 9s
159375360/267313385 [================>.............] - ETA: 9s
159965184/267313385 [================>.............] - ETA: 9s
160555008/267313385 [=================>............] - ETA: 9s
161144832/267313385 [=================>............] - ETA: 9s
161734656/267313385 [=================>............] - ETA: 9s
162324480/267313385 [=================>............] - ETA: 9s
162914304/267313385 [=================>............] - ETA: 9s
163504128/267313385 [=================>............] - ETA: 8s
164093952/267313385 [=================>............] - ETA: 8s
164683776/267313385 [=================>............] - ETA: 8s
165273600/267313385 [=================>............] - ETA: 8s
165863424/267313385 [=================>............] - ETA: 8s
166453248/267313385 [=================>............] - ETA: 8s
167043072/267313385 [=================>............] - ETA: 8s
167632896/267313385 [=================>............] - ETA: 8s
168222720/267313385 [=================>............] - ETA: 8s
168812544/267313385 [=================>............] - ETA: 8s
169402368/267313385 [==================>...........] - ETA: 8s
169992192/267313385 [==================>...........] - ETA: 8s
170582016/267313385 [==================>...........] - ETA: 8s
171171840/267313385 [==================>...........] - ETA: 8s
171761664/267313385 [==================>...........] - ETA: 8s
172351488/267313385 [==================>...........] - ETA: 8s
172941312/267313385 [==================>...........] - ETA: 8s
173531136/267313385 [==================>...........] - ETA: 8s
174120960/267313385 [==================>...........] - ETA: 8s
174710784/267313385 [==================>...........] - ETA: 8s
175300608/267313385 [==================>...........] - ETA: 7s
175890432/267313385 [==================>...........] - ETA: 7s
176480256/267313385 [==================>...........] - ETA: 7s
177070080/267313385 [==================>...........] - ETA: 7s
177659904/267313385 [==================>...........] - ETA: 7s
178233344/267313385 [===================>..........] - ETA: 7s
178806784/267313385 [===================>..........] - ETA: 7s
179412992/267313385 [===================>..........] - ETA: 7s
180002816/267313385 [===================>..........] - ETA: 7s
180576256/267313385 [===================>..........] - ETA: 7s
181149696/267313385 [===================>..........] - ETA: 7s
181706752/267313385 [===================>..........] - ETA: 7s
182263808/267313385 [===================>..........] - ETA: 7s
182820864/267313385 [===================>..........] - ETA: 7s
183377920/267313385 [===================>..........] - ETA: 7s
183934976/267313385 [===================>..........] - ETA: 7s
184475648/267313385 [===================>..........] - ETA: 7s
185032704/267313385 [===================>..........] - ETA: 7s
185589760/267313385 [===================>..........] - ETA: 7s
186163200/267313385 [===================>..........] - ETA: 7s
186736640/267313385 [===================>..........] - ETA: 6s
187293696/267313385 [====================>.........] - ETA: 6s
187850752/267313385 [====================>.........] - ETA: 6s
188407808/267313385 [====================>.........] - ETA: 6s
188964864/267313385 [====================>.........] - ETA: 6s
189521920/267313385 [====================>.........] - ETA: 6s
190078976/267313385 [====================>.........] - ETA: 6s
190636032/267313385 [====================>.........] - ETA: 6s
191193088/267313385 [====================>.........] - ETA: 6s
191733760/267313385 [====================>.........] - ETA: 6s
192307200/267313385 [====================>.........] - ETA: 6s
192847872/267313385 [====================>.........] - ETA: 6s
193404928/267313385 [====================>.........] - ETA: 6s
193978368/267313385 [====================>.........] - ETA: 6s
194551808/267313385 [====================>.........] - ETA: 6s
195125248/267313385 [====================>.........] - ETA: 6s
195698688/267313385 [====================>.........] - ETA: 6s
196272128/267313385 [=====================>........] - ETA: 6s
196845568/267313385 [=====================>........] - ETA: 6s
197402624/267313385 [=====================>........] - ETA: 6s
197976064/267313385 [=====================>........] - ETA: 6s
198533120/267313385 [=====================>........] - ETA: 5s
199106560/267313385 [=====================>........] - ETA: 5s
199696384/267313385 [=====================>........] - ETA: 5s
200286208/267313385 [=====================>........] - ETA: 5s
200876032/267313385 [=====================>........] - ETA: 5s
201465856/267313385 [=====================>........] - ETA: 5s
202055680/267313385 [=====================>........] - ETA: 5s
202645504/267313385 [=====================>........] - ETA: 5s
203235328/267313385 [=====================>........] - ETA: 5s
203825152/267313385 [=====================>........] - ETA: 5s
204414976/267313385 [=====================>........] - ETA: 5s
205004800/267313385 [======================>.......] - ETA: 5s
205594624/267313385 [======================>.......] - ETA: 5s
206184448/267313385 [======================>.......] - ETA: 5s
206774272/267313385 [======================>.......] - ETA: 5s
207364096/267313385 [======================>.......] - ETA: 5s
207953920/267313385 [======================>.......] - ETA: 5s
208543744/267313385 [======================>.......] - ETA: 5s
209125376/267313385 [======================>.......] - ETA: 5s
209707008/267313385 [======================>.......] - ETA: 4s
210296832/267313385 [======================>.......] - ETA: 4s
210886656/267313385 [======================>.......] - ETA: 4s
211476480/267313385 [======================>.......] - ETA: 4s
212066304/267313385 [======================>.......] - ETA: 4s
212656128/267313385 [======================>.......] - ETA: 4s
213245952/267313385 [======================>.......] - ETA: 4s
213835776/267313385 [======================>.......] - ETA: 4s
214425600/267313385 [=======================>......] - ETA: 4s
215031808/267313385 [=======================>......] - ETA: 4s
215621632/267313385 [=======================>......] - ETA: 4s
216211456/267313385 [=======================>......] - ETA: 4s
216801280/267313385 [=======================>......] - ETA: 4s
217391104/267313385 [=======================>......] - ETA: 4s
217980928/267313385 [=======================>......] - ETA: 4s
218570752/267313385 [=======================>......] - ETA: 4s
219160576/267313385 [=======================>......] - ETA: 4s
219750400/267313385 [=======================>......] - ETA: 4s
220340224/267313385 [=======================>......] - ETA: 4s
220930048/267313385 [=======================>......] - ETA: 4s
221519872/267313385 [=======================>......] - ETA: 3s
222109696/267313385 [=======================>......] - ETA: 3s
222699520/267313385 [=======================>......] - ETA: 3s
223289344/267313385 [========================>.....] - ETA: 3s
223879168/267313385 [========================>.....] - ETA: 3s
224468992/267313385 [========================>.....] - ETA: 3s
225058816/267313385 [========================>.....] - ETA: 3s
225648640/267313385 [========================>.....] - ETA: 3s
226238464/267313385 [========================>.....] - ETA: 3s
226828288/267313385 [========================>.....] - ETA: 3s
227418112/267313385 [========================>.....] - ETA: 3s
228007936/267313385 [========================>.....] - ETA: 3s
228597760/267313385 [========================>.....] - ETA: 3s
229187584/267313385 [========================>.....] - ETA: 3s
229777408/267313385 [========================>.....] - ETA: 3s
230367232/267313385 [========================>.....] - ETA: 3s
230957056/267313385 [========================>.....] - ETA: 3s
231546880/267313385 [========================>.....] - ETA: 3s
232136704/267313385 [=========================>....] - ETA: 3s
232726528/267313385 [=========================>....] - ETA: 2s
233316352/267313385 [=========================>....] - ETA: 2s
233889792/267313385 [=========================>....] - ETA: 2s
234479616/267313385 [=========================>....] - ETA: 2s
235069440/267313385 [=========================>....] - ETA: 2s
235659264/267313385 [=========================>....] - ETA: 2s
236249088/267313385 [=========================>....] - ETA: 2s
236838912/267313385 [=========================>....] - ETA: 2s
237428736/267313385 [=========================>....] - ETA: 2s
238002176/267313385 [=========================>....] - ETA: 2s
238575616/267313385 [=========================>....] - ETA: 2s
239165440/267313385 [=========================>....] - ETA: 2s
239755264/267313385 [=========================>....] - ETA: 2s
240345088/267313385 [=========================>....] - ETA: 2s
240902144/267313385 [==========================>...] - ETA: 2s
241491968/267313385 [==========================>...] - ETA: 2s
242081792/267313385 [==========================>...] - ETA: 2s
242671616/267313385 [==========================>...] - ETA: 2s
243261440/267313385 [==========================>...] - ETA: 2s
243851264/267313385 [==========================>...] - ETA: 2s
244441088/267313385 [==========================>...] - ETA: 1s
245030912/267313385 [==========================>...] - ETA: 1s
245604352/267313385 [==========================>...] - ETA: 1s
246210560/267313385 [==========================>...] - ETA: 1s
246800384/267313385 [==========================>...] - ETA: 1s
247390208/267313385 [==========================>...] - ETA: 1s
247980032/267313385 [==========================>...] - ETA: 1s
248569856/267313385 [==========================>...] - ETA: 1s
249159680/267313385 [==========================>...] - ETA: 1s
249749504/267313385 [===========================>..] - ETA: 1s
250339328/267313385 [===========================>..] - ETA: 1s
250929152/267313385 [===========================>..] - ETA: 1s
251518976/267313385 [===========================>..] - ETA: 1s
252108800/267313385 [===========================>..] - ETA: 1s
252698624/267313385 [===========================>..] - ETA: 1s
253288448/267313385 [===========================>..] - ETA: 1s
253878272/267313385 [===========================>..] - ETA: 1s
254468096/267313385 [===========================>..] - ETA: 1s
255057920/267313385 [===========================>..] - ETA: 1s
255647744/267313385 [===========================>..] - ETA: 1s
256253952/267313385 [===========================>..] - ETA: 0s
256843776/267313385 [===========================>..] - ETA: 0s
257433600/267313385 [===========================>..] - ETA: 0s
258023424/267313385 [===========================>..] - ETA: 0s
258613248/267313385 [============================>.] - ETA: 0s
259203072/267313385 [============================>.] - ETA: 0s
259792896/267313385 [============================>.] - ETA: 0s
260382720/267313385 [============================>.] - ETA: 0s
260980736/267313385 [============================>.] - ETA: 0s
261562368/267313385 [============================>.] - ETA: 0s
262152192/267313385 [============================>.] - ETA: 0s
262742016/267313385 [============================>.] - ETA: 0s
263331840/267313385 [============================>.] - ETA: 0s
263921664/267313385 [============================>.] - ETA: 0s
264511488/267313385 [============================>.] - ETA: 0s
265101312/267313385 [============================>.] - ETA: 0s
265691136/267313385 [============================>.] - ETA: 0s
266280960/267313385 [============================>.] - ETA: 0s
266608640/267313385 [============================>.] - ETA: 0s
267313385/267313385 [==============================] - 23s 0us/step
Download complete.

2. Load a pre-trained native TF-Keras model

The model used in this example is AkidaUNet. It has an AkidaNet (0.5) backbone to extract features combined with a succession of separable transposed convolutional blocks to build an image segmentation map. A pre-trained floating-point TF-Keras model is downloaded to save training time.

Note

  • The “transposed” convolutional feature is new in Akida 2.0.

  • The “separable transposed” operation is realized through the combination of a QuantizeML custom DepthwiseConv2DTranspose layer with a standard pointwise convolution.

The performance of the model is evaluated using both pixel accuracy and Binary IoU. The pixel accuracy describes how well the model can predict the segmentation mask pixel by pixel and the Binary IoU takes into account how close the predicted mask is to the ground truth.

from akida_models.model_io import load_model

# Retrieve the model file from Brainchip data server
model_file = fetch_file(fname="akida_unet_portrait128.h5",
                        origin="https://data.brainchip.com/models/AkidaV2/akida_unet/akida_unet_portrait128.h5",
                        cache_subdir='models')

# Load the native TF-Keras pre-trained model
model_keras = load_model(model_file)
model_keras.summary()
Downloading data from https://data.brainchip.com/models/AkidaV2/akida_unet/akida_unet_portrait128.h5.

      0/4501976 [..............................] - ETA: 0s
 245760/4501976 [>.............................] - ETA: 0s
 819200/4501976 [====>.........................] - ETA: 0s
1376256/4501976 [========>.....................] - ETA: 0s
1933312/4501976 [===========>..................] - ETA: 0s
2490368/4501976 [===============>..............] - ETA: 0s
3063808/4501976 [===================>..........] - ETA: 0s
3620864/4501976 [=======================>......] - ETA: 0s
4177920/4501976 [==========================>...] - ETA: 0s
4501976/4501976 [==============================] - 0s 0us/step
Download complete.
Model: "akida_unet"
_________________________________________________________________
 Layer (type)                Output Shape              Param #
=================================================================
 input (InputLayer)          [(None, 128, 128, 3)]     0

 rescaling (Rescaling)       (None, 128, 128, 3)       0

 conv_0 (Conv2D)             (None, 64, 64, 16)        432

 conv_0/BN (BatchNormalizat  (None, 64, 64, 16)        64
 ion)

 conv_0/relu (ReLU)          (None, 64, 64, 16)        0

 conv_1 (Conv2D)             (None, 64, 64, 32)        4608

 conv_1/BN (BatchNormalizat  (None, 64, 64, 32)        128
 ion)

 conv_1/relu (ReLU)          (None, 64, 64, 32)        0

 conv_2 (Conv2D)             (None, 32, 32, 64)        18432

 conv_2/BN (BatchNormalizat  (None, 32, 32, 64)        256
 ion)

 conv_2/relu (ReLU)          (None, 32, 32, 64)        0

 conv_3 (Conv2D)             (None, 32, 32, 64)        36864

 conv_3/BN (BatchNormalizat  (None, 32, 32, 64)        256
 ion)

 conv_3/relu (ReLU)          (None, 32, 32, 64)        0

 dw_separable_4 (DepthwiseC  (None, 16, 16, 64)        576
 onv2D)

 pw_separable_4 (Conv2D)     (None, 16, 16, 128)       8192

 pw_separable_4/BN (BatchNo  (None, 16, 16, 128)       512
 rmalization)

 pw_separable_4/relu (ReLU)  (None, 16, 16, 128)       0

 dw_separable_5 (DepthwiseC  (None, 16, 16, 128)       1152
 onv2D)

 pw_separable_5 (Conv2D)     (None, 16, 16, 128)       16384

 pw_separable_5/BN (BatchNo  (None, 16, 16, 128)       512
 rmalization)

 pw_separable_5/relu (ReLU)  (None, 16, 16, 128)       0

 dw_separable_6 (DepthwiseC  (None, 8, 8, 128)         1152
 onv2D)

 pw_separable_6 (Conv2D)     (None, 8, 8, 256)         32768

 pw_separable_6/BN (BatchNo  (None, 8, 8, 256)         1024
 rmalization)

 pw_separable_6/relu (ReLU)  (None, 8, 8, 256)         0

 dw_separable_7 (DepthwiseC  (None, 8, 8, 256)         2304
 onv2D)

 pw_separable_7 (Conv2D)     (None, 8, 8, 256)         65536

 pw_separable_7/BN (BatchNo  (None, 8, 8, 256)         1024
 rmalization)

 pw_separable_7/relu (ReLU)  (None, 8, 8, 256)         0

 dw_separable_8 (DepthwiseC  (None, 8, 8, 256)         2304
 onv2D)

 pw_separable_8 (Conv2D)     (None, 8, 8, 256)         65536

 pw_separable_8/BN (BatchNo  (None, 8, 8, 256)         1024
 rmalization)

 pw_separable_8/relu (ReLU)  (None, 8, 8, 256)         0

 dw_separable_9 (DepthwiseC  (None, 8, 8, 256)         2304
 onv2D)

 pw_separable_9 (Conv2D)     (None, 8, 8, 256)         65536

 pw_separable_9/BN (BatchNo  (None, 8, 8, 256)         1024
 rmalization)

 pw_separable_9/relu (ReLU)  (None, 8, 8, 256)         0

 dw_separable_10 (Depthwise  (None, 8, 8, 256)         2304
 Conv2D)

 pw_separable_10 (Conv2D)    (None, 8, 8, 256)         65536

 pw_separable_10/BN (BatchN  (None, 8, 8, 256)         1024
 ormalization)

 pw_separable_10/relu (ReLU  (None, 8, 8, 256)         0
 )

 dw_separable_11 (Depthwise  (None, 8, 8, 256)         2304
 Conv2D)

 pw_separable_11 (Conv2D)    (None, 8, 8, 256)         65536

 pw_separable_11/BN (BatchN  (None, 8, 8, 256)         1024
 ormalization)

 pw_separable_11/relu (ReLU  (None, 8, 8, 256)         0
 )

 dw_separable_12 (Depthwise  (None, 4, 4, 256)         2304
 Conv2D)

 pw_separable_12 (Conv2D)    (None, 4, 4, 512)         131072

 pw_separable_12/BN (BatchN  (None, 4, 4, 512)         2048
 ormalization)

 pw_separable_12/relu (ReLU  (None, 4, 4, 512)         0
 )

 dw_separable_13 (Depthwise  (None, 4, 4, 512)         4608
 Conv2D)

 pw_separable_13 (Conv2D)    (None, 4, 4, 512)         262144

 pw_separable_13/BN (BatchN  (None, 4, 4, 512)         2048
 ormalization)

 pw_separable_13/relu (ReLU  (None, 4, 4, 512)         0
 )

 dw_sepconv_t_0 (DepthwiseC  (None, 8, 8, 512)         5120
 onv2DTranspose)

 pw_sepconv_t_0 (Conv2D)     (None, 8, 8, 256)         131328

 pw_sepconv_t_0/BN (BatchNo  (None, 8, 8, 256)         1024
 rmalization)

 pw_sepconv_t_0/relu (ReLU)  (None, 8, 8, 256)         0

 dropout (Dropout)           (None, 8, 8, 256)         0

 dw_sepconv_t_1 (DepthwiseC  (None, 16, 16, 256)       2560
 onv2DTranspose)

 pw_sepconv_t_1 (Conv2D)     (None, 16, 16, 128)       32896

 pw_sepconv_t_1/BN (BatchNo  (None, 16, 16, 128)       512
 rmalization)

 pw_sepconv_t_1/relu (ReLU)  (None, 16, 16, 128)       0

 dropout_1 (Dropout)         (None, 16, 16, 128)       0

 dw_sepconv_t_2 (DepthwiseC  (None, 32, 32, 128)       1280
 onv2DTranspose)

 pw_sepconv_t_2 (Conv2D)     (None, 32, 32, 64)        8256

 pw_sepconv_t_2/BN (BatchNo  (None, 32, 32, 64)        256
 rmalization)

 pw_sepconv_t_2/relu (ReLU)  (None, 32, 32, 64)        0

 dropout_2 (Dropout)         (None, 32, 32, 64)        0

 dw_sepconv_t_3 (DepthwiseC  (None, 64, 64, 64)        640
 onv2DTranspose)

 pw_sepconv_t_3 (Conv2D)     (None, 64, 64, 32)        2080

 pw_sepconv_t_3/BN (BatchNo  (None, 64, 64, 32)        128
 rmalization)

 pw_sepconv_t_3/relu (ReLU)  (None, 64, 64, 32)        0

 dropout_3 (Dropout)         (None, 64, 64, 32)        0

 dw_sepconv_t_4 (DepthwiseC  (None, 128, 128, 32)      320
 onv2DTranspose)

 pw_sepconv_t_4 (Conv2D)     (None, 128, 128, 16)      528

 pw_sepconv_t_4/BN (BatchNo  (None, 128, 128, 16)      64
 rmalization)

 pw_sepconv_t_4/relu (ReLU)  (None, 128, 128, 16)      0

 dropout_4 (Dropout)         (None, 128, 128, 16)      0

 head (Conv2D)               (None, 128, 128, 1)       17

=================================================================
Total params: 1058865 (4.04 MB)
Trainable params: 1051889 (4.01 MB)
Non-trainable params: 6976 (27.25 KB)
_________________________________________________________________
from tf_keras.metrics import BinaryIoU

# Compile the native TF-Keras model (required to evaluate the metrics)
model_keras.compile(loss='binary_crossentropy', metrics=[BinaryIoU(), 'accuracy'])

# Check Keras model performance
_, biou, acc = model_keras.evaluate(x_val, y_val, steps=steps, verbose=0)

print(f"TF-Keras binary IoU / pixel accuracy: {biou:.4f} / {100*acc:.2f}%")
TF-Keras binary IoU / pixel accuracy: 0.9356 / 96.78%

3. Load a pre-trained quantized Keras model

The next step is to quantize and potentially perform Quantize Aware Training (QAT) on the TF-Keras model from the previous step. After the TF-Keras model is quantized to 8-bit for all weights and activations, QAT is used to maintain the performance of the quantized model. Again, a pre-trained model is downloaded to save runtime.

from akida_models import akida_unet_portrait128_pretrained

# Load the pre-trained quantized model
model_quantized_keras = akida_unet_portrait128_pretrained()
model_quantized_keras.summary()
Downloading data from https://data.brainchip.com/models/AkidaV2/akida_unet/akida_unet_portrait128_i8_w8_a8.h5.

      0/4527816 [..............................] - ETA: 0s
 147456/4527816 [..............................] - ETA: 1s
 696320/4527816 [===>..........................] - ETA: 0s
1286144/4527816 [=======>......................] - ETA: 0s
1875968/4527816 [===========>..................] - ETA: 0s
2465792/4527816 [===============>..............] - ETA: 0s
3055616/4527816 [===================>..........] - ETA: 0s
3645440/4527816 [=======================>......] - ETA: 0s
4235264/4527816 [===========================>..] - ETA: 0s
4527816/4527816 [==============================] - 0s 0us/step
Download complete.
Model: "akida_unet"
_________________________________________________________________
 Layer (type)                Output Shape              Param #
=================================================================
 input (InputLayer)          [(None, 128, 128, 3)]     0

 rescaling (QuantizedRescal  (None, 128, 128, 3)       0
 ing)

 conv_0 (QuantizedConv2D)    (None, 64, 64, 16)        448

 conv_0/relu (QuantizedReLU  (None, 64, 64, 16)        32
 )

 conv_1 (QuantizedConv2D)    (None, 64, 64, 32)        4640

 conv_1/relu (QuantizedReLU  (None, 64, 64, 32)        64
 )

 conv_2 (QuantizedConv2D)    (None, 32, 32, 64)        18496

 conv_2/relu (QuantizedReLU  (None, 32, 32, 64)        128
 )

 conv_3 (QuantizedConv2D)    (None, 32, 32, 64)        36928

 conv_3/relu (QuantizedReLU  (None, 32, 32, 64)        128
 )

 dw_separable_4 (QuantizedD  (None, 16, 16, 64)        704
 epthwiseConv2D)

 pw_separable_4 (QuantizedC  (None, 16, 16, 128)       8320
 onv2D)

 pw_separable_4/relu (Quant  (None, 16, 16, 128)       256
 izedReLU)

 dw_separable_5 (QuantizedD  (None, 16, 16, 128)       1408
 epthwiseConv2D)

 pw_separable_5 (QuantizedC  (None, 16, 16, 128)       16512
 onv2D)

 pw_separable_5/relu (Quant  (None, 16, 16, 128)       256
 izedReLU)

 dw_separable_6 (QuantizedD  (None, 8, 8, 128)         1408
 epthwiseConv2D)

 pw_separable_6 (QuantizedC  (None, 8, 8, 256)         33024
 onv2D)

 pw_separable_6/relu (Quant  (None, 8, 8, 256)         512
 izedReLU)

 dw_separable_7 (QuantizedD  (None, 8, 8, 256)         2816
 epthwiseConv2D)

 pw_separable_7 (QuantizedC  (None, 8, 8, 256)         65792
 onv2D)

 pw_separable_7/relu (Quant  (None, 8, 8, 256)         512
 izedReLU)

 dw_separable_8 (QuantizedD  (None, 8, 8, 256)         2816
 epthwiseConv2D)

 pw_separable_8 (QuantizedC  (None, 8, 8, 256)         65792
 onv2D)

 pw_separable_8/relu (Quant  (None, 8, 8, 256)         512
 izedReLU)

 dw_separable_9 (QuantizedD  (None, 8, 8, 256)         2816
 epthwiseConv2D)

 pw_separable_9 (QuantizedC  (None, 8, 8, 256)         65792
 onv2D)

 pw_separable_9/relu (Quant  (None, 8, 8, 256)         512
 izedReLU)

 dw_separable_10 (Quantized  (None, 8, 8, 256)         2816
 DepthwiseConv2D)

 pw_separable_10 (Quantized  (None, 8, 8, 256)         65792
 Conv2D)

 pw_separable_10/relu (Quan  (None, 8, 8, 256)         512
 tizedReLU)

 dw_separable_11 (Quantized  (None, 8, 8, 256)         2816
 DepthwiseConv2D)

 pw_separable_11 (Quantized  (None, 8, 8, 256)         65792
 Conv2D)

 pw_separable_11/relu (Quan  (None, 8, 8, 256)         512
 tizedReLU)

 dw_separable_12 (Quantized  (None, 4, 4, 256)         2816
 DepthwiseConv2D)

 pw_separable_12 (Quantized  (None, 4, 4, 512)         131584
 Conv2D)

 pw_separable_12/relu (Quan  (None, 4, 4, 512)         1024
 tizedReLU)

 dw_separable_13 (Quantized  (None, 4, 4, 512)         5632
 DepthwiseConv2D)

 pw_separable_13 (Quantized  (None, 4, 4, 512)         262656
 Conv2D)

 pw_separable_13/relu (Quan  (None, 4, 4, 512)         1024
 tizedReLU)

 dw_sepconv_t_0 (QuantizedD  (None, 8, 8, 512)         6144
 epthwiseConv2DTranspose)

 pw_sepconv_t_0 (QuantizedC  (None, 8, 8, 256)         131328
 onv2D)

 pw_sepconv_t_0/relu (Quant  (None, 8, 8, 256)         512
 izedReLU)

 dropout (QuantizedDropout)  (None, 8, 8, 256)         0

 dw_sepconv_t_1 (QuantizedD  (None, 16, 16, 256)       3072
 epthwiseConv2DTranspose)

 pw_sepconv_t_1 (QuantizedC  (None, 16, 16, 128)       32896
 onv2D)

 pw_sepconv_t_1/relu (Quant  (None, 16, 16, 128)       256
 izedReLU)

 dropout_1 (QuantizedDropou  (None, 16, 16, 128)       0
 t)

 dw_sepconv_t_2 (QuantizedD  (None, 32, 32, 128)       1536
 epthwiseConv2DTranspose)

 pw_sepconv_t_2 (QuantizedC  (None, 32, 32, 64)        8256
 onv2D)

 pw_sepconv_t_2/relu (Quant  (None, 32, 32, 64)        128
 izedReLU)

 dropout_2 (QuantizedDropou  (None, 32, 32, 64)        0
 t)

 dw_sepconv_t_3 (QuantizedD  (None, 64, 64, 64)        768
 epthwiseConv2DTranspose)

 pw_sepconv_t_3 (QuantizedC  (None, 64, 64, 32)        2080
 onv2D)

 pw_sepconv_t_3/relu (Quant  (None, 64, 64, 32)        64
 izedReLU)

 dropout_3 (QuantizedDropou  (None, 64, 64, 32)        0
 t)

 dw_sepconv_t_4 (QuantizedD  (None, 128, 128, 32)      384
 epthwiseConv2DTranspose)

 pw_sepconv_t_4 (QuantizedC  (None, 128, 128, 16)      528
 onv2D)

 pw_sepconv_t_4/relu (Quant  (None, 128, 128, 16)      32
 izedReLU)

 dropout_4 (QuantizedDropou  (None, 128, 128, 16)      0
 t)

 head (QuantizedConv2D)      (None, 128, 128, 1)       19

 dequantizer (Dequantizer)   (None, 128, 128, 1)       0

=================================================================
Total params: 1061603 (4.05 MB)
Trainable params: 1047905 (4.00 MB)
Non-trainable params: 13698 (53.51 KB)
_________________________________________________________________
# Compile the quantized TF-Keras model (required to evaluate the metrics)
model_quantized_keras.compile(loss='binary_crossentropy', metrics=[BinaryIoU(), 'accuracy'])

# Check Keras model performance
_, biou, acc = model_quantized_keras.evaluate(x_val, y_val, steps=steps, verbose=0)

print(f"TF-Keras quantized binary IoU / pixel accuracy: {biou:.4f} / {100*acc:.2f}%")
TF-Keras quantized binary IoU / pixel accuracy: 0.9344 / 96.77%

4. Conversion to Akida

Finally, the quantized TF-Keras model from the previous step is converted into an Akida model and its performance is evaluated. Note that the original performance of the TF-Keras floating-point model is maintained throughout the conversion process in this example.

from cnn2snn import convert

# Convert the model
model_akida = convert(model_quantized_keras)
model_akida.summary()
                  Model Summary
_________________________________________________
Input shape    Output shape   Sequences  Layers
=================================================
[128, 128, 3]  [128, 128, 1]  1          36
_________________________________________________

_____________________________________________________________________________
Layer (type)                               Output shape    Kernel shape

====================== SW/conv_0-dequantizer (Software) =====================

conv_0 (InputConv2D)                       [64, 64, 16]    (3, 3, 3, 16)
_____________________________________________________________________________
conv_1 (Conv2D)                            [64, 64, 32]    (3, 3, 16, 32)
_____________________________________________________________________________
conv_2 (Conv2D)                            [32, 32, 64]    (3, 3, 32, 64)
_____________________________________________________________________________
conv_3 (Conv2D)                            [32, 32, 64]    (3, 3, 64, 64)
_____________________________________________________________________________
dw_separable_4 (DepthwiseConv2D)           [16, 16, 64]    (3, 3, 64, 1)
_____________________________________________________________________________
pw_separable_4 (Conv2D)                    [16, 16, 128]   (1, 1, 64, 128)
_____________________________________________________________________________
dw_separable_5 (DepthwiseConv2D)           [16, 16, 128]   (3, 3, 128, 1)
_____________________________________________________________________________
pw_separable_5 (Conv2D)                    [16, 16, 128]   (1, 1, 128, 128)
_____________________________________________________________________________
dw_separable_6 (DepthwiseConv2D)           [8, 8, 128]     (3, 3, 128, 1)
_____________________________________________________________________________
pw_separable_6 (Conv2D)                    [8, 8, 256]     (1, 1, 128, 256)
_____________________________________________________________________________
dw_separable_7 (DepthwiseConv2D)           [8, 8, 256]     (3, 3, 256, 1)
_____________________________________________________________________________
pw_separable_7 (Conv2D)                    [8, 8, 256]     (1, 1, 256, 256)
_____________________________________________________________________________
dw_separable_8 (DepthwiseConv2D)           [8, 8, 256]     (3, 3, 256, 1)
_____________________________________________________________________________
pw_separable_8 (Conv2D)                    [8, 8, 256]     (1, 1, 256, 256)
_____________________________________________________________________________
dw_separable_9 (DepthwiseConv2D)           [8, 8, 256]     (3, 3, 256, 1)
_____________________________________________________________________________
pw_separable_9 (Conv2D)                    [8, 8, 256]     (1, 1, 256, 256)
_____________________________________________________________________________
dw_separable_10 (DepthwiseConv2D)          [8, 8, 256]     (3, 3, 256, 1)
_____________________________________________________________________________
pw_separable_10 (Conv2D)                   [8, 8, 256]     (1, 1, 256, 256)
_____________________________________________________________________________
dw_separable_11 (DepthwiseConv2D)          [8, 8, 256]     (3, 3, 256, 1)
_____________________________________________________________________________
pw_separable_11 (Conv2D)                   [8, 8, 256]     (1, 1, 256, 256)
_____________________________________________________________________________
dw_separable_12 (DepthwiseConv2D)          [4, 4, 256]     (3, 3, 256, 1)
_____________________________________________________________________________
pw_separable_12 (Conv2D)                   [4, 4, 512]     (1, 1, 256, 512)
_____________________________________________________________________________
dw_separable_13 (DepthwiseConv2D)          [4, 4, 512]     (3, 3, 512, 1)
_____________________________________________________________________________
pw_separable_13 (Conv2D)                   [4, 4, 512]     (1, 1, 512, 512)
_____________________________________________________________________________
dw_sepconv_t_0 (DepthwiseConv2DTranspose)  [8, 8, 512]     (3, 3, 512, 1)
_____________________________________________________________________________
pw_sepconv_t_0 (Conv2D)                    [8, 8, 256]     (1, 1, 512, 256)
_____________________________________________________________________________
dw_sepconv_t_1 (DepthwiseConv2DTranspose)  [16, 16, 256]   (3, 3, 256, 1)
_____________________________________________________________________________
pw_sepconv_t_1 (Conv2D)                    [16, 16, 128]   (1, 1, 256, 128)
_____________________________________________________________________________
dw_sepconv_t_2 (DepthwiseConv2DTranspose)  [32, 32, 128]   (3, 3, 128, 1)
_____________________________________________________________________________
pw_sepconv_t_2 (Conv2D)                    [32, 32, 64]    (1, 1, 128, 64)
_____________________________________________________________________________
dw_sepconv_t_3 (DepthwiseConv2DTranspose)  [64, 64, 64]    (3, 3, 64, 1)
_____________________________________________________________________________
pw_sepconv_t_3 (Conv2D)                    [64, 64, 32]    (1, 1, 64, 32)
_____________________________________________________________________________
dw_sepconv_t_4 (DepthwiseConv2DTranspose)  [128, 128, 32]  (3, 3, 32, 1)
_____________________________________________________________________________
pw_sepconv_t_4 (Conv2D)                    [128, 128, 16]  (1, 1, 32, 16)
_____________________________________________________________________________
head (Conv2D)                              [128, 128, 1]   (1, 1, 16, 1)
_____________________________________________________________________________
dequantizer (Dequantizer)                  [128, 128, 1]   N/A
_____________________________________________________________________________
import tf_keras as keras

# Check Akida model performance
labels, pots = None, None

for s in range(steps):
    batch = x_val[s * batch_size: (s + 1) * batch_size, :]
    label_batch = y_val[s * batch_size: (s + 1) * batch_size, :]
    pots_batch = model_akida.predict(batch.astype('uint8'))

    if labels is None:
        labels = label_batch
        pots = pots_batch
    else:
        labels = np.concatenate((labels, label_batch))
        pots = np.concatenate((pots, pots_batch))
preds = keras.activations.sigmoid(pots)

m_binary_iou = keras.metrics.BinaryIoU(target_class_ids=[0, 1], threshold=0.5)
m_binary_iou.update_state(labels, preds)
binary_iou = m_binary_iou.result().numpy()

m_accuracy = keras.metrics.Accuracy()
m_accuracy.update_state(labels, preds > 0.5)
accuracy = m_accuracy.result().numpy()
print(f"Akida binary IoU / pixel accuracy: {binary_iou:.4f} / {100*accuracy:.2f}%")

# For non-regression purpose
assert binary_iou > 0.9
Akida binary IoU / pixel accuracy: 0.9249 / 96.71%

5. Segment a single image

For visualization of the person segmentation performed by the Akida model, display a single image along with the segmentation produced by the original floating-point model and the ground truth segmentation.

import matplotlib.pyplot as plt

# Estimate age on a random single image and display TF-Keras and Akida outputs
sample = np.expand_dims(x_val[id, :], 0)
keras_out = model_keras(sample)
akida_out = keras.activations.sigmoid(model_akida.forward(sample.astype('uint8')))

fig, axs = plt.subplots(1, 3, constrained_layout=True)
axs[0].imshow(keras_out[0] * sample[0] / 255.)
axs[0].set_title('Keras segmentation', fontsize=10)
axs[0].axis('off')

axs[1].imshow(akida_out[0] * sample[0] / 255.)
axs[1].set_title('Akida segmentation', fontsize=10)
axs[1].axis('off')

axs[2].imshow(y_val[id] * sample[0] / 255.)
axs[2].set_title('Expected segmentation', fontsize=10)
axs[2].axis('off')

plt.show()
Keras segmentation, Akida segmentation, Expected segmentation

Total running time of the script: (2 minutes 15.379 seconds)

Gallery generated by Sphinx-Gallery