AR# 72804

|

How to Quantize, Compile, and test the TensorFlow ResNet-50 example on a ZCU102 board

Description

The TensorFlow ResNet-50 Convolutional Neural Network (CNN) is provided with the Xilinx Deep Neural Network Development Kit (DNNDK) v3.1.

The model can be found in the in the folder below:

(...)\xilinx_dnndk_v3.1\host_x86\models\tensorflow\resnet_v1_50\

How can I test this example on hardware?

Solution

The CNN Resnet50 developed with TensorFlow can be tested on hardware by completing the following steps:

  1. Install Anaconda and create a dedicated virtual environment, following the steps in the TensorFlow version: installing with Anaconda section in (UG1327).
    This installs the TensorFlow DNNDK on the host.
  2. Install CUDA and cuDNN on the host
  3. Flash the SD Card according to the steps in (UG1327)
  4. Complete the Quantization process and Compilation on the Host
  5. Build the model on the Target


This Answer Record provides additional help on some of these steps.

Model origin

It is important to know how the model was trained: the same image pre-processing sequence must be used during quantization and the hardware test.

The Resnet50 CNN in the example comes from the Model Zoo (https://github.com/Xilinx/AI-Model-Zoo)

The CNN has been pre-trained with images coming from the ImageNet set, with the following characteristics:

  1. data channel order: RGB(0~255)
  2. resize: resize short side to 256 and keep the aspect ratio.
  3. center crop: 224 * 224
  4. input = input - [123.68, 116.78, 103.94]


Scaling the images to 0-1 during quantization, or reversing the order to BGR, or forgetting the average value subtraction are very common errors that will cause unexpected results in hardware.

Quantization

The Quantization process requires some images for calibration, but they are not provided with the model. 

The user should download 100 to 1000 images of ImageNet data set from http://academictorrents.com/collection/imagenet-2012 or http://www.image-net.org/download.php

Note that the TensorFlow Calibration does not require the label value, so you will need to slightly modify the resnet_v1_50_input_fn.py and skip the label information.

You might also need to edit line 21 and 22 that set the path to the calibration folder.

Notes on the resnet_v1_50_input_fn.py:

Remember that the image for training was in RGB format. The resnet_v1_50_input_fn.py preprocesses the images using two functions:

  • cv2.imread (which converts the image to a BGR NumPy array)
  • The mean_image_subtraction (which brings the order back to RGB).

Compile:

The Deep Neural Network Compiler (DNNC) generates one .elf file.

The kernel names are resnet_v1_50_0 (DPUKernel) and resnet_v1_50_1 (CPUKernel)

The same kernel names must be used in the main.cc file and Makefile.


 

Build on Target

The TensorFlow model needs a dedicated main.cc file which is different from the Caffe model.

In particular, the Kernel name, input/output names, images extensions, and some other cosmetic changes have been edited in the attached main.cc file.

Also the Makefile which comes with DNNDK 3.1 should be fixed due to a typo in the .elf model name.

Attachments

Associated Attachments

Name File Size File Type
resnet_v1_50_input_fn.py 1 KB PY
Makefile 3 KB NA
main.cc 7 KB CC
AR# 72804
Date 08/07/2020
Status Active
Type General Article
Devices
People Also Viewed