This notebook covers how to use Binary Neural Networks on Pynq. It shows an example of image recognition with a Binarized Neural Network inspired at VGG-16, featuring 6 convolutional layers, 3 max pool layers and 3 fully connected layers
Creating a classifier will automatically download the correct bitstream onto device and load the weights trained on the specified dataset. By default there are three sets of weights available for the CNV network using 1 bit for weights and activation - this example uses the streetview house number set.
In [1]:
import bnn
print(bnn.available_params(bnn.NETWORK_CNVW1A1))
classifier = bnn.CnvClassifier(bnn.NETWORK_CNVW1A1,"streetview",bnn.RUNTIME_HW)
In [2]:
print(classifier.classes)
In [3]:
from PIL import Image
import numpy as np
img = Image.open('/home/xilinx/jupyter_notebooks/bnn/pictures/6.png')
img
Out[3]:
In [4]:
result_class_idx = classifier.classify_image(img)
print("Inferred number: {0}".format(classifier.class_name(result_class_idx)))
In [5]:
sw_classifier = bnn.CnvClassifier(bnn.NETWORK_CNVW1A1, "streetview", bnn.RUNTIME_SW)
result_class_idx = sw_classifier.classify_image(img)
print("Inferred number: {0}".format(sw_classifier.class_name(result_class_idx)))
In [6]:
from pynq import Xlnk
xlnk = Xlnk();
xlnk.xlnk_reset()