Note
Go to the end to download the full example code.
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()

Downloading data from https://data.brainchip.com/dataset-mirror/portrait128/val.tar.gz.
0/267313385 [..............................] - ETA: 0s
229376/267313385 [..............................] - ETA: 1:00
819200/267313385 [..............................] - ETA: 33s
1409024/267313385 [..............................] - ETA: 28s
1835008/267313385 [..............................] - ETA: 29s
2129920/267313385 [..............................] - ETA: 31s
2719744/267313385 [..............................] - ETA: 29s
2965504/267313385 [..............................] - ETA: 31s
3424256/267313385 [..............................] - ETA: 31s
4014080/267313385 [..............................] - ETA: 29s
4603904/267313385 [..............................] - ETA: 29s
5193728/267313385 [..............................] - ETA: 28s
5783552/267313385 [..............................] - ETA: 27s
6373376/267313385 [..............................] - ETA: 26s
6963200/267313385 [..............................] - ETA: 26s
7553024/267313385 [..............................] - ETA: 26s
8142848/267313385 [..............................] - ETA: 25s
8732672/267313385 [..............................] - ETA: 25s
9306112/267313385 [>.............................] - ETA: 25s
9895936/267313385 [>.............................] - ETA: 25s
10485760/267313385 [>.............................] - ETA: 24s
11075584/267313385 [>.............................] - ETA: 24s
11665408/267313385 [>.............................] - ETA: 24s
12189696/267313385 [>.............................] - ETA: 24s
12812288/267313385 [>.............................] - ETA: 24s
13402112/267313385 [>.............................] - ETA: 23s
13991936/267313385 [>.............................] - ETA: 23s
14581760/267313385 [>.............................] - ETA: 23s
15155200/267313385 [>.............................] - ETA: 23s
15745024/267313385 [>.............................] - ETA: 23s
16334848/267313385 [>.............................] - ETA: 23s
16924672/267313385 [>.............................] - ETA: 23s
17530880/267313385 [>.............................] - ETA: 23s
18120704/267313385 [=>............................] - ETA: 22s
18710528/267313385 [=>............................] - ETA: 22s
19283968/267313385 [=>............................] - ETA: 22s
19742720/267313385 [=>............................] - ETA: 22s
20480000/267313385 [=>............................] - ETA: 22s
21086208/267313385 [=>............................] - ETA: 22s
21659648/267313385 [=>............................] - ETA: 22s
22249472/267313385 [=>............................] - ETA: 22s
22839296/267313385 [=>............................] - ETA: 22s
23429120/267313385 [=>............................] - ETA: 22s
24018944/267313385 [=>............................] - ETA: 22s
24608768/267313385 [=>............................] - ETA: 21s
25198592/267313385 [=>............................] - ETA: 21s
25788416/267313385 [=>............................] - ETA: 21s
26378240/267313385 [=>............................] - ETA: 21s
26968064/267313385 [==>...........................] - ETA: 21s
27557888/267313385 [==>...........................] - ETA: 21s
28147712/267313385 [==>...........................] - ETA: 21s
28737536/267313385 [==>...........................] - ETA: 21s
29294592/267313385 [==>...........................] - ETA: 21s
29343744/267313385 [==>...........................] - ETA: 22s
30294016/267313385 [==>...........................] - ETA: 22s
32055296/267313385 [==>...........................] - ETA: 21s
32636928/267313385 [==>...........................] - ETA: 21s
33226752/267313385 [==>...........................] - ETA: 21s
33701888/267313385 [==>...........................] - ETA: 21s
34291712/267313385 [==>...........................] - ETA: 20s
34881536/267313385 [==>...........................] - ETA: 20s
35471360/267313385 [==>...........................] - ETA: 20s
36061184/267313385 [===>..........................] - ETA: 20s
36651008/267313385 [===>..........................] - ETA: 20s
37240832/267313385 [===>..........................] - ETA: 20s
37830656/267313385 [===>..........................] - ETA: 20s
38420480/267313385 [===>..........................] - ETA: 20s
39010304/267313385 [===>..........................] - ETA: 20s
39600128/267313385 [===>..........................] - ETA: 20s
40189952/267313385 [===>..........................] - ETA: 20s
40779776/267313385 [===>..........................] - ETA: 20s
41369600/267313385 [===>..........................] - ETA: 20s
41959424/267313385 [===>..........................] - ETA: 20s
42549248/267313385 [===>..........................] - ETA: 20s
43139072/267313385 [===>..........................] - ETA: 19s
43728896/267313385 [===>..........................] - ETA: 19s
44318720/267313385 [===>..........................] - ETA: 19s
44908544/267313385 [====>.........................] - ETA: 19s
45498368/267313385 [====>.........................] - ETA: 19s
46088192/267313385 [====>.........................] - ETA: 19s
46678016/267313385 [====>.........................] - ETA: 19s
47267840/267313385 [====>.........................] - ETA: 19s
47857664/267313385 [====>.........................] - ETA: 19s
48447488/267313385 [====>.........................] - ETA: 19s
49037312/267313385 [====>.........................] - ETA: 19s
49627136/267313385 [====>.........................] - ETA: 19s
50216960/267313385 [====>.........................] - ETA: 19s
50806784/267313385 [====>.........................] - ETA: 19s
51396608/267313385 [====>.........................] - ETA: 19s
51986432/267313385 [====>.........................] - ETA: 19s
52576256/267313385 [====>.........................] - ETA: 18s
53133312/267313385 [====>.........................] - ETA: 18s
53739520/267313385 [=====>........................] - ETA: 18s
54329344/267313385 [=====>........................] - ETA: 18s
54919168/267313385 [=====>........................] - ETA: 18s
55508992/267313385 [=====>........................] - ETA: 18s
56098816/267313385 [=====>........................] - ETA: 18s
56688640/267313385 [=====>........................] - ETA: 18s
57278464/267313385 [=====>........................] - ETA: 18s
57868288/267313385 [=====>........................] - ETA: 18s
58458112/267313385 [=====>........................] - ETA: 18s
59015168/267313385 [=====>........................] - ETA: 18s
59670528/267313385 [=====>........................] - ETA: 18s
60260352/267313385 [=====>........................] - ETA: 18s
60850176/267313385 [=====>........................] - ETA: 18s
61440000/267313385 [=====>........................] - ETA: 18s
62029824/267313385 [=====>........................] - ETA: 18s
62619648/267313385 [======>.......................] - ETA: 18s
63209472/267313385 [======>.......................] - ETA: 17s
63799296/267313385 [======>.......................] - ETA: 17s
64389120/267313385 [======>.......................] - ETA: 17s
64978944/267313385 [======>.......................] - ETA: 17s
65568768/267313385 [======>.......................] - ETA: 17s
66158592/267313385 [======>.......................] - ETA: 17s
66568192/267313385 [======>.......................] - ETA: 17s
67158016/267313385 [======>.......................] - ETA: 17s
67747840/267313385 [======>.......................] - ETA: 17s
68337664/267313385 [======>.......................] - ETA: 17s
68927488/267313385 [======>.......................] - ETA: 17s
69517312/267313385 [======>.......................] - ETA: 17s
70107136/267313385 [======>.......................] - ETA: 17s
70680576/267313385 [======>.......................] - ETA: 17s
71270400/267313385 [======>.......................] - ETA: 17s
71860224/267313385 [=======>......................] - ETA: 17s
72433664/267313385 [=======>......................] - ETA: 17s
73039872/267313385 [=======>......................] - ETA: 17s
73629696/267313385 [=======>......................] - ETA: 17s
74219520/267313385 [=======>......................] - ETA: 16s
74809344/267313385 [=======>......................] - ETA: 16s
75431936/267313385 [=======>......................] - ETA: 16s
76021760/267313385 [=======>......................] - ETA: 16s
76611584/267313385 [=======>......................] - ETA: 16s
77201408/267313385 [=======>......................] - ETA: 16s
77791232/267313385 [=======>......................] - ETA: 16s
78381056/267313385 [=======>......................] - ETA: 16s
78970880/267313385 [=======>......................] - ETA: 16s
79560704/267313385 [=======>......................] - ETA: 16s
80150528/267313385 [=======>......................] - ETA: 16s
80740352/267313385 [========>.....................] - ETA: 16s
81330176/267313385 [========>.....................] - ETA: 16s
81920000/267313385 [========>.....................] - ETA: 16s
82509824/267313385 [========>.....................] - ETA: 16s
83099648/267313385 [========>.....................] - ETA: 16s
83673088/267313385 [========>.....................] - ETA: 16s
84262912/267313385 [========>.....................] - ETA: 16s
84852736/267313385 [========>.....................] - ETA: 15s
85442560/267313385 [========>.....................] - ETA: 15s
86032384/267313385 [========>.....................] - ETA: 15s
86622208/267313385 [========>.....................] - ETA: 15s
87212032/267313385 [========>.....................] - ETA: 15s
87801856/267313385 [========>.....................] - ETA: 15s
88391680/267313385 [========>.....................] - ETA: 15s
88981504/267313385 [========>.....................] - ETA: 15s
89571328/267313385 [=========>....................] - ETA: 15s
90161152/267313385 [=========>....................] - ETA: 15s
90750976/267313385 [=========>....................] - ETA: 15s
91340800/267313385 [=========>....................] - ETA: 15s
91930624/267313385 [=========>....................] - ETA: 15s
92438528/267313385 [=========>....................] - ETA: 15s
92454912/267313385 [=========>....................] - ETA: 15s
94568448/267313385 [=========>....................] - ETA: 15s
95125504/267313385 [=========>....................] - ETA: 15s
95682560/267313385 [=========>....................] - ETA: 15s
96239616/267313385 [=========>....................] - ETA: 14s
96698368/267313385 [=========>....................] - ETA: 14s
97255424/267313385 [=========>....................] - ETA: 14s
97796096/267313385 [=========>....................] - ETA: 14s
98353152/267313385 [==========>...................] - ETA: 14s
98910208/267313385 [==========>...................] - ETA: 14s
99418112/267313385 [==========>...................] - ETA: 14s
99991552/267313385 [==========>...................] - ETA: 14s
100532224/267313385 [==========>...................] - ETA: 14s
101089280/267313385 [==========>...................] - ETA: 14s
101662720/267313385 [==========>...................] - ETA: 14s
102227968/267313385 [==========>...................] - ETA: 14s
102793216/267313385 [==========>...................] - ETA: 14s
103350272/267313385 [==========>...................] - ETA: 14s
103874560/267313385 [==========>...................] - ETA: 14s
104497152/267313385 [==========>...................] - ETA: 14s
105054208/267313385 [==========>...................] - ETA: 14s
105611264/267313385 [==========>...................] - ETA: 14s
106168320/267313385 [==========>...................] - ETA: 14s
106725376/267313385 [==========>...................] - ETA: 14s
107282432/267313385 [===========>..................] - ETA: 14s
107839488/267313385 [===========>..................] - ETA: 14s
108396544/267313385 [===========>..................] - ETA: 14s
108953600/267313385 [===========>..................] - ETA: 13s
109494272/267313385 [===========>..................] - ETA: 13s
109953024/267313385 [===========>..................] - ETA: 13s
110575616/267313385 [===========>..................] - ETA: 13s
111165440/267313385 [===========>..................] - ETA: 13s
111755264/267313385 [===========>..................] - ETA: 13s
112345088/267313385 [===========>..................] - ETA: 13s
112951296/267313385 [===========>..................] - ETA: 13s
113541120/267313385 [===========>..................] - ETA: 13s
114147328/267313385 [===========>..................] - ETA: 13s
114737152/267313385 [===========>..................] - ETA: 13s
115326976/267313385 [===========>..................] - ETA: 13s
115916800/267313385 [============>.................] - ETA: 13s
116506624/267313385 [============>.................] - ETA: 13s
117096448/267313385 [============>.................] - ETA: 13s
117686272/267313385 [============>.................] - ETA: 13s
118276096/267313385 [============>.................] - ETA: 13s
118865920/267313385 [============>.................] - ETA: 13s
119455744/267313385 [============>.................] - ETA: 13s
120045568/267313385 [============>.................] - ETA: 12s
120553472/267313385 [============>.................] - ETA: 12s
121208832/267313385 [============>.................] - ETA: 12s
121798656/267313385 [============>.................] - ETA: 12s
122388480/267313385 [============>.................] - ETA: 12s
122978304/267313385 [============>.................] - ETA: 12s
123568128/267313385 [============>.................] - ETA: 12s
124141568/267313385 [============>.................] - ETA: 12s
124731392/267313385 [============>.................] - ETA: 12s
125321216/267313385 [=============>................] - ETA: 12s
125894656/267313385 [=============>................] - ETA: 12s
126451712/267313385 [=============>................] - ETA: 12s
126894080/267313385 [=============>................] - ETA: 12s
127614976/267313385 [=============>................] - ETA: 12s
128172032/267313385 [=============>................] - ETA: 12s
128729088/267313385 [=============>................] - ETA: 12s
129302528/267313385 [=============>................] - ETA: 12s
129908736/267313385 [=============>................] - ETA: 12s
130498560/267313385 [=============>................] - ETA: 12s
131088384/267313385 [=============>................] - ETA: 11s
131678208/267313385 [=============>................] - ETA: 11s
132235264/267313385 [=============>................] - ETA: 11s
132874240/267313385 [=============>................] - ETA: 11s
133464064/267313385 [=============>................] - ETA: 11s
134037504/267313385 [==============>...............] - ETA: 11s
134643712/267313385 [==============>...............] - ETA: 11s
135217152/267313385 [==============>...............] - ETA: 11s
135839744/267313385 [==============>...............] - ETA: 11s
136429568/267313385 [==============>...............] - ETA: 11s
137019392/267313385 [==============>...............] - ETA: 11s
137609216/267313385 [==============>...............] - ETA: 11s
138199040/267313385 [==============>...............] - ETA: 11s
138788864/267313385 [==============>...............] - ETA: 11s
139378688/267313385 [==============>...............] - ETA: 11s
139968512/267313385 [==============>...............] - ETA: 11s
140558336/267313385 [==============>...............] - ETA: 11s
141148160/267313385 [==============>...............] - ETA: 11s
141737984/267313385 [==============>...............] - ETA: 11s
142327808/267313385 [==============>...............] - ETA: 10s
142917632/267313385 [===============>..............] - ETA: 10s
143507456/267313385 [===============>..............] - ETA: 10s
144097280/267313385 [===============>..............] - ETA: 10s
144687104/267313385 [===============>..............] - ETA: 10s
145276928/267313385 [===============>..............] - ETA: 10s
145866752/267313385 [===============>..............] - ETA: 10s
146456576/267313385 [===============>..............] - ETA: 10s
147046400/267313385 [===============>..............] - ETA: 10s
147374080/267313385 [===============>..............] - ETA: 10s
147472384/267313385 [===============>..............] - ETA: 10s
148340736/267313385 [===============>..............] - ETA: 10s
149979136/267313385 [===============>..............] - ETA: 10s
150536192/267313385 [===============>..............] - ETA: 10s
151158784/267313385 [===============>..............] - ETA: 10s
151748608/267313385 [================>.............] - ETA: 10s
152338432/267313385 [================>.............] - ETA: 10s
152928256/267313385 [================>.............] - ETA: 10s
153518080/267313385 [================>.............] - ETA: 9s
154091520/267313385 [================>.............] - ETA: 9s
154681344/267313385 [================>.............] - ETA: 9s
155271168/267313385 [================>.............] - ETA: 9s
155860992/267313385 [================>.............] - ETA: 9s
156450816/267313385 [================>.............] - ETA: 9s
157040640/267313385 [================>.............] - ETA: 9s
157630464/267313385 [================>.............] - ETA: 9s
158056448/267313385 [================>.............] - ETA: 9s
158646272/267313385 [================>.............] - ETA: 9s
159236096/267313385 [================>.............] - ETA: 9s
159825920/267313385 [================>.............] - ETA: 9s
160415744/267313385 [=================>............] - ETA: 9s
161005568/267313385 [=================>............] - ETA: 9s
161595392/267313385 [=================>............] - ETA: 9s
162185216/267313385 [=================>............] - ETA: 9s
162775040/267313385 [=================>............] - ETA: 9s
163364864/267313385 [=================>............] - ETA: 9s
163938304/267313385 [=================>............] - ETA: 9s
164528128/267313385 [=================>............] - ETA: 9s
165117952/267313385 [=================>............] - ETA: 8s
165707776/267313385 [=================>............] - ETA: 8s
166297600/267313385 [=================>............] - ETA: 8s
166887424/267313385 [=================>............] - ETA: 8s
167477248/267313385 [=================>............] - ETA: 8s
168067072/267313385 [=================>............] - ETA: 8s
168656896/267313385 [=================>............] - ETA: 8s
169246720/267313385 [=================>............] - ETA: 8s
169803776/267313385 [==================>...........] - ETA: 8s
170426368/267313385 [==================>...........] - ETA: 8s
171016192/267313385 [==================>...........] - ETA: 8s
171606016/267313385 [==================>...........] - ETA: 8s
172195840/267313385 [==================>...........] - ETA: 8s
172785664/267313385 [==================>...........] - ETA: 8s
173375488/267313385 [==================>...........] - ETA: 8s
173965312/267313385 [==================>...........] - ETA: 8s
174555136/267313385 [==================>...........] - ETA: 8s
175144960/267313385 [==================>...........] - ETA: 8s
175734784/267313385 [==================>...........] - ETA: 8s
176324608/267313385 [==================>...........] - ETA: 7s
176914432/267313385 [==================>...........] - ETA: 7s
177504256/267313385 [==================>...........] - ETA: 7s
177913856/267313385 [==================>...........] - ETA: 7s
178192384/267313385 [==================>...........] - ETA: 7s
178782208/267313385 [===================>..........] - ETA: 7s
179388416/267313385 [===================>..........] - ETA: 7s
179978240/267313385 [===================>..........] - ETA: 7s
180568064/267313385 [===================>..........] - ETA: 7s
181059584/267313385 [===================>..........] - ETA: 7s
181469184/267313385 [===================>..........] - ETA: 7s
182067200/267313385 [===================>..........] - ETA: 7s
182640640/267313385 [===================>..........] - ETA: 7s
183205888/267313385 [===================>..........] - ETA: 7s
183812096/267313385 [===================>..........] - ETA: 7s
184401920/267313385 [===================>..........] - ETA: 7s
184991744/267313385 [===================>..........] - ETA: 7s
185581568/267313385 [===================>..........] - ETA: 7s
186171392/267313385 [===================>..........] - ETA: 7s
186761216/267313385 [===================>..........] - ETA: 7s
187351040/267313385 [====================>.........] - ETA: 7s
187809792/267313385 [====================>.........] - ETA: 6s
188383232/267313385 [====================>.........] - ETA: 6s
188973056/267313385 [====================>.........] - ETA: 6s
189382656/267313385 [====================>.........] - ETA: 6s
189825024/267313385 [====================>.........] - ETA: 6s
190414848/267313385 [====================>.........] - ETA: 6s
191004672/267313385 [====================>.........] - ETA: 6s
191594496/267313385 [====================>.........] - ETA: 6s
192167936/267313385 [====================>.........] - ETA: 6s
192724992/267313385 [====================>.........] - ETA: 6s
193298432/267313385 [====================>.........] - ETA: 6s
193888256/267313385 [====================>.........] - ETA: 6s
194478080/267313385 [====================>.........] - ETA: 6s
195051520/267313385 [====================>.........] - ETA: 6s
195624960/267313385 [====================>.........] - ETA: 6s
196198400/267313385 [=====================>........] - ETA: 6s
196788224/267313385 [=====================>........] - ETA: 6s
197312512/267313385 [=====================>........] - ETA: 6s
198017024/267313385 [=====================>........] - ETA: 6s
198590464/267313385 [=====================>........] - ETA: 6s
199147520/267313385 [=====================>........] - ETA: 6s
199770112/267313385 [=====================>........] - ETA: 5s
200343552/267313385 [=====================>........] - ETA: 5s
200949760/267313385 [=====================>........] - ETA: 5s
201555968/267313385 [=====================>........] - ETA: 5s
202145792/267313385 [=====================>........] - ETA: 5s
202588160/267313385 [=====================>........] - ETA: 5s
203603968/267313385 [=====================>........] - ETA: 5s
204193792/267313385 [=====================>........] - ETA: 5s
204783616/267313385 [=====================>........] - ETA: 5s
205373440/267313385 [======================>.......] - ETA: 5s
205963264/267313385 [======================>.......] - ETA: 5s
206553088/267313385 [======================>.......] - ETA: 5s
207142912/267313385 [======================>.......] - ETA: 5s
207716352/267313385 [======================>.......] - ETA: 5s
208289792/267313385 [======================>.......] - ETA: 5s
208863232/267313385 [======================>.......] - ETA: 5s
209420288/267313385 [======================>.......] - ETA: 5s
209977344/267313385 [======================>.......] - ETA: 5s
210534400/267313385 [======================>.......] - ETA: 4s
211091456/267313385 [======================>.......] - ETA: 4s
211648512/267313385 [======================>.......] - ETA: 4s
212205568/267313385 [======================>.......] - ETA: 4s
212762624/267313385 [======================>.......] - ETA: 4s
213319680/267313385 [======================>.......] - ETA: 4s
213893120/267313385 [=======================>......] - ETA: 4s
214351872/267313385 [=======================>......] - ETA: 4s
214990848/267313385 [=======================>......] - ETA: 4s
215384064/267313385 [=======================>......] - ETA: 4s
216104960/267313385 [=======================>......] - ETA: 4s
216662016/267313385 [=======================>......] - ETA: 4s
217235456/267313385 [=======================>......] - ETA: 4s
217776128/267313385 [=======================>......] - ETA: 4s
218333184/267313385 [=======================>......] - ETA: 4s
218890240/267313385 [=======================>......] - ETA: 4s
219447296/267313385 [=======================>......] - ETA: 4s
220004352/267313385 [=======================>......] - ETA: 4s
220561408/267313385 [=======================>......] - ETA: 4s
221118464/267313385 [=======================>......] - ETA: 4s
221675520/267313385 [=======================>......] - ETA: 4s
222248960/267313385 [=======================>......] - ETA: 3s
222806016/267313385 [========================>.....] - ETA: 3s
223363072/267313385 [========================>.....] - ETA: 3s
223870976/267313385 [========================>.....] - ETA: 3s
224411648/267313385 [========================>.....] - ETA: 3s
224968704/267313385 [========================>.....] - ETA: 3s
225460224/267313385 [========================>.....] - ETA: 3s
225656832/267313385 [========================>.....] - ETA: 3s
227311616/267313385 [========================>.....] - ETA: 3s
227901440/267313385 [========================>.....] - ETA: 3s
228409344/267313385 [========================>.....] - ETA: 3s
228999168/267313385 [========================>.....] - ETA: 3s
229588992/267313385 [========================>.....] - ETA: 3s
230178816/267313385 [========================>.....] - ETA: 3s
230768640/267313385 [========================>.....] - ETA: 3s
231358464/267313385 [========================>.....] - ETA: 3s
231948288/267313385 [=========================>....] - ETA: 3s
232538112/267313385 [=========================>....] - ETA: 3s
233127936/267313385 [=========================>....] - ETA: 3s
233717760/267313385 [=========================>....] - ETA: 2s
234307584/267313385 [=========================>....] - ETA: 2s
234897408/267313385 [=========================>....] - ETA: 2s
235487232/267313385 [=========================>....] - ETA: 2s
236077056/267313385 [=========================>....] - ETA: 2s
236650496/267313385 [=========================>....] - ETA: 2s
237240320/267313385 [=========================>....] - ETA: 2s
237830144/267313385 [=========================>....] - ETA: 2s
238419968/267313385 [=========================>....] - ETA: 2s
239009792/267313385 [=========================>....] - ETA: 2s
239599616/267313385 [=========================>....] - ETA: 2s
240189440/267313385 [=========================>....] - ETA: 2s
240779264/267313385 [==========================>...] - ETA: 2s
241369088/267313385 [==========================>...] - ETA: 2s
241958912/267313385 [==========================>...] - ETA: 2s
242548736/267313385 [==========================>...] - ETA: 2s
243138560/267313385 [==========================>...] - ETA: 2s
243728384/267313385 [==========================>...] - ETA: 2s
244318208/267313385 [==========================>...] - ETA: 2s
244908032/267313385 [==========================>...] - ETA: 1s
245497856/267313385 [==========================>...] - ETA: 1s
246087680/267313385 [==========================>...] - ETA: 1s
246661120/267313385 [==========================>...] - ETA: 1s
247267328/267313385 [==========================>...] - ETA: 1s
247857152/267313385 [==========================>...] - ETA: 1s
248446976/267313385 [==========================>...] - ETA: 1s
248807424/267313385 [==========================>...] - ETA: 1s
249184256/267313385 [==========================>...] - ETA: 1s
249774080/267313385 [===========================>..] - ETA: 1s
250363904/267313385 [===========================>..] - ETA: 1s
250937344/267313385 [===========================>..] - ETA: 1s
251543552/267313385 [===========================>..] - ETA: 1s
252133376/267313385 [===========================>..] - ETA: 1s
252723200/267313385 [===========================>..] - ETA: 1s
253313024/267313385 [===========================>..] - ETA: 1s
253902848/267313385 [===========================>..] - ETA: 1s
254492672/267313385 [===========================>..] - ETA: 1s
255082496/267313385 [===========================>..] - ETA: 1s
255672320/267313385 [===========================>..] - ETA: 1s
256262144/267313385 [===========================>..] - ETA: 0s
256851968/267313385 [===========================>..] - ETA: 0s
257441792/267313385 [===========================>..] - ETA: 0s
258031616/267313385 [===========================>..] - ETA: 0s
258621440/267313385 [============================>.] - ETA: 0s
259211264/267313385 [============================>.] - ETA: 0s
259801088/267313385 [============================>.] - ETA: 0s
260390912/267313385 [============================>.] - ETA: 0s
260980736/267313385 [============================>.] - ETA: 0s
261570560/267313385 [============================>.] - ETA: 0s
262160384/267313385 [============================>.] - ETA: 0s
262750208/267313385 [============================>.] - ETA: 0s
263340032/267313385 [============================>.] - ETA: 0s
263929856/267313385 [============================>.] - ETA: 0s
264519680/267313385 [============================>.] - ETA: 0s
265109504/267313385 [============================>.] - ETA: 0s
265699328/267313385 [============================>.] - ETA: 0s
266289152/267313385 [============================>.] - ETA: 0s
266878976/267313385 [============================>.] - ETA: 0s
267313385/267313385 [==============================] - 24s 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
139264/4501976 [..............................] - ETA: 1s
663552/4501976 [===>..........................] - ETA: 0s
1253376/4501976 [=======>......................] - ETA: 0s
1843200/4501976 [===========>..................] - ETA: 0s
2433024/4501976 [===============>..............] - ETA: 0s
3022848/4501976 [===================>..........] - ETA: 0s
3612672/4501976 [=======================>......] - ETA: 0s
4202496/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
8192/4527816 [..............................] - ETA: 1:04
40960/4527816 [..............................] - ETA: 28s
98304/4527816 [..............................] - ETA: 16s
188416/4527816 [>.............................] - ETA: 10s
278528/4527816 [>.............................] - ETA: 8s
360448/4527816 [=>............................] - ETA: 7s
458752/4527816 [==>...........................] - ETA: 6s
557056/4527816 [==>...........................] - ETA: 5s
671744/4527816 [===>..........................] - ETA: 5s
786432/4527816 [====>.........................] - ETA: 4s
901120/4527816 [====>.........................] - ETA: 4s
1032192/4527816 [=====>........................] - ETA: 4s
1146880/4527816 [======>.......................] - ETA: 3s
1294336/4527816 [=======>......................] - ETA: 3s
1425408/4527816 [========>.....................] - ETA: 3s
1589248/4527816 [=========>....................] - ETA: 3s
1736704/4527816 [==========>...................] - ETA: 2s
1900544/4527816 [===========>..................] - ETA: 2s
2080768/4527816 [============>.................] - ETA: 2s
2260992/4527816 [=============>................] - ETA: 2s
2457600/4527816 [===============>..............] - ETA: 1s
2654208/4527816 [================>.............] - ETA: 1s
2867200/4527816 [=================>............] - ETA: 1s
3096576/4527816 [===================>..........] - ETA: 1s
3325952/4527816 [=====================>........] - ETA: 0s
3571712/4527816 [======================>.......] - ETA: 0s
3817472/4527816 [========================>.....] - ETA: 0s
4079616/4527816 [==========================>...] - ETA: 0s
4096000/4527816 [==========================>...] - ETA: 0s
4358144/4527816 [===========================>..] - ETA: 0s
4374528/4527816 [===========================>..] - ETA: 0s
4527816/4527816 [==============================] - 3s 1us/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()

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