|
本帖最后由 自由的学习 于 2023-9-11 15:57 编辑
用C++推理RKNN模型遇到了一个很奇葩的问题,如果是单独用一张图片推理结果是完全正确的,然后我循环读取多张图片去推理,就出现了一个很奇怪的现象,第一张结果正确,第二张的推理结果还是第一张的,第三张是第二张的结果,依次类推之后每一张的结果都是前一张图片的了;经检查代码,先将每次读取的图片数据在传入resize_buf前保存下来,查看是没问题的,resize_buf接收数据前都有memset,用rga进行resize和转格式将数据塞给resize_buf,然后就直接传给inputs了,中间也没有其他任何多余处理;最后推理结果就是上面描述的奇怪现象了 我的API和DRV版本都是1.7.5
下面是处理的接口
- void HRKNNDecode::setInputsData(cv::Mat rgbImg, std::string imgname)
- {
- long long begs = NowMs1();
- int height = rgbImg.rows;
- int width = rgbImg.cols;
- int ret = 0;
- IM_STATUS STATUS;
- rga_buffer_t src;
- rga_buffer_t dst;
- rga_buffer_t mid;
- memset(&src, 0, sizeof(src));
- memset(&dst, 0, sizeof(dst));
- memset(&mid, 0, sizeof(mid));
- memset(resize_buf, 0, m_input_height * m_input_width * m_channel);
- rknn_input inputs[1];
- memset(inputs, 0, sizeof(inputs));
- inputs[0].index = 0;
- inputs[0].size = m_input_width * m_input_height * m_channel;
- inputs[0].pass_through = 0;
- inputs[0].fmt = RKNN_TENSOR_NHWC;
- inputs[0].type = RKNN_TENSOR_UINT8;
- long long beforecvttimes, cvttimes, resizetimes;
- src = wrapbuffer_virtualaddr((void *)rgbImg.data, width, height, RK_FORMAT_BGR_888);
- dst = wrapbuffer_virtualaddr((void *)resize_buf, m_input_width, m_input_height, RK_FORMAT_RGB_888);
- if (src.width == 0 || dst.width == 0) {
- printf("wrapbuffer_virtualaddr src or dst width == 0,%s\n", imStrError());
- return;
- }
-
- STATUS = imresize(src, dst);
- if (STATUS == IM_STATUS_SUCCESS) {
- //printf("---------------imresize success!\n");
- }
- else {
- printf("**********imresize failed,ret==%d,%s\n", STATUS, imStrError(STATUS));
- return;
- }
- inputs[0].buf = resize_buf;
- long long afterimgs = NowMs1();
- printf("before process times == %lld\n", afterimgs - begs);
- int res = rknn_inputs_set(m_ctx, 1, inputs);
- if (res < 0)
- {
- printf("rknn_input_set fail! ret=%d\n", res);
- return;
- }
- rknn_output outputs[io_num.n_output];
- memset(outputs, 0, sizeof(outputs));
- for (int i = 0; i < io_num.n_output; i++)
- {
- outputs[i].want_float = 0;
- outputs[i].index = i;
- }
- res = rknn_run(m_ctx, NULL);
- if (res < 0)
- {
- printf("rknn_run fail!ret=%d\n", res);
- return;
- }
- res = rknn_outputs_get(m_ctx, io_num.n_output, outputs, NULL);
- if (res < 0)
- {
- printf("rknn_outputs_get fail!ret=%d\n", res);
- rknn_outputs_release(m_ctx, io_num.n_output, outputs);
- return;
- }
- long long rknntimes = NowMs1();
- printf("rknn run times == %lld\n", rknntimes - afterimgs);
- float scale_w = (float)width / m_input_width;
- float scale_h = (float)height / m_input_height;
- std::vector<OutputPose> output_res;
- uint8_t* outputs_buf[io_num.n_output];
- memset(outputs_buf, 0, sizeof(outputs_buf));
- for (int i = 0; i < io_num.n_output; ++i)
- {
- outputs_buf[i] = (uint8_t*)outputs[i].buf;
- }
- if (post_process(outputs_buf, out_zps, out_scales, output_res, scale_w, scale_h, width, height))
- {
- //
- }
- else
- {
- printf("have res == %d\n", output_res.size());
- long long postptime = NowMs1();
- printf("postprocess times == %lld\n", postptime - rknntimes);
- //DrawPred(rgbImg, output_res, SKELETON, KPS_COLORS, LIMB_COLORS);
- //cv::imwrite(imgname, rgbImg);
- }
- rknn_outputs_release(m_ctx, io_num.n_output, outputs);
- return;
- }
|
|