Files

293 lines
11 KiB
C

/*
Copyright (c), 2001-2022, Shenshu Tech. Co., Ltd.
*/
#ifndef SAMPLE_COMMON_NNIE_H
#define SAMPLE_COMMON_NNIE_H
#include "ot_common_nnie.h"
#include "sample_common_svp.h"
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
/* 16Byte align */
#define SAMPLE_SVP_NNIE_ALIGN_16 16
#define sample_svp_nnie_align_16(num) (((num) + SAMPLE_SVP_NNIE_ALIGN_16 - 1) / \
SAMPLE_SVP_NNIE_ALIGN_16 * SAMPLE_SVP_NNIE_ALIGN_16)
#define SAMPLE_SVP_NNIE_ALIGN_16_MASK 0xF
#define sample_svp_nnie_check_align_16(num) ((num) & SAMPLE_SVP_NNIE_ALIGN_16_MASK)
/* 32Byte align */
#define SAMPLE_SVP_NNIE_ALIGN_32 32
#define sample_svp_nnie_align_32(num) (((num) + SAMPLE_SVP_NNIE_ALIGN_32 - 1) / \
SAMPLE_SVP_NNIE_ALIGN_32 * SAMPLE_SVP_NNIE_ALIGN_32)
#define SAMPLE_SVP_NNIE_BSI_S20Q12_HEIGHT 6
#define SAMPLE_SVP_NNIE_DOUBLE 2
#define SAMPLE_SVP_NNIE_OFFSET_TWO 2
#define SAMPLE_SVP_NNIE_COORD_NUM 4 /* num of coordinates */
#define SAMPLE_SVP_NNIE_PROPOSAL_WIDTH 6 /* the width of each proposal array */
#define SAMPLE_SVP_NNIE_QUANT_BASE 4096 /* the basic quantity */
#define SAMPLE_SVP_NNIE_S12Q20_QUANT 1048576 /* the basic quantity */
#define SAMPLE_SVP_NNIE_MAX_SOFTWARE_MEM_NUM 4
#define SAMPLE_SVP_NNIE_SSD_REPORT_NODE_NUM 12
#define SAMPLE_SVP_NNIE_SSD_REPORT_BBOX_NODE_NUM 6
#define SAMPLE_SVP_NNIE_SSD_REPORT_CONF_NODE_NUM 6
#define SAMPLE_SVP_NNIE_SSD_MAX_ASPECT_RATIO_NUM 6
#define SAMPLE_SVP_NNIE_SSD_PRIOR_BOX_VAR_NUM 4
#define SAMPLE_SVP_NNIE_SSD_MIN_SIZE_MAX_NUM 1
#define SAMPLE_SVP_NNIE_SSD_MAX_SIZE_MAX_NUM 1
#define SAMPLE_SVP_NNIE_SSD_SOFTMAX_NUM 6
#define SAMPLE_SVP_NNIE_SSD_DETEC_BLOB_NUM 6
#define SAMPLE_SVP_NNIE_YOLOV1_WIDTH_GRID_NUM 7
#define SAMPLE_SVP_NNIE_YOLOV1_HEIGHT_GRID_NUM 7
#define SAMPLE_SVP_NNIE_EACH_SEG_STEP_ADDR_NUM 2
#define SAMPLE_SVP_NNIE_MAX_CLASS_NUM 100
#define SAMPLE_SVP_NNIE_MAX_ROI_NUM_OF_CLASS 50
#define SAMPLE_SVP_NNIE_REPORT_NAME_LENGTH 64
#define SAMPLE_SVP_NNIE_FILE_LEN 256
#define SAMPLE_SVP_NNIE_RPN_MAX_SCALE_NUM 9
#define SAMPLE_SVP_NNIE_RPN_MAX_RATIO_NUM 9
#define SAMPLE_SVP_NNIE_RPN_INPUT_BLOB_NUM 2
#define SAMPLE_SVP_NNIE_YOLOV2_BIAS_NUM 10
#define SAMPLE_SVP_NNIE_YOLOV3_REPORT_BLOB_NUM 3
#define SAMPLE_SVP_NNIE_YOLOV3_BIAS_NUM 6
#define SAMPLE_SVP_NNIE_ALEXNET_FASTER_RCNN_RATIO_NUM 1
#define SAMPLE_SVP_NNIE_ALEXNET_FASTER_RCNN_SCALE_NUM 9
#define SAMPLE_SVP_NNIE_PVANET_FASTER_RCNN_RATIO_NUM 7
#define SAMPLE_SVP_NNIE_PVANET_FASTER_RCNN_SCALE_NUM 6
#define SAMPLE_SVP_NNIE_DOUBLE_ROI_POOL_FASTER_RCNN_RATIO_NUM 3
#define SAMPLE_SVP_NNIE_DOUBLE_ROI_POOL_FASTER_RCNN_SCALE_NUM 3
#define SAMPLE_SVP_NNIE_RFCN_RATIO_NUM 3
#define SAMPLE_SVP_NNIE_RFCN_SCALE_NUM 3
#define SAMPLE_SVP_NNIE_RECT_LEFT_TOP 0
#define SAMPLE_SVP_NNIE_RECT_RIGHT_TOP 1
#define SAMPLE_SVP_NNIE_RECT_RIGHT_BOTTOM 2
#define SAMPLE_SVP_NNIE_RECT_LEFT_BOTTOM 3
#define SAMPLE_SVP_NNIE_MAX_STEP 1024
#define SAMPLE_SVP_NNIE_MAX_U32 0xFFFFFFFF
#define SAMPLE_SVP_NNIE_MAX_IMG_BATCH 256
#define SAMPLE_SVP_NNIE_MAX_CNN_CLASS_NUM 256
typedef struct {
ot_svp_nnie_model model;
ot_svp_mem_info model_buf;
} sample_svp_nnie_model;
/* each seg input and output memory */
typedef struct {
ot_svp_src_blob src[OT_SVP_NNIE_MAX_IN_NUM];
ot_svp_dst_blob dst[OT_SVP_NNIE_MAX_OUT_NUM];
} sample_svp_nnie_seg_data;
/* each seg input and output data memory size */
typedef struct {
td_u32 src_size[OT_SVP_NNIE_MAX_IN_NUM];
td_u32 dst_size[OT_SVP_NNIE_MAX_OUT_NUM];
} sample_svp_nnie_blob_size;
/* NNIE Execution parameters */
typedef struct {
ot_svp_nnie_model *model;
td_u32 tmp_buf_size;
ot_svp_nnie_task_buf_info tsk_buf_info[OT_SVP_NNIE_MAX_NET_SEG_NUM];
ot_svp_mem_info task_buf;
ot_svp_mem_info tmp_buf;
ot_svp_mem_info step_buf; /* store Lstm step info */
sample_svp_nnie_seg_data seg_data[OT_SVP_NNIE_MAX_NET_SEG_NUM]; /* each seg's input and output blob */
ot_svp_nnie_forward_ctrl forward_ctrl[OT_SVP_NNIE_MAX_NET_SEG_NUM];
} sample_svp_nnie_param;
typedef struct {
/* the index of the previous seg whose output results are current processing seg's input data */
td_u32 prev_seg_idx;
td_u32 proc_seg_idx; /* index of the current processing seg */
td_bool is_instant;
} sample_svp_nnie_forward_info;
typedef enum {
SAMPLE_SVP_NNIE_ALEXNET_FASTER_RCNN = 0x0, /* alexnet faster_rcnn */
SAMPLE_SVP_NNIE_VGG16_FASTER_RCNN = 0x1, /* vgg16 faster_rcnn */
SAMPLE_SVP_NNIE_PVANET_FASTER_RCNN = 0x2, /* pvanet faster_rcnn */
SAMPLE_OT_SVP_NNIE_NET_TYPE_BUTT
} sample_svp_nnie_net_type;
/* NNIE configuration parameter */
typedef struct {
td_u32 max_input_num;
td_u32 max_roi_num;
td_u64 step_virt_addr[SAMPLE_SVP_NNIE_EACH_SEG_STEP_ADDR_NUM * OT_SVP_NNIE_MAX_NET_SEG_NUM];
ot_svp_nnie_id nnie_core_id[OT_SVP_NNIE_MAX_NET_SEG_NUM];
ot_svp_nnie_threshold threshold[OT_SVP_NNIE_MAX_NET_SEG_NUM][OT_SVP_NNIE_MAX_THRESHOLD_NUM];
td_u32 threshold_num[OT_SVP_NNIE_MAX_NET_SEG_NUM];
td_bool is_sw_gen_bbox; /* whether bboxes are generated by software */
} sample_svp_nnie_cfg;
/* CNN GetTopN parameter */
typedef struct {
td_u32 top_n;
ot_svp_dst_blob top_n_result;
ot_svp_mem_info assist_buf;
} sample_svp_nnie_cnn_sw_param;
/* FasterRcnn software parameter */
typedef struct {
td_u32 scales[SAMPLE_SVP_NNIE_RPN_MAX_SCALE_NUM];
td_u32 ratios[SAMPLE_SVP_NNIE_RPN_MAX_RATIO_NUM];
ot_svp_src_blob conv[SAMPLE_SVP_NNIE_RPN_INPUT_BLOB_NUM];
td_u32 conv_num;
td_u32 ratio_anchor_num;
td_u32 scale_anchor_num;
td_u32 ori_img_height;
td_u32 ori_img_width;
td_u32 min_size;
td_u32 spatial_scale;
td_u32 nms_threshold;
td_u32 filter_threshold;
td_u32 num_before_nms;
td_u32 max_roi_num;
td_u32 class_num;
td_u32 conf_threshold[SAMPLE_SVP_NNIE_MAX_CLASS_NUM];
td_u32 valid_nms_threshold;
ot_svp_mem_info rpn_base_anchor;
ot_svp_mem_info rpn_tmp_buf;
ot_svp_dst_blob rpn_bbox;
ot_svp_dst_blob class_roi_num;
ot_svp_dst_blob dst_roi;
ot_svp_dst_blob dst_score;
ot_svp_mem_info get_result_tmp_buf;
td_char *rpn_data_layer_name[SAMPLE_SVP_NNIE_RPN_INPUT_BLOB_NUM];
} sample_svp_nnie_two_seg_net_sw_param;
/* RFCN software parameter */
typedef sample_svp_nnie_two_seg_net_sw_param sample_svp_nnie_rfcn_sw_param;
typedef sample_svp_nnie_two_seg_net_sw_param sample_svp_nnie_faster_rcnn_sw_param;
typedef struct {
/* bbox delta Parameters */
td_u32 loc_blob_idx[SAMPLE_SVP_NNIE_SSD_REPORT_BBOX_NODE_NUM];
/* Softmax Parameters */
td_u32 conf_blob_idx[SAMPLE_SVP_NNIE_SSD_REPORT_CONF_NODE_NUM];
td_u32 concat_num;
/* DetectionOut Parameters */
td_u32 class_num;
td_u32 top_k;
td_u32 keep_top_k;
td_u32 nms_threshold;
td_u32 conf_threshold;
td_s32 *prior_box[SAMPLE_SVP_NNIE_SSD_REPORT_BBOX_NODE_NUM];
td_u32 prior_box_size[SAMPLE_SVP_NNIE_SSD_REPORT_BBOX_NODE_NUM];
td_u32 prior_box_num;
ot_svp_mem_info prior_box_tmp_buf;
ot_svp_mem_info softmax_tmp_buf;
ot_svp_mem_info get_result_tmp_buf;
ot_svp_dst_blob class_roi_num;
ot_svp_dst_blob dst_roi;
ot_svp_dst_blob dst_score;
} sample_svp_nnie_ssd_sw_param;
/* Yolov1 software parameter */
typedef struct {
td_u32 ori_img_height;
td_u32 ori_img_width;
td_u32 bbox_num_each_grid;
td_u32 class_num;
td_u32 grid_num_height;
td_u32 grid_num_width;
td_u32 nms_threshold;
td_u32 conf_threshold;
ot_svp_mem_info get_result_tmp_buf;
ot_svp_dst_blob class_roi_num;
ot_svp_dst_blob dst_roi;
ot_svp_dst_blob dst_score;
} sample_svp_nnie_yolov1_sw_param;
/* Yolov2 software parameter */
typedef struct {
td_u32 ori_img_height;
td_u32 ori_img_width;
td_u32 bbox_num_each_grid;
td_u32 class_num;
td_u32 grid_num_height;
td_u32 grid_num_width;
td_u32 nms_threshold;
td_u32 conf_threshold;
td_float bias[SAMPLE_SVP_NNIE_YOLOV2_BIAS_NUM];
ot_svp_mem_info get_result_tmp_buf;
ot_svp_dst_blob class_roi_num;
ot_svp_dst_blob dst_roi;
ot_svp_dst_blob dst_score;
} sample_svp_nnie_yolov2_sw_param;
/* Yolov3 software parameter */
typedef struct {
td_u32 ori_img_height;
td_u32 ori_img_width;
td_u32 bbox_num_each_grid;
td_u32 class_num;
td_u32 grid_num_height[SAMPLE_SVP_NNIE_YOLOV3_REPORT_BLOB_NUM];
td_u32 grid_num_width[SAMPLE_SVP_NNIE_YOLOV3_REPORT_BLOB_NUM];
td_u32 nms_threshold;
td_u32 conf_threshold;
td_float bias[SAMPLE_SVP_NNIE_YOLOV3_REPORT_BLOB_NUM][SAMPLE_SVP_NNIE_YOLOV3_BIAS_NUM];
ot_svp_mem_info get_result_tmp_buf;
ot_svp_dst_blob class_roi_num;
ot_svp_dst_blob dst_roi;
ot_svp_dst_blob dst_score;
} sample_svp_nnie_yolov3_sw_param;
typedef struct {
td_s32 *x_min;
td_s32 *y_min;
td_s32 *x_max;
td_s32 *y_max;
td_s32 *score;
td_s32 *class_id;
} sample_svp_nnie_hw_detect_out_result;
typedef struct {
td_u32 class_num;
td_u32 total_num;
td_u32 roi_num[SAMPLE_SVP_NNIE_MAX_CLASS_NUM];
ot_sample_svp_rect rect[SAMPLE_SVP_NNIE_MAX_CLASS_NUM][SAMPLE_SVP_NNIE_MAX_ROI_NUM_OF_CLASS];
} sample_svp_nnie_rect_info;
td_s32 sample_common_svp_nnie_param_deinit(sample_svp_nnie_param *nnie_param);
td_s32 sample_common_svp_nnie_param_init(sample_svp_nnie_cfg *nnie_cfg, sample_svp_nnie_param *nnie_param);
td_s32 sample_common_svp_nnie_unload_model(sample_svp_nnie_model *nnie_model);
td_s32 sample_common_svp_nnie_load_model(const td_char *model_file, sample_svp_nnie_model *nnie_model);
td_s32 sample_common_svp_nnie_forward(sample_svp_nnie_forward_info *forward_info,
ot_svp_src_blob bbox[], td_u32 bbox_num, sample_svp_nnie_param *nnie_param);
td_s32 sample_common_svp_nnie_fill_src_data(const char *file_name, td_u32 seg_idx, td_u32 src_blob_idx,
sample_svp_nnie_param *nnie_param);
td_s32 sample_common_svp_nnie_print_report_result(sample_svp_nnie_param *nnie_param);
td_s32 sample_common_svp_nnie_print_sw_detect_result(ot_svp_blob *dst_score,
ot_svp_blob *dst_roi, ot_svp_blob *class_roi_num, td_bool has_bg_class, td_float print_result_thresh);
td_s32 sample_common_svp_nnie_print_hw_detect_result(ot_svp_blob *dst, td_u32 class_num,
td_bool has_bg_class, td_float print_result_thresh);
td_s32 sample_common_svp_nnie_fill_rect(ot_video_frame_info *frame_info,
sample_svp_nnie_rect_info *rect_info, td_u32 color);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* SAMPLE_COMMON_NNIE_H */