Skip to content

Commit

Permalink
Update DNNDK low-level API library
Browse files Browse the repository at this point in the history
  • Loading branch information
shua1zhang authored Jan 5, 2020
1 parent adad37f commit 99adfed
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 10 deletions.
Binary file added DPU-TRD/app/Vitis/dnndk/libdpuaol.so
Binary file not shown.
Binary file added DPU-TRD/app/Vitis/dnndk/libhineon.so
Binary file not shown.
Binary file modified DPU-TRD/app/Vitis/dnndk/libn2cube.so
Binary file not shown.
26 changes: 20 additions & 6 deletions DPU-TRD/app/Vitis/samples/resnet50/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
## Copyright 2019 Xilinx Inc.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.

CROSS_COMPILE := aarch64-linux-gnu-
SYSROOT := /home/yijianlong/platforms/zcu104_20183_PFM_20191_XRT/sdk/sysroots/aarch64-xilinx-linux
#SYSROOT = /home/yijianlong/platforms/zcu104_xrt/sdk/sysroots/aarch64-xilinx-linux
SYSROOT := /home/sysroots/aarch64-xilinx-linux

CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++

Expand All @@ -14,20 +27,21 @@ VPATH = $(SRC)
CFLAGS = -I. -I${SRC} -I${HEADER} -fPIC -std=c++11 -O2
CFLAGS += -I${SYSROOT}/usr/include -I${SYSROOT}/usr/local/include -I../../n2cube/include

LDFLAGS = -L../../lib -L${SYSROOT}/usr/lib -L${SYSROOT}/lib -L../../n2cube/build -L$(SYSROOT)/opt/xilinx/xrt/lib
LDFLAGS += -ln2cube -lopencv_imgcodecs -lopencv_highgui -lopencv_imgproc -lopencv_core -pthread -lxrt_core
LDFLAGS = -L/lib -L/usr/lib -L/usr/local/lib
LDFLAGS += -ln2cube -lhineon -lopencv_imgcodecs -lopencv_highgui -lopencv_imgproc -lopencv_core -pthread -lxrt_core


CFLAGS += -mcpu=cortex-a53


OBJ = main.o
OBJ = main.o
OBJ += dputils.o

all: $(BUILD) resnet50
.PHONY : clean

resnet50: $(OBJ)
$(CXX) --sysroot=${SYSROOT} $(addprefix $(BUILD)/, $^) -o $@ ${LDFLAGS}
$(CXX) $(addprefix $(BUILD)/, $^) -o $@ ${LDFLAGS}

%.o : %.cc
$(CXX) -c $(CFLAGS) $< -o $(BUILD)/$@
Expand Down
Binary file not shown.
Binary file modified DPU-TRD/app/Vitis/samples/resnet50/build/main.o
Binary file not shown.
Binary file modified DPU-TRD/app/Vitis/samples/resnet50/resnet50
Binary file not shown.
144 changes: 144 additions & 0 deletions DPU-TRD/app/Vitis/samples/resnet50/src/dputils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright 2019 Xilinx Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <dnndk/n2cube.h>
#include "dputils.h"

using namespace std;
using namespace cv;
#define N2CUBE_SUCCESS 0
#define USE_NEON_OPT
/**
* @brief Set image into DPU Task's input tensor, multiple IO supported.
*
* @note source data must be in in Caffe order: channel, height, width;
* source data type must be int8_t;
* source data will be converted from Caffe order to DPU order
*
* @param task - pointer to DPU task
* @param nodeName - pointer to DPU Node name
* @param image - input image in OpenCV Mat format. Single channel and 3- channel input image are supported
* Note: Only support CV_8U, please modify the code for other types.
* @param mean - pointer to mean value array which contains 1 member for single channel input image or 3 members for 3-channel input image
* Note: You can get the mean values from the input Caffe prototxt. At present, the format of mean value file is not yet supported.
* @param scale - scale value of input image
* @param idx - the index of a single input tensor for the Node, with default value as 0
*
* @return 0 on success, or negative error ID in case of failure.
*/
int dpuSetInputImageWithScale(DPUTask *task, const char* nodeName, const cv::Mat &image, float *mean, float scale, int idx)
{
int value;
int8_t *inputAddr;
unsigned char *resized_data;
cv::Mat newImage;
float scaleFix;
int height, width, channel;

height = dpuGetInputTensorHeight(task, nodeName, idx);
width = dpuGetInputTensorWidth(task, nodeName, idx);
channel = dpuGetInputTensorChannel(task, nodeName, idx);

if (height == image.rows && width == image.cols) {
newImage = image;
} else {
newImage = cv::Mat (height, width, CV_8SC3,
(void*)dpuGetInputTensorAddress(task, nodeName, idx));
cv::resize(image, newImage, newImage.size(), 0, 0, cv::INTER_LINEAR);
}
resized_data = newImage.data;

inputAddr = dpuGetInputTensorAddress(task, nodeName, idx);
scaleFix = dpuGetInputTensorScale(task, nodeName, idx);

scaleFix = scaleFix*scale;

if (newImage.channels() == 1) {
for (int idx_h=0; idx_h<height; idx_h++) {
for (int idx_w=0; idx_w<width; idx_w++) {
for (int idx_c=0; idx_c<channel; idx_c++) {
value = *(resized_data+idx_h*width*channel+idx_w*channel+idx_c);
value = (int)((value - *(mean+idx_c)) * scaleFix);
inputAddr[idx_h*newImage.cols+idx_w] = (char)value;
}
}
}
} else {
#ifdef USE_NEON_OPT
dpuProcessNormalizion(inputAddr, newImage.data, newImage.rows, newImage.cols, mean, scaleFix, newImage.step1());
#else
for (int idx_h=0; idx_h<newImage.rows; idx_h++) {
for (int idx_w=0; idx_w<newImage.cols; idx_w++) {
for (int idx_c=0; idx_c<3; idx_c++) {
value = (int)((newImage.at<Vec3b>(idx_h, idx_w)[idx_c] - mean[idx_c]) * scaleFix);
inputAddr[idx_h*newImage.cols*3+idx_w*3+idx_c] = (char)value;
}
}
}
#endif
}

return N2CUBE_SUCCESS;
}


/**
* @brief Set image into DPU Task's input tensor with mean values, multiple IO supported.
*
* @note source data must be in in Caffe order: channel, height, width;
* source data type must be int8_t;
* source data will be converted from Caffe order to DPU order
*
* @param task - pointer to DPU task
* @param nodeName - pointer to DPU Node name
* @param image - input image in OpenCV Mat format. Single channel and 3- channel input image are supported
* Note: Only support CV_8U, please modify the code for other types.
* @param mean - pointer to mean value array which contains 1 member for single channel input image or 3 members for 3-channel input image
* Note: You can get the mean values from the input Caffe prototxt. At present, the format of mean value file is not yet supported
* @param idx - the index of a single input tensor for the Node, with default value as 0
*
* @return 0 on success, or negative error ID in case of failure.
*/
int dpuSetInputImage(DPUTask *task, const char* nodeName, const cv::Mat &image, float *mean, int idx)
{

return dpuSetInputImageWithScale(task, nodeName, image, mean, 1.0f, idx);
}


/**
* @brief Set image into DPU Task's input tensor without mean values, multiple IO supported.
*
* @note source data must be in in Caffe order: channel, height, width;
* source data type must be int8_t;
* source data will be converted from Caffe order to DPU order
*
* @param task - pointer to DPU task
* @param nodeName - pointer to DPU Node name
* @param image - input image in OpenCV Mat format. Single channel and 3- channel input image are supported
* Note: Only support CV_8U, please modify the code for other types.
* @param idx - the index of a single input tensor for the Node, with default value as 0
*
* @return 0 on success, or negative error ID in case of failure.
*/
int dpuSetInputImage2(DPUTask *task, const char* nodeName, const cv::Mat &image, int idx)
{
float mean[3];

dpuGetKernelMean(task,mean,image.channels()); //This API is only available for Caffe model
return dpuSetInputImageWithScale(task, nodeName, image, mean, 1.0f, idx);
}

37 changes: 37 additions & 0 deletions DPU-TRD/app/Vitis/samples/resnet50/src/dputils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2019 Xilinx Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef _DPUTILS_H_
#define _DPUTILS_H_

#include <opencv2/opencv.hpp>

struct dpu_task;
typedef struct dpu_task DPUTask;


/* Set image into DPU Task's input Tensor */
int dpuSetInputImage(DPUTask *task, const char *nodeName,
const cv::Mat &image, float *mean, int idx = 0);

/* Set image into DPU Task's input Tensor with a specified scale parameter */
int dpuSetInputImageWithScale(DPUTask *task, const char *nodeName,
const cv::Mat &image, float *mean, float scale, int idx = 0);

/* Set image into DPU Task's input Tensor (mean values automatically processed by N2Cube) */
int dpuSetInputImage2(DPUTask *task, const char *nodeName, const cv::Mat &image, int idx = 0);

#endif
7 changes: 4 additions & 3 deletions DPU-TRD/app/Vitis/samples/resnet50/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
*/
#include "main.h"
#include <assert.h>
#include <dirent.h>
#include <dnndk.h>
#include <dnndk/dnndk.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
Expand All @@ -64,13 +63,15 @@
#include <queue>
#include <string>
#include <vector>
#include "main.h"
#include "dputils.h"

using namespace std;
using namespace std::chrono;
using namespace cv;

/* DPU Kernel Name for ResNet50 CONV & FC layers */
#define KRENEL_CONV "resnet50_0"
#define KRENEL_CONV "resnet50"

#define CONV_INPUT_NODE "conv1"
#define CONV_OUTPUT_NODE "res5c_branch2c"
Expand Down
2 changes: 1 addition & 1 deletion DPU-TRD/app/Vitis/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ alias c="clear"

cd ${SD_ROOT}
cp ${SD_ROOT}/dnndk/lib*so ${SD_ROOT}/dpu.xclbin /usr/lib

cp ${SD_ROOT}/models/* /usr/lib


#ifconfig eth0 192.168.1.2 netmask 255.255.255.0
Expand Down

0 comments on commit 99adfed

Please sign in to comment.