- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from rknn.api import RKNN
- INPUT_SIZE = 64
-
- if __name__ == '__main__':
- # 创建RKNN执行对象
- rknn = RKNN(verbose=True)
- # 配置模型输入,用于NPU对数据输入的预处理
- # channel_mean_value='0 0 0 255',那么模型推理时,将会对RGB数据做如下转换
- # (R - 0)/255, (G - 0)/255, (B - 0)/255。推理时,RKNN模型会自动做均值和归一化处理
- # reorder_channel=’0 1 2’用于指定是否调整图像通道顺序,设置成0 1 2即按输入的图像通道顺序不做调整
- # reorder_channel=’2 1 0’表示交换0和2通道,如果输入是RGB,将会被调整为BGR。如果是BGR将会被调整为RGB
- #图像通道顺序不做调整
- rknn.config(channel_mean_value='0 0 0 255', reorder_channel='2 1 0')
-
- # 加载TensorFlow模型
- # tf_pb='digital_gesture.pb'指定待转换的TensorFlow模型
- # inputs指定模型中的输入节点
- # outputs指定模型中输出节点
- # input_size_list指定模型输入的大小
- print('--> Loading model')
- # rknn.load_tensorflow(tf_pb='digital_gesture.pb',
- # inputs=['input_x'],
- # outputs=['probability'],
- # input_size_list=[[INPUT_SIZE, INPUT_SIZE, 3]])
- ret = rknn.load_caffe(model='./pose_deploy_linevec.1.prototxt',
- proto='caffe', blobs='./pose_iter_440000.caffemodel')
- print('done', ret)
-
- # 创建解析pb模型
- # do_quantization=False指定不进行量化
- # 量化会减小模型的体积和提升运算速度,但是会有精度的丢失
- print('--> Building model')
- rknn.build(do_quantization=False)
- print('done')
-
- # 导出保存rknn模型文件
- rknn.export_rknn('./openpose.rknn')
-
- # Release RKNN Context
- rknn.release()
复制代码
- rknn_context context;
- int ret = rknn_init(&context, model_buf, model_len, RKNN_FLAG_PRIOR_HIGH);
- if (model_fp)
- {
- fclose(model_fp);
- model_fp = NULL;
- }
- if (model_buf)
- {
- free(model_buf);
- model_buf = nullptr;
- }
- if (ret < 0)
- {
- std::cerr << "rknn init fail" << std::endl;
- return -1;
- }
- rknn_sdk_version version;
- rknn_query(context, RKNN_QUERY_SDK_VERSION, &version, sizeof(rknn_sdk_version));
- std::cout << "api version: " << version.api_version << std::endl;
- std::cout << "driver version: " << version.drv_version << std::endl;
- std::chrono::time_point<std::chrono::system_clock> startTP = std::chrono::system_clock::now();
- cv::Mat resized_img;
- cv::Size target_size = cv::Size(160, 160);
- cv::resize(input, resized_img, target_size);
- rknn_input inputs[1];
- inputs[0].index = 0;
- inputs[0].buf = resized_img.data;
- inputs[0].size = 3 * 160 * 160;
- inputs[0].pass_through = false;
- inputs[0].type = RKNN_TENSOR_UINT8;
- inputs[0].fmt = RKNN_TENSOR_NCHW;
- ret = rknn_inputs_set(context, 1, inputs);
- if (ret < 0)
- {
- std::cerr << "set input fail" << std::endl;
- return -1;
- }
- ret = rknn_run(context, nullptr);
- rknn_output outputs[1];
- outputs[0].want_float = true;
- outputs[0].is_prealloc = false;
- ret = rknn_outputs_get(context, 1, outputs, nullptr);
- if (ret < 0)
- {
- std::cerr << "get outputs fail" << std::endl;
- return -1;
- }
- void* ptr = (void*)(outputs[0].buf);
- int sz[] = {1, 57, 20, 20};
- cv::Mat netOutputBlob(4, sz, CV_32F, ptr);
复制代码
Lee 发表于 2019-5-28 09:28
同问,能否将例程开放出来?谢谢
欢迎光临 Toybrick (https://t.rock-chips.com/) | Powered by Discuz! X3.3 |