Convert model to Tensorflow

The next works will be heavily based on the glorious work of Wu-Tianze, specifically the dnndk-pynqz2 github project. I recommend you check it out and give him a star on his project!

The first thing you should know is that the YOLOv3 model is made on a framework called Darknet. What we will be addressing on this chapter is precisely the conversion from Darknet to Tensorflow because DNNDK only supports the second one.

The next image represents the process we will be addressing right here. As you can see we will use two useful scrips from other creators.

Darknet to Keras


The first conversion will involve Darknet to Keras. Keras is a high level neural network API written in Python and that is directly related to Tensorflow, making the process easier. To do this I stole a script from an amazing project on github keras-yolo3 that will do the conversion.

First you will access the folder called DNNDK from my github repository and then enter the model_conversion folder.

cd ~
cd Desktop/YOLO-on-PYNQ-Z2/DNNDK/model_conversion

Then to convert the YOLOv3 model we will need to use the script "convert.py" and indicate the YOLO configuration file as well as the weights.

To have a better idea of the project and for you to do something I didn't include the weights file on the repository folder. It wasn't totally because it wouldn't fit on the github restrictions in size of course ... It's pretty easy, you go to the official YOLO website from Joseph Redmon and scroll down a little until you find this table:

As you can see, I need you to click on "weights" and initialize the download of the file. Then I need you to put that file called "yolov3.weights" on the "model_conversion" folder so we can proceed.

With everything set, you can write the next command to convert the yolo model from Darknet to Keras. Don't forget to change the path!

python convert.py yolov3.cfg yolov3.weights /home/andre/Desktop/YOLO-on-PYNQ-Z2/DNNDK/model_conversion/model_data/yolo.h5

After the conversion, there will be created a file with the ".h5" extension on a new "model_data" folder, meaning the YOLO is now on Darknet!

Keras to Tensorflow


Net step is to convert the Keras model to Tensorflow. To do that we will use another script that this time I stole from Wu-Tianze. We now need to indicate the path to the Keras model and the path to the resulting Tensorflow model. We can achieve this with the next command:

python keras_to_tensorflow.py --input_model=/home/andre/Desktop/YOLO-on-PYNQ-Z2/DNNDK/model_conversion/model_data/yolo.h5 --output_model=/home/andre/Desktop/YOLO-on-PYNQ-Z2/DNNDK/model_conversion/model_data/yolo.pb

Now the resulting file will have the ".pb" extension and will be stored also on the "model_data" folder. This means that the yolo model is now in Tensorflow format, congratulations!

The last thing to do is to move or copy the Tensorflow model to the quantization folder so we can use it on the next chapter. You can do it manually on the interface or use this command (don't forget to change the path according to your user):

cp /model_data/yolo.pb /home/andre/Desktop/YOLO-on-PYNQ-Z2/DNNDK/quantization

This is all we needed to do for the conversion to Tensorflow. Next we will be quantizing the resulting model.

Last updated