|
本帖最后由 shopping 于 2019-10-23 16:35 编辑
问一下论坛里的各路大神及技术支持,官方说1.2版本RKNN支持输入一个batch,刚好我也有需要,所以写了一下:
- // Start Inference
- rknn_input inputs[2];
- rknn_output outputs[4];
- rknn_tensor_attr outputs_attr[4];
- int ret = 0;
- rknn_context ctx = 0;
- ret = rknn_init(&ctx, pmodel, model_len, RKNN_FLAG_PRIOR_MEDIUM);
- if(ret < 0) {
- printf("rknn_init fail! ret=%d\n", ret);
- return;
- }
- for(int i = 0;i < 4;i++){
- outputs_attr[i].index = i;
- ret = rknn_query(ctx, RKNN_QUERY_OUTPUT_ATTR, &(outputs_attr[i]), sizeof(outputs_attr[i]));
- if(ret < 0) {
- printf("rknn_query fail! ret=%d\n", ret);
- return;
- }
- }
- if (thread_id > sizeof(multi_npu_process_initialized) / sizeof(int) - 1){
- return;
- }
- multi_npu_process_initialized[thread_id] = 1;
- printf("The initialization of NPU Process %d has been completed.\n", thread_id);
- pair<Mat, Mat> pairIndexImage;
- while (true) {
- mtxQueueInput.lock();
- if (queueInput.size() < 2) {
- mtxQueueInput.unlock();
- usleep(20000);
- } else {
- // Get an image from input queue
- pairIndexImage = queueInput.front(); // the same time only allow one pthread operation to change the pairIndexImage .
- queueInput.pop();
- mtxQueueInput.unlock();
- }
- cv::resize(pairIndexImage.first, image_1, cv::Size(img_width, img_height), (0, 0), (0, 0), cv::INTER_LINEAR);
- cv::resize(pairIndexImage.second, image_2, cv::Size(img_width, img_height), (0, 0), (0, 0), cv::INTER_LINEAR);
- inputs[0].index = input_index;
- inputs[0].buf = image_1.data;
- inputs[0].size = img_width * img_height * img_channels;
- inputs[0].pass_through = false;
- inputs[0].type = RKNN_TENSOR_UINT8;
- inputs[0].fmt = RKNN_TENSOR_NHWC;
- inputs[1].index = input_index;
- inputs[1].buf = image_2.data;
- inputs[1].size = img_width * img_height * img_channels;
- inputs[1].pass_through = false;
- inputs[1].type = RKNN_TENSOR_UINT8;
- inputs[1].fmt = RKNN_TENSOR_NHWC;
- ret = rknn_inputs_set(ctx, 2, inputs);
- if(ret < 0) {
- printf("rknn_input_set fail! ret=%d\n", ret);
- return;
- }
- ret = rknn_run(ctx, nullptr);
- if(ret < 0) {
- printf("rknn_run fail! ret=%d\n", ret);
- return;
- }
- outputs[0].want_float = true;
- outputs[0].is_prealloc = false;
- outputs[1].want_float = true;
- outputs[1].is_prealloc = false;
- outputs[2].want_float = true;
- outputs[2].is_prealloc = false;
- outputs[3].want_float = true;
- outputs[3].is_prealloc = false;
- ret = rknn_outputs_get(ctx, 4, outputs, nullptr);
- if(ret < 0) {
- printf("rknn_outputs_get fail! ret=%d\n", ret);
- return;
- }
关键是RKNN的输出吧,请问 inputs[0] 是与 outpus[0]、outpus[1] , inputs[1] 是与 outpus[2]、outpus[3] 对应的吗?有需要我可以贴完整代码,方便各位帮忙。
|
|