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

在Jupyter中调用DPU

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

新建notebook

在Jupyter下用Python2模式新建一个notebook
在这里插入图片描述

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])
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值