1 /* 2 * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef SAMPLE_SVP_NNIE_SOFTWARE_H 16 #define SAMPLE_SVP_NNIE_SOFTWARE_H 17 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <math.h> 21 #include "hi_comm_svp.h" 22 #include "hi_nnie.h" 23 #include "mpi_nnie.h" 24 #include "sample_comm_svp.h" 25 #include "sample_comm_nnie.h" 26 27 #ifdef __cplusplus 28 #if __cplusplus 29 extern "C" { 30 #endif 31 #endif /* __cplusplus */ 32 33 #define SAMPLE_SVP_NNIE_ADDR_ALIGN_16 16 /* 16 byte alignment */ 34 #define SAMPLE_SVP_NNIE_MAX(a, b) (((a) > (b)) ? (a) : (b)) 35 #define SAMPLE_SVP_NNIE_MIN(a, b) (((a) < (b)) ? (a) : (b)) 36 #define SAMPLE_SVP_NNIE_SIGMOID(x) (HI_FLOAT)((1.0f) / (1 + exp(-(x)))) 37 38 #define SAMPLE_SVP_NNIE_COORDI_NUM 4 /* coordinate numbers */ 39 #define SAMPLE_SVP_NNIE_PROPOSAL_WIDTH 6 /* the number of proposal values */ 40 #define SAMPLE_SVP_NNIE_QUANT_BASE 4096 /* the base value */ 41 #define SAMPLE_SVP_NNIE_SCORE_NUM 2 /* the num of RPN scores */ 42 #define SAMPLE_SVP_NNIE_X_MIN_OFFSET 0 /* the offset of RPN min x */ 43 #define SAMPLE_SVP_NNIE_Y_MIN_OFFSET 1 /* the offset of RPN min y */ 44 #define SAMPLE_SVP_NNIE_X_MAX_OFFSET 2 /* the offset of RPN max x */ 45 #define SAMPLE_SVP_NNIE_Y_MAX_OFFSET 3 /* the offset of RPN max y */ 46 #define SAMPLE_SVP_NNIE_SCORE_OFFSET 4 /* the offset of RPN scores */ 47 #define SAMPLE_SVP_NNIE_SUPPRESS_FLAG_OFFSET 5 /* the offset of suppress flag */ 48 #define SAMPLE_SVP_NNIE_BBOX_AND_CONFIDENCE 5 /* bbox coordinate and confidence */ 49 #define SAMPLE_SVP_NNIE_HALF 0.5f /* the half value */ 50 #define SAMPLE_SVP_NNIE_YOLOV3_REPORT_BLOB_NUM 3 /* yolov3 report blob num */ 51 #define SAMPLE_SVP_NNIE_YOLOV3_EACH_GRID_BIAS_NUM 6 /* yolov3 bias num of each grid */ 52 #define SAMPLE_SVP_NNIE_YOLOV3_EACH_BBOX_INFER_RESULT_NUM 85 /* yolov3 inference result num of each bbox */ 53 54 /* CNN GetTopN unit */ 55 typedef struct hiSAMPLE_SVP_NNIE_CNN_GETTOPN_UNIT_S { 56 HI_U32 u32ClassId; 57 HI_U32 u32Confidence; 58 } SAMPLE_SVP_NNIE_CNN_GETTOPN_UNIT_S; 59 60 /* stack for sort */ 61 typedef struct hiSAMPLE_SVP_NNIE_STACK { 62 HI_S32 s32Min; 63 HI_S32 s32Max; 64 } SAMPLE_SVP_NNIE_STACK_S; 65 66 /* stack for sort */ 67 typedef struct hiSAMPLE_SVP_NNIE_YOLOV1_SCORE { 68 HI_U32 u32Idx; 69 HI_S32 s32Score; 70 } SAMPLE_SVP_NNIE_YOLOV1_SCORE_S; 71 72 typedef struct hiSAMPLE_SVP_NNIE_YOLOV2_BBOX { 73 HI_FLOAT f32Xmin; 74 HI_FLOAT f32Xmax; 75 HI_FLOAT f32Ymin; 76 HI_FLOAT f32Ymax; 77 HI_S32 s32ClsScore; 78 HI_U32 u32ClassIdx; 79 HI_U32 u32Mask; 80 } SAMPLE_SVP_NNIE_YOLOV2_BBOX_S; 81 82 typedef SAMPLE_SVP_NNIE_YOLOV2_BBOX_S SAMPLE_SVP_NNIE_YOLOV3_BBOX_S; 83 84 /* CNN */ 85 HI_S32 SAMPLE_SVP_NNIE_Cnn_GetTopN(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, 86 SAMPLE_SVP_NNIE_CNN_SOFTWARE_PARAM_S *pstSoftwareParam); 87 88 /* FasterRcnn */ 89 HI_U32 SAMPLE_SVP_NNIE_RpnTmpBufSize(HI_U32 u32RatioAnchorsNum, HI_U32 u32ScaleAnchorsNum, HI_U32 u32ConvHeight, 90 HI_U32 u32ConvWidth); 91 92 HI_U32 SAMPLE_SVP_NNIE_FasterRcnn_GetResultTmpBufSize(HI_U32 u32MaxRoiNum, HI_U32 u32ClassNum); 93 94 HI_S32 SAMPLE_SVP_NNIE_FasterRcnn_Rpn(SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S *pstSoftwareParam); 95 96 HI_S32 SAMPLE_SVP_NNIE_FasterRcnn_GetResult(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, 97 SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S *pstSoftwareParam); 98 99 100 /* Pvanet */ 101 HI_U32 SAMPLE_SVP_NNIE_Pvanet_GetResultTmpBufSize(HI_U32 u32MaxRoiNum, HI_U32 u32ClassNum); 102 103 HI_S32 SAMPLE_SVP_NNIE_Pvanet_Rpn(SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S *pstSoftwareParam); 104 105 HI_S32 SAMPLE_SVP_NNIE_Pvanet_GetResult(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, 106 SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S *pstSoftwareParam); 107 108 /* RFCN */ 109 HI_U32 SAMPLE_SVP_NNIE_Rfcn_GetResultTmpBuf(HI_U32 u32MaxRoiNum, HI_U32 u32ClassNum); 110 111 HI_S32 SAMPLE_SVP_NNIE_Rfcn_Rpn(SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S *pstSoftwareParam); 112 113 HI_S32 SAMPLE_SVP_NNIE_Rfcn_GetResult(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, 114 SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S *pstSoftwareParam); 115 116 /* SSD */ 117 HI_U32 SAMPLE_SVP_NNIE_Ssd_GetResultTmpBuf(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, 118 SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S *pstSoftwareParam); 119 120 HI_S32 SAMPLE_SVP_NNIE_Ssd_GetResult(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, 121 SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S *pstSoftwareParam); 122 123 /* YOLOV1 */ 124 HI_U32 SAMPLE_SVP_NNIE_Yolov1_GetResultTmpBuf(SAMPLE_SVP_NNIE_YOLOV1_SOFTWARE_PARAM_S *pstSoftwareParam); 125 126 HI_S32 SAMPLE_SVP_NNIE_Yolov1_GetResult(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, 127 SAMPLE_SVP_NNIE_YOLOV1_SOFTWARE_PARAM_S *pstSoftwareParam); 128 129 /* YOLOV2 */ 130 HI_U32 SAMPLE_SVP_NNIE_Yolov2_GetResultTmpBuf(SAMPLE_SVP_NNIE_YOLOV2_SOFTWARE_PARAM_S *pstSoftwareParam); 131 132 HI_S32 SAMPLE_SVP_NNIE_Yolov2_GetResult(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, 133 SAMPLE_SVP_NNIE_YOLOV2_SOFTWARE_PARAM_S *pstSoftwareParam); 134 135 /* YOLOV3 */ 136 HI_U32 SAMPLE_SVP_NNIE_Yolov3_GetResultTmpBuf(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, 137 SAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S *pstSoftwareParam); 138 139 HI_S32 SAMPLE_SVP_NNIE_Yolov3_GetResult(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, 140 SAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S *pstSoftwareParam); 141 #ifdef __cplusplus 142 #if __cplusplus 143 } 144 #endif 145 #endif /* __cplusplus */ 146 147 #endif /* SAMPLE_SVP_NNIE_SOFTWARE_H */ 148