forked from ROCm/MIVisionX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDGtest.cpp
154 lines (128 loc) · 4.85 KB
/
DGtest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <opencv2/opencv.hpp>
#include <vx_ext_amd.h>
#include "DGtest.h"
using namespace cv;
using namespace std;
#define ERROR_CHECK_OBJECT(obj) { vx_status status = vxGetStatus((vx_reference)(obj)); if(status != VX_SUCCESS) { vxAddLogEntry((vx_reference)context, status , "ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status, __LINE__); return status; } }
#define ERROR_CHECK_STATUS(call) { vx_status status = (call); if(status != VX_SUCCESS) { printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status, __LINE__); exit(-1); } }
static void VX_CALLBACK log_callback(vx_context context, vx_reference ref, vx_status status, const vx_char string[])
{
size_t len = strlen(string);
if (len > 0) {
printf("%s", string);
if (string[len - 1] != '\n')
printf("\n");
fflush(stdout);
}
}
DGtest::DGtest(const char* weights) {
// create contex
mContext = vxCreateContext();
vx_status status;
status = vxGetStatus((vx_reference)mContext);
if(status) {
printf("ERROR: vxCreateContext() failed\n");
exit(-1);
}
vxRegisterLogCallback(mContext, log_callback, vx_false_e);
// create graph
mGraph = vxCreateGraph(mContext);
status = vxGetStatus((vx_reference)mGraph);
if(status) {
printf("ERROR: vxCreateGraph(...) failed (%d)\n", status);
exit(-1);
}
// create and initialize input tensor data
vx_size input_dims[4] = { 28, 28, 1, 1 };
mInputTensor = vxCreateTensor(mContext, 4, input_dims, VX_TYPE_FLOAT32, 0);
if(vxGetStatus((vx_reference)mInputTensor)) {
printf("ERROR: vxCreateTensor() failed\n");
exit(-1);
}
// create and initialize output tensor data
vx_size output_dims[4] = { 1, 1, 10, 1 };
mOutputTensor = vxCreateTensor(mContext, 4, output_dims, VX_TYPE_FLOAT32, 0);
if(vxGetStatus((vx_reference)mOutputTensor)) {
printf("ERROR: vxCreateTensor() failed for mOutputTensor\n");
exit(-1);
}
//add input tensor, output tensor, and weights to the graph
status = annAddToGraph(mGraph, mInputTensor, mOutputTensor, weights);
if(status) {
printf("ERROR: annAddToGraph() failed (%d)\n", status);
exit(-1);
}
//verify the graph
status = vxVerifyGraph(mGraph);
if(status) {
printf("ERROR: vxVerifyGraph(...) failed (%d)\n", status);
exit(-1);
}
};
DGtest::~DGtest(){
//release the tensors
ERROR_CHECK_STATUS(vxReleaseTensor(&mInputTensor));
ERROR_CHECK_STATUS(vxReleaseTensor(&mOutputTensor));
//release the graph
ERROR_CHECK_STATUS(vxReleaseGraph(&mGraph));
// release context
ERROR_CHECK_STATUS(vxReleaseContext(&mContext));
};
int DGtest::runInference(Mat &image) {
Mat img = image.clone();
// convert to grayscale image
cvtColor(img, img, CV_BGR2GRAY);
// resize to 24 x 24
resize(img, img, Size(24, 24));
// dilate image
dilate(img, img, Mat::ones(2,2,CV_8U));
// add border to the image so that the digit will go center and become 28 x 28 image
copyMakeBorder(img, img, 2, 2, 2, 2, BORDER_CONSTANT, Scalar(0,0,0));
vx_size dims[4] = { 1, 1, 1, 1 }, stride[4];
vx_status status;
vx_map_id map_id;
float * ptr;
// query tensor for the dimension
vxQueryTensor(mInputTensor, VX_TENSOR_DIMS, &dims, sizeof(dims[0])*4);
// copy image to input tensor
status = vxMapTensorPatch(mInputTensor, 4, nullptr, nullptr, &map_id, stride, (void **)&ptr, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST);
if(status) {
std::cerr << "ERROR: vxMapTensorPatch() failed for " << std::endl;
return -1;
}
for(vx_size y = 0; y < dims[1]; y++) {
unsigned char * src = img.data + y*dims[0]*dims[2];
float * dst = ptr + ((y * stride[1]) >> 2);
for(vx_size x = 0; x < dims[0]; x++, src++) {
*dst++ = src[0];
}
}
status = vxUnmapTensorPatch(mInputTensor, map_id);
if(status) {
std::cerr << "ERROR: vxUnmapTensorPatch() failed for " << std::endl;
return -1;
}
//process the graph
status = vxProcessGraph(mGraph);
if(status != VX_SUCCESS) {
std::cerr << "ERROR: vxProcessGraph() failed" << std::endl;
return -1;
}
// get the output result from output tensor
status = vxMapTensorPatch(mOutputTensor, 4, nullptr, nullptr, &map_id, stride, (void **)&ptr, VX_READ_ONLY, VX_MEMORY_TYPE_HOST);
if(status) {
std::cerr << "ERROR: vxMapTensorPatch() failed for " << std::endl;
return -1;
}
mDigit = std::distance(ptr, std::max_element(ptr, ptr + 10));
status = vxUnmapTensorPatch(mOutputTensor, map_id);
if(status) {
std::cerr << "ERROR: vxUnmapTensorPatch() failed for " << std::endl;
return -1;
}
return 0;
}
int DGtest::getResult() {
return mDigit;
}