|
这会在板端调试啥信息都正确,但是结果全是0,是怎么回事呢?
- 2017-08-05 23:04:05.362 20048-20233/com.rockchip.gpadc.ssddemo E/rkssd4j: 查找的设备[0]: type=USB_DEVICE, id=0123456789ABCDEF
- 2017-08-05 23:04:09.608 20048-20233/com.rockchip.gpadc.ssddemo E/rkssd4j: model input num: 1, output num: 1
- 2017-08-05 23:04:26.675 20048-20233/com.rockchip.gpadc.ssddemo E/rkssd4j: model input[0] : , dim: 4, fmt: 0, type: 3, qnt_type: 2
- # 测试代码
- std::string lands_model_path = "/data/user/0/com.rockchip.gpadc.ssddemo/cache/shuffle-stage.rknn";
- FILE *fp_lands = fopen(lands_model_path.c_str(), "rb");
- if(fp_lands == NULL) {
- LOGE("fopen %s fail!\n", lands_model_path.c_str());
- return;
- }
- fseek(fp_lands, 0, SEEK_END);
- int lands_model_len = ftell(fp_lands);
- void *lands_model = malloc(lands_model_len);
- fseek(fp_lands, 0, SEEK_SET);
- if(lands_model_len != fread(lands_model, 1, lands_model_len, fp_lands)) {
- LOGE("fread %s fail!\n", lands_model_path.c_str());
- free(lands_model);
- fclose(fp_lands);
- return;
- }
- fclose(fp_lands);
- // RKNN_FLAG_ASYNC_MASK: enable async mode to use NPU efficiently.
- rknn_context lands_ctx = 0;
- int lands_ret = rknn_init(&lands_ctx, lands_model, lands_model_len, RKNN_FLAG_PRIOR_HIGH);
- free(lands_model);
- if(lands_ret < 0) {
- LOGE("rknn_init shuffle_stage landmarks fail! ret=%d\n", ret);
- return;
- }
- // 查询sdk版本信息
- rknn_sdk_version version;
- lands_ret = rknn_query(lands_ctx, RKNN_QUERY_SDK_VERSION, &version,
- sizeof(rknn_sdk_version));
- LOGE("api version: %s\n", version.api_version);
- LOGE("driver version: %s\n", version.drv_version);
- // 查找设备
- rknn_devices_id devids;
- ret = rknn_find_devices (&devids);
- LOGE("查找设备: n_devices = %d\n", devids.n_devices);
- for(int i=0; i<devids.n_devices; i++) {
- LOGE("查找的设备[%d]: type=%s, id=%s\n", i, devids.types[i], devids.ids[i]);
- }
- // 查询输入Tensor的属性
- rknn_input_output_num io_num;
- ret = rknn_query(lands_ctx, RKNN_QUERY_IN_OUT_NUM, &io_num, sizeof(io_num));
- if(ret < 0) {
- LOGE("rknn_query fail! ret=%d\n", ret);
- }
- LOGE("model input num: %d, output num: %d\n", io_num.n_input, io_num.n_output);
- // 查询Input的Tensor属性
- rknn_tensor_attr input_attrs[io_num.n_input];
- memset(input_attrs, 0, sizeof(input_attrs));
- for (int i = 0; i < io_num.n_input; i++) {
- input_attrs[i].index = i;
- ret = rknn_query(lands_ctx, RKNN_QUERY_INPUT_ATTR, &(input_attrs[i]),
- sizeof(rknn_tensor_attr));
- LOGE("model input[%d] : %s, dim: %d, fmt: %d, type: %d, qnt_type: %d \n", i, \
- input_attrs[i].name, input_attrs[i].n_dims, input_attrs[i].fmt, input_attrs[i].type,\
- input_attrs[i].qnt_type);
- // 打印各维度值就是shape
- }
- // 查询Ouput的Tensor属性
- rknn_tensor_attr output_attrs[io_num.n_output];
- memset(output_attrs, 0, sizeof(output_attrs));
- for (int i = 0; i < io_num.n_output; i++) {
- output_attrs[i].index = i;
- ret = rknn_query(lands_ctx, RKNN_QUERY_OUTPUT_ATTR, &(output_attrs[i]),
- sizeof(rknn_tensor_attr));
- LOGE("model output[%d] : %s, dim: %d, fmt: %d, type: %d, qnt_type: %d \n", i, \
- output_attrs[i].name, output_attrs[i].n_dims, output_attrs[i].fmt, output_attrs[i].type,\
- output_attrs[i].qnt_type);
- }
- // 设置输入
- /// 1. 读取图像作为BGR输入 112x112x3 NHWC格式
- std::string lands_img_path = "/data/user/0/com.rockchip.gpadc.ssddemo/cache/npu_test_lands.png";
- cv::Mat img = cv::imread(lands_img_path.c_str());
- /// 2. 将图像传入RKNN_INPUT_TENSOR
- rknn_input inputs[io_num.n_input];
- memset(inputs, 0, sizeof(inputs));
- for (uint32_t i = 0; i < io_num.n_input; i++){
- inputs[i].index = i;
- inputs[i].type = RKNN_TENSOR_UINT8;
- inputs[i].size = (uint32_t)img.cols*img.rows*img.channels();
- inputs[i].pass_through = (uint8_t)false; // 非直通模式,将当前数据类型传递到模型中,并在模型内部转换;
- inputs[i].fmt = RKNN_TENSOR_NHWC;
- inputs[i].buf = img.data;
- }
- ret = rknn_inputs_set(lands_ctx, io_num.n_input, inputs);
- // 运行rknn模型
- ret = rknn_run(lands_ctx, NULL);
- if(ret != RKNN_SUCC){
- LOGE("rknn_run fail! ret=%d\n", ret);
- }
- // 获取rknn结果信息
- 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 = (uint8_t)true;
- outputs[i].is_prealloc = (uint8_t)false;
- }
- ret = rknn_outputs_get(lands_ctx, io_num.n_output, outputs, NULL);
- // 绘制人脸图像并保存人脸结果
- float *pf_src = (float *)outputs[0].buf;
- for (int i = 0; i < output_attrs[0].dims[2]; ++i) {
- float x = pf_src[2*i];
- float y = pf_src[2*i + 1];
- float x_w = x * img.cols;
- float y_h = y * img.rows;
- cv::circle(img,cv::Point(x_w, y_h),2, cv::Scalar(0,0,255),1, 8);
- }
- std::string lands_img_out = "/data/user/0/com.rockchip.gpadc.ssddemo/cache/npu_res_lands.png";
- cv::imwrite(lands_img_out.c_str(), img);
- // 释放输出结果的内存
- ret = rknn_outputs_release(lands_ctx, io_num.n_output, outputs);
- // 释放模型
- rknn_destroy(lands_ctx);
- 2017-08-05 23:04:26.675 20048-20233/com.rockchip.gpadc.ssddemo E/rkssd4j: model output[0] : , dim: 4, fmt: 0, type: 3, qnt_type: 2
其中输出全是0,能分析一下 怎么回事吗? |
|