Model optimization and compilation

Overview


On this phase of the project, the objective consists in reducing and compiling the YOLOv3 object detection model. To do that, we will be using a tool called DNNDK v3.1. This tool allows to quantize and optimize Deep Learning models and also compile, in other words, transform the quantized model in a binary file so it later can interact with the DPU. Without this reduction in size, the original YOLO model wouldn't be compatible with the PYNQ-Z2 because of it's restricted memory.

To do all of the necessary procedures, we need the tools and the environment set. For instance, as mentioned earlier, we will be working with Ubuntu 16.04 or 18.04. In my case I chose the second option because "bigger is better", right?

I would also recommend using Virtual Environments when working with Deep Learning models as it helps to organize and test different packages and libraries without having to worry about losing progress. Anaconda is an example of a program that can manage environments and it is pretty famous at this point. The following image is meant to help you understand the concept of different environments on the same computer:

As you can see from the image, Anaconda allows you to create multiple environments with different libraries so you can experiment projects and the compatibility with different versions. Normally each environment is associated with a python version. Python, as you might already know, is a famous programming language that has been adopted by the big Deep Learning Frameworks and also by everyone in general ... Anaconda allows you to create "Virtual spaces" where you can work with a specific version of Python so you don't have to install and uninstall it form you computer constantly. Also, I should mention that the "Base" environment is assigned by Anaconda as the normal environment, in other words, it is your computer.

DNNDK installation


DNNDK download

We will obviously be needing the DNNDK v3.1 tool to work with the YOLO model. It is the last version of DNNDK before Xilinx decided to redesign everything and call it "Vitis AI". This section is dedicated to guide you through the installation process of DNNDK. For this project we will only be working with Tensorflow, which is a Deep Learning Framework, or you migh also call it a "magical deep learning engine". DNNDK also supports other magical engine called "Caffe" but we won't focus on that.

The first thing you need to do is download DNNDK. As Xilinx as completely moved on from DNNDK, the only way you can download it is through the following link:

This will take some time as the program has 2GB. After the download is complete, you need to extract DNNDK from the compressed file you downloaded. You can keep everything on the Downloads folder for convenience!

Anaconda download

Next, we will install Anaconda so we can work with Virtual Environments. We will be working with a suitable version for this project as it's from around the same years of the rest of the tools. First let's open the directory of installation with this command: cd /tmp on the Terminal.

Then download Anaconda version 2020.02 using the wget command. If it gives an error about wget not being installed just do sudo apt-get install wget.

wget https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh

After the download is complete, we can execute the installer using the following command:

bash Anaconda3-2020.02-Linux-x86_64.sh

You should accept the licenses and everything they ask. There will be suggested the installation of VScode but you can decline as we won't need it.

Now, open a new terminal and place conda --version to see the Anaconda version of your installation. If anything is displayed then you successfully installed Anaconda!

DNNDK setup

Now comes the part where we install and setup DNNDK properly according to our needs. First, we will create a environment for this tool so we can do everything safely. We will call it "decent", as the name of the quantization tool. Also, the environment will be based on Python 3.6 as it is required on the DNNDK v3.1 User Guide. You can do it as follows:

conda create -n decent pip python=3.6

Then we have to activate the newly created environment using the following command:

source activate decent

If you want to deactivate the environment and go back to base you can do it by inserting conda deactivate.

Now, we need to install DECENT_Q which is the quantization tool from DNNDK and Tensorflow. This can be done with one specific file. First you need to access the folder according to your OS version. Use the first if you are also working on Ubuntu 18.04 and have the DNNDK on the Downloads folder:

cd Downloads/xilinx_dnndk_v3.1/host_x86/decent-tf/ubuntu18.04
cd Downloads/xilinx_dnndk_v3.1/host_x86/decent-tf/ubuntu16.04

Next you install the tools by executing the "Wheel" file on that folder. It should do everything for you.

pip install ./tensorflow-1.12.0-cp36-cp36m-linux_x86_64.whl

After the installation is complete we need to install some other necessary packages. If you consulted the DNNDK User Guide you will notice that I specified some versions for some packages and they don't. This happens because some new ones are not compatible with Python 3.6 anymore and so we need to obtain older versions.

pip install numpy opencv-python==4.7.0.68 scikit-learn==0.24.2 scipy progressbar2 keras==2.2.4

Recently I tried this whole process again and there were some errors regarding the model conversion from Draknet to Keras. I was able to solve those problems by re-installing "pillow" package and by downgrading "h5py" package. To avoid having the same problems I did, make sure you paste the following commands!

pip install pillow
pip install 'h5py==2.10.0' --force-reinstall

Then when everything is installed as it should, go to this directory:

cd Downloads/xilinx_dnndk_v3.1/host_x86

Here I need you to open the document "install.sh" and change line 4 with this: support_board=(ZCU102 ZCU104 ZedBoard Ultra96 PynqZ2). You can edit the file using gedit install.sh or just using the Ubuntu interface.

Next we need to give admin permissions to execute the file: chmod +x install.sh.

Lastly, you execute the DNNDK installation for the PYNQ-Z2 using this command: sudo ./install.sh PynqZ2. After that, the output on the Terminal should be similar to this:

Inspect system environment ...
[system version]
No LSB modules are available.
18.04
Begin to install Xilinx DNNDK tools on host ...
Complete dnnc installation successfully.
Complete CPU version of decent for caffe installation successfully.
The host system environment supported by GPU version of decent for caffe is as follows:
1 - Ubuntu 14.04 + CUDA 8.0 + cuDNN 7.05
2 - Ubuntu 16.04 + CUDA 8.0 + cuDNN 7.05
3 - Ubuntu 16.04 + CUDA 9.0 + cuDNN 7.05
4 - Ubuntu 16.04 + CUDA 9.1 + cuDNN 7.05
But does not meet the above environment.
The GPU version of decent for caffe installation failed.

That's it, you have DNNDK v3.1 ready to work on your YOLO model!

Last updated