Toybrick

rknn 输入处理

yyc_lll

新手上路

积分
7
楼主
发表于 2025-1-9 13:58:53    查看: 271|回复: 0 | [复制链接]    打印 | 只看该作者
在把python转为C++时,遇到这样一个问题
如下为python代码    descriptors1 = np.expand_dims(descriptors1.transpose(), axis=0)
    descriptors2 = np.expand_dims(descriptors2.transpose(), axis=0)
    keypoints1 = np.expand_dims(keypoints1, axis=0).astype(np.float32)
    keypoints2 = np.expand_dims(keypoints2, axis=0).astype(np.float32)
    scores1 = np.expand_dims(scores1, axis=0)
    scores2 = np.expand_dims(scores2, axis=0)


    start = time.time()
    # shape, score, kpts, desc
    temp1 = superglue.inference(inputs=[np.array([1, 1, img_highth, img_width], dtype=np.float32),
                                               scores1,
                                               keypoints1,
                                               descriptors1,
                                               np.array([1, 1, img_highth, img_width], dtype=np.float32),
                                               scores2,
                                               keypoints2,
                                               descriptors2])

在C++中对应的cv::mat如下:
cv_keypoints1 shape: (1000, 2)
cv_scores1 shape: (1000, 1)
cv_descriptors1 shape: (256, 1000)
cv_keypoints2 shape: (1000, 2)
cv_scores2 shape: (1000, 1)
cv_descriptors2 shape: (256, 1000)

转为如何设置对应的 Ort::Value tensor
        int super_glue_input_image_shape_size=4;
        int64_t super_glue_input_num_image_shape_shape[]={4};
        Ort::Value image_shape_vec_tensor = Ort::Value::CreateTensor<float32_t >(memory_info,  image_shape_t.data(), super_glue_input_image_shape_size,super_glue_input_num_image_shape_shape, 1);
        int super_glue_input_scores_size = 1000;
        int64_t super_glue_input_scores_shape[] = {1, 1000};
        Ort::Value scores_vec_tensor1 = Ort::Value::CreateTensor<float32_t >(memory_info,  reinterpret_cast<float *>(cv_scores1.data), super_glue_input_scores_size,super_glue_input_scores_shape, 2);
        Ort::Value scores_vec_tensor2 = Ort::Value::CreateTensor<float32_t >(memory_info,  reinterpret_cast<float *>(cv_scores2.data), super_glue_input_scores_size,super_glue_input_scores_shape, 2);
        int super_glue_input_keypoints_size = 2000;
        int64_t super_glue_input_keypoints_shape[] = {1, 1000, 2};
        Ort::Value keypoints_vec_tensor1 = Ort::Value::CreateTensor<float32_t >(memory_info, reinterpret_cast<float *>(cv_keypoints1.data), super_glue_input_keypoints_size,super_glue_input_keypoints_shape, 3);
        Ort::Value keypoints_vec_tensor2 = Ort::Value::CreateTensor<float32_t >(memory_info, reinterpret_cast<float *>(cv_keypoints2.data), super_glue_input_keypoints_size,super_glue_input_keypoints_shape, 3);
        int super_glue_input_descriptors_size = 256000;
        int64_t super_glue_input_num_descriptors_shape[] = {1, 256, 1000};
        Ort::Value descriptors_vec_tensor1 = Ort::Value::CreateTensor<float32_t >(memory_info, reinterpret_cast<float *>(cv_descriptors1.data), super_glue_input_descriptors_size,super_glue_input_num_descriptors_shape, 3);
        Ort::Value descriptors_vec_tensor2 = Ort::Value::CreateTensor<float32_t >(memory_info, reinterpret_cast<float *>(cv_descriptors2.data), super_glue_input_descriptors_size,super_glue_input_num_descriptors_shape, 3);
在输入到对应的 superglue_stage1_inputs[2].buf = const_cast<void*>(static_cast<const void*>(keypoints_vec_tensor1.GetTensorData<float>()));
        ret = rknn_inputs_set(superglue_stage1_ctx, 8, superglue_stage1_inputs);
        ret = rknn_run(superglue_stage1_ctx, NULL);
        ret = rknn_outputs_get(superglue_stage1_ctx, 1, superglue_stage1_output, NULL);


发现结果和python不一致,请问该如何对对应的cv::mat处理呢?cv_keypoints1 shape: (1000, 2)
cv_scores1 shape: (1000, 1)
cv_descriptors1 shape: (256, 1000)
cv_keypoints2 shape: (1000, 2)
cv_scores2 shape: (1000, 1)
cv_descriptors2 shape: (256, 1000)
这是模型的输入参数
superglue_stage1_ctx model input num: 8, output num: 1
输入张量属性  index=0, name=image0_shape, n_dims=1, dims=[4], n_elems=4, size=8, w_stride = 0, size_with_stride=8, fmt=UNDEFINED, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000
输入张量属性  index=1, name=scores0, n_dims=2, dims=[1, 1000], n_elems=1000, size=2000, w_stride = 0, size_with_stride=2000, fmt=UNDEFINED, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000
输入张量属性  index=2, name=keypoints0, n_dims=3, dims=[1, 1000, 2], n_elems=2000, size=4000, w_stride = 0, size_with_stride=4000, fmt=UNDEFINED, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000
输入张量属性  index=3, name=descriptors0, n_dims=3, dims=[1, 256, 1000], n_elems=256000, size=512000, w_stride = 0, size_with_stride=512000, fmt=UNDEFINED, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000
输入张量属性  index=4, name=image1_shape, n_dims=1, dims=[4], n_elems=4, size=8, w_stride = 0, size_with_stride=8, fmt=UNDEFINED, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000
输入张量属性  index=5, name=scores1, n_dims=2, dims=[1, 1000], n_elems=1000, size=2000, w_stride = 0, size_with_stride=2000, fmt=UNDEFINED, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000
输入张量属性  index=6, name=keypoints1, n_dims=3, dims=[1, 1000, 2], n_elems=2000, size=4000, w_stride = 0, size_with_stride=4000, fmt=UNDEFINED, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000
输入张量属性  index=7, name=descriptors1, n_dims=3, dims=[1, 256, 1000], n_elems=256000, size=512000, w_stride = 0, size_with_stride=512000, fmt=UNDEFINED, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000
输出张量属性  index=0, name=/Div_4_output_0, n_dims=3, dims=[1, 1000, 1000], n_elems=1000000, size=2000000, w_stride = 0, size_with_stride=2000000, fmt=UNDEFINED, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000










回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

产品中心 购买渠道 开源社区 Wiki教程 资料下载 关于Toybrick


快速回复 返回顶部 返回列表