In [1]:
import bnn
In [2]:
print(bnn.available_params(bnn.NETWORK_CNVW1A1))
Creating a classifier will automatically download the correct bitstream onto device and load the weights trained on the specified dataset. Passing a runtime attribute will allow to choose between hardware accelerated or pure software inference.
Use CnvClassifier(network,parameters,runtime)
for CNV network topology:
In [3]:
hw_classifier = bnn.CnvClassifier(bnn.NETWORK_CNVW1A1,'cifar10',bnn.RUNTIME_HW)
sw_classifier = bnn.CnvClassifier(bnn.NETWORK_CNVW1A1,'cifar10',bnn.RUNTIME_SW)
In [4]:
print(hw_classifier.classes)
In [5]:
from PIL import Image
import numpy as np
im = Image.open('/home/xilinx/jupyter_notebooks/bnn/pictures/deer.jpg')
im
Out[5]:
In [6]:
class_out=hw_classifier.classify_image(im)
print("Class number: {0}".format(class_out))
print("Class name: {0}".format(hw_classifier.class_name(class_out)))
In [7]:
class_out = sw_classifier.classify_image(im)
print("Class number: {0}".format(class_out))
print("Class name: {0}".format(sw_classifier.class_name(class_out)))
As it can be seen, the software implementation is several orders of magnitude slower than the hardware implementation.
In addition to the highest ranked class output, it is possible to get the ranking of every class using the classify_details
function. To run this example, take another couple of images. Here, a car, an airplane, and a bird will be classified in order to compare the rankings.
In [8]:
from IPython.display import display
im = Image.open('/home/xilinx/jupyter_notebooks/bnn/pictures/car.png')
im.thumbnail((64, 64), Image.ANTIALIAS)
display(im)
car_class = hw_classifier.classify_image_details(im)
print("{: >10}{: >13}".format("[CLASS]","[RANKING]"))
for i in range(len(car_class)):
print("{: >10}{: >10}".format(hw_classifier.classes[i],car_class[i]))
im = Image.open('/home/xilinx/jupyter_notebooks/bnn/pictures/airplane.jpg')
im.thumbnail((64, 64), Image.ANTIALIAS)
display(im)
air_class = hw_classifier.classify_image_details(im)
print("{: >10}{: >13}".format("[CLASS]","[RANKING]"))
for i in range(len(air_class)):
print("{: >10}{: >10}".format(hw_classifier.classes[i],air_class[i]))
im = Image.open('/home/xilinx/jupyter_notebooks/bnn/pictures/bird.jpg')
im.thumbnail((64, 64), Image.ANTIALIAS)
display(im)
bird_class = hw_classifier.classify_image_details(im)
print("{: >10}{: >13}".format("[CLASS]","[RANKING]"))
for i in range(len(bird_class)):
print("{: >10}{: >10}".format(hw_classifier.classes[i],bird_class[i]))
The numbers can be difficult to visualise so we can use matplotlib to graph the output. Numbers are shown in units of 100:
In [9]:
%matplotlib inline
import matplotlib.pyplot as plt
x_pos = np.arange(len(car_class))
fig, ax = plt.subplots()
ax.bar(x_pos - 0.25, (car_class/100.0), 0.25)
ax.bar(x_pos, (air_class/100.0), 0.3)
ax.bar(x_pos + 0.25, (bird_class/100.0), 0.25)
ax.set_xticklabels(hw_classifier.classes, rotation='vertical')
ax.set_xticks(x_pos)
ax.set
plt.legend(["car","plane","bird"])
plt.show()
In [10]:
from pynq import Xlnk
xlnk = Xlnk()
xlnk.xlnk_reset()