当前位置:   article > 正文

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

dpusetinputtensorinhwcfp32

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

需要注意的是,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)
  • 1
  • 2
  • 3
  • 4

运行

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])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号