297 lines
9.5 KiB
C++
297 lines
9.5 KiB
C++
// Copyright (c) 2024 by Rockchip Electronics Co., Ltd. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
|
|
#include "yolov8-pose.h"
|
|
#if defined(YOLO_RUNTIME_SHARED_PREPROCESS)
|
|
#include "yolo_rknn_preprocess.h"
|
|
#endif
|
|
#include "common.h"
|
|
#include "file_utils.h"
|
|
#include "image_utils.h"
|
|
|
|
static thread_local float runtime_confidence_threshold = -1.0f;
|
|
static thread_local float runtime_nms_threshold = -1.0f;
|
|
static thread_local double runtime_preprocess_ms = 0.0;
|
|
static thread_local double runtime_inference_ms = 0.0;
|
|
static thread_local double runtime_postprocess_ms = 0.0;
|
|
|
|
extern "C" void yolo_adapter_set_thresholds(float confidence, float nms)
|
|
{
|
|
runtime_confidence_threshold = confidence;
|
|
runtime_nms_threshold = nms;
|
|
}
|
|
|
|
extern "C" void yolo_adapter_get_performance(double *preprocess_ms, double *inference_ms, double *postprocess_ms)
|
|
{
|
|
if (preprocess_ms) *preprocess_ms = runtime_preprocess_ms;
|
|
if (inference_ms) *inference_ms = runtime_inference_ms;
|
|
if (postprocess_ms) *postprocess_ms = runtime_postprocess_ms;
|
|
}
|
|
|
|
static float runtime_threshold(const char *name, float configured, float fallback)
|
|
{
|
|
if (configured >= 0.0f) return configured;
|
|
const char *value = getenv(name);
|
|
return value ? strtof(value, nullptr) : fallback;
|
|
}
|
|
|
|
#include <sys/time.h>
|
|
|
|
static inline int64_t getCurrentTimeUs()
|
|
{
|
|
struct timeval tv;
|
|
gettimeofday(&tv, NULL);
|
|
return tv.tv_sec * 1000000 + tv.tv_usec;
|
|
}
|
|
|
|
static void dump_tensor_attr(rknn_tensor_attr *attr)
|
|
{
|
|
printf(" index=%d, name=%s, n_dims=%d, dims=[%d, %d, %d, %d], n_elems=%d, size=%d, fmt=%s, type=%s, qnt_type=%s, "
|
|
"zp=%d, scale=%f\n",
|
|
attr->index, attr->name, attr->n_dims, attr->dims[0], attr->dims[1], attr->dims[2], attr->dims[3],
|
|
attr->n_elems, attr->size, get_format_string(attr->fmt), get_type_string(attr->type),
|
|
get_qnt_type_string(attr->qnt_type), attr->zp, attr->scale);
|
|
}
|
|
|
|
int init_yolov8_pose_model(const char *model_path, rknn_app_context_t *app_ctx)
|
|
{
|
|
int ret;
|
|
rknn_context ctx = 0;
|
|
|
|
ret = rknn_init(&ctx, (char *)model_path, 0, 0, NULL);
|
|
if (ret < 0)
|
|
{
|
|
printf("rknn_init fail! ret=%d\n", ret);
|
|
return -1;
|
|
}
|
|
|
|
// Get Model Input Output Number
|
|
rknn_input_output_num io_num;
|
|
ret = rknn_query(ctx, RKNN_QUERY_IN_OUT_NUM, &io_num, sizeof(io_num));
|
|
if (ret != RKNN_SUCC)
|
|
{
|
|
printf("rknn_query fail! ret=%d\n", ret);
|
|
return -1;
|
|
}
|
|
printf("model input num: %d, output num: %d\n", io_num.n_input, io_num.n_output);
|
|
|
|
// Get Model Input Info
|
|
printf("input tensors:\n");
|
|
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(ctx, RKNN_QUERY_INPUT_ATTR, &(input_attrs[i]), sizeof(rknn_tensor_attr));
|
|
if (ret != RKNN_SUCC)
|
|
{
|
|
printf("rknn_query fail! ret=%d\n", ret);
|
|
return -1;
|
|
}
|
|
dump_tensor_attr(&(input_attrs[i]));
|
|
}
|
|
|
|
// Get Model Output Info
|
|
printf("output tensors:\n");
|
|
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(ctx, RKNN_QUERY_OUTPUT_ATTR, &(output_attrs[i]), sizeof(rknn_tensor_attr));
|
|
if (ret != RKNN_SUCC)
|
|
{
|
|
printf("rknn_query fail! ret=%d\n", ret);
|
|
return -1;
|
|
}
|
|
dump_tensor_attr(&(output_attrs[i]));
|
|
}
|
|
|
|
// Set to context
|
|
app_ctx->rknn_ctx = ctx;
|
|
|
|
// Select quantized decoding from the output tensor attributes.
|
|
if (output_attrs[0].qnt_type == RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC && output_attrs[0].type != RKNN_TENSOR_FLOAT16)
|
|
{
|
|
app_ctx->is_quant = true;
|
|
}
|
|
else
|
|
{
|
|
app_ctx->is_quant = false;
|
|
}
|
|
|
|
app_ctx->io_num = io_num;
|
|
app_ctx->input_attrs = (rknn_tensor_attr *)malloc(io_num.n_input * sizeof(rknn_tensor_attr));
|
|
memcpy(app_ctx->input_attrs, input_attrs, io_num.n_input * sizeof(rknn_tensor_attr));
|
|
app_ctx->output_attrs = (rknn_tensor_attr *)malloc(io_num.n_output * sizeof(rknn_tensor_attr));
|
|
memcpy(app_ctx->output_attrs, output_attrs, io_num.n_output * sizeof(rknn_tensor_attr));
|
|
|
|
if (input_attrs[0].fmt == RKNN_TENSOR_NCHW)
|
|
{
|
|
printf("model is NCHW input fmt\n");
|
|
app_ctx->model_channel = input_attrs[0].dims[1];
|
|
app_ctx->model_height = input_attrs[0].dims[2];
|
|
app_ctx->model_width = input_attrs[0].dims[3];
|
|
}
|
|
else
|
|
{
|
|
printf("model is NHWC input fmt\n");
|
|
app_ctx->model_height = input_attrs[0].dims[1];
|
|
app_ctx->model_width = input_attrs[0].dims[2];
|
|
app_ctx->model_channel = input_attrs[0].dims[3];
|
|
}
|
|
printf("model input height=%d, width=%d, channel=%d\n",
|
|
app_ctx->model_height, app_ctx->model_width, app_ctx->model_channel);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int release_yolov8_pose_model(rknn_app_context_t *app_ctx)
|
|
{
|
|
if (app_ctx->input_attrs != NULL)
|
|
{
|
|
free(app_ctx->input_attrs);
|
|
app_ctx->input_attrs = NULL;
|
|
}
|
|
if (app_ctx->output_attrs != NULL)
|
|
{
|
|
free(app_ctx->output_attrs);
|
|
app_ctx->output_attrs = NULL;
|
|
}
|
|
if (app_ctx->rknn_ctx != 0)
|
|
{
|
|
rknn_destroy(app_ctx->rknn_ctx);
|
|
app_ctx->rknn_ctx = 0;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
int inference_yolov8_pose_model(rknn_app_context_t *app_ctx, image_buffer_t *img, object_detect_result_list *od_results)
|
|
{
|
|
int ret;
|
|
image_buffer_t dst_img;
|
|
letterbox_t letter_box;
|
|
rknn_input inputs[app_ctx->io_num.n_input];
|
|
rknn_output outputs[app_ctx->io_num.n_output];
|
|
const float nms_threshold = runtime_threshold("YOLO_NMS_THRESHOLD", runtime_nms_threshold, NMS_THRESH);
|
|
const float box_conf_threshold = runtime_threshold("YOLO_CONF_THRESHOLD", runtime_confidence_threshold, BOX_THRESH);
|
|
int bg_color = 114;
|
|
|
|
if ((!app_ctx) || !(img) || (!od_results))
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
memset(od_results, 0x00, sizeof(*od_results));
|
|
memset(&letter_box, 0, sizeof(letterbox_t));
|
|
memset(&dst_img, 0, sizeof(image_buffer_t));
|
|
memset(inputs, 0, sizeof(inputs));
|
|
memset(outputs, 0, sizeof(outputs));
|
|
runtime_preprocess_ms = 0.0;
|
|
runtime_inference_ms = 0.0;
|
|
runtime_postprocess_ms = 0.0;
|
|
|
|
// Pre Process
|
|
int64_t start_us = getCurrentTimeUs();
|
|
int64_t end_us = 0;
|
|
dst_img.width = app_ctx->model_width;
|
|
dst_img.height = app_ctx->model_height;
|
|
dst_img.format = IMAGE_FORMAT_RGB888;
|
|
dst_img.size = get_image_size(&dst_img);
|
|
dst_img.virt_addr = (unsigned char *)malloc(dst_img.size);
|
|
if (dst_img.virt_addr == NULL)
|
|
{
|
|
printf("malloc buffer size:%d fail!\n", dst_img.size);
|
|
goto out;
|
|
}
|
|
|
|
// letterbox
|
|
#if defined(YOLO_RUNTIME_SHARED_PREPROCESS)
|
|
ret = yolo_convert_image_with_letterbox(img, &dst_img, &letter_box, bg_color);
|
|
#else
|
|
ret = convert_image_with_letterbox(img, &dst_img, &letter_box, bg_color);
|
|
#endif
|
|
if (ret < 0)
|
|
{
|
|
printf("convert_image_with_letterbox fail! ret=%d\n", ret);
|
|
goto out;
|
|
}
|
|
// Set Input Data
|
|
inputs[0].index = 0;
|
|
inputs[0].type = RKNN_TENSOR_UINT8;
|
|
inputs[0].fmt = RKNN_TENSOR_NHWC;
|
|
inputs[0].size = app_ctx->model_width * app_ctx->model_height * app_ctx->model_channel;
|
|
inputs[0].buf = dst_img.virt_addr;
|
|
|
|
ret = rknn_inputs_set(app_ctx->rknn_ctx, app_ctx->io_num.n_input, inputs);
|
|
if (ret < 0)
|
|
{
|
|
printf("rknn_input_set fail! ret=%d\n", ret);
|
|
goto out;
|
|
}
|
|
runtime_preprocess_ms = (getCurrentTimeUs() - start_us) / 1000.0;
|
|
|
|
// Run
|
|
printf("rknn_run\n");
|
|
start_us = getCurrentTimeUs();
|
|
ret = rknn_run(app_ctx->rknn_ctx, nullptr);
|
|
end_us = getCurrentTimeUs() - start_us;
|
|
runtime_inference_ms = end_us / 1000.0;
|
|
printf("rknn_run time=%.2fms, FPS = %.2f\n",end_us / 1000.f,
|
|
1000.f * 1000.f / end_us);
|
|
|
|
if (ret < 0)
|
|
{
|
|
printf("rknn_run fail! ret=%d\n", ret);
|
|
goto out;
|
|
}
|
|
|
|
// Get Output
|
|
memset(outputs, 0, sizeof(outputs));
|
|
for (int i = 0; i < app_ctx->io_num.n_output; i++)
|
|
{
|
|
outputs[i].index = i;
|
|
outputs[i].want_float = (!app_ctx->is_quant);
|
|
}
|
|
ret = rknn_outputs_get(app_ctx->rknn_ctx, app_ctx->io_num.n_output, outputs, NULL);
|
|
if (ret < 0)
|
|
{
|
|
printf("rknn_outputs_get fail! ret=%d\n", ret);
|
|
goto out;
|
|
}
|
|
// Post Process
|
|
start_us = getCurrentTimeUs();
|
|
post_process(app_ctx, outputs, &letter_box, box_conf_threshold, nms_threshold, od_results);
|
|
end_us = getCurrentTimeUs() - start_us;
|
|
runtime_postprocess_ms = end_us / 1000.0;
|
|
printf("post_process time=%.2fms, FPS = %.2f\n",end_us / 1000.f,
|
|
1000.f * 1000.f / end_us);
|
|
// Remeber to release rknn output
|
|
rknn_outputs_release(app_ctx->rknn_ctx, app_ctx->io_num.n_output, outputs);
|
|
|
|
out:
|
|
if (dst_img.virt_addr != NULL)
|
|
{
|
|
free(dst_img.virt_addr);
|
|
}
|
|
|
|
return ret;
|
|
}
|