AI: Object Detection with Xilinx DPU IP
Xilinx DPU (Deep Learning Processing Unit) is a dedicated hardware IP core designed for accelerating Convolutional Neural Networks (CNN) on FPGA/SoC devices. Designers do not need to create a custom hardware accelerator from scratch; by simply quantizing and compiling a pre-trained deep learning model using Xilinx tools, you can deploy real-time inference on the board. This article walks through an end-to-end tutorial on Object Detection using YOLOv3 on the DPU of a PYNQ-Z2 board (Zynq-7000)—from building the hardware design in Vivado and compiling embedded Linux with PetaLinux, to converting, quantizing, and compiling the model for on-board execution.
Experimental Setup & Tools:
| Tool | Version |
|---|---|
| Vivado | 2020.1 (on Windows OS) |
| PetaLinux | 2019.2 |
| Ubuntu | 18.04 (in VMware 17.5.2) |
| Xilinx DNNDK | v3.1 (DECENT / DNNC / N2Cube) |
| YOLO | v3 (Object Detection running on PYNQ-Z2) |
Workflow Overview
DPU IP + Zynq] -->|.xsa| B[PetaLinux
build Linux] B -->|BOOT.BIN, image.ub, rootfs| E[SD card] C[Darknet YOLOv3
.cfg + .weights] -->|convert| D[Keras .h5 → TensorFlow .pb] D -->|decent_q| F[Quantized .pb] F -->|dnnc + dcf| G[dpu_yolo.elf] G --> E E --> H[PYNQ-Z2
Run yolo_image]
1. Xilinx DPU
1.1 Obtaining the Xilinx DPU IP
The latest DPU IP releases no longer support the Zynq-7000 family, but versions up to v3.3 retain support. In this project, we target Zynq-7000 and use DPU IP v3.0. While this legacy IP is no longer hosted on the main Xilinx download portal, it is included in zcu102-dpu-trd-2019-1-190809.zip (download link provided at the end of this article).
Extract zcu102-dpu-trd-2019-1-190809.zip. Under zcu102-dpu-trd-2019-1-timer\pl\srcs you will find a folder named dpu_ip. Copy the entire dpu_ip directory to your project workspace. In this guide, it is placed at D:\xilinx\pynq_z2\AI\DPU\ip_repo.
(Vivado project file: D:\xilinx\pynq_z2\AI\DPU\Pynq-Z2_2020_1\pynqz2_dpu.xpr)
1.2 Adding DPU IP to IP Catalog
In Vivado's IP Catalog, click Add Repository and select the dpu_ip folder.
1.3 Implementing DPU IP in Block Design
Open the Block Design, instantiate the DPU IP, and connect it to the Zynq Processing System as illustrated below:
The DPU core exposes several interface ports:
| Port | Function | Connected to |
|---|---|---|
S_AXI, s_axi_aclk |
DPU configuration & control registers | Zynq AXI master (M_AXI_GP) |
M_AXI_DATA0, M_AXI_DATA1, M_AXI_INSTR, m_axi_dpu_aclk |
DPU memory access for weights, input features, instructions, and output tensors | Zynq AXI slave HP ports (S_AXI_HP0~2) |
dpu_2x_clk |
2x compute clock input for internal DPU engine | PLL output clock |
dpu_interrupt |
DPU execution completion interrupt | Zynq IRQ_F2P |
The DPU IP requires three distinct clocks:
| Clock | Role | Frequency |
|---|---|---|
s_axi_aclk |
Zynq → DPU register communication | 100 MHz |
m_axi_dpu_aclk |
DPU computation engine & DPU → Zynq memory bus | 150 MHz |
dpu_2x_clk |
Internal DPU DSP computation pipeline | 300 MHz |
The 150 MHz and 300 MHz clocks are synthesized using a Clocking Wizard (PLL) and routed to m_axi_dpu_aclk and dpu_2x_clk.
Note:dpu_2x_clkmust always be exactly twice the frequency ofm_axi_dpu_aclkand must originate from the same PLL source to maintain strict phase alignment.
Because the Zynq-7020 on the PYNQ-Z2 board has limited logic capacity, set the Arch of DPU to B1152 to reduce logic resource utilization.
1.4 Exporting Hardware Description (.XSA)
After compiling the project in Vivado, export the hardware platform file (.xsa) via File → Export → Export Hardware (select Include bitstream).
2. PetaLinux for DPU
2.1 Environment Preparation
Install PetaLinux 2019.2 on a Linux workstation (Ubuntu 18.04). Start by installing required dependencies:
sudo apt update
sudo apt upgrade
sudo apt install htop vim openssh-server iproute2 gcc g++ net-tools libncurses5-dev zlib1g:i386 libssl-dev flex bison libselinux1 xterm autoconf libtool texinfo zlib1g-dev gcc-multilib build-essential screen pax gawk python3 python3-pexpect python3-pip python3-git python3-jinja2 xz-utils debianutils iputils-ping libegl1-mesa libsdl1.2-dev pylint3 cpio chrpath socat python
pip3 install --upgrade pip
sudo apt-get install tftp
sudo apt-get install tftpd-hpa
sudo apt-get install gparted
2.2 Installing PetaLinux
mkdir ~/petalinux_2019
mv ~/Downloads/petalinux-v2019.2-final-installer.run ~/petalinux_2019/.
cd ~/petalinux_2019
chmod +x petalinux-v2019.2-final-installer.run
./petalinux-v2019.2-final-installer.run
(Press [q] to dismiss license agreements during installation)
Change the default system shell in Ubuntu so scripts run with bash rather than dash:
chsh -s /bin/bash
# Reboot workstation
sudo dpkg-reconfigure dash
# Select 'No'
2.3 Building Embedded Linux
Source the PetaLinux settings:
source ~/petalinux_2019/settings.sh
Initialize a PetaLinux project and import the exported hardware definition:
cd ~/
petalinux-create --type project --template zynq --name pynqz2_dpu
cd pynqz2_dpu/
petalinux-config --get-hw-description=~/Desktop/Pynq-Z2_2020_1
Configure the system settings as follows:
- DTG Settings → Kernel Bootargs → Disable "generate boot args automatically" and set "user set kernel bootargs" to:
console=ttyPS0,115200 root=/dev/mmcblk0p2 rw earlyprintk quiet rootfstype=ext4 rootwait cma=256M - Image Packaging Configuration → Root filesystem type (SD card) → Select
EXT4 (SD/eMMC/SATA/USB)
2.3.1 Adding DPU Driver Configuration
Copy the DPU driver recipe from the zcu102-dpu-trd package:
cp -rp ~/Downloads/zcu102-dpu-trd-2019-1-190809/zcu102-dpu-trd-2019-1-timer/apu/dpu_petalinux_bsp/xilinx-dpu-trd-zcu102-v2019.1/zcu102-dpu-trd-2019-1/project-spec/meta-user/recipes-modules project-spec/meta-user
Add the dpu module to rootfs configuration:
gedit project-spec/meta-user/conf/user-rootfsconfig
CONFIG_dpu
gedit project-spec/meta-user/conf/petalinuxbsp.conf
IMAGE_INSTALL_append = "dpu"
Modify the Device Tree:
gedit project-spec/meta-user/recipes-bsp/device-tree/files/system-user.dtsi
/include/ "system-conf.dtsi"
&amba {
xlnk {
compatible = "xlnx,xlnk-1.0";
};
};
&amba {
dpu {
#address-cells = <1>;
#size-cells = <1>;
compatible = "xilinx,dpu";
base-addr = <0x4f000000>; //CHANGE THIS ACCORDING TO YOUR DESIGN
dpucore {
compatible = "xilinx,dpucore";
interrupt-parent = <&intc>;
interrupts = <0 29 4>; //CHANGE THIS ACCORDING TO YOUR DESIGN
core-num = <0x1>; //CHANGE THIS ACCORDING TO YOUR DESIGN
};
};
};
//usb device tree
/ {
usb_phy0: usb_phy@0 {
compatible = "ulpi-phy";
#phy-cells = <0>;
reg = <0xe0002000 0x1000>;
view-port = <0x0170>;
drv-vbus;
};
};
&usb0 {
dr_mode = "host";
usb-phy = <&usb_phy0>;
};
Note: The three parameters marked with "CHANGE THIS" must precisely match your Vivado hardware design.
| Parameter | Hardware Mapping Requirement |
|---|---|
base-addr |
DPU base address in Vivado Address Editor |
interrupts = <0 29 4> |
IRQ_F2P interrupt pin mapped to dpu_interrupt (here pin 61 → 61 − 32 = 29) |
core-num |
Number of DPU cores configured in hardware (here 1 core) |
2.3.2 Configuring Rootfs and Kernel
petalinux-config -c rootfs
Enable the following packages:
| Menu | Packages to Enable |
|---|---|
| modules | dpu |
| Filesystem Packages → admin | sudo, sudo-dev |
| Filesystem Packages → misc | packagegroup-petalinux-self-hosted |
| PetaLinux Package Groups | packagegroup-petalinux-python-modules, -dev |
| PetaLinux Package Groups | packagegroup-petalinux-x11, -dev |
| Filesystem Packages → console → utils → pkgconfig | pkgconfig, pkgconfig-dev |
| Filesystem Packages → libs → gtk+3 | gtk+3, gtk+3-demo, gtk+3-dev, gtk+3-dbg |
| PetaLinux Package Groups → packagegroup-petalinux-opencv | opencv, opencv-dev |
Configure the Linux kernel:
petalinux-config -c kernel
Navigate to Device Drivers → USB support → [*] USB announce new devices and save.
petalinux-build -c kernel -x finish
(This command finalizes kernel configuration). Then edit devtool-fragment.cfg:
gedit project-spec/meta-user/recipes-kernel/linux/linux-xlnx/devtool-fragment.cfg
Replace the file contents with the following options (to support USB UVC cameras and USB host mode):
CONFIG_USB_OTG=y
# CONFIG_USB_OTG_FSM is not set
# CONFIG_USB_ZERO_HNPTEST is not set
CONFIG_MEDIA_USB_SUPPORT=y
CONFIG_USB_VIDEO_CLASS=y
CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV=y
CONFIG_USB_GSPCA=m
CONFIG_V4L_PLATFORM_DRIVERS=y
CONFIG_VIDEO_ADV7604=y
CONFIG_USB_HID=y
CONFIG_USB_OHCI_LITTLE_ENDIAN=y
CONFIG_USB_SUPPORT=y
CONFIG_USB_COMMON=y
CONFIG_USB_ARCH_HAS_HCD=y
CONFIG_USB=y
CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
CONFIG_USB_DEFAULT_PERSIST=y
CONFIG_USB_EHCI_HCD=y
CONFIG_USB_EHCI_ROOT_HUB_TT=y
CONFIG_USB_EHCI_PCI=y
CONFIG_USB_EHCI_HCD_PLATFORM=y
CONFIG_USB_ACM=m
CONFIG_USB_PRINTER=m
CONFIG_USB_WDM=m
CONFIG_USB_TMC=m
CONFIG_USB_STORAGE=y
CONFIG_USB_CHIPIDEA=y
CONFIG_USB_CHIPIDEA_OF=y
CONFIG_USB_CHIPIDEA_PCI=y
CONFIG_USB_CHIPIDEA_HOST=y
CONFIG_USB_PHY=y
CONFIG_NOP_USB_XCEIV=y
CONFIG_AM335X_CONTROL_USB=y
CONFIG_AM335X_PHY_USB=y
CONFIG_USB_GPIO_VBUS=y
CONFIG_USB_ULPI=y
CONFIG_USB_ULPI_VIEWPORT=y
2.3.3 Building PetaLinux Images
petalinux-build
petalinux-package --boot --force --fsbl images/linux/zynq_fsbl.elf --fpga images/linux/*.bit --u-boot
petalinux-package --bsp -p ./ -o pynq-z2-dpu.bsp
The build artifacts will be generated in ./images/linux/:
| File | Role |
|---|---|
./images/linux/BOOT.BIN |
FSBL + FPGA bitstream + U-Boot |
./images/linux/image.ub |
Linux kernel + flattened device tree |
./images/linux/rootfs.tar.gz |
Root filesystem archive |
Note: IfBOOT.BINdoes not embed the FPGA bitstream, regenerate it by runningpetalinux-config, setting FPGA Manager → FPGA Manager to Disable, rebuilding PetaLinux, and then packagingBOOT.BINagain:
petalinux-package --boot --force --fsbl images/linux/zynq_fsbl.elf --fpga images/linux/*.bit --u-boot
(Any packaging warnings during build can be ignored)
BOOT.BIN without bitstream embedded:
BOOT.BIN with bitstream embedded (file size roughly equal to system.bit + u-boot.bin):
3. Building YOLO for Xilinx DPU
Deploying a neural network model to the DPU requires converting it into an executable ELF binary (.elf). The toolchain flow involves three primary stages: Model Conversion → Quantization (float32 → int8) → Compilation, as illustrated below:
3.1 Environment Preparation
Install Anaconda:
cd ~/
wget https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh
bash Anaconda3-2020.02-Linux-x86_64.sh
Restart your shell session, then create a conda environment for DNNDK (named decent):
conda create -n decent pip python=3.6
source activate decent
cd ~/Downloads/xilinx_dnndk_v3.1/host_x86/decent-tf/ubuntu18.04/
pip install ./tensorflow-1.12.0-cp36-cp36m-linux_x86_64.whl
pip install numpy opencv-python==4.6.0.66 scikit-learn==0.24.2 scipy progressbar2 keras==2.2.4
pip install pillow
pip install 'h5py==2.10.0' --force-reinstall
sudo apt install libgoogle-glog-dev
sudo apt-get install graphviz
Install DNNDK (host side). Add PynqZ2 to the list of supported boards in install.sh:
cd ~/Downloads/xilinx_dnndk_v3.1/host_x86
gedit install.sh
support_board=(ZCU102 ZCU104 ZedBoard Ultra96 PynqZ2)
chmod +x install.sh
sudo ./install.sh PynqZ2
3.2 Model Conversion to TensorFlow (.pb)
3.2.1 Convert Darknet → Keras
Visit https://pjreddie.com/darknet/yolo/ and download yolov3.weights. Copy the weights file to ~/Desktop/YOLO-on-PYNQ-Z2/DNNDK/model_conversion:
cd ~/Desktop/YOLO-on-PYNQ-Z2/DNNDK/model_conversion
python convert.py yolov3.cfg yolov3.weights ~/Desktop/YOLO-on-PYNQ-Z2/DNNDK/model_conversion/model_data/yolo.h5
3.2.2 Convert Keras → TensorFlow Frozen Graph
python keras_to_tensorflow.py --input_model=/home/hong/Desktop/YOLO-on-PYNQ-Z2/DNNDK/model_conversion/model_data/yolo.h5 --output_model=/home/hong/Desktop/YOLO-on-PYNQ-Z2/DNNDK/model_conversion/model_data/yolo.pb
3.3 Quantization (DECENT)
Copy yolo.pb to the quantization working folder:
cp ./model_data/yolo.pb /home/hong/Desktop/YOLO-on-PYNQ-Z2/DNNDK/quantization
Inspect the input and output tensor nodes of the model:
cd /home/hong/Desktop/YOLO-on-PYNQ-Z2/DNNDK/model_conversion
decent_q inspect --input_frozen_graph=model_data/yolo.pb
Note down the node names from the output:
Found 1 possible inputs: (name=input_1, type=float(1), shape=[?,?,?,3])
Found 3 possible outputs: (name=conv2d_59/BiasAdd, op=BiasAdd) (name=conv2d_67/BiasAdd, op=BiasAdd) (name=conv2d_75/BiasAdd, op=BiasAdd)
3.3.1 Preparing the COCO Dataset
Download calibration images from the COCO dataset:
- http://images.cocodataset.org/zips/train2017.zip
- http://images.cocodataset.org/annotations/annotations_trainval2017.zip
Create the dataset directory and extract the archives:
mkdir /home/hong/Desktop/YOLO-on-PYNQ-Z2/DNNDK/quantization/yolo_dataset
Rename the extracted folders as follows:
| Original Name | New Name |
|---|---|
train2017 |
images |
annotations_trainval2017 |
labels |
3.3.2 Updating the Calibration Script
gedit /home/hong/Desktop/YOLO-on-PYNQ-Z2/DNNDK/quantization/graph_input_fn.py
Update the dataset path variable:
path = "/home/hong/Desktop/YOLO-on-PYNQ-Z2/DNNDK/quantization/yolo_dataset/images/"
3.3.3 Running Quantization
cd /home/hong/Desktop/YOLO-on-PYNQ-Z2/DNNDK/quantization
gedit quant.sh
Set --output_nodes using the tensor names inspected earlier with decent_q inspect:
echo "###################"
echo "QUANTIZE"
echo "###################"
decent_q quantize \
--input_frozen_graph ./yolo.pb \
--input_nodes input_1 \
--input_shapes ?,416,416,3 \
--output_nodes "conv2d_59/BiasAdd,conv2d_67/BiasAdd,conv2d_75/BiasAdd" \
--method 1 \
--input_fn graph_input_fn.calib_input \
--gpu 0 \
--calib_iter 100
chmod +x quant.sh
./quant.sh
Quantized model artifacts will be saved in ~/Desktop/YOLO-on-PYNQ-Z2/DNNDK/quantization/quantize_results/ (producing quantize_eval_model.pb and deploy_model.pb; the subsequent step uses deploy_model.pb).
3.4 Compiling for DPU (DNNC)
cd ~/Desktop/YOLO-on-PYNQ-Z2/DNNDK/quantization
cp ./quantize_results/deploy_model.pb /home/hong/Desktop/YOLO-on-PYNQ-Z2/DNNDK/compilation
cd /home/hong/Desktop/YOLO-on-PYNQ-Z2/DNNDK/compilation
Generate the DPU configuration file (.dcf) from Vivado's hardware handoff file (.hwh) using dlet:
dlet -f ~/Desktop/Pynq-Z2_2020_1/pynqz2_dpu.srcs/sources_1/bd/base/hw_handoff/base.hwh
dlet -f ~/Desktop/Pynq-Z2_2020_1/pynqz2_dpu.srcs/sources_1/bd/base/hw_handoff/base.hwh /home/hong/Desktop/YOLO-on-PYNQ-Z2/DNNDK/compilation/pynq-z2.dcf -o
mv dpu-12181630-181630-202412181630-1630-30.dcf pynqz2_dpu.dcf
chmod 777 ~/Desktop/YOLO-on-PYNQ-Z2/DNNDK/compilation/pynqz2_dpu.dcf
Note: The generated.dcffilename includes a timestamp suffix that differs each time. Verify the actual generated filename before executingmv.
Run the compilation script:
sh compile.sh
The compiled DPU kernel files will be generated in the output directory (compilation is compute-intensive; on a Core i5-13500 PC it took approx. 12 hours):
(decent) hong@ubuntu:~/Desktop/YOLO-on-PYNQ-Z2/DNNDK/compilation/compile$ ls
dpu_yolo.elf yolo_kernel_graph.jpg yolo_kernel.info
4. Hardware Testing on Board
Testing on the physical board requires a C++ application runtime to feed images and schedule DPU tasks, provided by the reference implementation.
4.1 Preparing the SD Card
Partition the SD card with gparted as follows:
| Partition | Filesystem | Contents |
|---|---|---|
| Partition 1 | FAT32 | BOOT.BIN, image.ub |
| Partition 2 | ext4 | Root filesystem (rootfs) |
Copy boot images and unpack rootfs onto the SD card (here the SD card is at /dev/sdb; verify the device node name on your system before proceeding):
sudo mkdir /mnt/sdb1
sudo mkdir /mnt/sdb2
sudo mount /dev/sdb1 /mnt/sdb1
cd ~/pynqz2_dpu/images/linux/
sudo cp BOOT.BIN image.ub /mnt/sdb1/.
sudo umount /mnt/sdb1
sudo mount /dev/sdb2 /mnt/sdb2
sudo tar xzf rootfs.tar.gz -C /mnt/sdb2/
sync
Copy the board-side DNNDK package to the root user's home directory:
cd ~/Desktop/YOLO-on-PYNQ-Z2/DPU\ implementation/Petalinux/
sudo cp -vr ./zynq7020_dnndk_v3.1 /mnt/sdb2/home/root/
sync
sudo umount /mnt/sdb2
4.2 Deploying Custom Compiled YOLO Kernel
Copy your custom compiled YOLO kernel (dpu_yolo.elf) and info files to the application deployment folder:
cd ~/Desktop/YOLO-on-PYNQ-Z2/DNNDK/compilation/compile
cp dpu_yolo.elf ~/Desktop/YOLO-on-PYNQ-Z2/Deployment/yolo_pynqz2/model/.
cp yolo_kernel_graph.jpg ~/Desktop/YOLO-on-PYNQ-Z2/Deployment/yolo_pynqz2/info/.
cp yolo_kernel.info ~/Desktop/YOLO-on-PYNQ-Z2/Deployment/yolo_pynqz2/info/.
4.3 Booting and Verifying the DPU
Insert the SD card into PYNQ-Z2, power on the board, and wait for Linux to boot. Verify that the DPU IP registers are accessible using devmem. If register values are successfully returned, the DPU core is properly initialized (run directly on the PYNQ-Z2 board):
Note: Ifdevmemhangs or throws an error, verify thatbase-addrin the device tree matches the Address Editor and that the FPGA bitstream was successfully programmed (refer to the BOOT.BIN notes in Section 2.3.3).
Log in via SSH/serial terminal (e.g. MobaXterm) with username root and password root:
4.4 Installing DNNDK on the Board
(Executed on the PYNQ-Z2 board)
root@pynqz2_dpu:~# cd zynq7020_dnndk_v3.1/
root@pynqz2_dpu:~/zynq7020_dnndk_v3.1# sh install.sh
Transfer the application folder (yolo_pynqz2) to the PYNQ-Z2 board (via SFTP/SCP in MobaXterm):
4.5 Building the Application and Running Inference
(Executed on the PYNQ-Z2 board)
root@pynqz2_dpu:~/yolo_pynqz2# make
root@pynqz2_dpu:~/yolo_pynqz2# ./yolo_image dog.jpg
Upon successful inference, the detected bounding boxes and class labels are rendered as shown below:
5. References
Reference documentation and projects:
Legacy DPU IP for Zynq-7000 is no longer hosted on the main Xilinx site, but can be downloaded from the archive below: