DPU on PYNQ-Z2系列—3.3 部署DPU—在Jupyter中调用DPU

在Jupyter中调用DPU

DNNDK3.1提供了Python的编程接口,在此以ResNet50为例示范如何在Jupyter里调用DPU

新建notebook

在Jupyter下用Python2模式新建一个notebook
DPU on PYNQ-Z2系列—3.3 部署DPU—在Jupyter中调用DPU_第1张图片

import库

import sys
sys.path.append('/usr/local/lib/python2.7/dist-packages')
from dnndk import n2cube, dputils
from ctypes import *
import cv2
import numpy as np
import os
import threading
import time
import sys
from matplotlib import pyplot as plt
import matplotlib

需要注意的是,jupyter默认调用的是Python3,而DNNDK提供的是Python2的接口,因此需要手动把python2的路径添加到路径中

加载图片

img_path = '/home/xilinx/val5000/ILSVRC2012_val_00000020.JPEG'
img = plt.imread(img_path)
plt.imshow(img)
img = cv2.imread(img_path)

运行

KERNEL_CONV = "resnet50v1_0"
KERNEL_CONV_INPUT = "resnet_v1_50_conv1_Conv2D"
KERNEL_FC_OUTPUT = "resnet_v1_50_logits_Conv2D"
# Attach to DPU driver and prepare for runing
n2cube.dpuOpen()
# Create DPU Kernels for ResNet50
kernel = n2cube.dpuLoadKernel(KERNEL_CONV)
# Create DPU Tasks from DPU Kernel
task = n2cube.dpuCreateTask(kernel, 0)
# Get the output tensor channel from FC output
channel = n2cube.dpuGetOutputTensorChannel(task, KERNEL_FC_OUTPUT)

FCResult = [0 for i in range(channel)]
mean = [103.939,116.779,123.68]
# Load image to DPU
dputils.dpuSetInputImage(task, KERNEL_CONV_INPUT, img, mean)
# Model run on DPU
n2cube.dpuRunTask(task)
# Get the output from FC output
n2cube.dpuGetOutputTensorInHWCFP32(task, KERNEL_FC_OUTPUT, FCResult, channel)
# Get the label
label = FCResult.index(max(FCResult))
print(label, wordlist[label])

你可能感兴趣的:(DPU,on,PYNQ-Z2)