1 // Copyright (c) 2021 by Rockchip Electronics Co., Ltd. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /*-------------------------------------------
16 Includes
17 -------------------------------------------*/
18 #include "opencv2/core/core.hpp"
19 #include "opencv2/imgcodecs.hpp"
20 #include "opencv2/imgproc.hpp"
21 #include "rknn_api.h"
22
23 #include <float.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/time.h>
28
29 using namespace std;
30 using namespace cv;
31
32 /*-------------------------------------------
33 Functions
34 -------------------------------------------*/
getCurrentTimeUs()35 static inline int64_t getCurrentTimeUs()
36 {
37 struct timeval tv;
38 gettimeofday(&tv, NULL);
39 return tv.tv_sec * 1000000 + tv.tv_usec;
40 }
41
rknn_GetTopN(float * pfProb,float * pfMaxProb,uint32_t * pMaxClass,uint32_t outputCount,uint32_t topNum)42 static int rknn_GetTopN(float* pfProb, float* pfMaxProb, uint32_t* pMaxClass, uint32_t outputCount, uint32_t topNum)
43 {
44 uint32_t i, j;
45 uint32_t top_count = outputCount > topNum ? topNum : outputCount;
46
47 for (i = 0; i < topNum; ++i) {
48 pfMaxProb[i] = -FLT_MAX;
49 pMaxClass[i] = -1;
50 }
51
52 for (j = 0; j < top_count; j++) {
53 for (i = 0; i < outputCount; i++) {
54 if ((i == *(pMaxClass + 0)) || (i == *(pMaxClass + 1)) || (i == *(pMaxClass + 2)) || (i == *(pMaxClass + 3)) ||
55 (i == *(pMaxClass + 4))) {
56 continue;
57 }
58
59 if (pfProb[i] > *(pfMaxProb + j)) {
60 *(pfMaxProb + j) = pfProb[i];
61 *(pMaxClass + j) = i;
62 }
63 }
64 }
65
66 return 1;
67 }
68
dump_tensor_attr(rknn_tensor_attr * attr)69 static void dump_tensor_attr(rknn_tensor_attr* attr)
70 {
71 printf(" index=%d, name=%s, n_dims=%d, dims=[%d, %d, %d, %d], n_elems=%d, size=%d, fmt=%s, type=%s, qnt_type=%s, "
72 "zp=%d, scale=%f\n",
73 attr->index, attr->name, attr->n_dims, attr->dims[0], attr->dims[1], attr->dims[2], attr->dims[3],
74 attr->n_elems, attr->size, get_format_string(attr->fmt), get_type_string(attr->type),
75 get_qnt_type_string(attr->qnt_type), attr->zp, attr->scale);
76 }
77
78 /*-------------------------------------------
79 Main Functions
80 -------------------------------------------*/
main(int argc,char * argv[])81 int main(int argc, char* argv[])
82 {
83 if (argc < 3) {
84 printf("Usage:%s model_path input_path [loop_count]\n", argv[0]);
85 return -1;
86 }
87
88 char* model_path = argv[1];
89 char* input_path = argv[2];
90
91 int loop_count = 1;
92 if (argc > 3) {
93 loop_count = atoi(argv[3]);
94 }
95
96 rknn_context ctx = 0;
97
98 // Load RKNN Model
99 int ret = rknn_init(&ctx, model_path, 0, 0, NULL);
100 if (ret < 0) {
101 printf("rknn_init fail! ret=%d\n", ret);
102 return -1;
103 }
104
105 // Get sdk and driver version
106 rknn_sdk_version sdk_ver;
107 ret = rknn_query(ctx, RKNN_QUERY_SDK_VERSION, &sdk_ver, sizeof(sdk_ver));
108 if (ret != RKNN_SUCC) {
109 printf("rknn_query fail! ret=%d\n", ret);
110 return -1;
111 }
112 printf("rknn_api/rknnrt version: %s, driver version: %s\n", sdk_ver.api_version, sdk_ver.drv_version);
113
114 // Get Model Input Output Info
115 rknn_input_output_num io_num;
116 ret = rknn_query(ctx, RKNN_QUERY_IN_OUT_NUM, &io_num, sizeof(io_num));
117 if (ret != RKNN_SUCC) {
118 printf("rknn_query fail! ret=%d\n", ret);
119 return -1;
120 }
121 printf("model input num: %d, output num: %d\n", io_num.n_input, io_num.n_output);
122
123 printf("input tensors:\n");
124 rknn_tensor_attr input_attrs[io_num.n_input];
125 memset(input_attrs, 0, io_num.n_input * sizeof(rknn_tensor_attr));
126 for (uint32_t i = 0; i < io_num.n_input; i++) {
127 input_attrs[i].index = i;
128 // query info
129 ret = rknn_query(ctx, RKNN_QUERY_INPUT_ATTR, &(input_attrs[i]), sizeof(rknn_tensor_attr));
130 if (ret < 0) {
131 printf("rknn_init error! ret=%d\n", ret);
132 return -1;
133 }
134 dump_tensor_attr(&input_attrs[i]);
135 }
136
137 printf("output tensors:\n");
138 rknn_tensor_attr output_attrs[io_num.n_output];
139 memset(output_attrs, 0, io_num.n_output * sizeof(rknn_tensor_attr));
140 for (uint32_t i = 0; i < io_num.n_output; i++) {
141 output_attrs[i].index = i;
142 // query info
143 ret = rknn_query(ctx, RKNN_QUERY_OUTPUT_ATTR, &(output_attrs[i]), sizeof(rknn_tensor_attr));
144 if (ret != RKNN_SUCC) {
145 printf("rknn_query fail! ret=%d\n", ret);
146 return -1;
147 }
148 dump_tensor_attr(&output_attrs[i]);
149 }
150
151 // Get custom string
152 rknn_custom_string custom_string;
153 ret = rknn_query(ctx, RKNN_QUERY_CUSTOM_STRING, &custom_string, sizeof(custom_string));
154 if (ret != RKNN_SUCC) {
155 printf("rknn_query fail! ret=%d\n", ret);
156 return -1;
157 }
158 printf("custom string: %s\n", custom_string.string);
159
160 unsigned char* input_data = NULL;
161 rknn_tensor_type input_type = RKNN_TENSOR_UINT8;
162 rknn_tensor_format input_layout = RKNN_TENSOR_NHWC;
163
164 // Load image
165 int req_height = 0;
166 int req_width = 0;
167 int req_channel = 0;
168
169 switch (input_attrs[0].fmt) {
170 case RKNN_TENSOR_NHWC:
171 req_height = input_attrs[0].dims[1];
172 req_width = input_attrs[0].dims[2];
173 req_channel = input_attrs[0].dims[3];
174 break;
175 case RKNN_TENSOR_NCHW:
176 req_height = input_attrs[0].dims[2];
177 req_width = input_attrs[0].dims[3];
178 req_channel = input_attrs[0].dims[1];
179 break;
180 default:
181 printf("meet unsupported layout\n");
182 return -1;
183 }
184
185 int height = 0;
186 int width = 0;
187 int channel = 0;
188
189 cv::Mat orig_img = imread(input_path, cv::IMREAD_COLOR);
190 if (!orig_img.data) {
191 printf("cv::imread %s fail!\n", input_path);
192 return -1;
193 }
194
195 // if origin model is from Caffe, you maybe not need do BGR2RGB.
196 cv::Mat orig_img_rgb;
197 cv::cvtColor(orig_img, orig_img_rgb, cv::COLOR_BGR2RGB);
198
199 cv::Mat img = orig_img_rgb.clone();
200 if (orig_img.cols != req_width || orig_img.rows != req_height) {
201 printf("resize %d %d to %d %d\n", orig_img.cols, orig_img.rows, req_width, req_height);
202 cv::resize(orig_img_rgb, img, cv::Size(req_width, req_height), 0, 0, cv::INTER_LINEAR);
203 }
204 input_data = img.data;
205 if (!input_data) {
206 return -1;
207 }
208
209 // Create input tensor memory
210 rknn_tensor_mem* input_mems[1];
211 // default input type is int8 (normalize and quantize need compute in outside)
212 // if set uint8, will fuse normalize and quantize to npu
213 input_attrs[0].type = input_type;
214 // default fmt is NHWC, npu only support NHWC in zero copy mode
215 input_attrs[0].fmt = input_layout;
216
217 input_mems[0] = rknn_create_mem(ctx, input_attrs[0].size_with_stride);
218
219 // Copy input data to input tensor memory
220 width = input_attrs[0].dims[2];
221 int stride = input_attrs[0].w_stride;
222
223 if (width == stride) {
224 memcpy(input_mems[0]->virt_addr, input_data, width * input_attrs[0].dims[1] * input_attrs[0].dims[3]);
225 } else {
226 int height = input_attrs[0].dims[1];
227 int channel = input_attrs[0].dims[3];
228 // copy from src to dst with stride
229 uint8_t* src_ptr = input_data;
230 uint8_t* dst_ptr = (uint8_t*)input_mems[0]->virt_addr;
231 // width-channel elements
232 int src_wc_elems = width * channel;
233 int dst_wc_elems = stride * channel;
234 for (int h = 0; h < height; ++h) {
235 memcpy(dst_ptr, src_ptr, src_wc_elems);
236 src_ptr += src_wc_elems;
237 dst_ptr += dst_wc_elems;
238 }
239 }
240
241 // Create output tensor memory
242 rknn_tensor_mem* output_mems[io_num.n_output];
243 for (uint32_t i = 0; i < io_num.n_output; ++i) {
244 // default output type is depend on model, this require float32 to compute top5
245 // allocate float32 output tensor
246 int output_size = output_attrs[i].n_elems * sizeof(float);
247 output_mems[i] = rknn_create_mem(ctx, output_size);
248 }
249
250 // Set input tensor memory
251 ret = rknn_set_io_mem(ctx, input_mems[0], &input_attrs[0]);
252 if (ret < 0) {
253 printf("rknn_set_io_mem fail! ret=%d\n", ret);
254 return -1;
255 }
256
257 // Set output tensor memory
258 for (uint32_t i = 0; i < io_num.n_output; ++i) {
259 // default output type is depend on model, this require float32 to compute top5
260 output_attrs[i].type = RKNN_TENSOR_FLOAT32;
261 // set output memory and attribute
262 ret = rknn_set_io_mem(ctx, output_mems[i], &output_attrs[i]);
263 if (ret < 0) {
264 printf("rknn_set_io_mem fail! ret=%d\n", ret);
265 return -1;
266 }
267 }
268
269 // Run
270 printf("Begin perf ...\n");
271 for (int i = 0; i < loop_count; ++i) {
272 int64_t start_us = getCurrentTimeUs();
273 ret = rknn_run(ctx, NULL);
274 int64_t elapse_us = getCurrentTimeUs() - start_us;
275 if (ret < 0) {
276 printf("rknn run error %d\n", ret);
277 return -1;
278 }
279 printf("%4d: Elapse Time = %.2fms, FPS = %.2f\n", i, elapse_us / 1000.f, 1000.f * 1000.f / elapse_us);
280 }
281
282 // Get top 5
283 uint32_t topNum = 5;
284 for (uint32_t i = 0; i < io_num.n_output; i++) {
285 uint32_t MaxClass[topNum];
286 float fMaxProb[topNum];
287 float* buffer = (float*)output_mems[i]->virt_addr;
288 uint32_t sz = output_attrs[i].n_elems;
289 int top_count = sz > topNum ? topNum : sz;
290
291 rknn_GetTopN(buffer, fMaxProb, MaxClass, sz, topNum);
292
293 printf("---- Top%d ----\n", top_count);
294 for (int j = 0; j < top_count; j++) {
295 printf("%8.6f - %d\n", fMaxProb[j], MaxClass[j]);
296 }
297 }
298
299 // Destroy rknn memory
300 rknn_destroy_mem(ctx, input_mems[0]);
301 for (uint32_t i = 0; i < io_num.n_output; ++i) {
302 rknn_destroy_mem(ctx, output_mems[i]);
303 }
304
305 // destroy
306 rknn_destroy(ctx);
307 return 0;
308 }
309