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 keras floating point model, a keras quantized and Quantization Aware Trained (QAT) model, and an Akida-converted model. Notice that the performance of the original 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

id = np.random.randint(0, x_val.shape[0])

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
   114688/267313385 [..............................] - ETA: 1:59
   630784/267313385 [..............................] - ETA: 43s 
  1179648/267313385 [..............................] - ETA: 34s
  1744896/267313385 [..............................] - ETA: 30s
  2318336/267313385 [..............................] - ETA: 28s
  2891776/267313385 [..............................] - ETA: 27s
  3424256/267313385 [..............................] - ETA: 27s
  3964928/267313385 [..............................] - ETA: 26s
  4513792/267313385 [..............................] - ETA: 26s
  5013504/267313385 [..............................] - ETA: 26s
  5455872/267313385 [..............................] - ETA: 26s
  5988352/267313385 [..............................] - ETA: 26s
  6529024/267313385 [..............................] - ETA: 26s
  7094272/267313385 [..............................] - ETA: 26s
  7651328/267313385 [..............................] - ETA: 25s
  8200192/267313385 [..............................] - ETA: 25s
  8765440/267313385 [..............................] - ETA: 25s
  9322496/267313385 [>.............................] - ETA: 25s
  9912320/267313385 [>.............................] - ETA: 25s
 10502144/267313385 [>.............................] - ETA: 24s
 11091968/267313385 [>.............................] - ETA: 24s
 11689984/267313385 [>.............................] - ETA: 24s
 12279808/267313385 [>.............................] - ETA: 24s
 12869632/267313385 [>.............................] - ETA: 24s
 13385728/267313385 [>.............................] - ETA: 24s
 13647872/267313385 [>.............................] - ETA: 24s
 14073856/267313385 [>.............................] - ETA: 24s
 14368768/267313385 [>.............................] - ETA: 26s
 15712256/267313385 [>.............................] - ETA: 25s
 15745024/267313385 [>.............................] - ETA: 26s
 16793600/267313385 [>.............................] - ETA: 25s
 16941056/267313385 [>.............................] - ETA: 26s
 17350656/267313385 [>.............................] - ETA: 26s
 17563648/267313385 [>.............................] - ETA: 26s
 17924096/267313385 [=>............................] - ETA: 27s
 18268160/267313385 [=>............................] - ETA: 27s
 18448384/267313385 [=>............................] - ETA: 27s
 18907136/267313385 [=>............................] - ETA: 27s
 19316736/267313385 [=>............................] - ETA: 27s
 19447808/267313385 [=>............................] - ETA: 28s
 19660800/267313385 [=>............................] - ETA: 28s
 20021248/267313385 [=>............................] - ETA: 28s
 20414464/267313385 [=>............................] - ETA: 28s
 20561920/267313385 [=>............................] - ETA: 29s
 20987904/267313385 [=>............................] - ETA: 29s
 21282816/267313385 [=>............................] - ETA: 29s
 21725184/267313385 [=>............................] - ETA: 29s
 22102016/267313385 [=>............................] - ETA: 29s
 22478848/267313385 [=>............................] - ETA: 29s
 22822912/267313385 [=>............................] - ETA: 29s
 23150592/267313385 [=>............................] - ETA: 29s
 23330816/267313385 [=>............................] - ETA: 29s
 23379968/267313385 [=>............................] - ETA: 30s
 24166400/267313385 [=>............................] - ETA: 30s
 24330240/267313385 [=>............................] - ETA: 30s
 24625152/267313385 [=>............................] - ETA: 30s
 24887296/267313385 [=>............................] - ETA: 30s
 25051136/267313385 [=>............................] - ETA: 30s
 25296896/267313385 [=>............................] - ETA: 31s
 25624576/267313385 [=>............................] - ETA: 31s
 25853952/267313385 [=>............................] - ETA: 31s
 26001408/267313385 [=>............................] - ETA: 31s
 26361856/267313385 [=>............................] - ETA: 31s
 26787840/267313385 [==>...........................] - ETA: 31s
 27197440/267313385 [==>...........................] - ETA: 31s
 27590656/267313385 [==>...........................] - ETA: 31s
 28065792/267313385 [==>...........................] - ETA: 31s
 28639232/267313385 [==>...........................] - ETA: 30s
 29229056/267313385 [==>...........................] - ETA: 30s
 29818880/267313385 [==>...........................] - ETA: 30s
 30408704/267313385 [==>...........................] - ETA: 30s
 30982144/267313385 [==>...........................] - ETA: 29s
 31571968/267313385 [==>...........................] - ETA: 29s
 32161792/267313385 [==>...........................] - ETA: 29s
 32735232/267313385 [==>...........................] - ETA: 29s
 33308672/267313385 [==>...........................] - ETA: 28s
 33914880/267313385 [==>...........................] - ETA: 28s
 34504704/267313385 [==>...........................] - ETA: 28s
 35094528/267313385 [==>...........................] - ETA: 28s
 35651584/267313385 [===>..........................] - ETA: 28s
 36257792/267313385 [===>..........................] - ETA: 27s
 36847616/267313385 [===>..........................] - ETA: 27s
 37437440/267313385 [===>..........................] - ETA: 27s
 38027264/267313385 [===>..........................] - ETA: 27s
 38600704/267313385 [===>..........................] - ETA: 27s
 39174144/267313385 [===>..........................] - ETA: 26s
 39763968/267313385 [===>..........................] - ETA: 26s
 40353792/267313385 [===>..........................] - ETA: 26s
 40943616/267313385 [===>..........................] - ETA: 26s
 41533440/267313385 [===>..........................] - ETA: 26s
 42123264/267313385 [===>..........................] - ETA: 26s
 42713088/267313385 [===>..........................] - ETA: 25s
 43302912/267313385 [===>..........................] - ETA: 25s
 43892736/267313385 [===>..........................] - ETA: 25s
 44482560/267313385 [===>..........................] - ETA: 25s
 45072384/267313385 [====>.........................] - ETA: 25s
 45662208/267313385 [====>.........................] - ETA: 25s
 46252032/267313385 [====>.........................] - ETA: 24s
 46841856/267313385 [====>.........................] - ETA: 24s
 47431680/267313385 [====>.........................] - ETA: 24s
 48021504/267313385 [====>.........................] - ETA: 24s
 48611328/267313385 [====>.........................] - ETA: 24s
 49201152/267313385 [====>.........................] - ETA: 24s
 49790976/267313385 [====>.........................] - ETA: 24s
 50364416/267313385 [====>.........................] - ETA: 24s
 50954240/267313385 [====>.........................] - ETA: 23s
 51544064/267313385 [====>.........................] - ETA: 23s
 52133888/267313385 [====>.........................] - ETA: 23s
 52723712/267313385 [====>.........................] - ETA: 23s
 53313536/267313385 [====>.........................] - ETA: 23s
 53903360/267313385 [=====>........................] - ETA: 23s
 54493184/267313385 [=====>........................] - ETA: 23s
 55083008/267313385 [=====>........................] - ETA: 23s
 55672832/267313385 [=====>........................] - ETA: 22s
 56262656/267313385 [=====>........................] - ETA: 22s
 56852480/267313385 [=====>........................] - ETA: 22s
 57442304/267313385 [=====>........................] - ETA: 22s
 58032128/267313385 [=====>........................] - ETA: 22s
 58621952/267313385 [=====>........................] - ETA: 22s
 59211776/267313385 [=====>........................] - ETA: 22s
 59801600/267313385 [=====>........................] - ETA: 22s
 60391424/267313385 [=====>........................] - ETA: 22s
 60981248/267313385 [=====>........................] - ETA: 21s
 61571072/267313385 [=====>........................] - ETA: 21s
 62160896/267313385 [=====>........................] - ETA: 21s
 62750720/267313385 [======>.......................] - ETA: 21s
 63340544/267313385 [======>.......................] - ETA: 21s
 63930368/267313385 [======>.......................] - ETA: 21s
 64520192/267313385 [======>.......................] - ETA: 21s
 65110016/267313385 [======>.......................] - ETA: 21s
 65699840/267313385 [======>.......................] - ETA: 21s
 66289664/267313385 [======>.......................] - ETA: 21s
 66879488/267313385 [======>.......................] - ETA: 20s
 67469312/267313385 [======>.......................] - ETA: 20s
 68059136/267313385 [======>.......................] - ETA: 20s
 68632576/267313385 [======>.......................] - ETA: 20s
 69222400/267313385 [======>.......................] - ETA: 20s
 69812224/267313385 [======>.......................] - ETA: 20s
 70402048/267313385 [======>.......................] - ETA: 20s
 70991872/267313385 [======>.......................] - ETA: 20s
 71581696/267313385 [=======>......................] - ETA: 20s
 72171520/267313385 [=======>......................] - ETA: 20s
 72744960/267313385 [=======>......................] - ETA: 20s
 73326592/267313385 [=======>......................] - ETA: 20s
 73908224/267313385 [=======>......................] - ETA: 19s
 74498048/267313385 [=======>......................] - ETA: 19s
 75087872/267313385 [=======>......................] - ETA: 19s
 75677696/267313385 [=======>......................] - ETA: 19s
 76267520/267313385 [=======>......................] - ETA: 19s
 76857344/267313385 [=======>......................] - ETA: 19s
 77430784/267313385 [=======>......................] - ETA: 19s
 77987840/267313385 [=======>......................] - ETA: 19s
 78528512/267313385 [=======>......................] - ETA: 19s
 79101952/267313385 [=======>......................] - ETA: 19s
 79691776/267313385 [=======>......................] - ETA: 19s
 80281600/267313385 [========>.....................] - ETA: 19s
 80871424/267313385 [========>.....................] - ETA: 18s
 81412096/267313385 [========>.....................] - ETA: 18s
 82001920/267313385 [========>.....................] - ETA: 18s
 82608128/267313385 [========>.....................] - ETA: 18s
 83197952/267313385 [========>.....................] - ETA: 18s
 83787776/267313385 [========>.....................] - ETA: 18s
 84377600/267313385 [========>.....................] - ETA: 18s
 84951040/267313385 [========>.....................] - ETA: 18s
 85540864/267313385 [========>.....................] - ETA: 18s
 86130688/267313385 [========>.....................] - ETA: 18s
 86720512/267313385 [========>.....................] - ETA: 18s
 87310336/267313385 [========>.....................] - ETA: 18s
 87900160/267313385 [========>.....................] - ETA: 18s
 88473600/267313385 [========>.....................] - ETA: 17s
 89063424/267313385 [========>.....................] - ETA: 17s
 89636864/267313385 [=========>....................] - ETA: 17s
 90210304/267313385 [=========>....................] - ETA: 17s
 90800128/267313385 [=========>....................] - ETA: 17s
 91389952/267313385 [=========>....................] - ETA: 17s
 91963392/267313385 [=========>....................] - ETA: 17s
 92553216/267313385 [=========>....................] - ETA: 17s
 93159424/267313385 [=========>....................] - ETA: 17s
 93732864/267313385 [=========>....................] - ETA: 17s
 94322688/267313385 [=========>....................] - ETA: 17s
 94912512/267313385 [=========>....................] - ETA: 17s
 95502336/267313385 [=========>....................] - ETA: 17s
 96092160/267313385 [=========>....................] - ETA: 16s
 96681984/267313385 [=========>....................] - ETA: 16s
 97255424/267313385 [=========>....................] - ETA: 16s
 97845248/267313385 [=========>....................] - ETA: 16s
 98435072/267313385 [==========>...................] - ETA: 16s
 99024896/267313385 [==========>...................] - ETA: 16s
 99614720/267313385 [==========>...................] - ETA: 16s
100204544/267313385 [==========>...................] - ETA: 16s
100794368/267313385 [==========>...................] - ETA: 16s
101384192/267313385 [==========>...................] - ETA: 16s
101974016/267313385 [==========>...................] - ETA: 16s
102563840/267313385 [==========>...................] - ETA: 16s
103153664/267313385 [==========>...................] - ETA: 16s
103727104/267313385 [==========>...................] - ETA: 16s
104316928/267313385 [==========>...................] - ETA: 16s
104906752/267313385 [==========>...................] - ETA: 15s
105414656/267313385 [==========>...................] - ETA: 15s
105922560/267313385 [==========>...................] - ETA: 15s
106463232/267313385 [==========>...................] - ETA: 15s
107020288/267313385 [===========>..................] - ETA: 15s
107560960/267313385 [===========>..................] - ETA: 15s
108068864/267313385 [===========>..................] - ETA: 15s
108560384/267313385 [===========>..................] - ETA: 15s
109084672/267313385 [===========>..................] - ETA: 15s
109625344/267313385 [===========>..................] - ETA: 15s
110182400/267313385 [===========>..................] - ETA: 15s
110739456/267313385 [===========>..................] - ETA: 15s
111296512/267313385 [===========>..................] - ETA: 15s
111886336/267313385 [===========>..................] - ETA: 15s
112476160/267313385 [===========>..................] - ETA: 15s
113065984/267313385 [===========>..................] - ETA: 15s
113655808/267313385 [===========>..................] - ETA: 15s
114245632/267313385 [===========>..................] - ETA: 14s
114835456/267313385 [===========>..................] - ETA: 14s
115425280/267313385 [===========>..................] - ETA: 14s
116015104/267313385 [============>.................] - ETA: 14s
116604928/267313385 [============>.................] - ETA: 14s
117178368/267313385 [============>.................] - ETA: 14s
117768192/267313385 [============>.................] - ETA: 14s
118341632/267313385 [============>.................] - ETA: 14s
118931456/267313385 [============>.................] - ETA: 14s
119521280/267313385 [============>.................] - ETA: 14s
120111104/267313385 [============>.................] - ETA: 14s
120700928/267313385 [============>.................] - ETA: 14s
121290752/267313385 [============>.................] - ETA: 14s
121880576/267313385 [============>.................] - ETA: 14s
122470400/267313385 [============>.................] - ETA: 14s
123060224/267313385 [============>.................] - ETA: 13s
123650048/267313385 [============>.................] - ETA: 13s
124239872/267313385 [============>.................] - ETA: 13s
124829696/267313385 [=============>................] - ETA: 13s
125419520/267313385 [=============>................] - ETA: 13s
126009344/267313385 [=============>................] - ETA: 13s
126599168/267313385 [=============>................] - ETA: 13s
127172608/267313385 [=============>................] - ETA: 13s
127762432/267313385 [=============>................] - ETA: 13s
128352256/267313385 [=============>................] - ETA: 13s
128942080/267313385 [=============>................] - ETA: 13s
129531904/267313385 [=============>................] - ETA: 13s
130121728/267313385 [=============>................] - ETA: 13s
130711552/267313385 [=============>................] - ETA: 13s
131301376/267313385 [=============>................] - ETA: 13s
131891200/267313385 [=============>................] - ETA: 13s
132481024/267313385 [=============>................] - ETA: 12s
133070848/267313385 [=============>................] - ETA: 12s
133660672/267313385 [==============>...............] - ETA: 12s
134250496/267313385 [==============>...............] - ETA: 12s
134840320/267313385 [==============>...............] - ETA: 12s
135430144/267313385 [==============>...............] - ETA: 12s
136019968/267313385 [==============>...............] - ETA: 12s
136593408/267313385 [==============>...............] - ETA: 12s
137183232/267313385 [==============>...............] - ETA: 12s
137773056/267313385 [==============>...............] - ETA: 12s
138362880/267313385 [==============>...............] - ETA: 12s
138952704/267313385 [==============>...............] - ETA: 12s
139542528/267313385 [==============>...............] - ETA: 12s
140132352/267313385 [==============>...............] - ETA: 12s
140722176/267313385 [==============>...............] - ETA: 12s
141312000/267313385 [==============>...............] - ETA: 12s
141901824/267313385 [==============>...............] - ETA: 11s
142491648/267313385 [==============>...............] - ETA: 11s
143081472/267313385 [===============>..............] - ETA: 11s
143671296/267313385 [===============>..............] - ETA: 11s
144261120/267313385 [===============>..............] - ETA: 11s
144850944/267313385 [===============>..............] - ETA: 11s
145440768/267313385 [===============>..............] - ETA: 11s
146030592/267313385 [===============>..............] - ETA: 11s
146620416/267313385 [===============>..............] - ETA: 11s
147210240/267313385 [===============>..............] - ETA: 11s
147800064/267313385 [===============>..............] - ETA: 11s
148340736/267313385 [===============>..............] - ETA: 11s
148979712/267313385 [===============>..............] - ETA: 11s
149569536/267313385 [===============>..............] - ETA: 11s
150159360/267313385 [===============>..............] - ETA: 11s
150749184/267313385 [===============>..............] - ETA: 11s
151339008/267313385 [===============>..............] - ETA: 10s
151928832/267313385 [================>.............] - ETA: 10s
152518656/267313385 [================>.............] - ETA: 10s
153108480/267313385 [================>.............] - ETA: 10s
153698304/267313385 [================>.............] - ETA: 10s
154288128/267313385 [================>.............] - ETA: 10s
154877952/267313385 [================>.............] - ETA: 10s
155451392/267313385 [================>.............] - ETA: 10s
156041216/267313385 [================>.............] - ETA: 10s
156631040/267313385 [================>.............] - ETA: 10s
157220864/267313385 [================>.............] - ETA: 10s
157810688/267313385 [================>.............] - ETA: 10s
158400512/267313385 [================>.............] - ETA: 10s
158990336/267313385 [================>.............] - ETA: 10s
159580160/267313385 [================>.............] - ETA: 10s
160169984/267313385 [================>.............] - ETA: 10s
160759808/267313385 [=================>............] - ETA: 10s
161349632/267313385 [=================>............] - ETA: 9s 
161939456/267313385 [=================>............] - ETA: 9s
162529280/267313385 [=================>............] - ETA: 9s
163119104/267313385 [=================>............] - ETA: 9s
163708928/267313385 [=================>............] - ETA: 9s
164282368/267313385 [=================>............] - ETA: 9s
164872192/267313385 [=================>............] - ETA: 9s
165462016/267313385 [=================>............] - ETA: 9s
166051840/267313385 [=================>............] - ETA: 9s
166641664/267313385 [=================>............] - ETA: 9s
167231488/267313385 [=================>............] - ETA: 9s
167821312/267313385 [=================>............] - ETA: 9s
168411136/267313385 [=================>............] - ETA: 9s
168984576/267313385 [=================>............] - ETA: 9s
169590784/267313385 [==================>...........] - ETA: 9s
170180608/267313385 [==================>...........] - ETA: 9s
170770432/267313385 [==================>...........] - ETA: 9s
171360256/267313385 [==================>...........] - ETA: 8s
171950080/267313385 [==================>...........] - ETA: 8s
172539904/267313385 [==================>...........] - ETA: 8s
173129728/267313385 [==================>...........] - ETA: 8s
173719552/267313385 [==================>...........] - ETA: 8s
174309376/267313385 [==================>...........] - ETA: 8s
174899200/267313385 [==================>...........] - ETA: 8s
175489024/267313385 [==================>...........] - ETA: 8s
176078848/267313385 [==================>...........] - ETA: 8s
176668672/267313385 [==================>...........] - ETA: 8s
177258496/267313385 [==================>...........] - ETA: 8s
177848320/267313385 [==================>...........] - ETA: 8s
178438144/267313385 [===================>..........] - ETA: 8s
179027968/267313385 [===================>..........] - ETA: 8s
179617792/267313385 [===================>..........] - ETA: 8s
180207616/267313385 [===================>..........] - ETA: 8s
180797440/267313385 [===================>..........] - ETA: 8s
181387264/267313385 [===================>..........] - ETA: 8s
181977088/267313385 [===================>..........] - ETA: 7s
182566912/267313385 [===================>..........] - ETA: 7s
183156736/267313385 [===================>..........] - ETA: 7s
183746560/267313385 [===================>..........] - ETA: 7s
184336384/267313385 [===================>..........] - ETA: 7s
184926208/267313385 [===================>..........] - ETA: 7s
185516032/267313385 [===================>..........] - ETA: 7s
186105856/267313385 [===================>..........] - ETA: 7s
186695680/267313385 [===================>..........] - ETA: 7s
187285504/267313385 [====================>.........] - ETA: 7s
187875328/267313385 [====================>.........] - ETA: 7s
188465152/267313385 [====================>.........] - ETA: 7s
189054976/267313385 [====================>.........] - ETA: 7s
189644800/267313385 [====================>.........] - ETA: 7s
190234624/267313385 [====================>.........] - ETA: 7s
190824448/267313385 [====================>.........] - ETA: 7s
191414272/267313385 [====================>.........] - ETA: 7s
192004096/267313385 [====================>.........] - ETA: 6s
192593920/267313385 [====================>.........] - ETA: 6s
193183744/267313385 [====================>.........] - ETA: 6s
193757184/267313385 [====================>.........] - ETA: 6s
194314240/267313385 [====================>.........] - ETA: 6s
194887680/267313385 [====================>.........] - ETA: 6s
195477504/267313385 [====================>.........] - ETA: 6s
196067328/267313385 [=====================>........] - ETA: 6s
196657152/267313385 [=====================>........] - ETA: 6s
197246976/267313385 [=====================>........] - ETA: 6s
197836800/267313385 [=====================>........] - ETA: 6s
198426624/267313385 [=====================>........] - ETA: 6s
199016448/267313385 [=====================>........] - ETA: 6s
199606272/267313385 [=====================>........] - ETA: 6s
200196096/267313385 [=====================>........] - ETA: 6s
200785920/267313385 [=====================>........] - ETA: 6s
201375744/267313385 [=====================>........] - ETA: 6s
201965568/267313385 [=====================>........] - ETA: 6s
202555392/267313385 [=====================>........] - ETA: 5s
203145216/267313385 [=====================>........] - ETA: 5s
203735040/267313385 [=====================>........] - ETA: 5s
204324864/267313385 [=====================>........] - ETA: 5s
204914688/267313385 [=====================>........] - ETA: 5s
205504512/267313385 [======================>.......] - ETA: 5s
206094336/267313385 [======================>.......] - ETA: 5s
206684160/267313385 [======================>.......] - ETA: 5s
207273984/267313385 [======================>.......] - ETA: 5s
207863808/267313385 [======================>.......] - ETA: 5s
208453632/267313385 [======================>.......] - ETA: 5s
209043456/267313385 [======================>.......] - ETA: 5s
209633280/267313385 [======================>.......] - ETA: 5s
210223104/267313385 [======================>.......] - ETA: 5s
210812928/267313385 [======================>.......] - ETA: 5s
211402752/267313385 [======================>.......] - ETA: 5s
211992576/267313385 [======================>.......] - ETA: 5s
212582400/267313385 [======================>.......] - ETA: 5s
213172224/267313385 [======================>.......] - ETA: 4s
213745664/267313385 [======================>.......] - ETA: 4s
214335488/267313385 [=======================>......] - ETA: 4s
214925312/267313385 [=======================>......] - ETA: 4s
215515136/267313385 [=======================>......] - ETA: 4s
216104960/267313385 [=======================>......] - ETA: 4s
216694784/267313385 [=======================>......] - ETA: 4s
217284608/267313385 [=======================>......] - ETA: 4s
217874432/267313385 [=======================>......] - ETA: 4s
218464256/267313385 [=======================>......] - ETA: 4s
219054080/267313385 [=======================>......] - ETA: 4s
219643904/267313385 [=======================>......] - ETA: 4s
220200960/267313385 [=======================>......] - ETA: 4s
220790784/267313385 [=======================>......] - ETA: 4s
221380608/267313385 [=======================>......] - ETA: 4s
221970432/267313385 [=======================>......] - ETA: 4s
222543872/267313385 [=======================>......] - ETA: 4s
223100928/267313385 [========================>.....] - ETA: 4s
223657984/267313385 [========================>.....] - ETA: 4s
224215040/267313385 [========================>.....] - ETA: 3s
224772096/267313385 [========================>.....] - ETA: 3s
225329152/267313385 [========================>.....] - ETA: 3s
225886208/267313385 [========================>.....] - ETA: 3s
226443264/267313385 [========================>.....] - ETA: 3s
227000320/267313385 [========================>.....] - ETA: 3s
227557376/267313385 [========================>.....] - ETA: 3s
228130816/267313385 [========================>.....] - ETA: 3s
228720640/267313385 [========================>.....] - ETA: 3s
229310464/267313385 [========================>.....] - ETA: 3s
229900288/267313385 [========================>.....] - ETA: 3s
230490112/267313385 [========================>.....] - ETA: 3s
231079936/267313385 [========================>.....] - ETA: 3s
231669760/267313385 [========================>.....] - ETA: 3s
232259584/267313385 [=========================>....] - ETA: 3s
232849408/267313385 [=========================>....] - ETA: 3s
233439232/267313385 [=========================>....] - ETA: 3s
234029056/267313385 [=========================>....] - ETA: 3s
234045440/267313385 [=========================>....] - ETA: 3s
235749376/267313385 [=========================>....] - ETA: 2s
237551616/267313385 [=========================>....] - ETA: 2s
238141440/267313385 [=========================>....] - ETA: 2s
238731264/267313385 [=========================>....] - ETA: 2s
239321088/267313385 [=========================>....] - ETA: 2s
239910912/267313385 [=========================>....] - ETA: 2s
240500736/267313385 [=========================>....] - ETA: 2s
241057792/267313385 [==========================>...] - ETA: 2s
241647616/267313385 [==========================>...] - ETA: 2s
242188288/267313385 [==========================>...] - ETA: 2s
242778112/267313385 [==========================>...] - ETA: 2s
243335168/267313385 [==========================>...] - ETA: 2s
243924992/267313385 [==========================>...] - ETA: 2s
244514816/267313385 [==========================>...] - ETA: 2s
245104640/267313385 [==========================>...] - ETA: 2s
245694464/267313385 [==========================>...] - ETA: 1s
246284288/267313385 [==========================>...] - ETA: 1s
246874112/267313385 [==========================>...] - ETA: 1s
247463936/267313385 [==========================>...] - ETA: 1s
248053760/267313385 [==========================>...] - ETA: 1s
248643584/267313385 [==========================>...] - ETA: 1s
249233408/267313385 [==========================>...] - ETA: 1s
249823232/267313385 [===========================>..] - ETA: 1s
250413056/267313385 [===========================>..] - ETA: 1s
251002880/267313385 [===========================>..] - ETA: 1s
251592704/267313385 [===========================>..] - ETA: 1s
252182528/267313385 [===========================>..] - ETA: 1s
252772352/267313385 [===========================>..] - ETA: 1s
253362176/267313385 [===========================>..] - ETA: 1s
253952000/267313385 [===========================>..] - ETA: 1s
254541824/267313385 [===========================>..] - ETA: 1s
255131648/267313385 [===========================>..] - ETA: 1s
255721472/267313385 [===========================>..] - ETA: 1s
256311296/267313385 [===========================>..] - ETA: 1s
256901120/267313385 [===========================>..] - ETA: 0s
257490944/267313385 [===========================>..] - ETA: 0s
258080768/267313385 [===========================>..] - ETA: 0s
258670592/267313385 [============================>.] - ETA: 0s
259260416/267313385 [============================>.] - ETA: 0s
259850240/267313385 [============================>.] - ETA: 0s
260440064/267313385 [============================>.] - ETA: 0s
261029888/267313385 [============================>.] - ETA: 0s
261619712/267313385 [============================>.] - ETA: 0s
262209536/267313385 [============================>.] - ETA: 0s
262799360/267313385 [============================>.] - ETA: 0s
263389184/267313385 [============================>.] - ETA: 0s
263979008/267313385 [============================>.] - ETA: 0s
264568832/267313385 [============================>.] - ETA: 0s
265158656/267313385 [============================>.] - ETA: 0s
265748480/267313385 [============================>.] - ETA: 0s
266338304/267313385 [============================>.] - ETA: 0s
266928128/267313385 [============================>.] - ETA: 0s
267313385/267313385 [==============================] - 24s 0us/step

2. Load a pre-trained native 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 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 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/4493952 [..............................] - ETA: 0s
 212992/4493952 [>.............................] - ETA: 1s
 802816/4493952 [====>.........................] - ETA: 0s
1384448/4493952 [========>.....................] - ETA: 0s
1982464/4493952 [============>.................] - ETA: 0s
2572288/4493952 [================>.............] - ETA: 0s
3153920/4493952 [====================>.........] - ETA: 0s
3735552/4493952 [=======================>......] - ETA: 0s
4325376/4493952 [===========================>..] - ETA: 0s
4493952/4493952 [==============================] - 0s 0us/step
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 (BatchNormalizati  (None, 64, 64, 16)       64
 on)

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

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

 conv_1/BN (BatchNormalizati  (None, 64, 64, 32)       128
 on)

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

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

 conv_2/BN (BatchNormalizati  (None, 32, 32, 64)       256
 on)

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

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

 conv_3/BN (BatchNormalizati  (None, 32, 32, 64)       256
 on)

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

 dw_separable_4 (DepthwiseCo  (None, 16, 16, 64)       576
 nv2D)

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

 pw_separable_4/BN (BatchNor  (None, 16, 16, 128)      512
 malization)

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

 dw_separable_5 (DepthwiseCo  (None, 16, 16, 128)      1152
 nv2D)

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

 pw_separable_5/BN (BatchNor  (None, 16, 16, 128)      512
 malization)

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

 dw_separable_6 (DepthwiseCo  (None, 8, 8, 128)        1152
 nv2D)

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

 pw_separable_6/BN (BatchNor  (None, 8, 8, 256)        1024
 malization)

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

 dw_separable_7 (DepthwiseCo  (None, 8, 8, 256)        2304
 nv2D)

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

 pw_separable_7/BN (BatchNor  (None, 8, 8, 256)        1024
 malization)

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

 dw_separable_8 (DepthwiseCo  (None, 8, 8, 256)        2304
 nv2D)

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

 pw_separable_8/BN (BatchNor  (None, 8, 8, 256)        1024
 malization)

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

 dw_separable_9 (DepthwiseCo  (None, 8, 8, 256)        2304
 nv2D)

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

 pw_separable_9/BN (BatchNor  (None, 8, 8, 256)        1024
 malization)

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

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

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

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

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

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

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

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

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

 dw_separable_12 (DepthwiseC  (None, 4, 4, 256)        2304
 onv2D)

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

 pw_separable_12/BN (BatchNo  (None, 4, 4, 512)        2048
 rmalization)

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

 dw_separable_13 (DepthwiseC  (None, 4, 4, 512)        4608
 onv2D)

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

 pw_separable_13/BN (BatchNo  (None, 4, 4, 512)        2048
 rmalization)

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

 dw_sepconv_t_0 (DepthwiseCo  (None, 8, 8, 512)        5120
 nv2DTranspose)

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

 pw_sepconv_t_0/BN (BatchNor  (None, 8, 8, 256)        1024
 malization)

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

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

 dw_sepconv_t_1 (DepthwiseCo  (None, 16, 16, 256)      2560
 nv2DTranspose)

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

 pw_sepconv_t_1/BN (BatchNor  (None, 16, 16, 128)      512
 malization)

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

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

 dw_sepconv_t_2 (DepthwiseCo  (None, 32, 32, 128)      1280
 nv2DTranspose)

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

 pw_sepconv_t_2/BN (BatchNor  (None, 32, 32, 64)       256
 malization)

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

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

 dw_sepconv_t_3 (DepthwiseCo  (None, 64, 64, 64)       640
 nv2DTranspose)

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

 pw_sepconv_t_3/BN (BatchNor  (None, 64, 64, 32)       128
 malization)

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

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

 dw_sepconv_t_4 (DepthwiseCo  (None, 128, 128, 32)     320
 nv2DTranspose)

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

 pw_sepconv_t_4/BN (BatchNor  (None, 128, 128, 16)     64
 malization)

 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

 sigmoid_act (Activation)    (None, 128, 128, 1)       0

=================================================================
Total params: 1,058,865
Trainable params: 1,051,889
Non-trainable params: 6,976
_________________________________________________________________
from keras.metrics import BinaryIoU

# Compile the native 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"Keras binary IoU / pixel accuracy: {biou:.4f} / {100*acc:.2f}%")
Keras binary IoU / pixel accuracy: 0.9455 / 97.28%

3. Load a pre-trained quantized Keras model

The next step is to quantize and potentially perform Quantize Aware Training (QAT) on the Keras model from the previous step. After the Keras model is quantized to 8-bits 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/4520576 [..............................] - ETA: 0s
 212992/4520576 [>.............................] - ETA: 1s
 802816/4520576 [====>.........................] - ETA: 0s
1392640/4520576 [========>.....................] - ETA: 0s
1974272/4520576 [============>.................] - ETA: 0s
2555904/4520576 [===============>..............] - ETA: 0s
3153920/4520576 [===================>..........] - ETA: 0s
3735552/4520576 [=======================>......] - ETA: 0s
4325376/4520576 [===========================>..] - ETA: 0s
4520576/4520576 [==============================] - 0s 0us/step
Model: "akida_unet"
_________________________________________________________________
 Layer (type)                Output Shape              Param #
=================================================================
 input (InputLayer)          [(None, 128, 128, 3)]     0

 rescaling (QuantizedRescali  (None, 128, 128, 3)      0
 ng)

 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 (QuantizedDe  (None, 16, 16, 64)       704
 pthwiseConv2D)

 pw_separable_4 (QuantizedCo  (None, 16, 16, 128)      8320
 nv2D)

 pw_separable_4/relu (Quanti  (None, 16, 16, 128)      256
 zedReLU)

 dw_separable_5 (QuantizedDe  (None, 16, 16, 128)      1408
 pthwiseConv2D)

 pw_separable_5 (QuantizedCo  (None, 16, 16, 128)      16512
 nv2D)

 pw_separable_5/relu (Quanti  (None, 16, 16, 128)      256
 zedReLU)

 dw_separable_6 (QuantizedDe  (None, 8, 8, 128)        1408
 pthwiseConv2D)

 pw_separable_6 (QuantizedCo  (None, 8, 8, 256)        33024
 nv2D)

 pw_separable_6/relu (Quanti  (None, 8, 8, 256)        512
 zedReLU)

 dw_separable_7 (QuantizedDe  (None, 8, 8, 256)        2816
 pthwiseConv2D)

 pw_separable_7 (QuantizedCo  (None, 8, 8, 256)        65792
 nv2D)

 pw_separable_7/relu (Quanti  (None, 8, 8, 256)        512
 zedReLU)

 dw_separable_8 (QuantizedDe  (None, 8, 8, 256)        2816
 pthwiseConv2D)

 pw_separable_8 (QuantizedCo  (None, 8, 8, 256)        65792
 nv2D)

 pw_separable_8/relu (Quanti  (None, 8, 8, 256)        512
 zedReLU)

 dw_separable_9 (QuantizedDe  (None, 8, 8, 256)        2816
 pthwiseConv2D)

 pw_separable_9 (QuantizedCo  (None, 8, 8, 256)        65792
 nv2D)

 pw_separable_9/relu (Quanti  (None, 8, 8, 256)        512
 zedReLU)

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

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

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

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

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

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

 dw_separable_12 (QuantizedD  (None, 4, 4, 256)        2816
 epthwiseConv2D)

 pw_separable_12 (QuantizedC  (None, 4, 4, 512)        131584
 onv2D)

 pw_separable_12/relu (Quant  (None, 4, 4, 512)        1024
 izedReLU)

 dw_separable_13 (QuantizedD  (None, 4, 4, 512)        5632
 epthwiseConv2D)

 pw_separable_13 (QuantizedC  (None, 4, 4, 512)        262656
 onv2D)

 pw_separable_13/relu (Quant  (None, 4, 4, 512)        1024
 izedReLU)

 dw_sepconv_t_0 (QuantizedDe  (None, 8, 8, 512)        6144
 pthwiseConv2DTranspose)

 pw_sepconv_t_0 (QuantizedCo  (None, 8, 8, 256)        131328
 nv2D)

 pw_sepconv_t_0/relu (Quanti  (None, 8, 8, 256)        512
 zedReLU)

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

 dw_sepconv_t_1 (QuantizedDe  (None, 16, 16, 256)      3072
 pthwiseConv2DTranspose)

 pw_sepconv_t_1 (QuantizedCo  (None, 16, 16, 128)      32896
 nv2D)

 pw_sepconv_t_1/relu (Quanti  (None, 16, 16, 128)      256
 zedReLU)

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

 dw_sepconv_t_2 (QuantizedDe  (None, 32, 32, 128)      1536
 pthwiseConv2DTranspose)

 pw_sepconv_t_2 (QuantizedCo  (None, 32, 32, 64)       8256
 nv2D)

 pw_sepconv_t_2/relu (Quanti  (None, 32, 32, 64)       128
 zedReLU)

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

 dw_sepconv_t_3 (QuantizedDe  (None, 64, 64, 64)       768
 pthwiseConv2DTranspose)

 pw_sepconv_t_3 (QuantizedCo  (None, 64, 64, 32)       2080
 nv2D)

 pw_sepconv_t_3/relu (Quanti  (None, 64, 64, 32)       64
 zedReLU)

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

 dw_sepconv_t_4 (QuantizedDe  (None, 128, 128, 32)     384
 pthwiseConv2DTranspose)

 pw_sepconv_t_4 (QuantizedCo  (None, 128, 128, 16)     528
 nv2D)

 pw_sepconv_t_4/relu (Quanti  (None, 128, 128, 16)     32
 zedReLU)

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

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

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

 sigmoid_act (Activation)    (None, 128, 128, 1)       0

=================================================================
Total params: 1,061,601
Trainable params: 1,047,905
Non-trainable params: 13,696
_________________________________________________________________
# Compile the quantized 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"Keras quantized binary IoU / pixel accuracy: {biou:.4f} / {100*acc:.2f}%")
Keras quantized binary IoU / pixel accuracy: 0.9399 / 97.01%

4. Conversion to Akida

Finally, the quantized Keras model from the previous step is converted into an Akida model and its performance is evaluated. Note that the original performance of the 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()
/usr/local/lib/python3.8/dist-packages/cnn2snn/quantizeml/blocks.py:160: UserWarning: Conversion stops at layer head because of a dequantizer. The end of the model is ignored:
___________________________________________________
Layer (type)
===================================================
sigmoid_act (Activation)
===================================================

  warnings.warn("Conversion stops" + stop_layer_msg + " because of a dequantizer. "
                  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 tensorflow as tf

# 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 = tf.keras.activations.sigmoid(pots)

m_binary_iou = tf.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 = tf.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.9388 / 97.01%

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 Keras and Akida outputs
sample = np.expand_dims(x_val[id, :], 0)
keras_out = model_keras(sample)
akida_out = tf.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: (1 minutes 58.634 seconds)

Gallery generated by Sphinx-Gallery