• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <sys/mman.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <signal.h>
25 #include <pthread.h>
26 #include <sys/prctl.h>
27 #include <math.h>
28 
29 #include "hi_common.h"
30 #include "hi_comm_sys.h"
31 #include "hi_comm_svp.h"
32 #include "sample_comm.h"
33 #include "sample_comm_svp.h"
34 #include "sample_comm_nnie.h"
35 #include "sample_nnie_main.h"
36 #include "sample_svp_nnie_software.h"
37 #include "sample_comm_ive.h"
38 
39 static hi_bool g_stop_signal = HI_FALSE;
40 /* cnn para */
41 static SAMPLE_SVP_NNIE_MODEL_S s_stCnnModel = { 0 };
42 static SAMPLE_SVP_NNIE_PARAM_S s_stCnnNnieParam = { 0 };
43 static SAMPLE_SVP_NNIE_CNN_SOFTWARE_PARAM_S s_stCnnSoftwareParam = { 0 };
44 /* segment para */
45 static SAMPLE_SVP_NNIE_MODEL_S s_stSegnetModel = { 0 };
46 static SAMPLE_SVP_NNIE_PARAM_S s_stSegnetNnieParam = { 0 };
47 /* fasterrcnn para */
48 static SAMPLE_SVP_NNIE_MODEL_S s_stFasterRcnnModel = { 0 };
49 static SAMPLE_SVP_NNIE_PARAM_S s_stFasterRcnnNnieParam = { 0 };
50 static SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S s_stFasterRcnnSoftwareParam = { 0 };
51 static SAMPLE_SVP_NNIE_NET_TYPE_E s_enNetType;
52 /* rfcn para */
53 static SAMPLE_SVP_NNIE_MODEL_S s_stRfcnModel = { 0 };
54 static SAMPLE_SVP_NNIE_PARAM_S s_stRfcnNnieParam = { 0 };
55 static SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S s_stRfcnSoftwareParam = { 0 };
56 static SAMPLE_IVE_SWITCH_S s_stRfcnSwitch = { HI_FALSE, HI_FALSE };
57 static HI_BOOL s_bNnieStopSignal = HI_FALSE;
58 static pthread_t s_hNnieThread = 0;
59 static SAMPLE_VI_CONFIG_S s_stViConfig = { 0 };
60 
61 /* ssd para */
62 static SAMPLE_SVP_NNIE_MODEL_S s_stSsdModel = { 0 };
63 static SAMPLE_SVP_NNIE_PARAM_S s_stSsdNnieParam = { 0 };
64 static SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S s_stSsdSoftwareParam = { 0 };
65 /* yolov1 para */
66 static SAMPLE_SVP_NNIE_MODEL_S s_stYolov1Model = { 0 };
67 static SAMPLE_SVP_NNIE_PARAM_S s_stYolov1NnieParam = { 0 };
68 static SAMPLE_SVP_NNIE_YOLOV1_SOFTWARE_PARAM_S s_stYolov1SoftwareParam = { 0 };
69 /* yolov2 para */
70 static SAMPLE_SVP_NNIE_MODEL_S s_stYolov2Model = { 0 };
71 static SAMPLE_SVP_NNIE_PARAM_S s_stYolov2NnieParam = { 0 };
72 static SAMPLE_SVP_NNIE_YOLOV2_SOFTWARE_PARAM_S s_stYolov2SoftwareParam = { 0 };
73 /* yolov3 para */
74 static SAMPLE_SVP_NNIE_MODEL_S s_stYolov3Model = { 0 };
75 static SAMPLE_SVP_NNIE_PARAM_S s_stYolov3NnieParam = { 0 };
76 static SAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S s_stYolov3SoftwareParam = { 0 };
77 /* lstm para */
78 static SAMPLE_SVP_NNIE_MODEL_S s_stLstmModel = { 0 };
79 static SAMPLE_SVP_NNIE_PARAM_S s_stLstmNnieParam = { 0 };
80 /* pvanet para */
81 static SAMPLE_SVP_NNIE_MODEL_S s_stPvanetModel = { 0 };
82 static SAMPLE_SVP_NNIE_PARAM_S s_stPvanetNnieParam = { 0 };
83 static SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S s_stPvanetSoftwareParam = { 0 };
84 
85 /* function : NNIE Forward */
SAMPLE_SVP_NNIE_Forward(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S * pstInputDataIdx,SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S * pstProcSegIdx,HI_BOOL bInstant)86 static HI_S32 SAMPLE_SVP_NNIE_Forward(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
87     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S *pstInputDataIdx, SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S *pstProcSegIdx,
88     HI_BOOL bInstant)
89 {
90     HI_S32 s32Ret = HI_SUCCESS;
91     HI_U32 i, j;
92     HI_BOOL bFinish = HI_FALSE;
93     SVP_NNIE_HANDLE hSvpNnieHandle = 0;
94     HI_U32 u32TotalStepNum = 0;
95 
96     SAMPLE_SVP_CHECK_EXPR_RET(pstProcSegIdx->u32SegIdx >= pstNnieParam->pstModel->u32NetSegNum ||
97         pstInputDataIdx->u32SegIdx >= pstNnieParam->pstModel->u32NetSegNum ||
98         pstNnieParam->pstModel->u32NetSegNum > SVP_NNIE_MAX_NET_SEG_NUM,
99         HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
100         "Error, pstProcSegIdx->u32SegIdx(%u) and pstInputDataIdx->u32SegIdx(%u) "
101         "should be less than %u, pstNnieParam->pstModel->u32NetSegNum(%u) can't be greater than %u!\n",
102         pstProcSegIdx->u32SegIdx, pstInputDataIdx->u32SegIdx, pstNnieParam->pstModel->u32NetSegNum,
103         pstNnieParam->pstModel->u32NetSegNum, SVP_NNIE_MAX_NET_SEG_NUM);
104 
105     SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astForwardCtrl[pstProcSegIdx->u32SegIdx].stTskBuf.u64PhyAddr,
106         SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_VOID,
107         pstNnieParam->astForwardCtrl[pstProcSegIdx->u32SegIdx].stTskBuf.u64VirAddr),
108         pstNnieParam->astForwardCtrl[pstProcSegIdx->u32SegIdx].stTskBuf.u32Size);
109 
110     for (i = 0; i < pstNnieParam->astForwardCtrl[pstProcSegIdx->u32SegIdx].u32DstNum; i++) {
111         if (pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].enType == SVP_BLOB_TYPE_SEQ_S32) {
112             for (j = 0; j < pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Num; j++) {
113                 u32TotalStepNum += *(SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_U32,
114                     pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].unShape.stSeq.u64VirAddrStep) +
115                     j);
116             }
117             SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64PhyAddr,
118                 SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_VOID,
119                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64VirAddr),
120                 u32TotalStepNum * pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Stride);
121         } else {
122             SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64PhyAddr,
123                 SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_VOID,
124                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64VirAddr),
125                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Num *
126                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].unShape.stWhc.u32Chn *
127                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].unShape.stWhc.u32Height *
128                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Stride);
129         }
130     }
131 
132     /* set input blob according to node name */
133     if (pstInputDataIdx->u32SegIdx != pstProcSegIdx->u32SegIdx) {
134         for (i = 0; i < pstNnieParam->pstModel->astSeg[pstProcSegIdx->u32SegIdx].u16SrcNum; i++) {
135             for (j = 0; j < pstNnieParam->pstModel->astSeg[pstInputDataIdx->u32SegIdx].u16DstNum; j++) {
136                 if (strncmp(pstNnieParam->pstModel->astSeg[pstInputDataIdx->u32SegIdx].astDstNode[j].szName,
137                     pstNnieParam->pstModel->astSeg[pstProcSegIdx->u32SegIdx].astSrcNode[i].szName,
138                     SVP_NNIE_NODE_NAME_LEN) == 0) {
139                     pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astSrc[i] =
140                         pstNnieParam->astSegData[pstInputDataIdx->u32SegIdx].astDst[j];
141                     break;
142                 }
143             }
144             SAMPLE_SVP_CHECK_EXPR_RET((j == pstNnieParam->pstModel->astSeg[pstInputDataIdx->u32SegIdx].u16DstNum),
145                 HI_FAILURE, SAMPLE_SVP_ERR_LEVEL_ERROR, "Error,can't find %d-th seg's %d-th src blob!\n",
146                 pstProcSegIdx->u32SegIdx, i);
147         }
148     }
149 
150     /* NNIE_Forward */
151     s32Ret = HI_MPI_SVP_NNIE_Forward(&hSvpNnieHandle, pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astSrc,
152         pstNnieParam->pstModel, pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst,
153         &pstNnieParam->astForwardCtrl[pstProcSegIdx->u32SegIdx], bInstant);
154     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
155         "Error,HI_MPI_SVP_NNIE_Forward failed!\n");
156 
157     if (bInstant) {
158         /* Wait NNIE finish */
159         while (HI_ERR_SVP_NNIE_QUERY_TIMEOUT == (s32Ret = HI_MPI_SVP_NNIE_Query(
160             pstNnieParam->astForwardCtrl[pstProcSegIdx->u32SegIdx].enNnieId, hSvpNnieHandle, &bFinish, HI_TRUE))) {
161             usleep(100); /* sleep 100 micro_seconds */
162             SAMPLE_SVP_TRACE(SAMPLE_SVP_ERR_LEVEL_INFO, "HI_MPI_SVP_NNIE_Query Query timeout!\n");
163         }
164     }
165     u32TotalStepNum = 0;
166     for (i = 0; i < pstNnieParam->astForwardCtrl[pstProcSegIdx->u32SegIdx].u32DstNum; i++) {
167         if (SVP_BLOB_TYPE_SEQ_S32 == pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].enType) {
168             for (j = 0; j < pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Num; j++) {
169                 u32TotalStepNum += *(SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_U32,
170                     pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].unShape.stSeq.u64VirAddrStep) +
171                     j);
172             }
173             SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64PhyAddr,
174                 SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_VOID,
175                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64VirAddr),
176                 u32TotalStepNum * pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Stride);
177         } else {
178             SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64PhyAddr,
179                 SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_VOID,
180                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64VirAddr),
181                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Num *
182                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].unShape.stWhc.u32Chn *
183                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].unShape.stWhc.u32Height *
184                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Stride);
185         }
186     }
187 
188     return s32Ret;
189 }
190 
191 /* function : NNIE ForwardWithBbox */
SAMPLE_SVP_NNIE_ForwardWithBbox(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S * pstInputDataIdx,SVP_SRC_BLOB_S astBbox[],SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S * pstProcSegIdx,HI_BOOL bInstant)192 static HI_S32 SAMPLE_SVP_NNIE_ForwardWithBbox(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
193     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S *pstInputDataIdx, SVP_SRC_BLOB_S astBbox[],
194     SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S *pstProcSegIdx, HI_BOOL bInstant)
195 {
196     HI_S32 s32Ret = HI_SUCCESS;
197     HI_BOOL bFinish = HI_FALSE;
198     SVP_NNIE_HANDLE hSvpNnieHandle = 0;
199     HI_U32 u32TotalStepNum = 0;
200     HI_U32 i, j;
201 
202     SAMPLE_SVP_CHECK_EXPR_RET(pstProcSegIdx->u32SegIdx >= pstNnieParam->pstModel->u32NetSegNum ||
203         pstInputDataIdx->u32SegIdx >= pstNnieParam->pstModel->u32NetSegNum ||
204         pstNnieParam->pstModel->u32NetSegNum > SVP_NNIE_MAX_NET_SEG_NUM,
205         HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
206         "Error, pstProcSegIdx->u32SegIdx(%u) and pstInputDataIdx->u32SegIdx(%u) "
207         "should be less than %u, pstNnieParam->pstModel->u32NetSegNum(%u) should be less than %u!\n",
208         pstProcSegIdx->u32SegIdx, pstInputDataIdx->u32SegIdx, pstNnieParam->pstModel->u32NetSegNum,
209         pstNnieParam->pstModel->u32NetSegNum, SVP_NNIE_MAX_NET_SEG_NUM);
210     SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astForwardWithBboxCtrl[pstProcSegIdx->u32SegIdx].stTskBuf.u64PhyAddr,
211         SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_VOID,
212         pstNnieParam->astForwardWithBboxCtrl[pstProcSegIdx->u32SegIdx].stTskBuf.u64VirAddr),
213         pstNnieParam->astForwardWithBboxCtrl[pstProcSegIdx->u32SegIdx].stTskBuf.u32Size);
214 
215     for (i = 0; i < pstNnieParam->astForwardWithBboxCtrl[pstProcSegIdx->u32SegIdx].u32DstNum; i++) {
216         if (SVP_BLOB_TYPE_SEQ_S32 == pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].enType) {
217             for (j = 0; j < pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Num; j++) {
218                 u32TotalStepNum += *(SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_U32,
219                     pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].unShape.stSeq.u64VirAddrStep) +
220                     j);
221             }
222             SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64PhyAddr,
223                 SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_VOID,
224                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64VirAddr),
225                 u32TotalStepNum * pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Stride);
226         } else {
227             SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64PhyAddr,
228                 SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_VOID,
229                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64VirAddr),
230                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Num *
231                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].unShape.stWhc.u32Chn *
232                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].unShape.stWhc.u32Height *
233                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Stride);
234         }
235     }
236 
237     /* set input blob according to node name */
238     if (pstInputDataIdx->u32SegIdx != pstProcSegIdx->u32SegIdx) {
239         for (i = 0; i < pstNnieParam->pstModel->astSeg[pstProcSegIdx->u32SegIdx].u16SrcNum; i++) {
240             for (j = 0; j < pstNnieParam->pstModel->astSeg[pstInputDataIdx->u32SegIdx].u16DstNum; j++) {
241                 if (strncmp(pstNnieParam->pstModel->astSeg[pstInputDataIdx->u32SegIdx].astDstNode[j].szName,
242                     pstNnieParam->pstModel->astSeg[pstProcSegIdx->u32SegIdx].astSrcNode[i].szName,
243                     SVP_NNIE_NODE_NAME_LEN) == 0) {
244                     pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astSrc[i] =
245                         pstNnieParam->astSegData[pstInputDataIdx->u32SegIdx].astDst[j];
246                     break;
247                 }
248             }
249             SAMPLE_SVP_CHECK_EXPR_RET((j == pstNnieParam->pstModel->astSeg[pstInputDataIdx->u32SegIdx].u16DstNum),
250                 HI_FAILURE, SAMPLE_SVP_ERR_LEVEL_ERROR, "Error,can't find %d-th seg's %d-th src blob!\n",
251                 pstProcSegIdx->u32SegIdx, i);
252         }
253     }
254     /* NNIE_ForwardWithBbox */
255     s32Ret = HI_MPI_SVP_NNIE_ForwardWithBbox(&hSvpNnieHandle, pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astSrc,
256         astBbox, pstNnieParam->pstModel, pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst,
257         &pstNnieParam->astForwardWithBboxCtrl[pstProcSegIdx->u32SegIdx], bInstant);
258     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
259         "Error,HI_MPI_SVP_NNIE_ForwardWithBbox failed!\n");
260 
261     if (bInstant) {
262         /* Wait NNIE finish */
263         while (HI_ERR_SVP_NNIE_QUERY_TIMEOUT ==
264             (s32Ret = HI_MPI_SVP_NNIE_Query(pstNnieParam->astForwardWithBboxCtrl[pstProcSegIdx->u32SegIdx].enNnieId,
265             hSvpNnieHandle, &bFinish, HI_TRUE))) {
266             usleep(100); /* sleep 100 micro_seconds */
267             SAMPLE_SVP_TRACE(SAMPLE_SVP_ERR_LEVEL_INFO, "HI_MPI_SVP_NNIE_Query Query timeout!\n");
268         }
269     }
270     u32TotalStepNum = 0;
271 
272     for (i = 0; i < pstNnieParam->astForwardWithBboxCtrl[pstProcSegIdx->u32SegIdx].u32DstNum; i++) {
273         if (SVP_BLOB_TYPE_SEQ_S32 == pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].enType) {
274             for (j = 0; j < pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Num; j++) {
275                 u32TotalStepNum += *(SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_U32,
276                     pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].unShape.stSeq.u64VirAddrStep) +
277                     j);
278             }
279             SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64PhyAddr,
280                 SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_VOID,
281                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64VirAddr),
282                 u32TotalStepNum * pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Stride);
283         } else {
284             SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64PhyAddr,
285                 SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_VOID,
286                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64VirAddr),
287                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Num *
288                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].unShape.stWhc.u32Chn *
289                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].unShape.stWhc.u32Height *
290                 pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Stride);
291         }
292     }
293 
294     return s32Ret;
295 }
296 
297 /* function : Fill Src Data */
SAMPLE_SVP_NNIE_FillSrcData(SAMPLE_SVP_NNIE_CFG_S * pstNnieCfg,SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S * pstInputDataIdx)298 static HI_S32 SAMPLE_SVP_NNIE_FillSrcData(SAMPLE_SVP_NNIE_CFG_S *pstNnieCfg, SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
299     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S *pstInputDataIdx)
300 {
301     FILE *fp = NULL;
302     HI_U32 i = 0, j = 0, n = 0;
303     HI_U32 u32Height = 0, u32Width = 0, u32Chn = 0, u32Stride = 0, u32Dim = 0;
304     HI_U32 u32VarSize = 0;
305     HI_U8 *pu8PicAddr = NULL;
306     HI_U32 *pu32StepAddr = NULL;
307     HI_U32 u32SegIdx = pstInputDataIdx->u32SegIdx;
308     HI_U32 u32NodeIdx = pstInputDataIdx->u32NodeIdx;
309     HI_U32 u32TotalStepNum = 0;
310     HI_ULONG ulSize;
311     HI_CHAR path[PATH_MAX] = {0};
312 
313     /* open file */
314     SAMPLE_SVP_CHECK_EXPR_RET(pstNnieCfg->pszPic == HI_NULL, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
315         "Error, pstNnieCfg->pszPic is null!\n");
316     SAMPLE_SVP_CHECK_EXPR_RET(pstInputDataIdx->u32SegIdx >= SVP_NNIE_MAX_NET_SEG_NUM, HI_INVALID_VALUE,
317         SAMPLE_SVP_ERR_LEVEL_ERROR, "Error, u32SegIdx should be less than %u!\n", SVP_NNIE_MAX_NET_SEG_NUM);
318     SAMPLE_SVP_CHECK_EXPR_RET(pstInputDataIdx->u32NodeIdx >= SVP_NNIE_MAX_INPUT_NUM, HI_INVALID_VALUE,
319         SAMPLE_SVP_ERR_LEVEL_ERROR, "Error, u32NodeIdx should be less than %u!\n", SVP_NNIE_MAX_INPUT_NUM);
320 
321     SAMPLE_SVP_CHECK_EXPR_RET((strlen(pstNnieCfg->pszPic) > PATH_MAX) ||
322         (realpath(pstNnieCfg->pszPic, path) == HI_NULL),
323         HI_ERR_SVP_NNIE_ILLEGAL_PARAM, SAMPLE_SVP_ERR_LEVEL_ERROR, "Error, file_name is invalid!\n");
324     fp = fopen(path, "rb");
325     SAMPLE_SVP_CHECK_EXPR_RET(fp == NULL, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR, "Error, open file failed!\n");
326 
327     /* get data size */
328     if (SVP_BLOB_TYPE_U8 <= pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].enType &&
329         SVP_BLOB_TYPE_YVU422SP >= pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].enType) {
330         u32VarSize = sizeof(HI_U8);
331     } else {
332         u32VarSize = sizeof(HI_U32);
333     }
334 
335     /* fill src data */
336     if (SVP_BLOB_TYPE_SEQ_S32 == pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].enType) {
337         u32Dim = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stSeq.u32Dim;
338         u32Stride = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Stride;
339         pu32StepAddr = SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_U32,
340             pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stSeq.u64VirAddrStep);
341         pu8PicAddr = SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_U8,
342             pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr);
343         for (n = 0; n < pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Num; n++) {
344             for (i = 0; i < *(pu32StepAddr + n); i++) {
345                 ulSize = fread(pu8PicAddr, u32Dim * u32VarSize, 1, fp);
346                 SAMPLE_SVP_CHECK_EXPR_GOTO(ulSize != 1, FAIL, SAMPLE_SVP_ERR_LEVEL_ERROR,
347                     "Error,Read image file failed!\n");
348                 pu8PicAddr += u32Stride;
349             }
350             u32TotalStepNum += *(pu32StepAddr + n);
351         }
352         SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64PhyAddr,
353             SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_VOID,
354             pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr),
355             u32TotalStepNum * u32Stride);
356     } else {
357         u32Height = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stWhc.u32Height;
358         u32Width = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stWhc.u32Width;
359         u32Chn = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stWhc.u32Chn;
360         u32Stride = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Stride;
361         pu8PicAddr = SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_U8,
362             pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr);
363         if (SVP_BLOB_TYPE_YVU420SP == pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].enType) {
364             for (n = 0; n < pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Num; n++) {
365                 for (i = 0; i < u32Chn * u32Height / 2; i++) { /* Brightness: 1 height, Chroma: 1/2 height */
366                     ulSize = fread(pu8PicAddr, u32Width * u32VarSize, 1, fp);
367                     SAMPLE_SVP_CHECK_EXPR_GOTO(ulSize != 1, FAIL, SAMPLE_SVP_ERR_LEVEL_ERROR,
368                         "Error,Read image file failed!\n");
369                     pu8PicAddr += u32Stride;
370                 }
371             }
372         } else if (SVP_BLOB_TYPE_YVU422SP == pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].enType) {
373             for (n = 0; n < pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Num; n++) {
374                 for (i = 0; i < u32Height * 2; i++) { /* Brightness: 1 height, Chroma: 1 height */
375                     ulSize = fread(pu8PicAddr, u32Width * u32VarSize, 1, fp);
376                     SAMPLE_SVP_CHECK_EXPR_GOTO(ulSize != 1, FAIL, SAMPLE_SVP_ERR_LEVEL_ERROR,
377                         "Error,Read image file failed!\n");
378                     pu8PicAddr += u32Stride;
379                 }
380             }
381         } else {
382             for (n = 0; n < pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Num; n++) {
383                 for (i = 0; i < u32Chn; i++) {
384                     for (j = 0; j < u32Height; j++) {
385                         ulSize = fread(pu8PicAddr, u32Width * u32VarSize, 1, fp);
386                         SAMPLE_SVP_CHECK_EXPR_GOTO(ulSize != 1, FAIL, SAMPLE_SVP_ERR_LEVEL_ERROR,
387                             "Error,Read image file failed!\n");
388                         pu8PicAddr += u32Stride;
389                     }
390                 }
391             }
392         }
393         SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64PhyAddr,
394             SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_VOID,
395             pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr),
396             pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Num * u32Chn * u32Height * u32Stride);
397     }
398 
399     (HI_VOID)fclose(fp);
400     return HI_SUCCESS;
401 FAIL:
402 
403     (HI_VOID)fclose(fp);
404     return HI_FAILURE;
405 }
406 
407 /* function : print report result */
SAMPLE_SVP_NNIE_PrintReportResult(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam)408 static HI_S32 SAMPLE_SVP_NNIE_PrintReportResult(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam)
409 {
410     HI_U32 u32SegNum = pstNnieParam->pstModel->u32NetSegNum;
411     HI_U32 i = 0, j = 0, k = 0, n = 0;
412     HI_U32 u32SegIdx = 0, u32NodeIdx = 0;
413     HI_S32 s32Ret = HI_SUCCESS;
414     HI_CHAR acReportFileName[SAMPLE_SVP_NNIE_REPORT_NAME_LENGTH] = {'\0'};
415     FILE *fp = NULL;
416     HI_U32 *pu32StepAddr = NULL;
417     HI_S32 *ps32ResultAddr = NULL;
418     HI_U32 u32Height = 0, u32Width = 0, u32Chn = 0, u32Stride = 0, u32Dim = 0;
419 
420     for (u32SegIdx = 0; u32SegIdx < u32SegNum; u32SegIdx++) {
421         for (u32NodeIdx = 0; u32NodeIdx < pstNnieParam->pstModel->astSeg[u32SegIdx].u16DstNum; u32NodeIdx++) {
422             s32Ret = snprintf_s(acReportFileName, SAMPLE_SVP_NNIE_REPORT_NAME_LENGTH,
423                 SAMPLE_SVP_NNIE_REPORT_NAME_LENGTH - 1, "seg%d_layer%d_output%d_inst.linear.hex", u32SegIdx,
424                 pstNnieParam->pstModel->astSeg[u32SegIdx].astDstNode[u32NodeIdx].u32NodeId, 0);
425             SAMPLE_SVP_CHECK_EXPR_RET(s32Ret < 0, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
426                 "Error,create file name failed!\n");
427 
428             fp = fopen(acReportFileName, "w");
429             SAMPLE_SVP_CHECK_EXPR_RET(fp == NULL, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
430                 "Error,open file failed!\n");
431 
432             if (SVP_BLOB_TYPE_SEQ_S32 == pstNnieParam->astSegData[u32SegIdx].astDst[u32NodeIdx].enType) {
433                 u32Dim = pstNnieParam->astSegData[u32SegIdx].astDst[u32NodeIdx].unShape.stSeq.u32Dim;
434                 u32Stride = pstNnieParam->astSegData[u32SegIdx].astDst[u32NodeIdx].u32Stride;
435                 pu32StepAddr = SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_U32,
436                     pstNnieParam->astSegData[u32SegIdx].astDst[u32NodeIdx].unShape.stSeq.u64VirAddrStep);
437                 ps32ResultAddr = SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_S32,
438                     pstNnieParam->astSegData[u32SegIdx].astDst[u32NodeIdx].u64VirAddr);
439 
440                 for (n = 0; n < pstNnieParam->astSegData[u32SegIdx].astDst[u32NodeIdx].u32Num; n++) {
441                     for (i = 0; i < *(pu32StepAddr + n); i++) {
442                         for (j = 0; j < u32Dim; j++) {
443                             s32Ret = fprintf(fp, "%08x\n", *(ps32ResultAddr + j));
444                             SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret < 0, PRINT_FAIL, SAMPLE_SVP_ERR_LEVEL_ERROR,
445                                 "Error,write report result file failed!\n");
446                         }
447                         ps32ResultAddr += u32Stride / sizeof(HI_U32);
448                     }
449                 }
450             } else {
451                 u32Height = pstNnieParam->astSegData[u32SegIdx].astDst[u32NodeIdx].unShape.stWhc.u32Height;
452                 u32Width = pstNnieParam->astSegData[u32SegIdx].astDst[u32NodeIdx].unShape.stWhc.u32Width;
453                 u32Chn = pstNnieParam->astSegData[u32SegIdx].astDst[u32NodeIdx].unShape.stWhc.u32Chn;
454                 u32Stride = pstNnieParam->astSegData[u32SegIdx].astDst[u32NodeIdx].u32Stride;
455                 ps32ResultAddr = SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_S32,
456                     pstNnieParam->astSegData[u32SegIdx].astDst[u32NodeIdx].u64VirAddr);
457                 for (n = 0; n < pstNnieParam->astSegData[u32SegIdx].astDst[u32NodeIdx].u32Num; n++) {
458                     for (i = 0; i < u32Chn; i++) {
459                         for (j = 0; j < u32Height; j++) {
460                             for (k = 0; k < u32Width; k++) {
461                                 s32Ret = fprintf(fp, "%08x\n", *(ps32ResultAddr + k));
462                                 SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret < 0, PRINT_FAIL, SAMPLE_SVP_ERR_LEVEL_ERROR,
463                                     "Error,write report result file failed!\n");
464                             }
465                             ps32ResultAddr += u32Stride / sizeof(HI_U32);
466                         }
467                     }
468                 }
469             }
470             (HI_VOID)fclose(fp);
471         }
472     }
473     return HI_SUCCESS;
474 
475 PRINT_FAIL:
476     (HI_VOID)fclose(fp);
477     return HI_FAILURE;
478 }
479 
480 /* function : Cnn software deinit */
SAMPLE_SVP_NNIE_Cnn_SoftwareDeinit(SAMPLE_SVP_NNIE_CNN_SOFTWARE_PARAM_S * pstCnnSoftWarePara)481 static HI_S32 SAMPLE_SVP_NNIE_Cnn_SoftwareDeinit(SAMPLE_SVP_NNIE_CNN_SOFTWARE_PARAM_S *pstCnnSoftWarePara)
482 {
483     HI_S32 s32Ret = HI_SUCCESS;
484     SAMPLE_SVP_CHECK_EXPR_RET(pstCnnSoftWarePara == NULL, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
485         "Error, pstCnnSoftWarePara can't be NULL!\n");
486     if ((pstCnnSoftWarePara->stGetTopN.u64PhyAddr != 0) && (pstCnnSoftWarePara->stGetTopN.u64VirAddr != 0)) {
487         SAMPLE_SVP_MMZ_FREE(pstCnnSoftWarePara->stGetTopN.u64PhyAddr, pstCnnSoftWarePara->stGetTopN.u64VirAddr);
488         pstCnnSoftWarePara->stGetTopN.u64PhyAddr = 0;
489         pstCnnSoftWarePara->stGetTopN.u64VirAddr = 0;
490     }
491     return s32Ret;
492 }
493 
494 /* function : Cnn Deinit */
SAMPLE_SVP_NNIE_Cnn_Deinit(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_CNN_SOFTWARE_PARAM_S * pstSoftWareParam,SAMPLE_SVP_NNIE_MODEL_S * pstNnieModel)495 static HI_S32 SAMPLE_SVP_NNIE_Cnn_Deinit(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
496     SAMPLE_SVP_NNIE_CNN_SOFTWARE_PARAM_S *pstSoftWareParam, SAMPLE_SVP_NNIE_MODEL_S *pstNnieModel)
497 {
498     HI_S32 s32Ret = HI_SUCCESS;
499     /* hardware para deinit */
500     if (pstNnieParam != NULL) {
501         s32Ret = SAMPLE_COMM_SVP_NNIE_ParamDeinit(pstNnieParam);
502         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
503             "Error,SAMPLE_COMM_SVP_NNIE_ParamDeinit failed!\n");
504     }
505     /* software para deinit */
506     if (pstSoftWareParam != NULL) {
507         s32Ret = SAMPLE_SVP_NNIE_Cnn_SoftwareDeinit(pstSoftWareParam);
508         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
509             "Error,SAMPLE_SVP_NNIE_Cnn_SoftwareDeinit failed!\n");
510     }
511     /* model deinit */
512     if (pstNnieModel != NULL) {
513         s32Ret = SAMPLE_COMM_SVP_NNIE_UnloadModel(pstNnieModel);
514         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
515             "Error,SAMPLE_COMM_SVP_NNIE_UnloadModel failed!\n");
516     }
517     return s32Ret;
518 }
519 
520 /* function : Cnn software para init */
SAMPLE_SVP_NNIE_Cnn_SoftwareParaInit(SAMPLE_SVP_NNIE_CFG_S * pstNnieCfg,SAMPLE_SVP_NNIE_PARAM_S * pstCnnPara,SAMPLE_SVP_NNIE_CNN_SOFTWARE_PARAM_S * pstCnnSoftWarePara)521 static HI_S32 SAMPLE_SVP_NNIE_Cnn_SoftwareParaInit(SAMPLE_SVP_NNIE_CFG_S *pstNnieCfg,
522     SAMPLE_SVP_NNIE_PARAM_S *pstCnnPara, SAMPLE_SVP_NNIE_CNN_SOFTWARE_PARAM_S *pstCnnSoftWarePara)
523 {
524     HI_U32 u32GetTopNMemSize = 0;
525     HI_U32 u32GetTopNAssistBufSize = 0;
526     HI_U32 u32GetTopNPerFrameSize = 0;
527     HI_U32 u32TotalSize = 0;
528     HI_U32 u32ClassNum = pstCnnPara->pstModel->astSeg[0].astDstNode[0].unShape.stWhc.u32Width;
529     HI_U64 u64PhyAddr = 0;
530     HI_U8 *pu8VirAddr = NULL;
531     HI_S32 s32Ret = HI_SUCCESS;
532     HI_U64 u64Tmp;
533 
534     /* get mem size */
535     u64Tmp = (HI_U64)pstCnnSoftWarePara->u32TopN * sizeof(SAMPLE_SVP_NNIE_CNN_GETTOPN_UNIT_S);
536     SAMPLE_SVP_CHECK_EXPR_RET(u64Tmp > SAMPLE_SVP_NNIE_MAX_MEM, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
537         "Error,u64Tmp should be less than %u!\n", SAMPLE_SVP_NNIE_MAX_MEM);
538     u32GetTopNPerFrameSize = (HI_U32)u64Tmp;
539 
540     u64Tmp = SAMPLE_SVP_NNIE_ALIGN16(u32GetTopNPerFrameSize) * (HI_U64)pstNnieCfg->u32MaxInputNum;
541     SAMPLE_SVP_CHECK_EXPR_RET(u64Tmp > SAMPLE_SVP_NNIE_MAX_MEM, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
542         "Error,u64Tmp should be less than %u!\n", SAMPLE_SVP_NNIE_MAX_MEM);
543     u32GetTopNMemSize = (HI_U32)u64Tmp;
544 
545     u64Tmp = (HI_U64)u32ClassNum * sizeof(SAMPLE_SVP_NNIE_CNN_GETTOPN_UNIT_S);
546     SAMPLE_SVP_CHECK_EXPR_RET(u64Tmp > SAMPLE_SVP_NNIE_MAX_MEM, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
547         "Error,u64Tmp should be less than %u!\n", SAMPLE_SVP_NNIE_MAX_MEM);
548     u32GetTopNAssistBufSize = (HI_U32)u64Tmp;
549 
550     SAMPLE_SVP_CHECK_EXPR_RET((HI_U64)u32GetTopNMemSize + u32GetTopNAssistBufSize > SAMPLE_SVP_NNIE_MAX_MEM,
551         HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR, "Error,total size should be less than %u!\n",
552         SAMPLE_SVP_NNIE_MAX_MEM);
553     u32TotalSize = u32GetTopNMemSize + u32GetTopNAssistBufSize;
554 
555     /* malloc mem */
556     s32Ret =
557         SAMPLE_COMM_SVP_MallocMem("SAMPLE_CNN_INIT", NULL, (HI_U64 *)&u64PhyAddr, (void **)&pu8VirAddr, u32TotalSize);
558     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
559         "Error,Malloc memory failed!\n");
560     (HI_VOID)memset_s(pu8VirAddr, u32TotalSize, 0, u32TotalSize);
561 
562     /* init GetTopn */
563     pstCnnSoftWarePara->stGetTopN.u32Num = pstNnieCfg->u32MaxInputNum;
564     pstCnnSoftWarePara->stGetTopN.unShape.stWhc.u32Chn = 1;
565     pstCnnSoftWarePara->stGetTopN.unShape.stWhc.u32Height = 1;
566     pstCnnSoftWarePara->stGetTopN.unShape.stWhc.u32Width = u32GetTopNPerFrameSize / sizeof(HI_U32);
567     pstCnnSoftWarePara->stGetTopN.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(u32GetTopNPerFrameSize);
568     pstCnnSoftWarePara->stGetTopN.u64PhyAddr = u64PhyAddr;
569     pstCnnSoftWarePara->stGetTopN.u64VirAddr = SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64, pu8VirAddr);
570 
571     /* init AssistBuf */
572     pstCnnSoftWarePara->stAssistBuf.u32Size = u32GetTopNAssistBufSize;
573     pstCnnSoftWarePara->stAssistBuf.u64PhyAddr = u64PhyAddr + u32GetTopNMemSize;
574     pstCnnSoftWarePara->stAssistBuf.u64VirAddr =
575         SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64, pu8VirAddr) + u32GetTopNMemSize;
576 
577     return s32Ret;
578 }
579 
580 /* function : Cnn init */
SAMPLE_SVP_NNIE_Cnn_ParamInit(SAMPLE_SVP_NNIE_CFG_S * pstNnieCfg,SAMPLE_SVP_NNIE_PARAM_S * pstCnnPara,SAMPLE_SVP_NNIE_CNN_SOFTWARE_PARAM_S * pstCnnSoftWarePara)581 static HI_S32 SAMPLE_SVP_NNIE_Cnn_ParamInit(SAMPLE_SVP_NNIE_CFG_S *pstNnieCfg, SAMPLE_SVP_NNIE_PARAM_S *pstCnnPara,
582     SAMPLE_SVP_NNIE_CNN_SOFTWARE_PARAM_S *pstCnnSoftWarePara)
583 {
584     HI_S32 s32Ret = HI_SUCCESS;
585     /* init hardware para */
586     s32Ret = SAMPLE_COMM_SVP_NNIE_ParamInit(pstNnieCfg, pstCnnPara);
587     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
588         "Error(%#x),SAMPLE_COMM_SVP_NNIE_ParamInit failed!\n", s32Ret);
589 
590     /* init software para */
591     if (pstCnnSoftWarePara != NULL) {
592         s32Ret = SAMPLE_SVP_NNIE_Cnn_SoftwareParaInit(pstNnieCfg, pstCnnPara, pstCnnSoftWarePara);
593         SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
594             "Error(%#x),SAMPLE_SVP_NNIE_Cnn_SoftwareParaInit failed!\n", s32Ret);
595     }
596 
597     return s32Ret;
598 INIT_FAIL_0:
599     s32Ret = SAMPLE_SVP_NNIE_Cnn_Deinit(pstCnnPara, pstCnnSoftWarePara, NULL);
600     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
601         "Error(%#x),SAMPLE_SVP_NNIE_Cnn_Deinit failed!\n", s32Ret);
602     return HI_FAILURE;
603 }
604 
605 /* function : Cnn process */
SAMPLE_SVP_NNIE_Cnn_PrintResult(SVP_BLOB_S * pstGetTopN,HI_U32 u32TopN)606 static HI_S32 SAMPLE_SVP_NNIE_Cnn_PrintResult(SVP_BLOB_S *pstGetTopN, HI_U32 u32TopN)
607 {
608     HI_U32 i, j;
609     HI_U32 *pu32Tmp = NULL;
610     HI_U32 u32Stride;
611     SAMPLE_SVP_CHECK_EXPR_RET(pstGetTopN == NULL, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
612         "Error,pstGetTopN can't be NULL!\n");
613 
614     u32Stride = pstGetTopN->u32Stride;
615     for (j = 0; j < pstGetTopN->u32Num; j++) {
616         SAMPLE_SVP_TRACE_INFO("==== The %dth image info====\n", j);
617         pu32Tmp = SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_U32, ((HI_UL)pstGetTopN->u64VirAddr + j * u32Stride));
618         for (i = 0; i < u32TopN * 2; i += 2) { /* leave a space */
619             SAMPLE_SVP_TRACE_INFO("%d:%d\n", pu32Tmp[i], pu32Tmp[i + 1]);
620         }
621     }
622     return HI_SUCCESS;
623 }
SAMPLE_SVP_NNIE_Cnn_Stop(void)624 static hi_void SAMPLE_SVP_NNIE_Cnn_Stop(void)
625 {
626     SAMPLE_SVP_NNIE_Cnn_Deinit(&s_stCnnNnieParam, &s_stCnnSoftwareParam, &s_stCnnModel);
627     (HI_VOID)memset_s(&s_stCnnNnieParam, sizeof(SAMPLE_SVP_NNIE_PARAM_S), 0, sizeof(SAMPLE_SVP_NNIE_PARAM_S));
628     (HI_VOID)memset_s(&s_stCnnSoftwareParam, sizeof(SAMPLE_SVP_NNIE_CNN_SOFTWARE_PARAM_S), 0,
629         sizeof(SAMPLE_SVP_NNIE_CNN_SOFTWARE_PARAM_S));
630     (HI_VOID)memset_s(&s_stCnnModel, sizeof(SAMPLE_SVP_NNIE_MODEL_S), 0, sizeof(SAMPLE_SVP_NNIE_MODEL_S));
631     SAMPLE_COMM_SVP_CheckSysExit();
632     printf("\033[0;31mprogram termination abnormally!\033[0;39m\n");
633 }
634 
635 /* function : show Cnn sample(image 28x28 U8_C1) */
SAMPLE_SVP_NNIE_Cnn(void)636 void SAMPLE_SVP_NNIE_Cnn(void)
637 {
638     const HI_CHAR *pcSrcFile = "./data/nnie_image/y/0_28x28.y";
639     const HI_CHAR *pcModelName = "./data/nnie_model/classification/inst_mnist_cycle.wk";
640     const HI_U32 u32PicNum = 1;
641     HI_S32 s32Ret = HI_SUCCESS;
642     SAMPLE_SVP_NNIE_CFG_S stNnieCfg = { 0 };
643     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S stInputDataIdx = { 0 };
644     SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S stProcSegIdx = { 0 };
645 
646     /* Set configuration parameter */
647     stNnieCfg.pszPic = pcSrcFile;
648     stNnieCfg.u32MaxInputNum = u32PicNum; // max input image num in each batch
649     stNnieCfg.u32MaxRoiNum = 0;
650     stNnieCfg.aenNnieCoreId[0] = SVP_NNIE_ID_0; // set NNIE core
651     s_stCnnSoftwareParam.u32TopN = 5; /* to get 5 highest results */
652     g_stop_signal = HI_FALSE;
653 
654     /* Sys init */
655     s32Ret = SAMPLE_COMM_SVP_CheckSysInit();
656     SAMPLE_SVP_CHECK_EXPR_RET_VOID(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
657         "Error,SAMPLE_COMM_SVP_CheckSysInit failed!\n");
658 
659     if (g_stop_signal == HI_TRUE) {
660         SAMPLE_SVP_NNIE_Cnn_Stop();
661         return;
662     }
663 
664     /* CNN Load model */
665     SAMPLE_SVP_TRACE_INFO("Cnn Load model!\n");
666     s32Ret = SAMPLE_COMM_SVP_NNIE_LoadModel(pcModelName, &s_stCnnModel);
667     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, CNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
668         "Error,SAMPLE_COMM_SVP_NNIE_LoadModel failed!\n");
669 
670     /* CNN parameter initialization */
671     /* Cnn software parameters are set in SAMPLE_SVP_NNIE_Cnn_SoftwareParaInit,
672      if user has changed net struct, please make sure the parameter settings in
673      SAMPLE_SVP_NNIE_Cnn_SoftwareParaInit function are correct */
674     SAMPLE_SVP_TRACE_INFO("Cnn parameter initialization!\n");
675     if (g_stop_signal == HI_TRUE) {
676         SAMPLE_SVP_NNIE_Cnn_Stop();
677         return;
678     }
679     s_stCnnNnieParam.pstModel = &s_stCnnModel.stModel;
680     s32Ret = SAMPLE_SVP_NNIE_Cnn_ParamInit(&stNnieCfg, &s_stCnnNnieParam, &s_stCnnSoftwareParam);
681     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, CNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
682         "Error,SAMPLE_SVP_NNIE_Cnn_ParamInit failed!\n");
683 
684     /* record tskBuf */
685     s32Ret = HI_MPI_SVP_NNIE_AddTskBuf(&(s_stCnnNnieParam.astForwardCtrl[0].stTskBuf));
686     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, CNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
687         "Error,HI_MPI_SVP_NNIE_AddTskBuf failed!\n");
688 
689     /* Fill src data */
690     SAMPLE_SVP_TRACE_INFO("Cnn start!\n");
691     if (g_stop_signal == HI_TRUE) {
692         SAMPLE_SVP_NNIE_Cnn_Stop();
693         return;
694     }
695     stInputDataIdx.u32SegIdx = 0;
696     stInputDataIdx.u32NodeIdx = 0;
697     s32Ret = SAMPLE_SVP_NNIE_FillSrcData(&stNnieCfg, &s_stCnnNnieParam, &stInputDataIdx);
698     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, CNN_FAIL_1, SAMPLE_SVP_ERR_LEVEL_ERROR,
699         "Error,SAMPLE_SVP_NNIE_FillSrcData failed!\n");
700 
701     /* NNIE process(process the 0-th segment) */
702     if (g_stop_signal == HI_TRUE) {
703         SAMPLE_SVP_NNIE_Cnn_Stop();
704         return;
705     }
706     stProcSegIdx.u32SegIdx = 0;
707     s32Ret = SAMPLE_SVP_NNIE_Forward(&s_stCnnNnieParam, &stInputDataIdx, &stProcSegIdx, HI_TRUE);
708     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, CNN_FAIL_1, SAMPLE_SVP_ERR_LEVEL_ERROR,
709         "Error,SAMPLE_SVP_NNIE_Forward failed!\n");
710 
711     /* Software process */
712     /* if user has changed net struct, please make sure SAMPLE_SVP_NNIE_Cnn_GetTopN
713      function's input data are correct */
714     if (g_stop_signal == HI_TRUE) {
715         SAMPLE_SVP_NNIE_Cnn_Stop();
716         return;
717     }
718     s32Ret = SAMPLE_SVP_NNIE_Cnn_GetTopN(&s_stCnnNnieParam, &s_stCnnSoftwareParam);
719     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, CNN_FAIL_1, SAMPLE_SVP_ERR_LEVEL_ERROR,
720         "Error,SAMPLE_SVP_NNIE_CnnGetTopN failed!\n");
721 
722     /* Print result */
723     SAMPLE_SVP_TRACE_INFO("Cnn result:\n");
724     s32Ret = SAMPLE_SVP_NNIE_Cnn_PrintResult(&(s_stCnnSoftwareParam.stGetTopN), s_stCnnSoftwareParam.u32TopN);
725     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, CNN_FAIL_1, SAMPLE_SVP_ERR_LEVEL_ERROR,
726         "Error,SAMPLE_SVP_NNIE_Cnn_PrintResult failed!\n");
727 
728 CNN_FAIL_1:
729     /* Remove TskBuf */
730     s32Ret = HI_MPI_SVP_NNIE_RemoveTskBuf(&(s_stCnnNnieParam.astForwardCtrl[0].stTskBuf));
731     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, CNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
732         "Error,HI_MPI_SVP_NNIE_RemoveTskBuf failed!\n");
733 CNN_FAIL_0:
734     SAMPLE_SVP_NNIE_Cnn_Deinit(&s_stCnnNnieParam, &s_stCnnSoftwareParam, &s_stCnnModel);
735     SAMPLE_COMM_SVP_CheckSysExit();
736 }
737 
SAMPLE_SVP_NNIE_Cnn_HandleSig(hi_void)738 hi_void SAMPLE_SVP_NNIE_Cnn_HandleSig(hi_void)
739 {
740     g_stop_signal = HI_TRUE;
741 }
SAMPLE_SVP_NNIE_Segnet_Stop(void)742 static void SAMPLE_SVP_NNIE_Segnet_Stop(void)
743 {
744     SAMPLE_SVP_NNIE_Cnn_Deinit(&s_stSegnetNnieParam, NULL, &s_stSegnetModel);
745     (HI_VOID)memset_s(&s_stSegnetNnieParam, sizeof(SAMPLE_SVP_NNIE_PARAM_S), 0, sizeof(SAMPLE_SVP_NNIE_PARAM_S));
746     (HI_VOID)memset_s(&s_stSegnetModel, sizeof(SAMPLE_SVP_NNIE_MODEL_S), 0, sizeof(SAMPLE_SVP_NNIE_MODEL_S));
747     SAMPLE_COMM_SVP_CheckSysExit();
748     printf("\033[0;31mprogram termination abnormally!\033[0;39m\n");
749 }
750 
751 /* function : show Segnet sample(image 224x224 U8_C3) */
SAMPLE_SVP_NNIE_Segnet(void)752 void SAMPLE_SVP_NNIE_Segnet(void)
753 {
754     const HI_CHAR *pcSrcFile = "./data/nnie_image/rgb_planar/segnet_image_224x224.bgr";
755     const HI_CHAR *pcModelName = "./data/nnie_model/segmentation/inst_segnet_cycle.wk";
756     const HI_U32 u32PicNum = 1;
757     HI_S32 s32Ret = HI_SUCCESS;
758     SAMPLE_SVP_NNIE_CFG_S stNnieCfg = { 0 };
759     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S stInputDataIdx = { 0 };
760     SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S stProcSegIdx = { 0 };
761 
762     /* Set configuration parameter */
763     stNnieCfg.pszPic = pcSrcFile;
764     stNnieCfg.u32MaxInputNum = u32PicNum; // max input image num in each batch
765     stNnieCfg.u32MaxRoiNum = 0;
766     stNnieCfg.aenNnieCoreId[0] = SVP_NNIE_ID_0;
767     g_stop_signal = HI_FALSE;
768 
769     /* Sys init */
770     s32Ret = SAMPLE_COMM_SVP_CheckSysInit();
771     SAMPLE_SVP_CHECK_EXPR_RET_VOID(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
772         "Error,SAMPLE_COMM_SVP_CheckSysInit failed!\n");
773     if (g_stop_signal == HI_TRUE) {
774         SAMPLE_SVP_NNIE_Segnet_Stop();
775         return;
776     }
777     /* Segnet Load model */
778     SAMPLE_SVP_TRACE_INFO("Segnet Load model!\n");
779     s32Ret = SAMPLE_COMM_SVP_NNIE_LoadModel(pcModelName, &s_stSegnetModel);
780     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, SEGNET_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
781         "Error,SAMPLE_COMM_SVP_NNIE_LoadModel failed!\n");
782 
783     /* Segnet parameter initialization */
784     SAMPLE_SVP_TRACE_INFO("Segnet parameter initialization!\n");
785     if (g_stop_signal == HI_TRUE) {
786         SAMPLE_SVP_NNIE_Segnet_Stop();
787         return;
788     }
789     s_stSegnetNnieParam.pstModel = &s_stSegnetModel.stModel;
790     s32Ret = SAMPLE_SVP_NNIE_Cnn_ParamInit(&stNnieCfg, &s_stSegnetNnieParam, NULL);
791     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, SEGNET_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
792         "Error,SAMPLE_SVP_NNIE_Cnn_ParamInit failed!\n");
793 
794     /* Fill src data */
795     SAMPLE_SVP_TRACE_INFO("Segnet start!\n");
796     stInputDataIdx.u32SegIdx = 0;
797     stInputDataIdx.u32NodeIdx = 0;
798     if (g_stop_signal == HI_TRUE) {
799         SAMPLE_SVP_NNIE_Segnet_Stop();
800     }
801     s32Ret = SAMPLE_SVP_NNIE_FillSrcData(&stNnieCfg, &s_stSegnetNnieParam, &stInputDataIdx);
802     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, SEGNET_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
803         "Error,SAMPLE_SVP_NNIE_FillSrcData failed!\n");
804 
805     /* NNIE process(process the 0-th segment) */
806     if (g_stop_signal == HI_TRUE) {
807         SAMPLE_SVP_NNIE_Segnet_Stop();
808         return;
809     }
810     stProcSegIdx.u32SegIdx = 0;
811     s32Ret = SAMPLE_SVP_NNIE_Forward(&s_stSegnetNnieParam, &stInputDataIdx, &stProcSegIdx, HI_TRUE);
812     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, SEGNET_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
813         "Error,SAMPLE_SVP_NNIE_Forward failed!\n");
814 
815     /* print report result */
816     s32Ret = SAMPLE_SVP_NNIE_PrintReportResult(&s_stSegnetNnieParam);
817     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, SEGNET_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
818         "Error,SAMPLE_SVP_NNIE_PrintReportResult failed!\n");
819 
820     SAMPLE_SVP_TRACE_INFO("Segnet is successfully processed!\n");
821 
822 SEGNET_FAIL_0:
823     SAMPLE_SVP_NNIE_Cnn_Deinit(&s_stSegnetNnieParam, NULL, &s_stSegnetModel);
824     SAMPLE_COMM_SVP_CheckSysExit();
825 }
826 
827 /* function : Segnet sample signal handle */
SAMPLE_SVP_NNIE_Segnet_HandleSig(hi_void)828 hi_void SAMPLE_SVP_NNIE_Segnet_HandleSig(hi_void)
829 {
830     g_stop_signal = HI_TRUE;
831 }
832 
833 /* function : print detection result */
SAMPLE_SVP_NNIE_Detection_PrintResult(SVP_BLOB_S * pstDstScore,SVP_BLOB_S * pstDstRoi,SVP_BLOB_S * pstClassRoiNum,HI_FLOAT f32PrintResultThresh)834 static HI_S32 SAMPLE_SVP_NNIE_Detection_PrintResult(SVP_BLOB_S *pstDstScore, SVP_BLOB_S *pstDstRoi,
835     SVP_BLOB_S *pstClassRoiNum, HI_FLOAT f32PrintResultThresh)
836 {
837     HI_U32 i = 0, j = 0;
838     HI_U32 u32RoiNumBias = 0;
839     HI_U32 u32ScoreBias = 0;
840     HI_U32 u32BboxBias = 0;
841     HI_FLOAT f32Score = 0.0f;
842     HI_S32 *ps32Score = SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_S32, pstDstScore->u64VirAddr);
843     HI_S32 *ps32Roi = SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_S32, pstDstRoi->u64VirAddr);
844     HI_S32 *ps32ClassRoiNum = SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_S32, pstClassRoiNum->u64VirAddr);
845     HI_U32 u32ClassNum = pstClassRoiNum->unShape.stWhc.u32Width;
846     HI_S32 s32XMin = 0, s32YMin = 0, s32XMax = 0, s32YMax = 0;
847 
848     u32RoiNumBias += ps32ClassRoiNum[0];
849     for (i = 1; i < u32ClassNum; i++) {
850         u32ScoreBias = u32RoiNumBias;
851         u32BboxBias = u32RoiNumBias * SAMPLE_SVP_NNIE_COORDI_NUM;
852         /* if the confidence score greater than result threshold, the result will be printed */
853         if ((HI_FLOAT)ps32Score[u32ScoreBias] / SAMPLE_SVP_NNIE_QUANT_BASE >= f32PrintResultThresh &&
854             ps32ClassRoiNum[i] != 0) {
855             SAMPLE_SVP_TRACE_INFO("==== The %dth class box info====\n", i);
856         }
857         for (j = 0; j < (HI_U32)ps32ClassRoiNum[i]; j++) {
858             f32Score = (HI_FLOAT)ps32Score[u32ScoreBias + j] / SAMPLE_SVP_NNIE_QUANT_BASE;
859             if (f32Score < f32PrintResultThresh) {
860                 break;
861             }
862             s32XMin = ps32Roi[u32BboxBias + j * SAMPLE_SVP_NNIE_COORDI_NUM];
863             s32YMin = ps32Roi[u32BboxBias + j * SAMPLE_SVP_NNIE_COORDI_NUM + 1]; /* to get next element of this array */
864             s32XMax = ps32Roi[u32BboxBias + j * SAMPLE_SVP_NNIE_COORDI_NUM + 2]; /* to get next element of this array */
865             s32YMax = ps32Roi[u32BboxBias + j * SAMPLE_SVP_NNIE_COORDI_NUM + 3]; /* to get next element of this array */
866             SAMPLE_SVP_TRACE_INFO("%d %d %d %d %f\n", s32XMin, s32YMin, s32XMax, s32YMax, f32Score);
867         }
868         u32RoiNumBias += ps32ClassRoiNum[i];
869     }
870     return HI_SUCCESS;
871 }
872 
873 /* function : FasterRcnn software deinit */
SAMPLE_SVP_NNIE_FasterRcnn_SoftwareDeinit(SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S * pstSoftWareParam)874 static HI_S32 SAMPLE_SVP_NNIE_FasterRcnn_SoftwareDeinit(SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S *pstSoftWareParam)
875 {
876     HI_S32 s32Ret = HI_SUCCESS;
877     SAMPLE_SVP_CHECK_EXPR_RET(pstSoftWareParam == NULL, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
878         "Error, pstSoftWareParam can't be NULL!\n");
879     if ((pstSoftWareParam->stRpnTmpBuf.u64PhyAddr != 0) && (pstSoftWareParam->stRpnTmpBuf.u64VirAddr != 0)) {
880         SAMPLE_SVP_MMZ_FREE(pstSoftWareParam->stRpnTmpBuf.u64PhyAddr, pstSoftWareParam->stRpnTmpBuf.u64VirAddr);
881         pstSoftWareParam->stRpnTmpBuf.u64PhyAddr = 0;
882         pstSoftWareParam->stRpnTmpBuf.u64VirAddr = 0;
883     }
884     return s32Ret;
885 }
886 
887 /* function : FasterRcnn Deinit */
SAMPLE_SVP_NNIE_FasterRcnn_Deinit(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S * pstSoftWareParam,SAMPLE_SVP_NNIE_MODEL_S * pstNnieModel)888 static HI_S32 SAMPLE_SVP_NNIE_FasterRcnn_Deinit(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
889     SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S *pstSoftWareParam, SAMPLE_SVP_NNIE_MODEL_S *pstNnieModel)
890 {
891     HI_S32 s32Ret = HI_SUCCESS;
892     /* hardware deinit */
893     if (pstNnieParam != NULL) {
894         s32Ret = SAMPLE_COMM_SVP_NNIE_ParamDeinit(pstNnieParam);
895         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
896             "Error,SAMPLE_COMM_SVP_NNIE_ParamDeinit failed!\n");
897     }
898     /* software deinit */
899     if (pstSoftWareParam != NULL) {
900         s32Ret = SAMPLE_SVP_NNIE_FasterRcnn_SoftwareDeinit(pstSoftWareParam);
901         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
902             "Error,SAMPLE_SVP_NNIE_FasterRcnn_SoftwareDeinit failed!\n");
903     }
904     /* model deinit */
905     if (pstNnieModel != NULL) {
906         s32Ret = SAMPLE_COMM_SVP_NNIE_UnloadModel(pstNnieModel);
907         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
908             "Error,SAMPLE_COMM_SVP_NNIE_UnloadModel failed!\n");
909     }
910     return s32Ret;
911 }
912 
913 /* function : FasterRcnn software para init */
SAMPLE_SVP_NNIE_FasterRcnn_SoftwareInit(SAMPLE_SVP_NNIE_CFG_S * pstCfg,SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S * pstSoftWareParam)914 static HI_S32 SAMPLE_SVP_NNIE_FasterRcnn_SoftwareInit(SAMPLE_SVP_NNIE_CFG_S *pstCfg,
915     SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S *pstSoftWareParam)
916 {
917     HI_U32 i = 0, j = 0;
918     HI_U32 u32RpnTmpBufSize = 0;
919     HI_U32 u32RpnBboxBufSize = 0;
920     HI_U32 u32GetResultTmpBufSize = 0;
921     HI_U32 u32DstRoiSize = 0;
922     HI_U32 u32DstScoreSize = 0;
923     HI_U32 u32ClassRoiNumSize = 0;
924     HI_U32 u32ClassNum = 0;
925     HI_U32 u32TotalSize = 0;
926     HI_S32 s32Ret = HI_SUCCESS;
927     HI_U64 u64PhyAddr = 0;
928     HI_U8 *pu8VirAddr = NULL;
929 
930     /* RPN parameter init */
931     /* The values of the following parameters are related to algorithm principles.
932         For details, see related algorithms. */
933     pstSoftWareParam->u32MaxRoiNum = pstCfg->u32MaxRoiNum;
934     if (SAMPLE_SVP_NNIE_VGG16_FASTER_RCNN == s_enNetType) {
935         pstSoftWareParam->u32ClassNum = 4;
936         pstSoftWareParam->u32NumRatioAnchors = 3;
937         pstSoftWareParam->u32NumScaleAnchors = 3;
938         pstSoftWareParam->au32Scales[0] = 8 * SAMPLE_SVP_QUANT_BASE;
939         pstSoftWareParam->au32Scales[1] = 16 * SAMPLE_SVP_QUANT_BASE;
940         pstSoftWareParam->au32Scales[2] = 32 * SAMPLE_SVP_QUANT_BASE;
941         pstSoftWareParam->au32Ratios[0] = 0.5 * SAMPLE_SVP_QUANT_BASE;
942         pstSoftWareParam->au32Ratios[1] = 1 * SAMPLE_SVP_QUANT_BASE;
943         pstSoftWareParam->au32Ratios[2] = 2 * SAMPLE_SVP_QUANT_BASE;
944     } else {
945         pstSoftWareParam->u32ClassNum = 2;
946         pstSoftWareParam->u32NumRatioAnchors = 1;
947         pstSoftWareParam->u32NumScaleAnchors = 9;
948         pstSoftWareParam->au32Scales[0] = 1.5 * SAMPLE_SVP_QUANT_BASE;
949         pstSoftWareParam->au32Scales[1] = 2.1 * SAMPLE_SVP_QUANT_BASE;
950         pstSoftWareParam->au32Scales[2] = 2.9 * SAMPLE_SVP_QUANT_BASE;
951         pstSoftWareParam->au32Scales[3] = 4.1 * SAMPLE_SVP_QUANT_BASE;
952         pstSoftWareParam->au32Scales[4] = 5.8 * SAMPLE_SVP_QUANT_BASE;
953         pstSoftWareParam->au32Scales[5] = 8.0 * SAMPLE_SVP_QUANT_BASE;
954         pstSoftWareParam->au32Scales[6] = 11.3 * SAMPLE_SVP_QUANT_BASE;
955         pstSoftWareParam->au32Scales[7] = 15.8 * SAMPLE_SVP_QUANT_BASE;
956         pstSoftWareParam->au32Scales[8] = 22.1 * SAMPLE_SVP_QUANT_BASE;
957         pstSoftWareParam->au32Ratios[0] = 2.44 * SAMPLE_SVP_QUANT_BASE;
958     }
959 
960     pstSoftWareParam->u32OriImHeight = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Height;
961     pstSoftWareParam->u32OriImWidth = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Width;
962     pstSoftWareParam->u32MinSize = 16;
963     pstSoftWareParam->u32FilterThresh = 16;
964     pstSoftWareParam->u32SpatialScale = (HI_U32)(0.0625 * SAMPLE_SVP_QUANT_BASE);
965     pstSoftWareParam->u32NmsThresh = (HI_U32)(0.7 * SAMPLE_SVP_QUANT_BASE);
966     pstSoftWareParam->u32FilterThresh = 0;
967     pstSoftWareParam->u32NumBeforeNms = 6000;
968     for (i = 0; i < pstSoftWareParam->u32ClassNum; i++) {
969         pstSoftWareParam->au32ConfThresh[i] = 1;
970     }
971     pstSoftWareParam->u32ValidNmsThresh = (HI_U32)(0.3 * SAMPLE_SVP_QUANT_BASE);
972     pstSoftWareParam->stRpnBbox.enType = SVP_BLOB_TYPE_S32;
973     pstSoftWareParam->stRpnBbox.unShape.stWhc.u32Chn = 1;
974     pstSoftWareParam->stRpnBbox.unShape.stWhc.u32Height = pstCfg->u32MaxRoiNum;
975     pstSoftWareParam->stRpnBbox.unShape.stWhc.u32Width = SAMPLE_SVP_COORDI_NUM;
976     pstSoftWareParam->stRpnBbox.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(SAMPLE_SVP_COORDI_NUM * sizeof(HI_U32));
977     pstSoftWareParam->stRpnBbox.u32Num = 1;
978     for (i = 0; i < SAMPLE_SVP_NNIE_SEGMENT_NUM; i++) {
979         for (j = 0; j < pstNnieParam->pstModel->astSeg[0].u16DstNum; j++) {
980             if (strncmp(pstNnieParam->pstModel->astSeg[0].astDstNode[j].szName,
981                 pstSoftWareParam->apcRpnDataLayerName[i], SVP_NNIE_NODE_NAME_LEN) == 0) {
982                 pstSoftWareParam->aps32Conv[i] =
983                     SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_S32, pstNnieParam->astSegData[0].astDst[j].u64VirAddr);
984                 pstSoftWareParam->au32ConvHeight[i] =
985                     pstNnieParam->pstModel->astSeg[0].astDstNode[j].unShape.stWhc.u32Height;
986                 pstSoftWareParam->au32ConvWidth[i] =
987                     pstNnieParam->pstModel->astSeg[0].astDstNode[j].unShape.stWhc.u32Width;
988                 pstSoftWareParam->au32ConvChannel[i] =
989                     pstNnieParam->pstModel->astSeg[0].astDstNode[j].unShape.stWhc.u32Chn;
990                 break;
991             }
992         }
993         SAMPLE_SVP_CHECK_EXPR_RET((j == pstNnieParam->pstModel->astSeg[0].u16DstNum), HI_FAILURE,
994             SAMPLE_SVP_ERR_LEVEL_ERROR, "Error,failed to find report node %s!\n",
995             pstSoftWareParam->apcRpnDataLayerName[i]);
996         if (i == 0) {
997             pstSoftWareParam->u32ConvStride = pstNnieParam->astSegData[0].astDst[j].u32Stride;
998         }
999     }
1000 
1001     /* calculate software mem size */
1002     u32ClassNum = pstSoftWareParam->u32ClassNum;
1003     u32RpnTmpBufSize = SAMPLE_SVP_NNIE_RpnTmpBufSize(pstSoftWareParam->u32NumRatioAnchors,
1004         pstSoftWareParam->u32NumScaleAnchors, pstSoftWareParam->au32ConvHeight[0], pstSoftWareParam->au32ConvWidth[0]);
1005     SAMPLE_SVP_CHECK_EXPR_RET(u32RpnTmpBufSize == 0, HI_ERR_SVP_NNIE_ILLEGAL_PARAM, SAMPLE_SVP_ERR_LEVEL_ERROR,
1006         "Error,SAMPLE_SVP_NNIE_RpnTmpBufSize failed!\n");
1007     u32RpnTmpBufSize = SAMPLE_SVP_NNIE_ALIGN16(u32RpnTmpBufSize);
1008     u32RpnBboxBufSize = pstSoftWareParam->stRpnBbox.u32Num * pstSoftWareParam->stRpnBbox.unShape.stWhc.u32Height *
1009         pstSoftWareParam->stRpnBbox.u32Stride;
1010     u32GetResultTmpBufSize = SAMPLE_SVP_NNIE_FasterRcnn_GetResultTmpBufSize(pstCfg->u32MaxRoiNum, u32ClassNum);
1011     SAMPLE_SVP_CHECK_EXPR_RET(u32GetResultTmpBufSize == 0, HI_ERR_SVP_NNIE_ILLEGAL_PARAM, SAMPLE_SVP_ERR_LEVEL_ERROR,
1012         "Error,SAMPLE_SVP_NNIE_FasterRcnn_GetResultTmpBufSize failed!\n");
1013     u32GetResultTmpBufSize = SAMPLE_SVP_NNIE_ALIGN16(u32GetResultTmpBufSize);
1014     u32DstRoiSize =
1015         SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstCfg->u32MaxRoiNum * sizeof(HI_U32) * SAMPLE_SVP_COORDI_NUM);
1016     u32DstScoreSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstCfg->u32MaxRoiNum * sizeof(HI_U32));
1017     u32ClassRoiNumSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * sizeof(HI_U32));
1018     u32TotalSize = u32RpnTmpBufSize + u32RpnBboxBufSize + u32GetResultTmpBufSize + u32DstRoiSize + u32DstScoreSize +
1019         u32ClassRoiNumSize;
1020 
1021     /* malloc mem */
1022     s32Ret = SAMPLE_COMM_SVP_MallocCached("SAMPLE_RCNN_INIT", NULL, (HI_U64 *)&u64PhyAddr, (void **)&pu8VirAddr,
1023         u32TotalSize);
1024     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
1025         "Error,Malloc memory failed!\n");
1026     (HI_VOID)memset_s(pu8VirAddr, u32TotalSize, 0, u32TotalSize);
1027     SAMPLE_COMM_SVP_FlushCache(u64PhyAddr, (void *)pu8VirAddr, u32TotalSize);
1028 
1029     /* set addr */
1030     pstSoftWareParam->stRpnTmpBuf.u64PhyAddr = u64PhyAddr;
1031     pstSoftWareParam->stRpnTmpBuf.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr);
1032     pstSoftWareParam->stRpnTmpBuf.u32Size = u32RpnTmpBufSize;
1033 
1034     pstSoftWareParam->stRpnBbox.u64PhyAddr = u64PhyAddr + u32RpnTmpBufSize;
1035     pstSoftWareParam->stRpnBbox.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr) + u32RpnTmpBufSize;
1036 
1037     pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr = u64PhyAddr + u32RpnBboxBufSize + u32RpnTmpBufSize;
1038     pstSoftWareParam->stGetResultTmpBuf.u64VirAddr =
1039         (HI_U64)((HI_UINTPTR_T)pu8VirAddr + u32RpnBboxBufSize + u32RpnTmpBufSize);
1040     pstSoftWareParam->stGetResultTmpBuf.u32Size = u32GetResultTmpBufSize;
1041 
1042     pstSoftWareParam->stDstRoi.enType = SVP_BLOB_TYPE_S32;
1043     pstSoftWareParam->stDstRoi.u64PhyAddr = u64PhyAddr + u32RpnBboxBufSize + u32RpnTmpBufSize + u32GetResultTmpBufSize;
1044     pstSoftWareParam->stDstRoi.u64VirAddr =
1045         (HI_U64)((HI_UINTPTR_T)pu8VirAddr + u32RpnBboxBufSize + u32RpnTmpBufSize + u32GetResultTmpBufSize);
1046     pstSoftWareParam->stDstRoi.u32Stride =
1047         SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstSoftWareParam->u32MaxRoiNum * sizeof(HI_U32) * SAMPLE_SVP_COORDI_NUM);
1048     pstSoftWareParam->stDstRoi.u32Num = 1;
1049     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Chn = 1;
1050     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Height = 1;
1051     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Width =
1052         u32ClassNum * pstSoftWareParam->u32MaxRoiNum * SAMPLE_SVP_COORDI_NUM;
1053 
1054     pstSoftWareParam->stDstScore.enType = SVP_BLOB_TYPE_S32;
1055     pstSoftWareParam->stDstScore.u64PhyAddr =
1056         u64PhyAddr + u32RpnBboxBufSize + u32RpnTmpBufSize + u32GetResultTmpBufSize + u32DstRoiSize;
1057     pstSoftWareParam->stDstScore.u64VirAddr = SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64, pu8VirAddr) +
1058         u32RpnBboxBufSize + u32RpnTmpBufSize + u32GetResultTmpBufSize + u32DstRoiSize;
1059     pstSoftWareParam->stDstScore.u32Stride =
1060         SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstSoftWareParam->u32MaxRoiNum * sizeof(HI_U32));
1061     pstSoftWareParam->stDstScore.u32Num = 1;
1062     pstSoftWareParam->stDstScore.unShape.stWhc.u32Chn = 1;
1063     pstSoftWareParam->stDstScore.unShape.stWhc.u32Height = 1;
1064     pstSoftWareParam->stDstScore.unShape.stWhc.u32Width = u32ClassNum * pstSoftWareParam->u32MaxRoiNum;
1065 
1066     pstSoftWareParam->stClassRoiNum.enType = SVP_BLOB_TYPE_S32;
1067     pstSoftWareParam->stClassRoiNum.u64PhyAddr =
1068         u64PhyAddr + u32RpnBboxBufSize + u32RpnTmpBufSize + u32GetResultTmpBufSize + u32DstRoiSize + u32DstScoreSize;
1069     pstSoftWareParam->stClassRoiNum.u64VirAddr = SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64, pu8VirAddr) +
1070         u32RpnBboxBufSize + u32RpnTmpBufSize + u32GetResultTmpBufSize + u32DstRoiSize + u32DstScoreSize;
1071     pstSoftWareParam->stClassRoiNum.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * sizeof(HI_U32));
1072     pstSoftWareParam->stClassRoiNum.u32Num = 1;
1073     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Chn = 1;
1074     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Height = 1;
1075     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Width = u32ClassNum;
1076 
1077     return s32Ret;
1078 }
1079 
1080 /* function : FasterRcnn parameter initialization */
SAMPLE_SVP_NNIE_FasterRcnn_ParamInit(SAMPLE_SVP_NNIE_CFG_S * pstFasterRcnnCfg,SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S * pstSoftWareParam)1081 static HI_S32 SAMPLE_SVP_NNIE_FasterRcnn_ParamInit(SAMPLE_SVP_NNIE_CFG_S *pstFasterRcnnCfg,
1082     SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S *pstSoftWareParam)
1083 {
1084     HI_S32 s32Ret = HI_SUCCESS;
1085     /* init hardware parameter */
1086     s32Ret = SAMPLE_COMM_SVP_NNIE_ParamInit(pstFasterRcnnCfg, pstNnieParam);
1087     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1088         "Error(%#x),SAMPLE_COMM_SVP_NNIE_ParamInit failed!\n", s32Ret);
1089 
1090     /* init software parameter */
1091     s32Ret = SAMPLE_SVP_NNIE_FasterRcnn_SoftwareInit(pstFasterRcnnCfg, pstNnieParam, pstSoftWareParam);
1092     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1093         "Error(%#x),SAMPLE_SVP_NNIE_FasterRcnn_SoftwareInit failed!\n", s32Ret);
1094 
1095     return s32Ret;
1096 INIT_FAIL_0:
1097     s32Ret = SAMPLE_SVP_NNIE_FasterRcnn_Deinit(pstNnieParam, pstSoftWareParam, NULL);
1098     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
1099         "Error(%#x),SAMPLE_SVP_NNIE_FasterRcnn_Deinit failed!\n", s32Ret);
1100     return HI_FAILURE;
1101 }
SAMPLE_SVP_NNIE_FasterRcnn_Stop(hi_void)1102 static hi_void SAMPLE_SVP_NNIE_FasterRcnn_Stop(hi_void)
1103 {
1104     SAMPLE_SVP_NNIE_FasterRcnn_Deinit(&s_stFasterRcnnNnieParam, &s_stFasterRcnnSoftwareParam, &s_stFasterRcnnModel);
1105     (HI_VOID)memset_s(&s_stFasterRcnnNnieParam, sizeof(SAMPLE_SVP_NNIE_PARAM_S), 0, sizeof(SAMPLE_SVP_NNIE_PARAM_S));
1106     (HI_VOID)memset_s(&s_stFasterRcnnSoftwareParam, sizeof(SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S), 0,
1107         sizeof(SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S));
1108     (HI_VOID)memset_s(&s_stFasterRcnnModel, sizeof(SAMPLE_SVP_NNIE_MODEL_S), 0, sizeof(SAMPLE_SVP_NNIE_MODEL_S));
1109     SAMPLE_COMM_SVP_CheckSysExit();
1110     printf("\033[0;31mprogram termination abnormally!\033[0;39m\n");
1111 }
1112 
1113 /* function : show fasterRcnn sample(image 1240x375 U8_C3) */
SAMPLE_SVP_NNIE_FasterRcnn(void)1114 void SAMPLE_SVP_NNIE_FasterRcnn(void)
1115 {
1116     const HI_CHAR *pcSrcFile = "./data/nnie_image/rgb_planar/single_person_1240x375.bgr";
1117     const HI_CHAR *pcModelName = "./data/nnie_model/detection/inst_alexnet_frcnn_cycle.wk";
1118     const HI_U32 u32PicNum = 1;
1119     HI_FLOAT f32PrintResultThresh = 0.0f;
1120     HI_S32 s32Ret = HI_SUCCESS;
1121     HI_U32 i = 0;
1122     SAMPLE_SVP_NNIE_CFG_S stNnieCfg = { 0 };
1123     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S stInputDataIdx = { 0 };
1124     SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S stProcSegIdx = { 0 };
1125     g_stop_signal = HI_FALSE;
1126 
1127     /* Set configuration parameter */
1128     s_enNetType = SAMPLE_SVP_NNIE_ALEXNET_FASTER_RCNN;
1129     f32PrintResultThresh = 0.8f;
1130     stNnieCfg.pszPic = pcSrcFile;
1131     stNnieCfg.u32MaxInputNum = u32PicNum; // max input image num in each batch
1132     stNnieCfg.u32MaxRoiNum = 300; // set maxmum 300 ROIs
1133     stNnieCfg.aenNnieCoreId[0] = SVP_NNIE_ID_0; // set NNIE core for 0-th Seg
1134     stNnieCfg.aenNnieCoreId[1] = SVP_NNIE_ID_0; // set NNIE core for 1-th Seg
1135 
1136     /* Sys init */
1137     s32Ret = SAMPLE_COMM_SVP_CheckSysInit();
1138     SAMPLE_SVP_CHECK_EXPR_RET_VOID(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
1139         "Error,SAMPLE_COMM_SVP_CheckSysInit failed!\n");
1140     if (g_stop_signal == HI_TRUE) {
1141         SAMPLE_SVP_NNIE_FasterRcnn_Stop();
1142         return;
1143     }
1144     /* FasterRcnn Load model */
1145     SAMPLE_SVP_TRACE_INFO("FasterRcnn Load model!\n");
1146     s32Ret = SAMPLE_COMM_SVP_NNIE_LoadModel(pcModelName, &s_stFasterRcnnModel);
1147     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FRCNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1148         "Error,SAMPLE_COMM_SVP_NNIE_LoadModel failed!\n");
1149 
1150     /* FasterRcnn para init */
1151     /* apcRpnDataLayerName is used to set RPN data layer name
1152       and search RPN input data,if user has changed network struct, please
1153       make sure the data layer names are correct */
1154     /* FasterRcnn parameters are set in SAMPLE_SVP_NNIE_FasterRcnn_SoftwareInit,
1155      if user has changed network struct, please make sure the parameter settings in
1156      SAMPLE_SVP_NNIE_FasterRcnn_SoftwareInit function are correct */
1157     SAMPLE_SVP_TRACE_INFO("FasterRcnn parameter initialization!\n");
1158     if (g_stop_signal == HI_TRUE) {
1159         SAMPLE_SVP_NNIE_FasterRcnn_Stop();
1160         return;
1161     }
1162     s_stFasterRcnnNnieParam.pstModel = &s_stFasterRcnnModel.stModel;
1163     s_stFasterRcnnSoftwareParam.apcRpnDataLayerName[0] = "rpn_cls_score";
1164     s_stFasterRcnnSoftwareParam.apcRpnDataLayerName[1] = "rpn_bbox_pred";
1165     s32Ret = SAMPLE_SVP_NNIE_FasterRcnn_ParamInit(&stNnieCfg, &s_stFasterRcnnNnieParam, &s_stFasterRcnnSoftwareParam);
1166     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FRCNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1167         "Error,SAMPLE_SVP_NNIE_FasterRcnn_ParamInit failed!\n");
1168 
1169     /* Fill 0-th input node of 0-th seg */
1170     SAMPLE_SVP_TRACE_INFO("FasterRcnn start!\n");
1171     if (g_stop_signal == HI_TRUE) {
1172         SAMPLE_SVP_NNIE_FasterRcnn_Stop();
1173         return;
1174     }
1175     stInputDataIdx.u32SegIdx = 0;
1176     stInputDataIdx.u32NodeIdx = 0;
1177     s32Ret = SAMPLE_SVP_NNIE_FillSrcData(&stNnieCfg, &s_stFasterRcnnNnieParam, &stInputDataIdx);
1178     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FRCNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1179         "Error,SAMPLE_SVP_NNIE_FillSrcData failed!\n");
1180 
1181     /* NNIE process 0-th seg */
1182     stProcSegIdx.u32SegIdx = 0;
1183     s32Ret = SAMPLE_SVP_NNIE_Forward(&s_stFasterRcnnNnieParam, &stInputDataIdx, &stProcSegIdx, HI_TRUE);
1184     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FRCNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1185         "Error,SAMPLE_SVP_NNIE_Forward failed!\n");
1186 
1187     /* RPN */
1188     if (g_stop_signal == HI_TRUE) {
1189         SAMPLE_SVP_NNIE_FasterRcnn_Stop();
1190         return;
1191     }
1192     s32Ret = SAMPLE_SVP_NNIE_FasterRcnn_Rpn(&s_stFasterRcnnSoftwareParam);
1193     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FRCNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1194         "Error,SAMPLE_SVP_NNIE_FasterRcnn_Rpn failed!\n");
1195 
1196     if (s_stFasterRcnnSoftwareParam.stRpnBbox.unShape.stWhc.u32Height != 0) {
1197         /* NNIE process 1-th seg, the input conv data comes from 0-th seg's 0-th report node,
1198          the input roi comes from RPN results */
1199         stInputDataIdx.u32SegIdx = 0;
1200         stInputDataIdx.u32NodeIdx = 0;
1201         stProcSegIdx.u32SegIdx = 1;
1202         if (g_stop_signal == HI_TRUE) {
1203             SAMPLE_SVP_NNIE_FasterRcnn_Stop();
1204             return;
1205         }
1206         s32Ret = SAMPLE_SVP_NNIE_ForwardWithBbox(&s_stFasterRcnnNnieParam, &stInputDataIdx,
1207             &s_stFasterRcnnSoftwareParam.stRpnBbox, &stProcSegIdx, HI_TRUE);
1208         SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FRCNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1209             "Error,SAMPLE_SVP_NNIE_Forward failed!\n");
1210 
1211         /* GetResult */
1212         /* if user has changed net struct, please make sure SAMPLE_SVP_NNIE_FasterRcnn_GetResult
1213          function's input data are correct */
1214         if (g_stop_signal == HI_TRUE) {
1215             SAMPLE_SVP_NNIE_FasterRcnn_Stop();
1216             return;
1217         }
1218         s32Ret = SAMPLE_SVP_NNIE_FasterRcnn_GetResult(&s_stFasterRcnnNnieParam, &s_stFasterRcnnSoftwareParam);
1219         SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FRCNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1220             "Error,SAMPLE_SVP_NNIE_FasterRcnn_GetResult failed!\n");
1221     } else {
1222         for (i = 0; i < s_stFasterRcnnSoftwareParam.stClassRoiNum.unShape.stWhc.u32Width; i++) {
1223             *(SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_U32, s_stFasterRcnnSoftwareParam.stClassRoiNum.u64VirAddr) + i) = 0;
1224         }
1225     }
1226     /* print result, Alexnet_FasterRcnn has 2 classes:
1227      class 0:background     class 1:pedestrian */
1228     if (g_stop_signal == HI_TRUE) {
1229         SAMPLE_SVP_NNIE_FasterRcnn_Stop();
1230         return;
1231     }
1232     SAMPLE_SVP_TRACE_INFO("FasterRcnn result:\n");
1233     (void)SAMPLE_SVP_NNIE_Detection_PrintResult(&s_stFasterRcnnSoftwareParam.stDstScore,
1234         &s_stFasterRcnnSoftwareParam.stDstRoi, &s_stFasterRcnnSoftwareParam.stClassRoiNum, f32PrintResultThresh);
1235 
1236 FRCNN_FAIL_0:
1237     SAMPLE_SVP_NNIE_FasterRcnn_Deinit(&s_stFasterRcnnNnieParam, &s_stFasterRcnnSoftwareParam, &s_stFasterRcnnModel);
1238     SAMPLE_COMM_SVP_CheckSysExit();
1239 }
1240 
1241 /* function :show fasterrcnn double_roipooling sample(image 224x224 U8_C3) */
SAMPLE_SVP_NNIE_FasterRcnn_DoubleRoiPooling(void)1242 void SAMPLE_SVP_NNIE_FasterRcnn_DoubleRoiPooling(void)
1243 {
1244     const HI_CHAR *pcSrcFile = "./data/nnie_image/rgb_planar/double_roipooling_224_224.bgr";
1245     const HI_CHAR *pcModelName = "./data/nnie_model/detection/inst_fasterrcnn_double_roipooling_cycle.wk";
1246     const HI_U32 u32PicNum = 1;
1247     HI_FLOAT f32PrintResultThresh = 0.0f;
1248     HI_S32 s32Ret = HI_SUCCESS;
1249     HI_U32 i = 0;
1250     SAMPLE_SVP_NNIE_CFG_S stNnieCfg = { 0 };
1251     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S stInputDataIdx = { 0 };
1252     SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S stProcSegIdx = { 0 };
1253 
1254     /* Set configuration parameter */
1255     s_enNetType = SAMPLE_SVP_NNIE_VGG16_FASTER_RCNN;
1256     f32PrintResultThresh = 0.8f;
1257     stNnieCfg.pszPic = pcSrcFile;
1258     stNnieCfg.u32MaxInputNum = u32PicNum; // max input image num in each batch
1259     stNnieCfg.u32MaxRoiNum = 300; // set maxmum 300 ROIs
1260     stNnieCfg.aenNnieCoreId[0] = SVP_NNIE_ID_0; // set NNIE core for 0-th Seg
1261     stNnieCfg.aenNnieCoreId[1] = SVP_NNIE_ID_0; // set NNIE core for 1-th Seg
1262     g_stop_signal = HI_FALSE;
1263 
1264     /* Sys init */
1265     s32Ret = SAMPLE_COMM_SVP_CheckSysInit();
1266     SAMPLE_SVP_CHECK_EXPR_RET_VOID(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
1267         "Error,SAMPLE_COMM_SVP_CheckSysInit failed!\n");
1268     if (g_stop_signal == HI_TRUE) {
1269         SAMPLE_SVP_NNIE_FasterRcnn_Stop();
1270         return;
1271     }
1272     /* FasterRcnn Load model */
1273     SAMPLE_SVP_TRACE_INFO("FasterRcnn Load model!\n");
1274     s32Ret = SAMPLE_COMM_SVP_NNIE_LoadModel(pcModelName, &s_stFasterRcnnModel);
1275     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FRCNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1276         "Error,SAMPLE_COMM_SVP_NNIE_LoadModel failed!\n");
1277 
1278     /* FasterRcnn para init */
1279     /* apcRpnDataLayerName is used to set RPN data layer name
1280       and search RPN input data,if user has changed network struct, please
1281       make sure the data layer names are correct */
1282     /* FasterRcnn parameters are set in SAMPLE_SVP_NNIE_FasterRcnn_SoftwareInit,
1283      if user has changed network struct, please make sure the parameter settings in
1284      SAMPLE_SVP_NNIE_FaasterRcnn_SoftwareInit function are correct */
1285     SAMPLE_SVP_TRACE_INFO("FasterRcnn parameter initialization!\n");
1286     if (g_stop_signal == HI_TRUE) {
1287         SAMPLE_SVP_NNIE_FasterRcnn_Stop();
1288         return;
1289     }
1290     s_stFasterRcnnNnieParam.pstModel = &s_stFasterRcnnModel.stModel;
1291     s_stFasterRcnnSoftwareParam.apcRpnDataLayerName[0] = "rpn_cls_score";
1292     s_stFasterRcnnSoftwareParam.apcRpnDataLayerName[1] = "rpn_bbox_pred";
1293     s32Ret = SAMPLE_SVP_NNIE_FasterRcnn_ParamInit(&stNnieCfg, &s_stFasterRcnnNnieParam, &s_stFasterRcnnSoftwareParam);
1294     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FRCNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1295         "Error,SAMPLE_SVP_NNIE_FasterRcnn_ParamInit failed!\n");
1296 
1297     /* Fill 0-th input node of 0-th seg */
1298     SAMPLE_SVP_TRACE_INFO("FasterRcnn start!\n");
1299     if (g_stop_signal == HI_TRUE) {
1300         SAMPLE_SVP_NNIE_FasterRcnn_Stop();
1301         return;
1302     }
1303     stInputDataIdx.u32SegIdx = 0;
1304     stInputDataIdx.u32NodeIdx = 0;
1305     s32Ret = SAMPLE_SVP_NNIE_FillSrcData(&stNnieCfg, &s_stFasterRcnnNnieParam, &stInputDataIdx);
1306     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FRCNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1307         "Error,SAMPLE_SVP_NNIE_FillSrcData failed!\n");
1308 
1309     /* NNIE process 0-th seg */
1310     stProcSegIdx.u32SegIdx = 0;
1311     s32Ret = SAMPLE_SVP_NNIE_Forward(&s_stFasterRcnnNnieParam, &stInputDataIdx, &stProcSegIdx, HI_TRUE);
1312     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FRCNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1313         "Error,SAMPLE_SVP_NNIE_Forward failed!\n");
1314 
1315     /* RPN */
1316     if (g_stop_signal == HI_TRUE) {
1317         SAMPLE_SVP_NNIE_FasterRcnn_Stop();
1318         return;
1319     }
1320     s32Ret = SAMPLE_SVP_NNIE_FasterRcnn_Rpn(&s_stFasterRcnnSoftwareParam);
1321     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FRCNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1322         "Error,SAMPLE_SVP_NNIE_FasterRcnn_Rpn failed!\n");
1323     if (s_stFasterRcnnSoftwareParam.stRpnBbox.unShape.stWhc.u32Height != 0) {
1324         /* NNIE process 1-st seg, the input conv data comes from 0-th seg's 0-th and
1325           1-st report node,the input roi comes from RPN results */
1326         stInputDataIdx.u32SegIdx = 0;
1327         stInputDataIdx.u32NodeIdx = 0;
1328         stProcSegIdx.u32SegIdx = 1;
1329         s32Ret = SAMPLE_SVP_NNIE_ForwardWithBbox(&s_stFasterRcnnNnieParam, &stInputDataIdx,
1330             &s_stFasterRcnnSoftwareParam.stRpnBbox, &stProcSegIdx, HI_TRUE);
1331         SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FRCNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1332             "Error,SAMPLE_SVP_NNIE_Forward failed!\n");
1333 
1334         /* GetResult */
1335         /* if user has changed net struct, please make sure SAMPLE_SVP_NNIE_FasterRcnn_GetResult
1336          function's input data are correct */
1337         s32Ret = SAMPLE_SVP_NNIE_FasterRcnn_GetResult(&s_stFasterRcnnNnieParam, &s_stFasterRcnnSoftwareParam);
1338         SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, FRCNN_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1339             "Error,SAMPLE_SVP_NNIE_FasterRcnn_GetResult failed!\n");
1340     } else {
1341         for (i = 0; i < s_stFasterRcnnSoftwareParam.stClassRoiNum.unShape.stWhc.u32Width; i++) {
1342             *(SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_U32, s_stFasterRcnnSoftwareParam.stClassRoiNum.u64VirAddr) + i) = 0;
1343         }
1344     }
1345     /* print result, FasterRcnn has 4 classes:
1346      class 0:background  class 1:person  class 2:people  class 3:person sitting */
1347     SAMPLE_SVP_TRACE_INFO("FasterRcnn result:\n");
1348     (void)SAMPLE_SVP_NNIE_Detection_PrintResult(&s_stFasterRcnnSoftwareParam.stDstScore,
1349         &s_stFasterRcnnSoftwareParam.stDstRoi, &s_stFasterRcnnSoftwareParam.stClassRoiNum, f32PrintResultThresh);
1350 FRCNN_FAIL_0:
1351     SAMPLE_SVP_NNIE_FasterRcnn_Deinit(&s_stFasterRcnnNnieParam, &s_stFasterRcnnSoftwareParam, &s_stFasterRcnnModel);
1352     SAMPLE_COMM_SVP_CheckSysExit();
1353 }
1354 
1355 /* function : fasterRcnn sample signal handle */
SAMPLE_SVP_NNIE_FasterRcnn_HandleSig(hi_void)1356 hi_void SAMPLE_SVP_NNIE_FasterRcnn_HandleSig(hi_void)
1357 {
1358     g_stop_signal = HI_TRUE;
1359 }
1360 
1361 /* function : Rfcn software deinit */
SAMPLE_SVP_NNIE_Rfcn_SoftwareDeinit(SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S * pstSoftWareParam)1362 static HI_S32 SAMPLE_SVP_NNIE_Rfcn_SoftwareDeinit(SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S *pstSoftWareParam)
1363 {
1364     HI_S32 s32Ret = HI_SUCCESS;
1365     SAMPLE_SVP_CHECK_EXPR_RET(pstSoftWareParam == NULL, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
1366         "Error, pstSoftWareParam can't be NULL!\n");
1367     if (pstSoftWareParam->stRpnTmpBuf.u64PhyAddr != 0 && pstSoftWareParam->stRpnTmpBuf.u64VirAddr != 0) {
1368         SAMPLE_SVP_MMZ_FREE(pstSoftWareParam->stRpnTmpBuf.u64PhyAddr, pstSoftWareParam->stRpnTmpBuf.u64VirAddr);
1369         pstSoftWareParam->stRpnTmpBuf.u64PhyAddr = 0;
1370         pstSoftWareParam->stRpnTmpBuf.u64VirAddr = 0;
1371     }
1372     return s32Ret;
1373 }
1374 
1375 /* function : Rfcn Deinit */
SAMPLE_SVP_NNIE_Rfcn_Deinit(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S * pstSoftWareParam,SAMPLE_SVP_NNIE_MODEL_S * pstNnieModel)1376 static HI_S32 SAMPLE_SVP_NNIE_Rfcn_Deinit(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
1377     SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S *pstSoftWareParam, SAMPLE_SVP_NNIE_MODEL_S *pstNnieModel)
1378 {
1379     HI_S32 s32Ret = HI_SUCCESS;
1380     /* hardware deinit */
1381     if (pstNnieParam != NULL) {
1382         s32Ret = SAMPLE_COMM_SVP_NNIE_ParamDeinit(pstNnieParam);
1383         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
1384             "Error,SAMPLE_COMM_SVP_NNIE_ParamDeinit failed!\n");
1385     }
1386     /* software deinit */
1387     if (pstSoftWareParam != NULL) {
1388         s32Ret = SAMPLE_SVP_NNIE_Rfcn_SoftwareDeinit(pstSoftWareParam);
1389         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
1390             "Error,SAMPLE_SVP_NNIE_Rfcn_SoftwareDeinit failed!\n");
1391     }
1392     /* model deinit */
1393     if (pstNnieModel != NULL) {
1394         s32Ret = SAMPLE_COMM_SVP_NNIE_UnloadModel(pstNnieModel);
1395         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
1396             "Error,SAMPLE_COMM_SVP_NNIE_UnloadModel failed!\n");
1397     }
1398     return s32Ret;
1399 }
1400 
1401 /* function : Rfcn software para init */
SAMPLE_SVP_NNIE_Rfcn_SoftwareInit(SAMPLE_SVP_NNIE_CFG_S * pstCfg,SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S * pstSoftWareParam)1402 static HI_S32 SAMPLE_SVP_NNIE_Rfcn_SoftwareInit(SAMPLE_SVP_NNIE_CFG_S *pstCfg, SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
1403     SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S *pstSoftWareParam)
1404 {
1405     HI_U32 i = 0, j = 0;
1406     HI_U32 u32RpnTmpBufSize = 0;
1407     HI_U32 u32RpnBboxBufSize = 0;
1408     HI_U32 u32GetResultTmpBufSize = 0;
1409     HI_U32 u32DstRoiSize = 0;
1410     HI_U32 u32DstScoreSize = 0;
1411     HI_U32 u32ClassRoiNumSize = 0;
1412     HI_U32 u32ClassNum = 0;
1413     HI_U32 u32TotalSize = 0;
1414     HI_S32 s32Ret = HI_SUCCESS;
1415     HI_U64 u64PhyAddr = 0;
1416     HI_U8 *pu8VirAddr = NULL;
1417 
1418     /* init Rpn para */
1419     /* The values of the following parameters are related to algorithm principles.
1420         For details, see related algorithms. */
1421     pstSoftWareParam->u32MaxRoiNum = pstCfg->u32MaxRoiNum;
1422     pstSoftWareParam->u32ClassNum = 21;
1423     pstSoftWareParam->u32NumRatioAnchors = 3;
1424     pstSoftWareParam->u32NumScaleAnchors = 3;
1425     pstSoftWareParam->au32Scales[0] = 8 * SAMPLE_SVP_NNIE_QUANT_BASE;
1426     pstSoftWareParam->au32Scales[1] = 16 * SAMPLE_SVP_NNIE_QUANT_BASE;
1427     pstSoftWareParam->au32Scales[2] = 32 * SAMPLE_SVP_NNIE_QUANT_BASE;
1428     pstSoftWareParam->au32Ratios[0] = 0.5 * SAMPLE_SVP_NNIE_QUANT_BASE;
1429     pstSoftWareParam->au32Ratios[1] = 1 * SAMPLE_SVP_NNIE_QUANT_BASE;
1430     pstSoftWareParam->au32Ratios[2] = 2 * SAMPLE_SVP_NNIE_QUANT_BASE;
1431     pstSoftWareParam->u32OriImHeight = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Height;
1432     pstSoftWareParam->u32OriImWidth = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Width;
1433     pstSoftWareParam->u32MinSize = 16;
1434     pstSoftWareParam->u32FilterThresh = 0;
1435     pstSoftWareParam->u32SpatialScale = (HI_U32)(0.0625 * SAMPLE_SVP_NNIE_QUANT_BASE);
1436     pstSoftWareParam->u32NmsThresh = (HI_U32)(0.7 * SAMPLE_SVP_NNIE_QUANT_BASE);
1437     pstSoftWareParam->u32FilterThresh = 0;
1438     pstSoftWareParam->u32NumBeforeNms = 6000;
1439     for (i = 0; i < pstSoftWareParam->u32ClassNum; i++) {
1440         pstSoftWareParam->au32ConfThresh[i] = 1;
1441         pstSoftWareParam->af32ScoreThr[i] = 0.8f;
1442     }
1443     pstSoftWareParam->u32ValidNmsThresh = (HI_U32)(0.3 * 4096);
1444 
1445     /* set rpn input data info, the input info is set according to RPN data layers' name */
1446     for (i = 0; i < SAMPLE_SVP_NNIE_SEGMENT_NUM; i++) {
1447         for (j = 0; j < pstNnieParam->pstModel->astSeg[0].u16DstNum; j++) {
1448             if (strncmp(pstNnieParam->pstModel->astSeg[0].astDstNode[j].szName,
1449                 pstSoftWareParam->apcRpnDataLayerName[i], SVP_NNIE_NODE_NAME_LEN) == 0) {
1450                 pstSoftWareParam->aps32Conv[i] =
1451                     SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_S32, pstNnieParam->astSegData[0].astDst[j].u64VirAddr);
1452                 pstSoftWareParam->au32ConvHeight[i] =
1453                     pstNnieParam->pstModel->astSeg[0].astDstNode[j].unShape.stWhc.u32Height;
1454                 pstSoftWareParam->au32ConvWidth[i] =
1455                     pstNnieParam->pstModel->astSeg[0].astDstNode[j].unShape.stWhc.u32Width;
1456                 pstSoftWareParam->au32ConvChannel[i] =
1457                     pstNnieParam->pstModel->astSeg[0].astDstNode[j].unShape.stWhc.u32Chn;
1458                 break;
1459             }
1460         }
1461         SAMPLE_SVP_CHECK_EXPR_RET((j == pstNnieParam->pstModel->astSeg[0].u16DstNum), HI_FAILURE,
1462             SAMPLE_SVP_ERR_LEVEL_ERROR, "Error,failed to find report node %s!\n",
1463             pstSoftWareParam->apcRpnDataLayerName[i]);
1464         if (i == 0) {
1465             pstSoftWareParam->u32ConvStride = pstNnieParam->astSegData[0].astDst[j].u32Stride;
1466         }
1467     }
1468 
1469     pstSoftWareParam->stRpnBbox.enType = SVP_BLOB_TYPE_S32;
1470     pstSoftWareParam->stRpnBbox.unShape.stWhc.u32Chn = 1;
1471     pstSoftWareParam->stRpnBbox.unShape.stWhc.u32Height = pstCfg->u32MaxRoiNum;
1472     pstSoftWareParam->stRpnBbox.unShape.stWhc.u32Width = SAMPLE_SVP_COORDI_NUM;
1473     pstSoftWareParam->stRpnBbox.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(SAMPLE_SVP_COORDI_NUM * sizeof(HI_U32));
1474     pstSoftWareParam->stRpnBbox.u32Num = 1;
1475 
1476     /* malloc software mem */
1477     u32RpnTmpBufSize = SAMPLE_SVP_NNIE_RpnTmpBufSize(pstSoftWareParam->u32NumRatioAnchors,
1478         pstSoftWareParam->u32NumScaleAnchors, pstSoftWareParam->au32ConvHeight[0], pstSoftWareParam->au32ConvWidth[0]);
1479     SAMPLE_SVP_CHECK_EXPR_RET(u32RpnTmpBufSize == 0, HI_ERR_SVP_NNIE_ILLEGAL_PARAM, SAMPLE_SVP_ERR_LEVEL_ERROR,
1480         "Error,SAMPLE_SVP_NNIE_RpnTmpBufSize failed!\n");
1481     u32RpnTmpBufSize = SAMPLE_SVP_NNIE_ALIGN16(u32RpnTmpBufSize);
1482     u32RpnBboxBufSize = pstSoftWareParam->stRpnBbox.u32Num * pstSoftWareParam->stRpnBbox.unShape.stWhc.u32Height *
1483         pstSoftWareParam->stRpnBbox.u32Stride;
1484     u32GetResultTmpBufSize = SAMPLE_SVP_NNIE_Rfcn_GetResultTmpBuf(pstCfg->u32MaxRoiNum, pstSoftWareParam->u32ClassNum);
1485     SAMPLE_SVP_CHECK_EXPR_RET(u32GetResultTmpBufSize == 0, HI_ERR_SVP_NNIE_ILLEGAL_PARAM, SAMPLE_SVP_ERR_LEVEL_ERROR,
1486         "Error,SAMPLE_SVP_NNIE_Rfcn_GetResultTmpBuf failed!\n");
1487     u32GetResultTmpBufSize = SAMPLE_SVP_NNIE_ALIGN16(u32GetResultTmpBufSize);
1488     u32ClassNum = pstSoftWareParam->u32ClassNum;
1489     u32DstRoiSize =
1490         SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstCfg->u32MaxRoiNum * sizeof(HI_U32) * SAMPLE_SVP_NNIE_COORDI_NUM);
1491     u32DstScoreSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstCfg->u32MaxRoiNum * sizeof(HI_U32));
1492     u32ClassRoiNumSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * sizeof(HI_U32));
1493     u32TotalSize = u32RpnTmpBufSize + u32RpnBboxBufSize + u32GetResultTmpBufSize + u32DstRoiSize + u32DstScoreSize +
1494         u32ClassRoiNumSize;
1495 
1496     s32Ret = SAMPLE_COMM_SVP_MallocCached("SAMPLE_RFCN_INIT", NULL, (HI_U64 *)&u64PhyAddr, (void **)&pu8VirAddr,
1497         u32TotalSize);
1498     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
1499         "Error,Malloc memory failed!\n");
1500     (HI_VOID)memset_s(pu8VirAddr, u32TotalSize, 0, u32TotalSize);
1501     SAMPLE_COMM_SVP_FlushCache(u64PhyAddr, (void *)pu8VirAddr, u32TotalSize);
1502 
1503     pstSoftWareParam->stRpnTmpBuf.u64PhyAddr = u64PhyAddr;
1504     pstSoftWareParam->stRpnTmpBuf.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr);
1505     pstSoftWareParam->stRpnTmpBuf.u32Size = u32RpnTmpBufSize;
1506 
1507     pstSoftWareParam->stRpnBbox.u64PhyAddr = u64PhyAddr + u32RpnTmpBufSize;
1508     pstSoftWareParam->stRpnBbox.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr) + u32RpnTmpBufSize;
1509 
1510     pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr = u64PhyAddr + u32RpnTmpBufSize + u32RpnBboxBufSize;
1511     pstSoftWareParam->stGetResultTmpBuf.u64VirAddr =
1512         (HI_U64)((HI_UINTPTR_T)pu8VirAddr + u32RpnTmpBufSize + u32RpnBboxBufSize);
1513     pstSoftWareParam->stGetResultTmpBuf.u32Size = u32GetResultTmpBufSize;
1514 
1515     pstSoftWareParam->stDstRoi.enType = SVP_BLOB_TYPE_S32;
1516     pstSoftWareParam->stDstRoi.u64PhyAddr = u64PhyAddr + u32RpnTmpBufSize + u32RpnBboxBufSize + u32GetResultTmpBufSize;
1517     pstSoftWareParam->stDstRoi.u64VirAddr =
1518         (HI_U64)((HI_UINTPTR_T)pu8VirAddr + u32RpnTmpBufSize + u32RpnBboxBufSize + u32GetResultTmpBufSize);
1519     pstSoftWareParam->stDstRoi.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstSoftWareParam->u32MaxRoiNum *
1520         sizeof(HI_U32) * SAMPLE_SVP_NNIE_COORDI_NUM);
1521     pstSoftWareParam->stDstRoi.u32Num = 1;
1522     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Chn = 1;
1523     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Height = 1;
1524     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Width =
1525         u32ClassNum * pstSoftWareParam->u32MaxRoiNum * SAMPLE_SVP_NNIE_COORDI_NUM;
1526 
1527     pstSoftWareParam->stDstScore.enType = SVP_BLOB_TYPE_S32;
1528     pstSoftWareParam->stDstScore.u64PhyAddr =
1529         u64PhyAddr + u32RpnTmpBufSize + u32RpnBboxBufSize + u32GetResultTmpBufSize + u32DstRoiSize;
1530     pstSoftWareParam->stDstScore.u64VirAddr = SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64, pu8VirAddr) +
1531         u32RpnTmpBufSize + u32RpnBboxBufSize + u32GetResultTmpBufSize + u32DstRoiSize;
1532     pstSoftWareParam->stDstScore.u32Stride =
1533         SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstSoftWareParam->u32MaxRoiNum * sizeof(HI_U32));
1534     pstSoftWareParam->stDstScore.u32Num = 1;
1535     pstSoftWareParam->stDstScore.unShape.stWhc.u32Chn = 1;
1536     pstSoftWareParam->stDstScore.unShape.stWhc.u32Height = 1;
1537     pstSoftWareParam->stDstScore.unShape.stWhc.u32Width = u32ClassNum * pstSoftWareParam->u32MaxRoiNum;
1538 
1539     pstSoftWareParam->stClassRoiNum.enType = SVP_BLOB_TYPE_S32;
1540     pstSoftWareParam->stClassRoiNum.u64PhyAddr =
1541         u64PhyAddr + u32RpnTmpBufSize + u32RpnBboxBufSize + u32GetResultTmpBufSize + u32DstRoiSize + u32DstScoreSize;
1542     pstSoftWareParam->stClassRoiNum.u64VirAddr = SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64, pu8VirAddr) +
1543         u32RpnTmpBufSize + u32RpnBboxBufSize + u32GetResultTmpBufSize + u32DstRoiSize + u32DstScoreSize;
1544     pstSoftWareParam->stClassRoiNum.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * sizeof(HI_U32));
1545     pstSoftWareParam->stClassRoiNum.u32Num = 1;
1546     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Chn = 1;
1547     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Height = 1;
1548     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Width = u32ClassNum;
1549     return s32Ret;
1550 }
1551 
1552 /* function : Rfcn init */
SAMPLE_SVP_NNIE_Rfcn_ParamInit(SAMPLE_SVP_NNIE_CFG_S * pstCfg,SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S * pstSoftWareParam)1553 static HI_S32 SAMPLE_SVP_NNIE_Rfcn_ParamInit(SAMPLE_SVP_NNIE_CFG_S *pstCfg, SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
1554     SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S *pstSoftWareParam)
1555 {
1556     HI_S32 s32Ret = HI_SUCCESS;
1557     /* init hardware para */
1558     s32Ret = SAMPLE_COMM_SVP_NNIE_ParamInit(pstCfg, pstNnieParam);
1559     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1560         "Error(%#x),SAMPLE_COMM_SVP_NNIE_ParamInit failed!\n", s32Ret);
1561 
1562     /* init software para */
1563     s32Ret = SAMPLE_SVP_NNIE_Rfcn_SoftwareInit(pstCfg, pstNnieParam, pstSoftWareParam);
1564     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1565         "Error(%#x),SAMPLE_SVP_NNIE_Rfcn_SoftwareInit failed!\n", s32Ret);
1566 
1567     return s32Ret;
1568 INIT_FAIL_0:
1569     s32Ret = SAMPLE_SVP_NNIE_Rfcn_Deinit(pstNnieParam, pstSoftWareParam, NULL);
1570     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
1571         "Error(%#x),SAMPLE_SVP_NNIE_Rfcn_Deinit failed!\n", s32Ret);
1572     return HI_FAILURE;
1573 }
1574 
1575 /* function : roi to rect */
SAMPLE_SVP_NNIE_RoiToRect(SVP_BLOB_S * pstDstScore,SVP_BLOB_S * pstDstRoi,SVP_BLOB_S * pstClassRoiNum,HI_FLOAT * paf32ScoreThr,HI_BOOL bRmBg,SAMPLE_SVP_NNIE_RECT_ARRAY_S * pstRect,HI_U32 u32SrcWidth,HI_U32 u32SrcHeight,HI_U32 u32DstWidth,HI_U32 u32DstHeight)1576 static HI_S32 SAMPLE_SVP_NNIE_RoiToRect(SVP_BLOB_S *pstDstScore, SVP_BLOB_S *pstDstRoi, SVP_BLOB_S *pstClassRoiNum,
1577     HI_FLOAT *paf32ScoreThr, HI_BOOL bRmBg, SAMPLE_SVP_NNIE_RECT_ARRAY_S *pstRect, HI_U32 u32SrcWidth,
1578     HI_U32 u32SrcHeight, HI_U32 u32DstWidth, HI_U32 u32DstHeight)
1579 {
1580     HI_U32 i = 0, j = 0;
1581     HI_U32 u32RoiNumBias = 0;
1582     HI_U32 u32ScoreBias = 0;
1583     HI_U32 u32BboxBias = 0;
1584     HI_FLOAT f32Score = 0.0f;
1585     HI_S32 *ps32Score = SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_S32, pstDstScore->u64VirAddr);
1586     HI_S32 *ps32Roi = SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_S32, pstDstRoi->u64VirAddr);
1587     HI_S32 *ps32ClassRoiNum = SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_S32, pstClassRoiNum->u64VirAddr);
1588     HI_U32 u32ClassNum = pstClassRoiNum->unShape.stWhc.u32Width;
1589     HI_U32 u32RoiNumTmp = 0;
1590 
1591     SAMPLE_SVP_CHECK_EXPR_RET(u32ClassNum > SAMPLE_SVP_NNIE_MAX_CLASS_NUM, HI_ERR_SVP_NNIE_ILLEGAL_PARAM,
1592         SAMPLE_SVP_ERR_LEVEL_ERROR, "Error(%#x),u32ClassNum(%u) must be less than or equal %u to!\n",
1593         HI_ERR_SVP_NNIE_ILLEGAL_PARAM, u32ClassNum, SAMPLE_SVP_NNIE_MAX_CLASS_NUM);
1594     pstRect->u32TotalNum = 0;
1595     pstRect->u32ClsNum = u32ClassNum;
1596     if (bRmBg) {
1597         pstRect->au32RoiNum[0] = 0;
1598         u32RoiNumBias += ps32ClassRoiNum[0];
1599         for (i = 1; i < u32ClassNum; i++) {
1600             u32ScoreBias = u32RoiNumBias;
1601             u32BboxBias = u32RoiNumBias * SAMPLE_SVP_NNIE_COORDI_NUM;
1602             u32RoiNumTmp = 0;
1603             /* if the confidence score greater than result thresh, the result will be drew */
1604             if (((HI_FLOAT)ps32Score[u32ScoreBias] / SAMPLE_SVP_NNIE_QUANT_BASE >= paf32ScoreThr[i]) &&
1605                 (ps32ClassRoiNum[i] != 0)) {
1606                 for (j = 0; j < (HI_U32)ps32ClassRoiNum[i]; j++) {
1607                     /* Score is descend order */
1608                     f32Score = (HI_FLOAT)ps32Score[u32ScoreBias + j] / SAMPLE_SVP_NNIE_QUANT_BASE;
1609                     if ((f32Score < paf32ScoreThr[i]) || (u32RoiNumTmp >= SAMPLE_SVP_NNIE_MAX_ROI_NUM_OF_CLASS)) {
1610                         break;
1611                     }
1612 
1613                     pstRect->astRect[i][u32RoiNumTmp].astPoint[0].s32X =
1614                         (HI_U32)((HI_FLOAT)ps32Roi[u32BboxBias + j * SAMPLE_SVP_NNIE_COORDI_NUM] /
1615                         (HI_FLOAT)u32SrcWidth * (HI_FLOAT)u32DstWidth) & (~1);
1616                     pstRect->astRect[i][u32RoiNumTmp].astPoint[0].s32Y =
1617                         (HI_U32)((HI_FLOAT)ps32Roi[u32BboxBias + j * SAMPLE_SVP_NNIE_COORDI_NUM + \
1618                         SAMPLE_SVP_NNIE_Y_MIN_OFFSET] / (HI_FLOAT)u32SrcHeight * (HI_FLOAT)u32DstHeight) & (~1);
1619                     pstRect->astRect[i][u32RoiNumTmp].astPoint[1].s32X =
1620                         (HI_U32)((HI_FLOAT)ps32Roi[u32BboxBias + j * SAMPLE_SVP_NNIE_COORDI_NUM + \
1621                         SAMPLE_SVP_NNIE_X_MAX_OFFSET] / (HI_FLOAT)u32SrcWidth * (HI_FLOAT)u32DstWidth) & (~1);
1622                     pstRect->astRect[i][u32RoiNumTmp].astPoint[1].s32Y =
1623                         pstRect->astRect[i][u32RoiNumTmp].astPoint[0].s32Y;
1624 
1625                     /* get the third point coordinate */
1626                     pstRect->astRect[i][u32RoiNumTmp].astPoint[2].s32X =
1627                         pstRect->astRect[i][u32RoiNumTmp].astPoint[1].s32X;
1628                     pstRect->astRect[i][u32RoiNumTmp].astPoint[2].s32Y =
1629                         (HI_U32)((HI_FLOAT)ps32Roi[u32BboxBias + j * SAMPLE_SVP_NNIE_COORDI_NUM + \
1630                         SAMPLE_SVP_NNIE_Y_MAX_OFFSET] / (HI_FLOAT)u32SrcHeight * (HI_FLOAT)u32DstHeight) & (~1);
1631 
1632                     /* get the fourth point coordinate */
1633                     pstRect->astRect[i][u32RoiNumTmp].astPoint[3].s32X =
1634                         pstRect->astRect[i][u32RoiNumTmp].astPoint[0].s32X;
1635                     pstRect->astRect[i][u32RoiNumTmp].astPoint[3].s32Y =
1636                         pstRect->astRect[i][u32RoiNumTmp].astPoint[2].s32Y;
1637 
1638                     u32RoiNumTmp++;
1639                 }
1640             }
1641 
1642             pstRect->au32RoiNum[i] = u32RoiNumTmp;
1643             pstRect->u32TotalNum += u32RoiNumTmp;
1644             u32RoiNumBias += ps32ClassRoiNum[i];
1645         }
1646     }
1647     return HI_SUCCESS;
1648 }
1649 
1650 /* function : Rfcn Proc */
SAMPLE_SVP_NNIE_Rfcn_Proc(SAMPLE_SVP_NNIE_PARAM_S * pstParam,SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S * pstSwParam)1651 static HI_S32 SAMPLE_SVP_NNIE_Rfcn_Proc(SAMPLE_SVP_NNIE_PARAM_S *pstParam,
1652     SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S *pstSwParam)
1653 {
1654     HI_S32 s32Ret = HI_FAILURE;
1655     HI_U32 i = 0;
1656     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S stInputDataIdx = { 0 };
1657     SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S stProcSegIdx = { 0 };
1658 
1659     stInputDataIdx.u32SegIdx = 0;
1660     stInputDataIdx.u32NodeIdx = 0;
1661 
1662     /* NNIE process 0-th seg */
1663     stProcSegIdx.u32SegIdx = 0;
1664     s32Ret = SAMPLE_SVP_NNIE_Forward(pstParam, &stInputDataIdx, &stProcSegIdx, HI_TRUE);
1665     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
1666         "Error,SAMPLE_SVP_NNIE_Forward failed!\n");
1667 
1668     /* RPN */
1669     s32Ret = SAMPLE_SVP_NNIE_Rfcn_Rpn(pstSwParam);
1670     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
1671         "Error,SAMPLE_SVP_NNIE_RFCN_Rpn failed!\n");
1672 
1673     if (pstSwParam->stRpnBbox.unShape.stWhc.u32Height != 0) {
1674         /* NNIE process 1-th seg, the input data comes from 3-rd report node of 0-th seg,
1675           the input roi comes from RPN results */
1676         stInputDataIdx.u32SegIdx = 0;
1677         stInputDataIdx.u32NodeIdx = 3;
1678         stProcSegIdx.u32SegIdx = 1;
1679         s32Ret =
1680             SAMPLE_SVP_NNIE_ForwardWithBbox(pstParam, &stInputDataIdx, &pstSwParam->stRpnBbox, &stProcSegIdx, HI_TRUE);
1681         SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
1682             "Error,SAMPLE_SVP_NNIE_Forward failed!\n");
1683 
1684         /* NNIE process 2-nd seg, the input data comes from 4-th report node of 0-th seg
1685           the input roi comes from RPN results */
1686         stInputDataIdx.u32SegIdx = 0;
1687         stInputDataIdx.u32NodeIdx = 4;
1688         stProcSegIdx.u32SegIdx = 2;
1689         s32Ret =
1690             SAMPLE_SVP_NNIE_ForwardWithBbox(pstParam, &stInputDataIdx, &pstSwParam->stRpnBbox, &stProcSegIdx, HI_TRUE);
1691         SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
1692             "Error,SAMPLE_SVP_NNIE_Forward failed!\n");
1693 
1694         /* GetResult */
1695         /* if user has changed net struct, please make sure SAMPLE_SVP_NNIE_Rfcn_GetResult
1696          function's input data are correct */
1697 
1698         s32Ret = SAMPLE_SVP_NNIE_Rfcn_GetResult(pstParam, pstSwParam);
1699         SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
1700             "Error,SAMPLE_SVP_NNIE_Rfcn_GetResult failed!\n");
1701     } else {
1702         for (i = 0; i < pstSwParam->stClassRoiNum.unShape.stWhc.u32Width; i++) {
1703             *(SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_U32, pstSwParam->stClassRoiNum.u64VirAddr) + i) = 0;
1704         }
1705     }
1706     return s32Ret;
1707 }
1708 
1709 /* function : Rfcn Proc */
SAMPLE_SVP_NNIE_Rfcn_Proc_ViToVo(SAMPLE_SVP_NNIE_PARAM_S * pstParam,SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S * pstSwParam,VIDEO_FRAME_INFO_S * pstExtFrmInfo,HI_U32 u32BaseWidth,HI_U32 u32BaseHeight)1710 static HI_S32 SAMPLE_SVP_NNIE_Rfcn_Proc_ViToVo(SAMPLE_SVP_NNIE_PARAM_S *pstParam,
1711     SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S *pstSwParam, VIDEO_FRAME_INFO_S *pstExtFrmInfo, HI_U32 u32BaseWidth,
1712     HI_U32 u32BaseHeight)
1713 {
1714     HI_S32 s32Ret = HI_FAILURE;
1715     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S stInputDataIdx = { 0 };
1716 
1717     stInputDataIdx.u32SegIdx = 0;
1718     stInputDataIdx.u32NodeIdx = 0;
1719     /* SP420 */
1720     pstParam->astSegData[stInputDataIdx.u32SegIdx].astSrc[stInputDataIdx.u32NodeIdx].u64VirAddr =
1721         pstExtFrmInfo->stVFrame.u64VirAddr[0];
1722     pstParam->astSegData[stInputDataIdx.u32SegIdx].astSrc[stInputDataIdx.u32NodeIdx].u64PhyAddr =
1723         pstExtFrmInfo->stVFrame.u64PhyAddr[0];
1724     pstParam->astSegData[stInputDataIdx.u32SegIdx].astSrc[stInputDataIdx.u32NodeIdx].u32Stride =
1725         pstExtFrmInfo->stVFrame.u32Stride[0];
1726 
1727     s32Ret = SAMPLE_SVP_NNIE_Rfcn_Proc(pstParam, pstSwParam);
1728     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
1729         "Error,SAMPLE_SVP_NNIE_Rfcn_Proc failed!\n");
1730     /* draw result, this sample has 21 classes:
1731      class 0:background     class 1:plane           class 2:bicycle
1732      class 3:bird           class 4:boat            class 5:bottle
1733      class 6:bus            class 7:car             class 8:cat
1734      class 9:chair          class10:cow             class11:diningtable
1735      class 12:dog           class13:horse           class14:motorbike
1736      class 15:person        class16:pottedplant     class17:sheep
1737      class 18:sofa          class19:train           class20:tvmonitor */
1738     s32Ret = SAMPLE_SVP_NNIE_RoiToRect(&(pstSwParam->stDstScore), &(pstSwParam->stDstRoi), &(pstSwParam->stClassRoiNum),
1739         pstSwParam->af32ScoreThr, HI_TRUE, &(pstSwParam->stRect), pstExtFrmInfo->stVFrame.u32Width,
1740         pstExtFrmInfo->stVFrame.u32Height, u32BaseWidth, u32BaseHeight);
1741     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
1742         "Error(%#x),SAMPLE_SVP_NNIE_RoiToRect failed!\n", s32Ret);
1743 
1744     return s32Ret;
1745 }
1746 
SAMPLE_SVP_NNIE_Rfcn_Stop(hi_void)1747 static HI_VOID SAMPLE_SVP_NNIE_Rfcn_Stop(hi_void)
1748 {
1749     s_bNnieStopSignal = HI_TRUE;
1750     if (s_hNnieThread != 0) {
1751         pthread_join(s_hNnieThread, HI_NULL);
1752         s_hNnieThread = 0;
1753     }
1754 
1755     SAMPLE_SVP_NNIE_Rfcn_Deinit(&s_stRfcnNnieParam, &s_stRfcnSoftwareParam, &s_stRfcnModel);
1756     (HI_VOID)memset_s(&s_stRfcnNnieParam, sizeof(SAMPLE_SVP_NNIE_PARAM_S), 0, sizeof(SAMPLE_SVP_NNIE_PARAM_S));
1757     (HI_VOID)memset_s(&s_stRfcnSoftwareParam, sizeof(SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S), 0,
1758         sizeof(SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S));
1759     (HI_VOID)memset_s(&s_stRfcnModel, sizeof(SAMPLE_SVP_NNIE_MODEL_S), 0, sizeof(SAMPLE_SVP_NNIE_MODEL_S));
1760 
1761     SAMPLE_COMM_IVE_StopViVpssVencVo(&s_stViConfig, &s_stRfcnSwitch);
1762     printf("\033[0;31mprogram termination abnormally!\033[0;39m\n");
1763 }
1764 
SAMPLE_SVP_NNIE_Rfcn_Pause(hi_void)1765 static HI_S32 SAMPLE_SVP_NNIE_Rfcn_Pause(hi_void)
1766 {
1767     printf("---------------press Enter key to exit!---------------\n");
1768     if (s_bNnieStopSignal == HI_TRUE) {
1769         SAMPLE_SVP_NNIE_Rfcn_Stop();
1770         return HI_FAILURE;
1771     }
1772     (hi_void)getchar();
1773     if (s_bNnieStopSignal == HI_TRUE) {
1774         SAMPLE_SVP_NNIE_Rfcn_Stop();
1775         return HI_FAILURE;
1776     }
1777     return HI_SUCCESS;
1778 }
1779 
1780 /* function : Rfcn vi to vo thread entry */
SAMPLE_SVP_NNIE_Rfcn_ViToVo(HI_VOID * pArgs)1781 static HI_VOID *SAMPLE_SVP_NNIE_Rfcn_ViToVo(HI_VOID *pArgs)
1782 {
1783     HI_S32 s32Ret;
1784     SAMPLE_SVP_NNIE_PARAM_S *pstParam = NULL;
1785     SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S *pstSwParam = NULL;
1786     VIDEO_FRAME_INFO_S stBaseFrmInfo;
1787     VIDEO_FRAME_INFO_S stExtFrmInfo;
1788     const HI_S32 s32MilliSec = 20000; /* 20000ms timeout */
1789     const VO_LAYER voLayer = 0;
1790     const VO_CHN voChn = 0;
1791     const HI_S32 s32VpssGrp = 0;
1792     const HI_S32 as32VpssChn[] = {VPSS_CHN0, VPSS_CHN1};
1793 
1794     hi_unused(pArgs);
1795     pstParam = &s_stRfcnNnieParam;
1796     pstSwParam = &s_stRfcnSoftwareParam;
1797 
1798     while (HI_FALSE == s_bNnieStopSignal) {
1799         s32Ret = HI_MPI_VPSS_GetChnFrame(s32VpssGrp, as32VpssChn[1], &stExtFrmInfo, s32MilliSec);
1800         if (s32Ret != HI_SUCCESS) {
1801             SAMPLE_PRT("Error(%#x),HI_MPI_VPSS_GetChnFrame failed, VPSS_GRP(%d), VPSS_CHN(%d)!\n", s32Ret, s32VpssGrp,
1802                 as32VpssChn[1]);
1803             continue;
1804         }
1805 
1806         s32Ret = HI_MPI_VPSS_GetChnFrame(s32VpssGrp, as32VpssChn[0], &stBaseFrmInfo, s32MilliSec);
1807         SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, EXT_RELEASE,
1808             "Error(%#x),HI_MPI_VPSS_GetChnFrame failed, VPSS_GRP(%d), VPSS_CHN(%d)!\n", s32Ret, s32VpssGrp,
1809             as32VpssChn[0]);
1810 
1811         s32Ret = SAMPLE_SVP_NNIE_Rfcn_Proc_ViToVo(pstParam, pstSwParam, &stExtFrmInfo, stBaseFrmInfo.stVFrame.u32Width,
1812             stBaseFrmInfo.stVFrame.u32Height);
1813         SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, BASE_RELEASE, "Error(%#x),SAMPLE_SVP_NNIE_Rfcn_Proc failed!\n",
1814             s32Ret);
1815 
1816         // Draw rect
1817         s32Ret = SAMPLE_COMM_SVP_NNIE_FillRect(&stBaseFrmInfo, &(pstSwParam->stRect), 0x0000FF00);
1818         SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, BASE_RELEASE,
1819             "SAMPLE_COMM_SVP_NNIE_FillRect failed, Error(%#x)!\n", s32Ret);
1820 
1821         s32Ret = HI_MPI_VO_SendFrame(voLayer, voChn, &stBaseFrmInfo, s32MilliSec);
1822         SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, BASE_RELEASE, "HI_MPI_VO_SendFrame failed, Error(%#x)!\n", s32Ret);
1823 
1824     BASE_RELEASE:
1825         s32Ret = HI_MPI_VPSS_ReleaseChnFrame(s32VpssGrp, as32VpssChn[0], &stBaseFrmInfo);
1826         if (s32Ret != HI_SUCCESS) {
1827             SAMPLE_PRT("Error(%#x),HI_MPI_VPSS_ReleaseChnFrame failed,Grp(%d) chn(%d)!\n", s32Ret, s32VpssGrp,
1828                 as32VpssChn[0]);
1829         }
1830 
1831     EXT_RELEASE:
1832         s32Ret = HI_MPI_VPSS_ReleaseChnFrame(s32VpssGrp, as32VpssChn[1], &stExtFrmInfo);
1833         if (s32Ret != HI_SUCCESS) {
1834             SAMPLE_PRT("Error(%#x),HI_MPI_VPSS_ReleaseChnFrame failed,Grp(%d) chn(%d)!\n", s32Ret, s32VpssGrp,
1835                 as32VpssChn[1]);
1836         }
1837     }
1838 
1839     return HI_NULL;
1840 }
1841 
1842 /* function : Rfcn Vi->VO */
SAMPLE_SVP_NNIE_Rfcn(void)1843 void SAMPLE_SVP_NNIE_Rfcn(void)
1844 {
1845     const HI_CHAR *pcModelName = "./data/nnie_model/detection/inst_rfcn_resnet50_cycle_352x288.wk";
1846     SAMPLE_SVP_NNIE_CFG_S stNnieCfg = { 0 };
1847     SIZE_S stSize;
1848     PIC_SIZE_E enSize = PIC_CIF;
1849     HI_S32 s32Ret = HI_SUCCESS;
1850 
1851     (HI_VOID)memset_s(&s_stRfcnModel, sizeof(s_stRfcnModel), 0, sizeof(s_stRfcnModel));
1852     (HI_VOID)memset_s(&s_stRfcnNnieParam, sizeof(s_stRfcnNnieParam), 0, sizeof(s_stRfcnNnieParam));
1853     (HI_VOID)memset_s(&s_stRfcnSoftwareParam, sizeof(s_stRfcnSoftwareParam), 0, sizeof(s_stRfcnSoftwareParam));
1854 
1855     /* step 1: start vi vpss vo */
1856     s_stRfcnSwitch.bVenc = HI_FALSE;
1857     s_stRfcnSwitch.bVo = HI_TRUE;
1858     s32Ret = SAMPLE_COMM_IVE_StartViVpssVencVo(&s_stViConfig, &s_stRfcnSwitch, &enSize);
1859     SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, END_RFCN_0, "Error(%#x),SAMPLE_COMM_IVE_StartViVpssVencVo failed!\n",
1860         s32Ret);
1861 
1862     s32Ret = SAMPLE_COMM_SYS_GetPicSize(enSize, &stSize);
1863     SAMPLE_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, END_RFCN_0, "Error(%#x),SAMPLE_COMM_SYS_GetPicSize failed!\n", s32Ret);
1864 
1865     /* step 2: init NNIE param */
1866     stNnieCfg.pszPic = NULL;
1867     stNnieCfg.u32MaxInputNum = 1; // max input image num in each batch
1868     stNnieCfg.u32MaxRoiNum = 300; // set maxmum 300 ROIs
1869     stNnieCfg.aenNnieCoreId[0] = SVP_NNIE_ID_0; // set NNIE core for 0-th Seg
1870     stNnieCfg.aenNnieCoreId[1] = SVP_NNIE_ID_0; // set NNIE core for 1-th Seg
1871     stNnieCfg.aenNnieCoreId[2] = SVP_NNIE_ID_0; // set NNIE core for 2-th Seg
1872 
1873     s32Ret = SAMPLE_COMM_SVP_NNIE_LoadModel(pcModelName, &s_stRfcnModel);
1874     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, END_RFCN_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1875         "Error,SAMPLE_COMM_SVP_NNIE_LoadModel failed!\n");
1876 
1877     /* apcRpnDataLayerName is used to set RPN data layer name
1878       and search RPN input data,if user has changed network struct, please
1879       make sure the data layer names are correct */
1880     s_stRfcnNnieParam.pstModel = &s_stRfcnModel.stModel;
1881     s_stRfcnSoftwareParam.apcRpnDataLayerName[0] = "rpn_cls_score";
1882     s_stRfcnSoftwareParam.apcRpnDataLayerName[1] = "rpn_bbox_pred";
1883     s32Ret = SAMPLE_SVP_NNIE_Rfcn_ParamInit(&stNnieCfg, &s_stRfcnNnieParam, &s_stRfcnSoftwareParam);
1884     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, END_RFCN_1, SAMPLE_SVP_ERR_LEVEL_ERROR,
1885         "Error,SAMPLE_SVP_NNIE_Rfcn_ParamInit failed!\n");
1886 
1887     s_bNnieStopSignal = HI_FALSE;
1888 
1889     /* step 3: Create work thread */
1890     s32Ret = prctl(PR_SET_NAME, "NNIE_ViToVo", 0, 0, 0);
1891     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, END_RFCN_1, SAMPLE_SVP_ERR_LEVEL_ERROR,
1892         "thread set name failed!\n");
1893     s32Ret = pthread_create(&s_hNnieThread, 0, SAMPLE_SVP_NNIE_Rfcn_ViToVo, NULL);
1894     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, END_RFCN_1, SAMPLE_SVP_ERR_LEVEL_ERROR, "thread create failed!\n");
1895 
1896     s32Ret = SAMPLE_SVP_NNIE_Rfcn_Pause();
1897     SAMPLE_SVP_CHECK_EXPR_RET_VOID(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR, "vi_rfcn_vo exit!\n");
1898 
1899     s_bNnieStopSignal = HI_TRUE;
1900     pthread_join(s_hNnieThread, HI_NULL);
1901     s_hNnieThread = 0;
1902 END_RFCN_1:
1903 
1904     SAMPLE_SVP_NNIE_Rfcn_Deinit(&s_stRfcnNnieParam, &s_stRfcnSoftwareParam, &s_stRfcnModel);
1905 END_RFCN_0:
1906     SAMPLE_COMM_IVE_StopViVpssVencVo(&s_stViConfig, &s_stRfcnSwitch);
1907     return;
1908 }
1909 
1910 /* function : rfcn sample signal handle */
SAMPLE_SVP_NNIE_Rfcn_HandleSig(void)1911 void SAMPLE_SVP_NNIE_Rfcn_HandleSig(void)
1912 {
1913     s_bNnieStopSignal = HI_TRUE;
1914 }
1915 
1916 /* function : rfcn sample signal handle */
SAMPLE_SVP_NNIE_Rfcn_Stop_File(hi_void)1917 static hi_void SAMPLE_SVP_NNIE_Rfcn_Stop_File(hi_void)
1918 {
1919     SAMPLE_SVP_NNIE_Rfcn_Deinit(&s_stRfcnNnieParam, &s_stRfcnSoftwareParam, &s_stRfcnModel);
1920     (HI_VOID)memset_s(&s_stRfcnModel, sizeof(s_stRfcnModel), 0, sizeof(s_stRfcnModel));
1921     (HI_VOID)memset_s(&s_stRfcnNnieParam, sizeof(s_stRfcnNnieParam), 0, sizeof(s_stRfcnNnieParam));
1922     (HI_VOID)memset_s(&s_stRfcnSoftwareParam, sizeof(s_stRfcnSoftwareParam), 0, sizeof(s_stRfcnSoftwareParam));
1923     SAMPLE_COMM_SVP_CheckSysExit();
1924     printf("\033[0;31mprogram termination abnormally!\033[0;39m\n");
1925 }
1926 
SAMPLE_SVP_NNIE_Rfcn_HandleSig_File(void)1927 void SAMPLE_SVP_NNIE_Rfcn_HandleSig_File(void)
1928 {
1929     g_stop_signal = HI_TRUE;
1930 }
1931 
1932 /* function : Rfcn Read file */
SAMPLE_SVP_NNIE_Rfcn_File(void)1933 void SAMPLE_SVP_NNIE_Rfcn_File(void)
1934 {
1935     const HI_CHAR *pcSrcFile = "./data/nnie_image/rgb_planar/hoser_dog_car_person_800x600.bgr";
1936     const HI_CHAR *pcModelName = "./data/nnie_model/detection/inst_rfcn_resnet50_cycle.wk";
1937     SAMPLE_SVP_NNIE_CFG_S stNnieCfg = { 0 };
1938     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S stInputDataIdx = { 0 };
1939     HI_FLOAT f32PrintResultThresh = 0.0f;
1940     HI_S32 s32Ret = HI_SUCCESS;
1941 
1942     (HI_VOID)memset_s(&s_stRfcnModel, sizeof(s_stRfcnModel), 0, sizeof(s_stRfcnModel));
1943     (HI_VOID)memset_s(&s_stRfcnNnieParam, sizeof(s_stRfcnNnieParam), 0, sizeof(s_stRfcnNnieParam));
1944     (HI_VOID)memset_s(&s_stRfcnSoftwareParam, sizeof(s_stRfcnSoftwareParam), 0, sizeof(s_stRfcnSoftwareParam));
1945 
1946     stNnieCfg.pszPic = pcSrcFile;
1947     f32PrintResultThresh = 0.8f;
1948     stNnieCfg.u32MaxInputNum = 1; // max input image num in each batch
1949     stNnieCfg.u32MaxRoiNum = 300; // set maxmum 300 ROIs
1950     stNnieCfg.aenNnieCoreId[0] = SVP_NNIE_ID_0; // set NNIE core for 0-th Seg
1951     stNnieCfg.aenNnieCoreId[1] = SVP_NNIE_ID_0; // set NNIE core for 1-th Seg
1952     stNnieCfg.aenNnieCoreId[2] = SVP_NNIE_ID_0; // set NNIE core for 2-th Seg
1953     g_stop_signal = HI_FALSE;
1954     /* Sys init */
1955     s32Ret = SAMPLE_COMM_SVP_CheckSysInit();
1956     SAMPLE_SVP_CHECK_EXPR_RET_VOID(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
1957         "Error,SAMPLE_COMM_SVP_CheckSysInit failed!\n");
1958     if (g_stop_signal == HI_TRUE) {
1959         SAMPLE_SVP_NNIE_Rfcn_Stop_File();
1960         return;
1961     }
1962 
1963     s32Ret = SAMPLE_COMM_SVP_NNIE_LoadModel(pcModelName, &s_stRfcnModel);
1964     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, END_RFCN_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1965         "Error,SAMPLE_COMM_SVP_NNIE_LoadModel failed!\n");
1966 
1967     /* apcRpnDataLayerName is used to set RPN data layer name
1968       and search RPN input data,if user has changed network struct, please
1969       make sure the data layer names are correct */
1970     s_stRfcnNnieParam.pstModel = &s_stRfcnModel.stModel;
1971     s_stRfcnSoftwareParam.apcRpnDataLayerName[0] = "rpn_cls_score";
1972     s_stRfcnSoftwareParam.apcRpnDataLayerName[1] = "rpn_bbox_pred";
1973     if (g_stop_signal == HI_TRUE) {
1974         SAMPLE_SVP_NNIE_Rfcn_Stop_File();
1975         return;
1976     }
1977     s32Ret = SAMPLE_SVP_NNIE_Rfcn_ParamInit(&stNnieCfg, &s_stRfcnNnieParam, &s_stRfcnSoftwareParam);
1978     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, END_RFCN_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1979         "Error,SAMPLE_SVP_NNIE_Rfcn_ParamInit failed!\n");
1980 
1981     if (g_stop_signal == HI_TRUE) {
1982         SAMPLE_SVP_NNIE_Rfcn_Stop_File();
1983         return;
1984     }
1985     stInputDataIdx.u32SegIdx = 0;
1986     stInputDataIdx.u32NodeIdx = 0;
1987     s32Ret = SAMPLE_SVP_NNIE_FillSrcData(&stNnieCfg, &s_stRfcnNnieParam, &stInputDataIdx);
1988     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, END_RFCN_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1989         "Error,SAMPLE_SVP_NNIE_FillSrcData failed!\n");
1990     if (g_stop_signal == HI_TRUE) {
1991         SAMPLE_SVP_NNIE_Rfcn_Stop_File();
1992         return;
1993     }
1994     s32Ret = SAMPLE_SVP_NNIE_Rfcn_Proc(&s_stRfcnNnieParam, &s_stRfcnSoftwareParam);
1995     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, END_RFCN_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
1996         "Error,SAMPLE_SVP_NNIE_Rfcn_Proc failed!\n");
1997 
1998     /* print result, this sample has 21 classes:
1999      class 0:background     class 1:plane           class 2:bicycle
2000      class 3:bird           class 4:boat            class 5:bottle
2001      class 6:bus            class 7:car             class 8:cat
2002      class 9:chair          class10:cow             class11:diningtable
2003      class 12:dog           class13:horse           class14:motorbike
2004      class 15:person        class16:pottedplant     class17:sheep
2005      class 18:sofa          class19:train           class20:tvmonitor */
2006 
2007     SAMPLE_SVP_TRACE_INFO("Rfcn result:\n");
2008     if (g_stop_signal == HI_TRUE) {
2009         SAMPLE_SVP_NNIE_Rfcn_Stop_File();
2010         return;
2011     }
2012     (void)SAMPLE_SVP_NNIE_Detection_PrintResult(&s_stRfcnSoftwareParam.stDstScore, &s_stRfcnSoftwareParam.stDstRoi,
2013         &s_stRfcnSoftwareParam.stClassRoiNum, f32PrintResultThresh);
2014 
2015 END_RFCN_0:
2016 
2017     SAMPLE_SVP_NNIE_Rfcn_Deinit(&s_stRfcnNnieParam, &s_stRfcnSoftwareParam, &s_stRfcnModel);
2018     (HI_VOID)memset_s(&s_stRfcnModel, sizeof(s_stRfcnModel), 0, sizeof(s_stRfcnModel));
2019     (HI_VOID)memset_s(&s_stRfcnNnieParam, sizeof(s_stRfcnNnieParam), 0, sizeof(s_stRfcnNnieParam));
2020     (HI_VOID)memset_s(&s_stRfcnSoftwareParam, sizeof(s_stRfcnSoftwareParam), 0, sizeof(s_stRfcnSoftwareParam));
2021     SAMPLE_COMM_SVP_CheckSysExit();
2022 
2023     return;
2024 }
2025 
2026 /* function : SSD software deinit */
SAMPLE_SVP_NNIE_Ssd_SoftwareDeinit(SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S * pstSoftWareParam)2027 static HI_S32 SAMPLE_SVP_NNIE_Ssd_SoftwareDeinit(SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S *pstSoftWareParam)
2028 {
2029     HI_S32 s32Ret = HI_SUCCESS;
2030     SAMPLE_SVP_CHECK_EXPR_RET(pstSoftWareParam == NULL, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
2031         "Error, pstSoftWareParam can't be NULL!\n");
2032     if ((pstSoftWareParam->stPriorBoxTmpBuf.u64PhyAddr != 0) && (pstSoftWareParam->stPriorBoxTmpBuf.u64VirAddr != 0)) {
2033         SAMPLE_SVP_MMZ_FREE(pstSoftWareParam->stPriorBoxTmpBuf.u64PhyAddr,
2034             pstSoftWareParam->stPriorBoxTmpBuf.u64VirAddr);
2035         pstSoftWareParam->stPriorBoxTmpBuf.u64PhyAddr = 0;
2036         pstSoftWareParam->stPriorBoxTmpBuf.u64VirAddr = 0;
2037     }
2038     return s32Ret;
2039 }
2040 
2041 /* function : Ssd Deinit */
SAMPLE_SVP_NNIE_Ssd_Deinit(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S * pstSoftWareParam,SAMPLE_SVP_NNIE_MODEL_S * pstNnieModel)2042 static HI_S32 SAMPLE_SVP_NNIE_Ssd_Deinit(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
2043     SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S *pstSoftWareParam, SAMPLE_SVP_NNIE_MODEL_S *pstNnieModel)
2044 {
2045     HI_S32 s32Ret = HI_SUCCESS;
2046     /* hardware deinit */
2047     if (pstNnieParam != NULL) {
2048         s32Ret = SAMPLE_COMM_SVP_NNIE_ParamDeinit(pstNnieParam);
2049         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
2050             "Error,SAMPLE_COMM_SVP_NNIE_ParamDeinit failed!\n");
2051     }
2052     /* software deinit */
2053     if (pstSoftWareParam != NULL) {
2054         s32Ret = SAMPLE_SVP_NNIE_Ssd_SoftwareDeinit(pstSoftWareParam);
2055         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
2056             "Error,SAMPLE_SVP_NNIE_Ssd_SoftwareDeinit failed!\n");
2057     }
2058     /* model deinit */
2059     if (pstNnieModel != NULL) {
2060         s32Ret = SAMPLE_COMM_SVP_NNIE_UnloadModel(pstNnieModel);
2061         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
2062             "Error,SAMPLE_COMM_SVP_NNIE_UnloadModel failed!\n");
2063     }
2064     return s32Ret;
2065 }
2066 
2067 /* function : Ssd software para init */
SAMPLE_SVP_NNIE_Ssd_SoftwareInit(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S * pstSoftWareParam)2068 static HI_S32 SAMPLE_SVP_NNIE_Ssd_SoftwareInit(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
2069     SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S *pstSoftWareParam)
2070 {
2071     HI_U32 i = 0;
2072     HI_S32 s32Ret = HI_SUCCESS;
2073     HI_U32 u32ClassNum = 0;
2074     HI_U32 u32TotalSize = 0;
2075     HI_U32 u32DstRoiSize = 0;
2076     HI_U32 u32DstScoreSize = 0;
2077     HI_U32 u32ClassRoiNumSize = 0;
2078     HI_U32 u32TmpBufTotalSize = 0;
2079     HI_U64 u64PhyAddr = 0;
2080     HI_U8 *pu8VirAddr = NULL;
2081 
2082     /* Set Conv Parameters */
2083     /* the SSD sample report resule is after permute operation,
2084      conv result is (C, H, W), after permute, the report node's
2085      (C1, H1, W1) is (H, W, C), the stride of report result is aligned according to C dim */
2086     for (i = 0; i < SAMPLE_SVP_NNIE_SSD_REPORT_NODE_NUM; i++) {
2087         pstSoftWareParam->au32ConvHeight[i] = pstNnieParam->pstModel->astSeg[0].astDstNode[i].unShape.stWhc.u32Chn;
2088         pstSoftWareParam->au32ConvWidth[i] = pstNnieParam->pstModel->astSeg[0].astDstNode[i].unShape.stWhc.u32Height;
2089         pstSoftWareParam->au32ConvChannel[i] = pstNnieParam->pstModel->astSeg[0].astDstNode[i].unShape.stWhc.u32Width;
2090         if (i % 2 == 1) { // only need to set the odd index parameter
2091             pstSoftWareParam->au32ConvStride[i / 2] =
2092                 SAMPLE_SVP_NNIE_ALIGN16(pstSoftWareParam->au32ConvChannel[i] * sizeof(HI_U32)) / sizeof(HI_U32);
2093         }
2094     }
2095 
2096     /* Set PriorBox Parameters */
2097     /* The values of the following parameters are related to algorithm principles.
2098         For details, see related algorithms. */
2099     pstSoftWareParam->au32PriorBoxWidth[0] = 38;
2100     pstSoftWareParam->au32PriorBoxWidth[1] = 19;
2101     pstSoftWareParam->au32PriorBoxWidth[2] = 10;
2102     pstSoftWareParam->au32PriorBoxWidth[3] = 5;
2103     pstSoftWareParam->au32PriorBoxWidth[4] = 3;
2104     pstSoftWareParam->au32PriorBoxWidth[5] = 1;
2105 
2106     pstSoftWareParam->au32PriorBoxHeight[0] = 38;
2107     pstSoftWareParam->au32PriorBoxHeight[1] = 19;
2108     pstSoftWareParam->au32PriorBoxHeight[2] = 10;
2109     pstSoftWareParam->au32PriorBoxHeight[3] = 5;
2110     pstSoftWareParam->au32PriorBoxHeight[4] = 3;
2111     pstSoftWareParam->au32PriorBoxHeight[5] = 1;
2112 
2113     pstSoftWareParam->u32OriImHeight = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Height;
2114     pstSoftWareParam->u32OriImWidth = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Width;
2115 
2116     pstSoftWareParam->af32PriorBoxMinSize[0][0] = 30.0f;
2117     pstSoftWareParam->af32PriorBoxMinSize[1][0] = 60.0f;
2118     pstSoftWareParam->af32PriorBoxMinSize[2][0] = 111.0f;
2119     pstSoftWareParam->af32PriorBoxMinSize[3][0] = 162.0f;
2120     pstSoftWareParam->af32PriorBoxMinSize[4][0] = 213.0f;
2121     pstSoftWareParam->af32PriorBoxMinSize[5][0] = 264.0f;
2122 
2123     pstSoftWareParam->af32PriorBoxMaxSize[0][0] = 60.0f;
2124     pstSoftWareParam->af32PriorBoxMaxSize[1][0] = 111.0f;
2125     pstSoftWareParam->af32PriorBoxMaxSize[2][0] = 162.0f;
2126     pstSoftWareParam->af32PriorBoxMaxSize[3][0] = 213.0f;
2127     pstSoftWareParam->af32PriorBoxMaxSize[4][0] = 264.0f;
2128     pstSoftWareParam->af32PriorBoxMaxSize[5][0] = 315.0f;
2129 
2130     pstSoftWareParam->u32MinSizeNum = 1;
2131     pstSoftWareParam->u32MaxSizeNum = 1;
2132     pstSoftWareParam->bFlip = HI_TRUE;
2133     pstSoftWareParam->bClip = HI_FALSE;
2134 
2135     pstSoftWareParam->au32InputAspectRatioNum[0] = 1;
2136     pstSoftWareParam->au32InputAspectRatioNum[1] = 2;
2137     pstSoftWareParam->au32InputAspectRatioNum[2] = 2;
2138     pstSoftWareParam->au32InputAspectRatioNum[3] = 2;
2139     pstSoftWareParam->au32InputAspectRatioNum[4] = 1;
2140     pstSoftWareParam->au32InputAspectRatioNum[5] = 1;
2141 
2142     pstSoftWareParam->af32PriorBoxAspectRatio[0][0] = 2;
2143     pstSoftWareParam->af32PriorBoxAspectRatio[0][1] = 0;
2144     pstSoftWareParam->af32PriorBoxAspectRatio[1][0] = 2;
2145     pstSoftWareParam->af32PriorBoxAspectRatio[1][1] = 3;
2146     pstSoftWareParam->af32PriorBoxAspectRatio[2][0] = 2;
2147     pstSoftWareParam->af32PriorBoxAspectRatio[2][1] = 3;
2148     pstSoftWareParam->af32PriorBoxAspectRatio[3][0] = 2;
2149     pstSoftWareParam->af32PriorBoxAspectRatio[3][1] = 3;
2150     pstSoftWareParam->af32PriorBoxAspectRatio[4][0] = 2;
2151     pstSoftWareParam->af32PriorBoxAspectRatio[4][1] = 0;
2152     pstSoftWareParam->af32PriorBoxAspectRatio[5][0] = 2;
2153     pstSoftWareParam->af32PriorBoxAspectRatio[5][1] = 0;
2154 
2155     pstSoftWareParam->af32PriorBoxStepWidth[0] = 8;
2156     pstSoftWareParam->af32PriorBoxStepWidth[1] = 16;
2157     pstSoftWareParam->af32PriorBoxStepWidth[2] = 32;
2158     pstSoftWareParam->af32PriorBoxStepWidth[3] = 64;
2159     pstSoftWareParam->af32PriorBoxStepWidth[4] = 100;
2160     pstSoftWareParam->af32PriorBoxStepWidth[5] = 300;
2161 
2162     pstSoftWareParam->af32PriorBoxStepHeight[0] = 8;
2163     pstSoftWareParam->af32PriorBoxStepHeight[1] = 16;
2164     pstSoftWareParam->af32PriorBoxStepHeight[2] = 32;
2165     pstSoftWareParam->af32PriorBoxStepHeight[3] = 64;
2166     pstSoftWareParam->af32PriorBoxStepHeight[4] = 100;
2167     pstSoftWareParam->af32PriorBoxStepHeight[5] = 300;
2168 
2169     pstSoftWareParam->f32Offset = 0.5f;
2170 
2171     pstSoftWareParam->as32PriorBoxVar[0] = (HI_S32)(0.1f * SAMPLE_SVP_NNIE_QUANT_BASE);
2172     pstSoftWareParam->as32PriorBoxVar[1] = (HI_S32)(0.1f * SAMPLE_SVP_NNIE_QUANT_BASE);
2173     pstSoftWareParam->as32PriorBoxVar[2] = (HI_S32)(0.2f * SAMPLE_SVP_NNIE_QUANT_BASE);
2174     pstSoftWareParam->as32PriorBoxVar[3] = (HI_S32)(0.2f * SAMPLE_SVP_NNIE_QUANT_BASE);
2175 
2176     /* Set Softmax Parameters */
2177     pstSoftWareParam->u32SoftMaxInHeight = 21;
2178     pstSoftWareParam->au32SoftMaxInChn[0] = 121296;
2179     pstSoftWareParam->au32SoftMaxInChn[1] = 45486;
2180     pstSoftWareParam->au32SoftMaxInChn[2] = 12600;
2181     pstSoftWareParam->au32SoftMaxInChn[3] = 3150;
2182     pstSoftWareParam->au32SoftMaxInChn[4] = 756;
2183     pstSoftWareParam->au32SoftMaxInChn[5] = 84;
2184 
2185     pstSoftWareParam->u32ConcatNum = 6;
2186     pstSoftWareParam->u32SoftMaxOutWidth = 1;
2187     pstSoftWareParam->u32SoftMaxOutHeight = 21;
2188     pstSoftWareParam->u32SoftMaxOutChn = 8732;
2189 
2190     /* Set DetectionOut Parameters */
2191     pstSoftWareParam->u32ClassNum = 21;
2192     pstSoftWareParam->u32TopK = 400;
2193     pstSoftWareParam->u32KeepTopK = 200;
2194     pstSoftWareParam->u32NmsThresh = (HI_U32)(0.3f * SAMPLE_SVP_NNIE_QUANT_BASE);
2195     pstSoftWareParam->u32ConfThresh = (HI_U32)(0.000245f * SAMPLE_SVP_NNIE_QUANT_BASE);
2196     pstSoftWareParam->au32DetectInputChn[0] = 23104;
2197     pstSoftWareParam->au32DetectInputChn[1] = 8664;
2198     pstSoftWareParam->au32DetectInputChn[2] = 2400;
2199     pstSoftWareParam->au32DetectInputChn[3] = 600;
2200     pstSoftWareParam->au32DetectInputChn[4] = 144;
2201     pstSoftWareParam->au32DetectInputChn[5] = 16;
2202 
2203     /* Malloc assist buffer memory */
2204     u32ClassNum = pstSoftWareParam->u32ClassNum;
2205     u32TotalSize = SAMPLE_SVP_NNIE_Ssd_GetResultTmpBuf(pstNnieParam, pstSoftWareParam);
2206     SAMPLE_SVP_CHECK_EXPR_RET(u32TotalSize == 0, HI_ERR_SVP_NNIE_ILLEGAL_PARAM, SAMPLE_SVP_ERR_LEVEL_ERROR,
2207         "Error, SAMPLE_SVP_NNIE_Ssd_GetResultTmpBuf failed!\n");
2208     u32DstRoiSize =
2209         SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstSoftWareParam->u32TopK * sizeof(HI_U32) * SAMPLE_SVP_NNIE_COORDI_NUM);
2210     u32DstScoreSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstSoftWareParam->u32TopK * sizeof(HI_U32));
2211     u32ClassRoiNumSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * sizeof(HI_U32));
2212     u32TotalSize = u32TotalSize + u32DstRoiSize + u32DstScoreSize + u32ClassRoiNumSize;
2213     s32Ret = SAMPLE_COMM_SVP_MallocCached("SAMPLE_SSD_INIT", NULL, (HI_U64 *)&u64PhyAddr, (void **)&pu8VirAddr,
2214         u32TotalSize);
2215     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
2216         "Error,Malloc memory failed!\n");
2217     (HI_VOID)memset_s(pu8VirAddr, u32TotalSize, 0, u32TotalSize);
2218     SAMPLE_COMM_SVP_FlushCache(u64PhyAddr, (void *)pu8VirAddr, u32TotalSize);
2219 
2220     /* set each tmp buffer addr */
2221     pstSoftWareParam->stPriorBoxTmpBuf.u64PhyAddr = u64PhyAddr;
2222     pstSoftWareParam->stPriorBoxTmpBuf.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr);
2223 
2224     pstSoftWareParam->stSoftMaxTmpBuf.u64PhyAddr = u64PhyAddr + pstSoftWareParam->stPriorBoxTmpBuf.u32Size;
2225     pstSoftWareParam->stSoftMaxTmpBuf.u64VirAddr =
2226         (HI_U64)((HI_UINTPTR_T)pu8VirAddr + pstSoftWareParam->stPriorBoxTmpBuf.u32Size);
2227 
2228     pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr =
2229         u64PhyAddr + pstSoftWareParam->stPriorBoxTmpBuf.u32Size + pstSoftWareParam->stSoftMaxTmpBuf.u32Size;
2230     pstSoftWareParam->stGetResultTmpBuf.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr +
2231         pstSoftWareParam->stPriorBoxTmpBuf.u32Size + pstSoftWareParam->stSoftMaxTmpBuf.u32Size);
2232 
2233     u32TmpBufTotalSize = pstSoftWareParam->stPriorBoxTmpBuf.u32Size + pstSoftWareParam->stSoftMaxTmpBuf.u32Size +
2234         pstSoftWareParam->stGetResultTmpBuf.u32Size;
2235 
2236     /* set result blob */
2237     pstSoftWareParam->stDstRoi.enType = SVP_BLOB_TYPE_S32;
2238     pstSoftWareParam->stDstRoi.u64PhyAddr = u64PhyAddr + u32TmpBufTotalSize;
2239     pstSoftWareParam->stDstRoi.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr + u32TmpBufTotalSize);
2240     pstSoftWareParam->stDstRoi.u32Stride =
2241         SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstSoftWareParam->u32TopK * sizeof(HI_U32) * SAMPLE_SVP_NNIE_COORDI_NUM);
2242     pstSoftWareParam->stDstRoi.u32Num = 1;
2243     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Chn = 1;
2244     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Height = 1;
2245     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Width =
2246         u32ClassNum * pstSoftWareParam->u32TopK * SAMPLE_SVP_NNIE_COORDI_NUM;
2247 
2248     pstSoftWareParam->stDstScore.enType = SVP_BLOB_TYPE_S32;
2249     pstSoftWareParam->stDstScore.u64PhyAddr = u64PhyAddr + u32TmpBufTotalSize + u32DstRoiSize;
2250     pstSoftWareParam->stDstScore.u64VirAddr =
2251         SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64, pu8VirAddr) + u32TmpBufTotalSize + u32DstRoiSize;
2252     pstSoftWareParam->stDstScore.u32Stride =
2253         SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstSoftWareParam->u32TopK * sizeof(HI_U32));
2254     pstSoftWareParam->stDstScore.u32Num = 1;
2255     pstSoftWareParam->stDstScore.unShape.stWhc.u32Chn = 1;
2256     pstSoftWareParam->stDstScore.unShape.stWhc.u32Height = 1;
2257     pstSoftWareParam->stDstScore.unShape.stWhc.u32Width = u32ClassNum * pstSoftWareParam->u32TopK;
2258 
2259     pstSoftWareParam->stClassRoiNum.enType = SVP_BLOB_TYPE_S32;
2260     pstSoftWareParam->stClassRoiNum.u64PhyAddr = u64PhyAddr + u32TmpBufTotalSize + u32DstRoiSize + u32DstScoreSize;
2261     pstSoftWareParam->stClassRoiNum.u64VirAddr =
2262         SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64, pu8VirAddr) + u32TmpBufTotalSize + u32DstRoiSize + u32DstScoreSize;
2263     pstSoftWareParam->stClassRoiNum.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * sizeof(HI_U32));
2264     pstSoftWareParam->stClassRoiNum.u32Num = 1;
2265     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Chn = 1;
2266     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Height = 1;
2267     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Width = u32ClassNum;
2268 
2269     return s32Ret;
2270 }
2271 
2272 /* function : Ssd init */
SAMPLE_SVP_NNIE_Ssd_ParamInit(SAMPLE_SVP_NNIE_CFG_S * pstCfg,SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S * pstSoftWareParam)2273 static HI_S32 SAMPLE_SVP_NNIE_Ssd_ParamInit(SAMPLE_SVP_NNIE_CFG_S *pstCfg, SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
2274     SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S *pstSoftWareParam)
2275 {
2276     HI_S32 s32Ret = HI_SUCCESS;
2277     /* init hardware para */
2278     s32Ret = SAMPLE_COMM_SVP_NNIE_ParamInit(pstCfg, pstNnieParam);
2279     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2280         "Error(%#x),SAMPLE_COMM_SVP_NNIE_ParamInit failed!\n", s32Ret);
2281 
2282     /* init software para */
2283     s32Ret = SAMPLE_SVP_NNIE_Ssd_SoftwareInit(pstNnieParam, pstSoftWareParam);
2284     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2285         "Error(%#x),SAMPLE_SVP_NNIE_Ssd_SoftwareInit failed!\n", s32Ret);
2286 
2287     return s32Ret;
2288 INIT_FAIL_0:
2289     s32Ret = SAMPLE_SVP_NNIE_Ssd_Deinit(pstNnieParam, pstSoftWareParam, NULL);
2290     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
2291         "Error(%#x),SAMPLE_SVP_NNIE_Ssd_Deinit failed!\n", s32Ret);
2292     return HI_FAILURE;
2293 }
2294 
SAMPLE_SVP_NNIE_Ssd_Stop(void)2295 static void SAMPLE_SVP_NNIE_Ssd_Stop(void)
2296 {
2297     SAMPLE_SVP_NNIE_Ssd_Deinit(&s_stSsdNnieParam, &s_stSsdSoftwareParam, &s_stSsdModel);
2298     (HI_VOID)memset_s(&s_stSsdNnieParam, sizeof(SAMPLE_SVP_NNIE_PARAM_S), 0, sizeof(SAMPLE_SVP_NNIE_PARAM_S));
2299     (HI_VOID)memset_s(&s_stSsdSoftwareParam, sizeof(SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S), 0,
2300         sizeof(SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S));
2301     (HI_VOID)memset_s(&s_stSsdModel, sizeof(SAMPLE_SVP_NNIE_MODEL_S), 0, sizeof(SAMPLE_SVP_NNIE_MODEL_S));
2302     SAMPLE_COMM_SVP_CheckSysExit();
2303     printf("\033[0;31mprogram termination abnormally!\033[0;39m\n");
2304 }
2305 
2306 /* function : show SSD sample(image 300x300 U8_C3) */
SAMPLE_SVP_NNIE_Ssd(void)2307 void SAMPLE_SVP_NNIE_Ssd(void)
2308 {
2309     const HI_CHAR *pcSrcFile = "./data/nnie_image/rgb_planar/dog_bike_car_300x300.bgr";
2310     const HI_CHAR *pcModelName = "./data/nnie_model/detection/inst_ssd_cycle.wk";
2311     const HI_U32 u32PicNum = 1;
2312     HI_FLOAT f32PrintResultThresh = 0.0f;
2313     HI_S32 s32Ret = HI_SUCCESS;
2314     SAMPLE_SVP_NNIE_CFG_S stNnieCfg = { 0 };
2315     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S stInputDataIdx = { 0 };
2316     SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S stProcSegIdx = { 0 };
2317 
2318     /* Set configuration parameter */
2319     f32PrintResultThresh = 0.8f;
2320     stNnieCfg.pszPic = pcSrcFile;
2321     stNnieCfg.u32MaxInputNum = u32PicNum; // max input image num in each batch
2322     stNnieCfg.u32MaxRoiNum = 0;
2323     stNnieCfg.aenNnieCoreId[0] = SVP_NNIE_ID_0; // set NNIE core
2324     g_stop_signal = HI_FALSE;
2325 
2326     /* Sys init */
2327     s32Ret = SAMPLE_COMM_SVP_CheckSysInit();
2328     SAMPLE_SVP_CHECK_EXPR_RET_VOID(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
2329         "Error,SAMPLE_COMM_SVP_CheckSysInit failed!\n");
2330     if (g_stop_signal == HI_TRUE) {
2331         SAMPLE_SVP_NNIE_Ssd_Stop();
2332         return;
2333     }
2334     /* Ssd Load model */
2335     SAMPLE_SVP_TRACE_INFO("Ssd Load model!\n");
2336     s32Ret = SAMPLE_COMM_SVP_NNIE_LoadModel(pcModelName, &s_stSsdModel);
2337     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, SSD_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2338         "Error,SAMPLE_COMM_SVP_NNIE_LoadModel failed!\n");
2339 
2340     /* Ssd parameter initialization */
2341     /* Ssd parameters are set in SAMPLE_SVP_NNIE_Ssd_SoftwareInit,
2342       if user has changed net struct, please make sure the parameter settings in
2343       SAMPLE_SVP_NNIE_Ssd_SoftwareInit function are correct */
2344     SAMPLE_SVP_TRACE_INFO("Ssd parameter initialization!\n");
2345     if (g_stop_signal == HI_TRUE) {
2346         SAMPLE_SVP_NNIE_Ssd_Stop();
2347         return;
2348     }
2349     s_stSsdNnieParam.pstModel = &s_stSsdModel.stModel;
2350     s32Ret = SAMPLE_SVP_NNIE_Ssd_ParamInit(&stNnieCfg, &s_stSsdNnieParam, &s_stSsdSoftwareParam);
2351     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, SSD_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2352         "Error,SAMPLE_SVP_NNIE_Ssd_ParamInit failed!\n");
2353 
2354     /* Fill src data */
2355     SAMPLE_SVP_TRACE_INFO("Ssd start!\n");
2356     if (g_stop_signal == HI_TRUE) {
2357         SAMPLE_SVP_NNIE_Ssd_Stop();
2358         return;
2359     }
2360 
2361     stInputDataIdx.u32SegIdx = 0;
2362     stInputDataIdx.u32NodeIdx = 0;
2363     s32Ret = SAMPLE_SVP_NNIE_FillSrcData(&stNnieCfg, &s_stSsdNnieParam, &stInputDataIdx);
2364     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, SSD_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2365         "Error,SAMPLE_SVP_NNIE_FillSrcData failed!\n");
2366 
2367     /* NNIE process(process the 0-th segment) */
2368     if (g_stop_signal == HI_TRUE) {
2369         SAMPLE_SVP_NNIE_Ssd_Stop();
2370         return;
2371     }
2372     stProcSegIdx.u32SegIdx = 0;
2373     s32Ret = SAMPLE_SVP_NNIE_Forward(&s_stSsdNnieParam, &stInputDataIdx, &stProcSegIdx, HI_TRUE);
2374     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, SSD_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2375         "Error,SAMPLE_SVP_NNIE_Forward failed!\n");
2376     /* software process */
2377     /* if user has changed net struct, please make sure SAMPLE_SVP_NNIE_Ssd_GetResult
2378      function's input data are correct */
2379 
2380     if (g_stop_signal == HI_TRUE) {
2381         SAMPLE_SVP_NNIE_Ssd_Stop();
2382         return;
2383     }
2384     s32Ret = SAMPLE_SVP_NNIE_Ssd_GetResult(&s_stSsdNnieParam, &s_stSsdSoftwareParam);
2385     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, SSD_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2386         "Error,SAMPLE_SVP_NNIE_Ssd_GetResult failed!\n");
2387 
2388     /* print result, this sample has 21 classes:
2389      class 0:background     class 1:plane           class 2:bicycle
2390      class 3:bird           class 4:boat            class 5:bottle
2391      class 6:bus            class 7:car             class 8:cat
2392      class 9:chair          class10:cow             class11:diningtable
2393      class 12:dog           class13:horse           class14:motorbike
2394      class 15:person        class16:pottedplant     class17:sheep
2395      class 18:sofa          class19:train           class20:tvmonitor */
2396     if (g_stop_signal == HI_TRUE) {
2397         SAMPLE_SVP_NNIE_Ssd_Stop();
2398         return;
2399     }
2400     SAMPLE_SVP_TRACE_INFO("Ssd result:\n");
2401     (void)SAMPLE_SVP_NNIE_Detection_PrintResult(&s_stSsdSoftwareParam.stDstScore, &s_stSsdSoftwareParam.stDstRoi,
2402         &s_stSsdSoftwareParam.stClassRoiNum, f32PrintResultThresh);
2403 
2404 SSD_FAIL_0:
2405     SAMPLE_SVP_NNIE_Ssd_Deinit(&s_stSsdNnieParam, &s_stSsdSoftwareParam, &s_stSsdModel);
2406     SAMPLE_COMM_SVP_CheckSysExit();
2407 }
2408 
2409 /* function : SSD sample signal handle */
SAMPLE_SVP_NNIE_Ssd_HandleSig(void)2410 void SAMPLE_SVP_NNIE_Ssd_HandleSig(void)
2411 {
2412     g_stop_signal = HI_TRUE;
2413 }
2414 
2415 /* function : Yolov1 software deinit */
SAMPLE_SVP_NNIE_Yolov1_SoftwareDeinit(SAMPLE_SVP_NNIE_YOLOV1_SOFTWARE_PARAM_S * pstSoftWareParam)2416 static HI_S32 SAMPLE_SVP_NNIE_Yolov1_SoftwareDeinit(SAMPLE_SVP_NNIE_YOLOV1_SOFTWARE_PARAM_S *pstSoftWareParam)
2417 {
2418     HI_S32 s32Ret = HI_SUCCESS;
2419     SAMPLE_SVP_CHECK_EXPR_RET(pstSoftWareParam == NULL, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
2420         "Error, pstSoftWareParam can't be NULL!\n");
2421     if ((pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr != 0) &&
2422         (pstSoftWareParam->stGetResultTmpBuf.u64VirAddr != 0)) {
2423         SAMPLE_SVP_MMZ_FREE(pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr,
2424             pstSoftWareParam->stGetResultTmpBuf.u64VirAddr);
2425         pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr = 0;
2426         pstSoftWareParam->stGetResultTmpBuf.u64VirAddr = 0;
2427         pstSoftWareParam->stDstRoi.u64PhyAddr = 0;
2428         pstSoftWareParam->stDstRoi.u64VirAddr = 0;
2429         pstSoftWareParam->stDstScore.u64PhyAddr = 0;
2430         pstSoftWareParam->stDstScore.u64VirAddr = 0;
2431         pstSoftWareParam->stClassRoiNum.u64PhyAddr = 0;
2432         pstSoftWareParam->stClassRoiNum.u64VirAddr = 0;
2433     }
2434     return s32Ret;
2435 }
2436 
2437 /* function : Yolov1 Deinit */
SAMPLE_SVP_NNIE_Yolov1_Deinit(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_YOLOV1_SOFTWARE_PARAM_S * pstSoftWareParam,SAMPLE_SVP_NNIE_MODEL_S * pstNnieModel)2438 static HI_S32 SAMPLE_SVP_NNIE_Yolov1_Deinit(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
2439     SAMPLE_SVP_NNIE_YOLOV1_SOFTWARE_PARAM_S *pstSoftWareParam, SAMPLE_SVP_NNIE_MODEL_S *pstNnieModel)
2440 {
2441     HI_S32 s32Ret = HI_SUCCESS;
2442     /* hardware deinit */
2443     if (pstNnieParam != NULL) {
2444         s32Ret = SAMPLE_COMM_SVP_NNIE_ParamDeinit(pstNnieParam);
2445         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
2446             "Error,SAMPLE_COMM_SVP_NNIE_ParamDeinit failed!\n");
2447     }
2448     /* software deinit */
2449     if (pstSoftWareParam != NULL) {
2450         s32Ret = SAMPLE_SVP_NNIE_Yolov1_SoftwareDeinit(pstSoftWareParam);
2451         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
2452             "Error,SAMPLE_SVP_NNIE_Yolov1_SoftwareDeinit failed!\n");
2453     }
2454     /* model deinit */
2455     if (pstNnieModel != NULL) {
2456         s32Ret = SAMPLE_COMM_SVP_NNIE_UnloadModel(pstNnieModel);
2457         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
2458             "Error,SAMPLE_COMM_SVP_NNIE_UnloadModel failed!\n");
2459     }
2460     return s32Ret;
2461 }
2462 
2463 /* function : Yolov1 software para init */
SAMPLE_SVP_NNIE_Yolov1_SoftwareInit(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_YOLOV1_SOFTWARE_PARAM_S * pstSoftWareParam)2464 static HI_S32 SAMPLE_SVP_NNIE_Yolov1_SoftwareInit(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
2465     SAMPLE_SVP_NNIE_YOLOV1_SOFTWARE_PARAM_S *pstSoftWareParam)
2466 {
2467     HI_S32 s32Ret = HI_SUCCESS;
2468     HI_U32 u32ClassNum = 0;
2469     HI_U32 u32BboxNum = 0;
2470     HI_U32 u32TotalSize = 0;
2471     HI_U32 u32DstRoiSize = 0;
2472     HI_U32 u32DstScoreSize = 0;
2473     HI_U32 u32ClassRoiNumSize = 0;
2474     HI_U32 u32TmpBufTotalSize = 0;
2475     HI_U64 u64PhyAddr = 0;
2476     HI_U8 *pu8VirAddr = NULL;
2477 
2478     pstSoftWareParam->u32OriImHeight = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Height;
2479     pstSoftWareParam->u32OriImWidth = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Width;
2480     pstSoftWareParam->u32BboxNumEachGrid = 2;
2481     pstSoftWareParam->u32ClassNum = 20;
2482     pstSoftWareParam->u32GridNumHeight = 7;
2483     pstSoftWareParam->u32GridNumWidth = 7;
2484     pstSoftWareParam->u32NmsThresh = (HI_U32)(0.5f * SAMPLE_SVP_NNIE_QUANT_BASE);
2485     pstSoftWareParam->u32ConfThresh = (HI_U32)(0.2f * SAMPLE_SVP_NNIE_QUANT_BASE);
2486 
2487     /* Malloc assist buffer memory */
2488     u32ClassNum = pstSoftWareParam->u32ClassNum + 1;
2489     u32BboxNum =
2490         pstSoftWareParam->u32BboxNumEachGrid * pstSoftWareParam->u32GridNumHeight * pstSoftWareParam->u32GridNumWidth;
2491     u32TmpBufTotalSize = SAMPLE_SVP_NNIE_Yolov1_GetResultTmpBuf(pstSoftWareParam);
2492     SAMPLE_SVP_CHECK_EXPR_RET(u32TmpBufTotalSize == 0, HI_ERR_SVP_NNIE_ILLEGAL_PARAM, SAMPLE_SVP_ERR_LEVEL_ERROR,
2493         "Error, SAMPLE_SVP_NNIE_Yolov1_GetResultTmpBuf failed!\n");
2494     u32DstRoiSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * u32BboxNum * sizeof(HI_U32) * SAMPLE_SVP_NNIE_COORDI_NUM);
2495     u32DstScoreSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * u32BboxNum * sizeof(HI_U32));
2496     u32ClassRoiNumSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * sizeof(HI_U32));
2497     u32TotalSize = u32TotalSize + u32DstRoiSize + u32DstScoreSize + u32ClassRoiNumSize + u32TmpBufTotalSize;
2498     s32Ret = SAMPLE_COMM_SVP_MallocCached("SAMPLE_YOLOV1_INIT", NULL, (HI_U64 *)&u64PhyAddr, (void **)&pu8VirAddr,
2499         u32TotalSize);
2500     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
2501         "Error,Malloc memory failed!\n");
2502     (HI_VOID)memset_s(pu8VirAddr, u32TotalSize, 0, u32TotalSize);
2503     SAMPLE_COMM_SVP_FlushCache(u64PhyAddr, (void *)pu8VirAddr, u32TotalSize);
2504 
2505     /* set each tmp buffer addr */
2506     pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr = u64PhyAddr;
2507     pstSoftWareParam->stGetResultTmpBuf.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr);
2508 
2509     /* set result blob */
2510     pstSoftWareParam->stDstRoi.enType = SVP_BLOB_TYPE_S32;
2511     pstSoftWareParam->stDstRoi.u64PhyAddr = u64PhyAddr + u32TmpBufTotalSize;
2512     pstSoftWareParam->stDstRoi.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr + u32TmpBufTotalSize);
2513     pstSoftWareParam->stDstRoi.u32Stride =
2514         SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * u32BboxNum * sizeof(HI_U32) * SAMPLE_SVP_NNIE_COORDI_NUM);
2515     pstSoftWareParam->stDstRoi.u32Num = 1;
2516     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Chn = 1;
2517     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Height = 1;
2518     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Width = u32ClassNum * u32BboxNum * SAMPLE_SVP_NNIE_COORDI_NUM;
2519 
2520     pstSoftWareParam->stDstScore.enType = SVP_BLOB_TYPE_S32;
2521     pstSoftWareParam->stDstScore.u64PhyAddr = u64PhyAddr + u32TmpBufTotalSize + u32DstRoiSize;
2522     pstSoftWareParam->stDstScore.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr + u32TmpBufTotalSize + u32DstRoiSize);
2523     pstSoftWareParam->stDstScore.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * u32BboxNum * sizeof(HI_U32));
2524     pstSoftWareParam->stDstScore.u32Num = 1;
2525     pstSoftWareParam->stDstScore.unShape.stWhc.u32Chn = 1;
2526     pstSoftWareParam->stDstScore.unShape.stWhc.u32Height = 1;
2527     pstSoftWareParam->stDstScore.unShape.stWhc.u32Width = u32ClassNum * u32BboxNum;
2528 
2529     pstSoftWareParam->stClassRoiNum.enType = SVP_BLOB_TYPE_S32;
2530     pstSoftWareParam->stClassRoiNum.u64PhyAddr = u64PhyAddr + u32TmpBufTotalSize + u32DstRoiSize + u32DstScoreSize;
2531     pstSoftWareParam->stClassRoiNum.u64VirAddr =
2532         SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64, pu8VirAddr) + u32TmpBufTotalSize + u32DstRoiSize + u32DstScoreSize;
2533     pstSoftWareParam->stClassRoiNum.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * sizeof(HI_U32));
2534     pstSoftWareParam->stClassRoiNum.u32Num = 1;
2535     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Chn = 1;
2536     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Height = 1;
2537     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Width = u32ClassNum;
2538 
2539     return s32Ret;
2540 }
2541 
2542 /* function : Yolov1 init */
SAMPLE_SVP_NNIE_Yolov1_ParamInit(SAMPLE_SVP_NNIE_CFG_S * pstCfg,SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_YOLOV1_SOFTWARE_PARAM_S * pstSoftWareParam)2543 static HI_S32 SAMPLE_SVP_NNIE_Yolov1_ParamInit(SAMPLE_SVP_NNIE_CFG_S *pstCfg, SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
2544     SAMPLE_SVP_NNIE_YOLOV1_SOFTWARE_PARAM_S *pstSoftWareParam)
2545 {
2546     HI_S32 s32Ret = HI_SUCCESS;
2547     /* init hardware para */
2548     s32Ret = SAMPLE_COMM_SVP_NNIE_ParamInit(pstCfg, pstNnieParam);
2549     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2550         "Error(%#x),SAMPLE_COMM_SVP_NNIE_ParamInit failed!\n", s32Ret);
2551 
2552     /* init software para */
2553     s32Ret = SAMPLE_SVP_NNIE_Yolov1_SoftwareInit(pstNnieParam, pstSoftWareParam);
2554     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2555         "Error(%#x),SAMPLE_SVP_NNIE_Yolov1_SoftwareInit failed!\n", s32Ret);
2556 
2557     return s32Ret;
2558 INIT_FAIL_0:
2559     s32Ret = SAMPLE_SVP_NNIE_Yolov1_Deinit(pstNnieParam, pstSoftWareParam, NULL);
2560     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
2561         "Error(%#x),SAMPLE_SVP_NNIE_Yolov1_Deinit failed!\n", s32Ret);
2562     return HI_FAILURE;
2563 }
2564 
SAMPLE_SVP_NNIE_Yolov1_Stop(hi_void)2565 static hi_void SAMPLE_SVP_NNIE_Yolov1_Stop(hi_void)
2566 {
2567     SAMPLE_SVP_NNIE_Yolov1_Deinit(&s_stYolov1NnieParam, &s_stYolov1SoftwareParam, &s_stYolov1Model);
2568     (HI_VOID)memset_s(&s_stYolov1NnieParam, sizeof(SAMPLE_SVP_NNIE_PARAM_S), 0, sizeof(SAMPLE_SVP_NNIE_PARAM_S));
2569     (HI_VOID)memset_s(&s_stYolov1SoftwareParam, sizeof(SAMPLE_SVP_NNIE_YOLOV1_SOFTWARE_PARAM_S), 0,
2570         sizeof(SAMPLE_SVP_NNIE_YOLOV1_SOFTWARE_PARAM_S));
2571     (HI_VOID)memset_s(&s_stYolov1Model, sizeof(SAMPLE_SVP_NNIE_MODEL_S), 0, sizeof(SAMPLE_SVP_NNIE_MODEL_S));
2572     SAMPLE_COMM_SVP_CheckSysExit();
2573     printf("\033[0;31mprogram termination abnormally!\033[0;39m\n");
2574 }
2575 
2576 /* function : show YOLOV1 sample(image 448x448 U8_C3) */
SAMPLE_SVP_NNIE_Yolov1(void)2577 void SAMPLE_SVP_NNIE_Yolov1(void)
2578 {
2579     const HI_CHAR *pcSrcFile = "./data/nnie_image/rgb_planar/dog_bike_car_448x448.bgr";
2580     const HI_CHAR *pcModelName = "./data/nnie_model/detection/inst_yolov1_cycle.wk";
2581     const HI_U32 u32PicNum = 1;
2582     HI_FLOAT f32PrintResultThresh = 0.0f;
2583     HI_S32 s32Ret = HI_SUCCESS;
2584     SAMPLE_SVP_NNIE_CFG_S stNnieCfg = { 0 };
2585     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S stInputDataIdx = { 0 };
2586     SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S stProcSegIdx = { 0 };
2587 
2588     /* Set configuration parameter */
2589     f32PrintResultThresh = 0.3f;
2590     stNnieCfg.pszPic = pcSrcFile;
2591     stNnieCfg.u32MaxInputNum = u32PicNum; // max input image num in each batch
2592     stNnieCfg.u32MaxRoiNum = 0;
2593     stNnieCfg.aenNnieCoreId[0] = SVP_NNIE_ID_0; // set NNIE core
2594     g_stop_signal = HI_FALSE;
2595 
2596     /* Sys init */
2597     s32Ret = SAMPLE_COMM_SVP_CheckSysInit();
2598     SAMPLE_SVP_CHECK_EXPR_RET_VOID(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
2599         "Error,SAMPLE_COMM_SVP_CheckSysInit failed!\n");
2600     if (g_stop_signal == HI_TRUE) {
2601         SAMPLE_SVP_NNIE_Yolov1_Stop();
2602         return;
2603     }
2604 
2605     /* Yolov1 Load model */
2606     SAMPLE_SVP_TRACE_INFO("Yolov1 Load model!\n");
2607     s32Ret = SAMPLE_COMM_SVP_NNIE_LoadModel(pcModelName, &s_stYolov1Model);
2608     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, YOLOV1_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2609         "Error,SAMPLE_COMM_SVP_NNIE_LoadModel failed!\n");
2610 
2611     /* Yolov1 parameter initialization */
2612     /* Yolov1 software parameters are set in SAMPLE_SVP_NNIE_Yolov1_SoftwareInit,
2613       if user has changed net struct, please make sure the parameter settings in
2614       SAMPLE_SVP_NNIE_Yolov1_SoftwareInit function are correct */
2615     SAMPLE_SVP_TRACE_INFO("Yolov1 parameter initialization!\n");
2616     s_stYolov1NnieParam.pstModel = &s_stYolov1Model.stModel;
2617     if (g_stop_signal == HI_TRUE) {
2618         SAMPLE_SVP_NNIE_Yolov1_Stop();
2619         return;
2620     }
2621     s32Ret = SAMPLE_SVP_NNIE_Yolov1_ParamInit(&stNnieCfg, &s_stYolov1NnieParam, &s_stYolov1SoftwareParam);
2622     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, YOLOV1_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2623         "Error,SAMPLE_SVP_NNIE_Yolov1_ParamInit failed!\n");
2624 
2625     /* Fill src data */
2626     SAMPLE_SVP_TRACE_INFO("Yolov1 start!\n");
2627 
2628     stInputDataIdx.u32SegIdx = 0;
2629     stInputDataIdx.u32NodeIdx = 0;
2630     if (g_stop_signal == HI_TRUE) {
2631         SAMPLE_SVP_NNIE_Yolov1_Stop();
2632         return;
2633     }
2634     s32Ret = SAMPLE_SVP_NNIE_FillSrcData(&stNnieCfg, &s_stYolov1NnieParam, &stInputDataIdx);
2635     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, YOLOV1_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2636         "Error,SAMPLE_SVP_NNIE_FillSrcData failed!\n");
2637 
2638     /* NNIE process(process the 0-th segment) */
2639     stProcSegIdx.u32SegIdx = 0;
2640     if (g_stop_signal == HI_TRUE) {
2641         SAMPLE_SVP_NNIE_Yolov1_Stop();
2642         return;
2643     }
2644     s32Ret = SAMPLE_SVP_NNIE_Forward(&s_stYolov1NnieParam, &stInputDataIdx, &stProcSegIdx, HI_TRUE);
2645     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, YOLOV1_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2646         "Error,SAMPLE_SVP_NNIE_Forward failed!\n");
2647     /* software process */
2648     /* if user has changed net struct, please make sure SAMPLE_SVP_NNIE_Yolov1_GetResult
2649      function input data are correct */
2650 
2651     if (g_stop_signal == HI_TRUE) {
2652         SAMPLE_SVP_NNIE_Yolov1_Stop();
2653         return;
2654     }
2655     s32Ret = SAMPLE_SVP_NNIE_Yolov1_GetResult(&s_stYolov1NnieParam, &s_stYolov1SoftwareParam);
2656     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, YOLOV1_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2657         "Error,SAMPLE_SVP_NNIE_Yolov1_GetResult failed!\n");
2658 
2659     /* print result, this sample has 21 classes:
2660      class 0:background     class 1:plane           class 2:bicycle
2661      class 3:bird           class 4:boat            class 5:bottle
2662      class 6:bus            class 7:car             class 8:cat
2663      class 9:chair          class10:cow             class11:diningtable
2664      class 12:dog           class13:horse           class14:motorbike
2665      class 15:person        class16:pottedplant     class17:sheep
2666      class 18:sofa          class19:train           class20:tvmonitor */
2667     if (g_stop_signal == HI_TRUE) {
2668         SAMPLE_SVP_NNIE_Yolov1_Stop();
2669         return;
2670     }
2671     SAMPLE_SVP_TRACE_INFO("Yolov1 result:\n");
2672     (void)SAMPLE_SVP_NNIE_Detection_PrintResult(&s_stYolov1SoftwareParam.stDstScore, &s_stYolov1SoftwareParam.stDstRoi,
2673         &s_stYolov1SoftwareParam.stClassRoiNum, f32PrintResultThresh);
2674 
2675 YOLOV1_FAIL_0:
2676     SAMPLE_SVP_NNIE_Yolov1_Deinit(&s_stYolov1NnieParam, &s_stYolov1SoftwareParam, &s_stYolov1Model);
2677     SAMPLE_COMM_SVP_CheckSysExit();
2678 }
2679 
2680 /* function : Yolov1 sample signal handle */
SAMPLE_SVP_NNIE_Yolov1_HandleSig(void)2681 void SAMPLE_SVP_NNIE_Yolov1_HandleSig(void)
2682 {
2683     g_stop_signal = HI_TRUE;
2684 }
2685 
2686 /* function : Yolov2 software deinit */
SAMPLE_SVP_NNIE_Yolov2_SoftwareDeinit(SAMPLE_SVP_NNIE_YOLOV2_SOFTWARE_PARAM_S * pstSoftWareParam)2687 static HI_S32 SAMPLE_SVP_NNIE_Yolov2_SoftwareDeinit(SAMPLE_SVP_NNIE_YOLOV2_SOFTWARE_PARAM_S *pstSoftWareParam)
2688 {
2689     HI_S32 s32Ret = HI_SUCCESS;
2690     SAMPLE_SVP_CHECK_EXPR_RET(pstSoftWareParam == NULL, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
2691         "Error, pstSoftWareParam can't be NULL!\n");
2692     if ((pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr != 0) &&
2693         (pstSoftWareParam->stGetResultTmpBuf.u64VirAddr != 0)) {
2694         SAMPLE_SVP_MMZ_FREE(pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr,
2695             pstSoftWareParam->stGetResultTmpBuf.u64VirAddr);
2696         pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr = 0;
2697         pstSoftWareParam->stGetResultTmpBuf.u64VirAddr = 0;
2698         pstSoftWareParam->stDstRoi.u64PhyAddr = 0;
2699         pstSoftWareParam->stDstRoi.u64VirAddr = 0;
2700         pstSoftWareParam->stDstScore.u64PhyAddr = 0;
2701         pstSoftWareParam->stDstScore.u64VirAddr = 0;
2702         pstSoftWareParam->stClassRoiNum.u64PhyAddr = 0;
2703         pstSoftWareParam->stClassRoiNum.u64VirAddr = 0;
2704     }
2705     return s32Ret;
2706 }
2707 
2708 /* function : Yolov2 Deinit */
SAMPLE_SVP_NNIE_Yolov2_Deinit(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_YOLOV2_SOFTWARE_PARAM_S * pstSoftWareParam,SAMPLE_SVP_NNIE_MODEL_S * pstNnieModel)2709 static HI_S32 SAMPLE_SVP_NNIE_Yolov2_Deinit(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
2710     SAMPLE_SVP_NNIE_YOLOV2_SOFTWARE_PARAM_S *pstSoftWareParam, SAMPLE_SVP_NNIE_MODEL_S *pstNnieModel)
2711 {
2712     HI_S32 s32Ret = HI_SUCCESS;
2713     /* hardware deinit */
2714     if (pstNnieParam != NULL) {
2715         s32Ret = SAMPLE_COMM_SVP_NNIE_ParamDeinit(pstNnieParam);
2716         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
2717             "Error,SAMPLE_COMM_SVP_NNIE_ParamDeinit failed!\n");
2718     }
2719     /* software deinit */
2720     if (pstSoftWareParam != NULL) {
2721         s32Ret = SAMPLE_SVP_NNIE_Yolov2_SoftwareDeinit(pstSoftWareParam);
2722         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
2723             "Error,SAMPLE_SVP_NNIE_Yolov2_SoftwareDeinit failed!\n");
2724     }
2725     /* model deinit */
2726     if (pstNnieModel != NULL) {
2727         s32Ret = SAMPLE_COMM_SVP_NNIE_UnloadModel(pstNnieModel);
2728         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
2729             "Error,SAMPLE_COMM_SVP_NNIE_UnloadModel failed!\n");
2730     }
2731     return s32Ret;
2732 }
2733 
2734 /* function : Yolov2 software para init */
SAMPLE_SVP_NNIE_Yolov2_SoftwareInit(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_YOLOV2_SOFTWARE_PARAM_S * pstSoftWareParam)2735 static HI_S32 SAMPLE_SVP_NNIE_Yolov2_SoftwareInit(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
2736     SAMPLE_SVP_NNIE_YOLOV2_SOFTWARE_PARAM_S *pstSoftWareParam)
2737 {
2738     HI_S32 s32Ret = HI_SUCCESS;
2739     HI_U32 u32ClassNum = 0;
2740     HI_U32 u32BboxNum = 0;
2741     HI_U32 u32TotalSize = 0;
2742     HI_U32 u32DstRoiSize = 0;
2743     HI_U32 u32DstScoreSize = 0;
2744     HI_U32 u32ClassRoiNumSize = 0;
2745     HI_U32 u32TmpBufTotalSize = 0;
2746     HI_U64 u64PhyAddr = 0;
2747     HI_U8 *pu8VirAddr = NULL;
2748 
2749     /* The values of the following parameters are related to algorithm principles.
2750         For details, see related algorithms. */
2751     pstSoftWareParam->u32OriImHeight = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Height;
2752     pstSoftWareParam->u32OriImWidth = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Width;
2753     pstSoftWareParam->u32BboxNumEachGrid = 5;
2754     pstSoftWareParam->u32ClassNum = 5;
2755     pstSoftWareParam->u32GridNumHeight = 13;
2756     pstSoftWareParam->u32GridNumWidth = 13;
2757     pstSoftWareParam->u32NmsThresh = (HI_U32)(0.3f * SAMPLE_SVP_NNIE_QUANT_BASE);
2758     pstSoftWareParam->u32ConfThresh = (HI_U32)(0.25f * SAMPLE_SVP_NNIE_QUANT_BASE);
2759     pstSoftWareParam->u32MaxRoiNum = 10;
2760     pstSoftWareParam->af32Bias[0] = 1.08;
2761     pstSoftWareParam->af32Bias[1] = 1.19;
2762     pstSoftWareParam->af32Bias[2] = 3.42;
2763     pstSoftWareParam->af32Bias[3] = 4.41;
2764     pstSoftWareParam->af32Bias[4] = 6.63;
2765     pstSoftWareParam->af32Bias[5] = 11.38;
2766     pstSoftWareParam->af32Bias[6] = 9.42;
2767     pstSoftWareParam->af32Bias[7] = 5.11;
2768     pstSoftWareParam->af32Bias[8] = 16.62;
2769     pstSoftWareParam->af32Bias[9] = 10.52;
2770 
2771     /* Malloc assist buffer memory */
2772     u32ClassNum = pstSoftWareParam->u32ClassNum + 1;
2773     u32BboxNum =
2774         pstSoftWareParam->u32BboxNumEachGrid * pstSoftWareParam->u32GridNumHeight * pstSoftWareParam->u32GridNumWidth;
2775     u32TmpBufTotalSize = SAMPLE_SVP_NNIE_Yolov2_GetResultTmpBuf(pstSoftWareParam);
2776     SAMPLE_SVP_CHECK_EXPR_RET(u32TmpBufTotalSize == 0, HI_ERR_SVP_NNIE_ILLEGAL_PARAM, SAMPLE_SVP_ERR_LEVEL_ERROR,
2777         "Error, SAMPLE_SVP_NNIE_Yolov2_GetResultTmpBuf failed!\n");
2778     u32DstRoiSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * u32BboxNum * sizeof(HI_U32) * SAMPLE_SVP_NNIE_COORDI_NUM);
2779     u32DstScoreSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * u32BboxNum * sizeof(HI_U32));
2780     u32ClassRoiNumSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * sizeof(HI_U32));
2781     u32TotalSize = u32TotalSize + u32DstRoiSize + u32DstScoreSize + u32ClassRoiNumSize + u32TmpBufTotalSize;
2782     s32Ret = SAMPLE_COMM_SVP_MallocCached("SAMPLE_YOLOV2_INIT", NULL, (HI_U64 *)&u64PhyAddr, (void **)&pu8VirAddr,
2783         u32TotalSize);
2784     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
2785         "Error,Malloc memory failed!\n");
2786     (HI_VOID)memset_s(pu8VirAddr, u32TotalSize, 0, u32TotalSize);
2787     SAMPLE_COMM_SVP_FlushCache(u64PhyAddr, (void *)pu8VirAddr, u32TotalSize);
2788 
2789     /* set each tmp buffer addr */
2790     pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr = u64PhyAddr;
2791     pstSoftWareParam->stGetResultTmpBuf.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr);
2792 
2793     /* set result blob */
2794     pstSoftWareParam->stDstRoi.enType = SVP_BLOB_TYPE_S32;
2795     pstSoftWareParam->stDstRoi.u64PhyAddr = u64PhyAddr + u32TmpBufTotalSize;
2796     pstSoftWareParam->stDstRoi.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr + u32TmpBufTotalSize);
2797     pstSoftWareParam->stDstRoi.u32Stride =
2798         SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * u32BboxNum * sizeof(HI_U32) * SAMPLE_SVP_NNIE_COORDI_NUM);
2799     pstSoftWareParam->stDstRoi.u32Num = 1;
2800     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Chn = 1;
2801     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Height = 1;
2802     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Width = u32ClassNum * u32BboxNum * SAMPLE_SVP_NNIE_COORDI_NUM;
2803 
2804     pstSoftWareParam->stDstScore.enType = SVP_BLOB_TYPE_S32;
2805     pstSoftWareParam->stDstScore.u64PhyAddr = u64PhyAddr + u32TmpBufTotalSize + u32DstRoiSize;
2806     pstSoftWareParam->stDstScore.u64VirAddr =
2807         SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64, pu8VirAddr) + u32TmpBufTotalSize + u32DstRoiSize;
2808     pstSoftWareParam->stDstScore.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * u32BboxNum * sizeof(HI_U32));
2809     pstSoftWareParam->stDstScore.u32Num = 1;
2810     pstSoftWareParam->stDstScore.unShape.stWhc.u32Chn = 1;
2811     pstSoftWareParam->stDstScore.unShape.stWhc.u32Height = 1;
2812     pstSoftWareParam->stDstScore.unShape.stWhc.u32Width = u32ClassNum * u32BboxNum;
2813 
2814     pstSoftWareParam->stClassRoiNum.enType = SVP_BLOB_TYPE_S32;
2815     pstSoftWareParam->stClassRoiNum.u64PhyAddr = u64PhyAddr + u32TmpBufTotalSize + u32DstRoiSize + u32DstScoreSize;
2816     pstSoftWareParam->stClassRoiNum.u64VirAddr =
2817         SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64, pu8VirAddr) + u32TmpBufTotalSize + u32DstRoiSize + u32DstScoreSize;
2818     pstSoftWareParam->stClassRoiNum.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * sizeof(HI_U32));
2819     pstSoftWareParam->stClassRoiNum.u32Num = 1;
2820     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Chn = 1;
2821     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Height = 1;
2822     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Width = u32ClassNum;
2823 
2824     return s32Ret;
2825 }
2826 
2827 /* function : Yolov1 init */
SAMPLE_SVP_NNIE_Yolov2_ParamInit(SAMPLE_SVP_NNIE_CFG_S * pstCfg,SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_YOLOV2_SOFTWARE_PARAM_S * pstSoftWareParam)2828 static HI_S32 SAMPLE_SVP_NNIE_Yolov2_ParamInit(SAMPLE_SVP_NNIE_CFG_S *pstCfg, SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
2829     SAMPLE_SVP_NNIE_YOLOV2_SOFTWARE_PARAM_S *pstSoftWareParam)
2830 {
2831     HI_S32 s32Ret = HI_SUCCESS;
2832     /* init hardware para */
2833     s32Ret = SAMPLE_COMM_SVP_NNIE_ParamInit(pstCfg, pstNnieParam);
2834     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2835         "Error(%#x),SAMPLE_COMM_SVP_NNIE_ParamInit failed!\n", s32Ret);
2836 
2837     /* init software para */
2838     s32Ret = SAMPLE_SVP_NNIE_Yolov2_SoftwareInit(pstNnieParam, pstSoftWareParam);
2839     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2840         "Error(%#x),SAMPLE_SVP_NNIE_Yolov2_SoftwareInit failed!\n", s32Ret);
2841 
2842     return s32Ret;
2843 INIT_FAIL_0:
2844     s32Ret = SAMPLE_SVP_NNIE_Yolov2_Deinit(pstNnieParam, pstSoftWareParam, NULL);
2845     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
2846         "Error(%#x),SAMPLE_SVP_NNIE_Yolov2_Deinit failed!\n", s32Ret);
2847     return HI_FAILURE;
2848 }
2849 
SAMPLE_SVP_NNIE_Yolov2_Stop(void)2850 static void SAMPLE_SVP_NNIE_Yolov2_Stop(void)
2851 {
2852     SAMPLE_SVP_NNIE_Yolov2_Deinit(&s_stYolov2NnieParam, &s_stYolov2SoftwareParam, &s_stYolov2Model);
2853     (HI_VOID)memset_s(&s_stYolov2NnieParam, sizeof(SAMPLE_SVP_NNIE_PARAM_S), 0, sizeof(SAMPLE_SVP_NNIE_PARAM_S));
2854     (HI_VOID)memset_s(&s_stYolov2SoftwareParam, sizeof(SAMPLE_SVP_NNIE_YOLOV2_SOFTWARE_PARAM_S), 0,
2855         sizeof(SAMPLE_SVP_NNIE_YOLOV2_SOFTWARE_PARAM_S));
2856     (HI_VOID)memset_s(&s_stYolov2Model, sizeof(SAMPLE_SVP_NNIE_MODEL_S), 0, sizeof(SAMPLE_SVP_NNIE_MODEL_S));
2857     SAMPLE_COMM_SVP_CheckSysExit();
2858     printf("\033[0;31mprogram termination abnormally!\033[0;39m\n");
2859 }
2860 
2861 /* function : show YOLOV2 sample(image 416x416 U8_C3) */
SAMPLE_SVP_NNIE_Yolov2(void)2862 void SAMPLE_SVP_NNIE_Yolov2(void)
2863 {
2864     const HI_CHAR *pcSrcFile = "./data/nnie_image/rgb_planar/street_cars_416x416.bgr";
2865     const HI_CHAR *pcModelName = "./data/nnie_model/detection/inst_yolov2_cycle.wk";
2866     const HI_U32 u32PicNum = 1;
2867     HI_FLOAT f32PrintResultThresh = 0.0f;
2868     HI_S32 s32Ret = HI_SUCCESS;
2869     SAMPLE_SVP_NNIE_CFG_S stNnieCfg = { 0 };
2870     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S stInputDataIdx = { 0 };
2871     SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S stProcSegIdx = { 0 };
2872 
2873     /* Set configuration parameter */
2874     f32PrintResultThresh = 0.25f;
2875     stNnieCfg.pszPic = pcSrcFile;
2876     stNnieCfg.u32MaxInputNum = u32PicNum; // max input image num in each batch
2877     stNnieCfg.u32MaxRoiNum = 0;
2878     stNnieCfg.aenNnieCoreId[0] = SVP_NNIE_ID_0; // set NNIE core
2879     g_stop_signal = HI_FALSE;
2880 
2881     /* Sys init */
2882     s32Ret = SAMPLE_COMM_SVP_CheckSysInit();
2883     SAMPLE_SVP_CHECK_EXPR_RET_VOID(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
2884         "Error,SAMPLE_COMM_SVP_CheckSysInit failed!\n");
2885     if (g_stop_signal == HI_TRUE) {
2886         SAMPLE_SVP_NNIE_Yolov2_Stop();
2887         return;
2888     }
2889 
2890     /* Yolov2 Load model */
2891     SAMPLE_SVP_TRACE_INFO("Yolov2 Load model!\n");
2892     s32Ret = SAMPLE_COMM_SVP_NNIE_LoadModel(pcModelName, &s_stYolov2Model);
2893     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, YOLOV2_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2894         "Error,SAMPLE_COMM_SVP_NNIE_LoadModel failed!\n");
2895 
2896     /* Yolov2 parameter initialization */
2897     /* Yolov2 software parameters are set in SAMPLE_SVP_NNIE_Yolov2_SoftwareInit,
2898       if user has changed net struct, please make sure the parameter settings in
2899       SAMPLE_SVP_NNIE_Yolov2_SoftwareInit function are correct */
2900     SAMPLE_SVP_TRACE_INFO("Yolov2 parameter initialization!\n");
2901     if (g_stop_signal == HI_TRUE) {
2902         SAMPLE_SVP_NNIE_Yolov2_Stop();
2903         return;
2904     }
2905     s_stYolov2NnieParam.pstModel = &s_stYolov2Model.stModel;
2906     s32Ret = SAMPLE_SVP_NNIE_Yolov2_ParamInit(&stNnieCfg, &s_stYolov2NnieParam, &s_stYolov2SoftwareParam);
2907     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, YOLOV2_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2908         "Error,SAMPLE_SVP_NNIE_Yolov2_ParamInit failed!\n");
2909 
2910     /* Fill src data */
2911     SAMPLE_SVP_TRACE_INFO("Yolov2 start!\n");
2912     stInputDataIdx.u32SegIdx = 0;
2913     stInputDataIdx.u32NodeIdx = 0;
2914     if (g_stop_signal == HI_TRUE) {
2915         SAMPLE_SVP_NNIE_Yolov2_Stop();
2916         return;
2917     }
2918     s32Ret = SAMPLE_SVP_NNIE_FillSrcData(&stNnieCfg, &s_stYolov2NnieParam, &stInputDataIdx);
2919     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, YOLOV2_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2920         "Error,SAMPLE_SVP_NNIE_FillSrcData failed!\n");
2921 
2922     /* NNIE process(process the 0-th segment) */
2923     stProcSegIdx.u32SegIdx = 0;
2924     if (g_stop_signal == HI_TRUE) {
2925         SAMPLE_SVP_NNIE_Yolov2_Stop();
2926         return;
2927     }
2928     s32Ret = SAMPLE_SVP_NNIE_Forward(&s_stYolov2NnieParam, &stInputDataIdx, &stProcSegIdx, HI_TRUE);
2929     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, YOLOV2_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2930         "Error,SAMPLE_SVP_NNIE_Forward failed!\n");
2931 
2932     /* Software process */
2933     /* if user has changed net struct, please make sure SAMPLE_SVP_NNIE_Yolov2_GetResult
2934      function input data are correct */
2935 
2936     if (g_stop_signal == HI_TRUE) {
2937         SAMPLE_SVP_NNIE_Yolov2_Stop();
2938         return;
2939     }
2940     s32Ret = SAMPLE_SVP_NNIE_Yolov2_GetResult(&s_stYolov2NnieParam, &s_stYolov2SoftwareParam);
2941     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, YOLOV2_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
2942         "Error,SAMPLE_SVP_NNIE_Yolov2_GetResult failed!\n");
2943 
2944     /* print result, this sample has 6 classes:
2945      class 0:background     class 1:Carclass           class 2:Vanclass
2946      class 3:Truckclass     class 4:Pedestrianclass    class 5:Cyclist */
2947     if (g_stop_signal == HI_TRUE) {
2948         SAMPLE_SVP_NNIE_Yolov2_Stop();
2949         return;
2950     }
2951     SAMPLE_SVP_TRACE_INFO("Yolov2 result:\n");
2952     (void)SAMPLE_SVP_NNIE_Detection_PrintResult(&s_stYolov2SoftwareParam.stDstScore, &s_stYolov2SoftwareParam.stDstRoi,
2953         &s_stYolov2SoftwareParam.stClassRoiNum, f32PrintResultThresh);
2954 
2955 YOLOV2_FAIL_0:
2956     SAMPLE_SVP_NNIE_Yolov2_Deinit(&s_stYolov2NnieParam, &s_stYolov2SoftwareParam, &s_stYolov2Model);
2957     SAMPLE_COMM_SVP_CheckSysExit();
2958 }
2959 
2960 /* function : Yolov2 sample signal handle */
SAMPLE_SVP_NNIE_Yolov2_HandleSig(void)2961 void SAMPLE_SVP_NNIE_Yolov2_HandleSig(void)
2962 {
2963     g_stop_signal = HI_TRUE;
2964 }
2965 
2966 /* function : Yolov3 software deinit */
SAMPLE_SVP_NNIE_Yolov3_SoftwareDeinit(SAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S * pstSoftWareParam)2967 static HI_S32 SAMPLE_SVP_NNIE_Yolov3_SoftwareDeinit(SAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S *pstSoftWareParam)
2968 {
2969     HI_S32 s32Ret = HI_SUCCESS;
2970     SAMPLE_SVP_CHECK_EXPR_RET(pstSoftWareParam == NULL, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
2971         "Error, pstSoftWareParam can't be NULL!\n");
2972     if ((pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr != 0) &&
2973         (pstSoftWareParam->stGetResultTmpBuf.u64VirAddr != 0)) {
2974         SAMPLE_SVP_MMZ_FREE(pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr,
2975             pstSoftWareParam->stGetResultTmpBuf.u64VirAddr);
2976         pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr = 0;
2977         pstSoftWareParam->stGetResultTmpBuf.u64VirAddr = 0;
2978         pstSoftWareParam->stDstRoi.u64PhyAddr = 0;
2979         pstSoftWareParam->stDstRoi.u64VirAddr = 0;
2980         pstSoftWareParam->stDstScore.u64PhyAddr = 0;
2981         pstSoftWareParam->stDstScore.u64VirAddr = 0;
2982         pstSoftWareParam->stClassRoiNum.u64PhyAddr = 0;
2983         pstSoftWareParam->stClassRoiNum.u64VirAddr = 0;
2984     }
2985     return s32Ret;
2986 }
2987 
2988 /* function : Yolov3 Deinit */
SAMPLE_SVP_NNIE_Yolov3_Deinit(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S * pstSoftWareParam,SAMPLE_SVP_NNIE_MODEL_S * pstNnieModel)2989 static HI_S32 SAMPLE_SVP_NNIE_Yolov3_Deinit(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
2990     SAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S *pstSoftWareParam, SAMPLE_SVP_NNIE_MODEL_S *pstNnieModel)
2991 {
2992     HI_S32 s32Ret = HI_SUCCESS;
2993     /* hardware deinit */
2994     if (pstNnieParam != NULL) {
2995         s32Ret = SAMPLE_COMM_SVP_NNIE_ParamDeinit(pstNnieParam);
2996         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
2997             "Error,SAMPLE_COMM_SVP_NNIE_ParamDeinit failed!\n");
2998     }
2999     /* software deinit */
3000     if (pstSoftWareParam != NULL) {
3001         s32Ret = SAMPLE_SVP_NNIE_Yolov3_SoftwareDeinit(pstSoftWareParam);
3002         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
3003             "Error,SAMPLE_SVP_NNIE_Yolov3_SoftwareDeinit failed!\n");
3004     }
3005     /* model deinit */
3006     if (pstNnieModel != NULL) {
3007         s32Ret = SAMPLE_COMM_SVP_NNIE_UnloadModel(pstNnieModel);
3008         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
3009             "Error,SAMPLE_COMM_SVP_NNIE_UnloadModel failed!\n");
3010     }
3011     return s32Ret;
3012 }
3013 
3014 /* function : Yolov3 software para init */
SAMPLE_SVP_NNIE_Yolov3_SoftwareInit(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S * pstSoftWareParam)3015 static HI_S32 SAMPLE_SVP_NNIE_Yolov3_SoftwareInit(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
3016     SAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S *pstSoftWareParam)
3017 {
3018     HI_S32 s32Ret = HI_SUCCESS;
3019     HI_U32 u32ClassNum = 0;
3020     HI_U32 u32TotalSize = 0;
3021     HI_U32 u32DstRoiSize = 0;
3022     HI_U32 u32DstScoreSize = 0;
3023     HI_U32 u32ClassRoiNumSize = 0;
3024     HI_U32 u32TmpBufTotalSize = 0;
3025     HI_U64 u64PhyAddr = 0;
3026     HI_U8 *pu8VirAddr = NULL;
3027 
3028     /* The values of the following parameters are related to algorithm principles.
3029         For details, see related algorithms. */
3030     pstSoftWareParam->u32OriImHeight = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Height;
3031     pstSoftWareParam->u32OriImWidth = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Width;
3032     pstSoftWareParam->u32BboxNumEachGrid = 3;
3033     pstSoftWareParam->u32ClassNum = 80;
3034     pstSoftWareParam->au32GridNumHeight[0] = 13;
3035     pstSoftWareParam->au32GridNumHeight[1] = 26;
3036     pstSoftWareParam->au32GridNumHeight[2] = 52;
3037     pstSoftWareParam->au32GridNumWidth[0] = 13;
3038     pstSoftWareParam->au32GridNumWidth[1] = 26;
3039     pstSoftWareParam->au32GridNumWidth[2] = 52;
3040     pstSoftWareParam->u32NmsThresh = (HI_U32)(0.3f * SAMPLE_SVP_NNIE_QUANT_BASE);
3041     pstSoftWareParam->u32ConfThresh = (HI_U32)(0.5f * SAMPLE_SVP_NNIE_QUANT_BASE);
3042     pstSoftWareParam->u32MaxRoiNum = 10;
3043     pstSoftWareParam->af32Bias[0][0] = 116;
3044     pstSoftWareParam->af32Bias[0][1] = 90;
3045     pstSoftWareParam->af32Bias[0][2] = 156;
3046     pstSoftWareParam->af32Bias[0][3] = 198;
3047     pstSoftWareParam->af32Bias[0][4] = 373;
3048     pstSoftWareParam->af32Bias[0][5] = 326;
3049     pstSoftWareParam->af32Bias[1][0] = 30;
3050     pstSoftWareParam->af32Bias[1][1] = 61;
3051     pstSoftWareParam->af32Bias[1][2] = 62;
3052     pstSoftWareParam->af32Bias[1][3] = 45;
3053     pstSoftWareParam->af32Bias[1][4] = 59;
3054     pstSoftWareParam->af32Bias[1][5] = 119;
3055     pstSoftWareParam->af32Bias[2][0] = 10;
3056     pstSoftWareParam->af32Bias[2][1] = 13;
3057     pstSoftWareParam->af32Bias[2][2] = 16;
3058     pstSoftWareParam->af32Bias[2][3] = 30;
3059     pstSoftWareParam->af32Bias[2][4] = 33;
3060     pstSoftWareParam->af32Bias[2][5] = 23;
3061 
3062     /* Malloc assist buffer memory */
3063     u32ClassNum = pstSoftWareParam->u32ClassNum + 1;
3064 
3065     SAMPLE_SVP_CHECK_EXPR_RET(SAMPLE_SVP_NNIE_YOLOV3_REPORT_BLOB_NUM != pstNnieParam->pstModel->astSeg[0].u16DstNum,
3066         HI_FAILURE, SAMPLE_SVP_ERR_LEVEL_ERROR, "Error,pstNnieParam->pstModel->astSeg[0].u16DstNum(%d) should be %d!\n",
3067         pstNnieParam->pstModel->astSeg[0].u16DstNum, SAMPLE_SVP_NNIE_YOLOV3_REPORT_BLOB_NUM);
3068     u32TmpBufTotalSize = SAMPLE_SVP_NNIE_Yolov3_GetResultTmpBuf(pstNnieParam, pstSoftWareParam);
3069     SAMPLE_SVP_CHECK_EXPR_RET(u32TmpBufTotalSize == 0, HI_ERR_SVP_NNIE_ILLEGAL_PARAM, SAMPLE_SVP_ERR_LEVEL_ERROR,
3070         "Error, SAMPLE_SVP_NNIE_Yolov3_GetResultTmpBuf failed!\n");
3071     u32DstRoiSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstSoftWareParam->u32MaxRoiNum * sizeof(HI_U32) *
3072         SAMPLE_SVP_NNIE_COORDI_NUM);
3073     u32DstScoreSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstSoftWareParam->u32MaxRoiNum * sizeof(HI_U32));
3074     u32ClassRoiNumSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * sizeof(HI_U32));
3075     u32TotalSize = u32TotalSize + u32DstRoiSize + u32DstScoreSize + u32ClassRoiNumSize + u32TmpBufTotalSize;
3076     s32Ret = SAMPLE_COMM_SVP_MallocCached("SAMPLE_YOLOV3_INIT", NULL, (HI_U64 *)&u64PhyAddr, (void **)&pu8VirAddr,
3077         u32TotalSize);
3078     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
3079         "Error,Malloc memory failed!\n");
3080     (HI_VOID)memset_s(pu8VirAddr, u32TotalSize, 0, u32TotalSize);
3081     SAMPLE_COMM_SVP_FlushCache(u64PhyAddr, (void *)pu8VirAddr, u32TotalSize);
3082 
3083     /* set each tmp buffer addr */
3084     pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr = u64PhyAddr;
3085     pstSoftWareParam->stGetResultTmpBuf.u64VirAddr = SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64, pu8VirAddr);
3086 
3087     /* set result blob */
3088     pstSoftWareParam->stDstRoi.enType = SVP_BLOB_TYPE_S32;
3089     pstSoftWareParam->stDstRoi.u64PhyAddr = u64PhyAddr + u32TmpBufTotalSize;
3090     pstSoftWareParam->stDstRoi.u64VirAddr =
3091         SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64, pu8VirAddr + u32TmpBufTotalSize);
3092     pstSoftWareParam->stDstRoi.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstSoftWareParam->u32MaxRoiNum *
3093         sizeof(HI_U32) * SAMPLE_SVP_NNIE_COORDI_NUM);
3094     pstSoftWareParam->stDstRoi.u32Num = 1;
3095     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Chn = 1;
3096     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Height = 1;
3097     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Width =
3098         u32ClassNum * pstSoftWareParam->u32MaxRoiNum * SAMPLE_SVP_NNIE_COORDI_NUM;
3099 
3100     pstSoftWareParam->stDstScore.enType = SVP_BLOB_TYPE_S32;
3101     pstSoftWareParam->stDstScore.u64PhyAddr = u64PhyAddr + u32TmpBufTotalSize + u32DstRoiSize;
3102     pstSoftWareParam->stDstScore.u64VirAddr =
3103         SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64, pu8VirAddr + u32TmpBufTotalSize + u32DstRoiSize);
3104     pstSoftWareParam->stDstScore.u32Stride =
3105         SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstSoftWareParam->u32MaxRoiNum * sizeof(HI_U32));
3106     pstSoftWareParam->stDstScore.u32Num = 1;
3107     pstSoftWareParam->stDstScore.unShape.stWhc.u32Chn = 1;
3108     pstSoftWareParam->stDstScore.unShape.stWhc.u32Height = 1;
3109     pstSoftWareParam->stDstScore.unShape.stWhc.u32Width = u32ClassNum * pstSoftWareParam->u32MaxRoiNum;
3110 
3111     pstSoftWareParam->stClassRoiNum.enType = SVP_BLOB_TYPE_S32;
3112     pstSoftWareParam->stClassRoiNum.u64PhyAddr = u64PhyAddr + u32TmpBufTotalSize + u32DstRoiSize + u32DstScoreSize;
3113     pstSoftWareParam->stClassRoiNum.u64VirAddr =
3114         SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64, pu8VirAddr + u32TmpBufTotalSize + u32DstRoiSize + u32DstScoreSize);
3115     pstSoftWareParam->stClassRoiNum.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * sizeof(HI_U32));
3116     pstSoftWareParam->stClassRoiNum.u32Num = 1;
3117     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Chn = 1;
3118     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Height = 1;
3119     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Width = u32ClassNum;
3120 
3121     return s32Ret;
3122 }
3123 
3124 /* function : Yolov3 init */
SAMPLE_SVP_NNIE_Yolov3_ParamInit(SAMPLE_SVP_NNIE_CFG_S * pstCfg,SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S * pstSoftWareParam)3125 static HI_S32 SAMPLE_SVP_NNIE_Yolov3_ParamInit(SAMPLE_SVP_NNIE_CFG_S *pstCfg, SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
3126     SAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S *pstSoftWareParam)
3127 {
3128     HI_S32 s32Ret = HI_SUCCESS;
3129     /* init hardware para */
3130     s32Ret = SAMPLE_COMM_SVP_NNIE_ParamInit(pstCfg, pstNnieParam);
3131     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3132         "Error(%#x),SAMPLE_COMM_SVP_NNIE_ParamInit failed!\n", s32Ret);
3133 
3134     /* init software para */
3135     s32Ret = SAMPLE_SVP_NNIE_Yolov3_SoftwareInit(pstNnieParam, pstSoftWareParam);
3136     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3137         "Error(%#x),SAMPLE_SVP_NNIE_Yolov3_SoftwareInit failed!\n", s32Ret);
3138 
3139     return s32Ret;
3140 INIT_FAIL_0:
3141     s32Ret = SAMPLE_SVP_NNIE_Yolov3_Deinit(pstNnieParam, pstSoftWareParam, NULL);
3142     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
3143         "Error(%#x),SAMPLE_SVP_NNIE_Yolov3_Deinit failed!\n", s32Ret);
3144     return HI_FAILURE;
3145 }
3146 
SAMPLE_SVP_NNIE_Yolov3_Stop(hi_void)3147 static hi_void SAMPLE_SVP_NNIE_Yolov3_Stop(hi_void)
3148 {
3149     SAMPLE_SVP_NNIE_Yolov3_Deinit(&s_stYolov3NnieParam, &s_stYolov3SoftwareParam, &s_stYolov3Model);
3150     (HI_VOID)memset_s(&s_stYolov3NnieParam, sizeof(SAMPLE_SVP_NNIE_PARAM_S), 0, sizeof(SAMPLE_SVP_NNIE_PARAM_S));
3151     (HI_VOID)memset_s(&s_stYolov3SoftwareParam, sizeof(SAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S), 0,
3152         sizeof(SAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S));
3153     (HI_VOID)memset_s(&s_stYolov3Model, sizeof(SAMPLE_SVP_NNIE_MODEL_S), 0, sizeof(SAMPLE_SVP_NNIE_MODEL_S));
3154     SAMPLE_COMM_SVP_CheckSysExit();
3155     printf("\033[0;31mprogram termination abnormally!\033[0;39m\n");
3156 }
3157 
3158 /* function : show YOLOV3 sample(image 416x416 U8_C3) */
SAMPLE_SVP_NNIE_Yolov3(void)3159 void SAMPLE_SVP_NNIE_Yolov3(void)
3160 {
3161     const HI_CHAR *pcSrcFile = "./data/nnie_image/rgb_planar/dog_bike_car_416x416.bgr";
3162     const HI_CHAR *pcModelName = "./data/nnie_model/detection/inst_yolov3_cycle.wk";
3163     const HI_U32 u32PicNum = 1;
3164     HI_FLOAT f32PrintResultThresh = 0.0f;
3165     HI_S32 s32Ret = HI_SUCCESS;
3166     SAMPLE_SVP_NNIE_CFG_S stNnieCfg = { 0 };
3167     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S stInputDataIdx = { 0 };
3168     SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S stProcSegIdx = { 0 };
3169 
3170     /* Set configuration parameter */
3171     f32PrintResultThresh = 0.8f;
3172     stNnieCfg.pszPic = pcSrcFile;
3173     stNnieCfg.u32MaxInputNum = u32PicNum; // max input image num in each batch
3174     stNnieCfg.u32MaxRoiNum = 0;
3175     stNnieCfg.aenNnieCoreId[0] = SVP_NNIE_ID_0; // set NNIE core
3176     g_stop_signal = HI_FALSE;
3177 
3178     /* Sys init */
3179     s32Ret = SAMPLE_COMM_SVP_CheckSysInit();
3180     SAMPLE_SVP_CHECK_EXPR_RET_VOID(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
3181         "Error,SAMPLE_COMM_SVP_CheckSysInit failed!\n");
3182     if (g_stop_signal == HI_TRUE) {
3183         SAMPLE_SVP_NNIE_Yolov3_Stop();
3184         return;
3185     }
3186 
3187     /* Yolov3 Load model */
3188     SAMPLE_SVP_TRACE_INFO("Yolov3 Load model!\n");
3189     s32Ret = SAMPLE_COMM_SVP_NNIE_LoadModel(pcModelName, &s_stYolov3Model);
3190     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, YOLOV3_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3191         "Error,SAMPLE_COMM_SVP_NNIE_LoadModel failed!\n");
3192 
3193     /* Yolov3 parameter initialization */
3194     /* Yolov3 software parameters are set in SAMPLE_SVP_NNIE_Yolov3_SoftwareInit,
3195       if user has changed net struct, please make sure the parameter settings in
3196       SAMPLE_SVP_NNIE_Yolov3_SoftwareInit function are correct */
3197     SAMPLE_SVP_TRACE_INFO("Yolov3 parameter initialization!\n");
3198     if (g_stop_signal == HI_TRUE) {
3199         SAMPLE_SVP_NNIE_Yolov3_Stop();
3200         return;
3201     }
3202     s_stYolov3NnieParam.pstModel = &s_stYolov3Model.stModel;
3203     s32Ret = SAMPLE_SVP_NNIE_Yolov3_ParamInit(&stNnieCfg, &s_stYolov3NnieParam, &s_stYolov3SoftwareParam);
3204     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, YOLOV3_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3205         "Error,SAMPLE_SVP_NNIE_Yolov3_ParamInit failed!\n");
3206 
3207     /* Fill src data */
3208     SAMPLE_SVP_TRACE_INFO("Yolov3 start!\n");
3209     stInputDataIdx.u32SegIdx = 0;
3210     stInputDataIdx.u32NodeIdx = 0;
3211     if (g_stop_signal == HI_TRUE) {
3212         SAMPLE_SVP_NNIE_Yolov3_Stop();
3213         return;
3214     }
3215     s32Ret = SAMPLE_SVP_NNIE_FillSrcData(&stNnieCfg, &s_stYolov3NnieParam, &stInputDataIdx);
3216     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, YOLOV3_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3217         "Error,SAMPLE_SVP_NNIE_FillSrcData failed!\n");
3218 
3219     /* NNIE process(process the 0-th segment) */
3220     stProcSegIdx.u32SegIdx = 0;
3221     if (g_stop_signal == HI_TRUE) {
3222         SAMPLE_SVP_NNIE_Yolov3_Stop();
3223         return;
3224     }
3225     s32Ret = SAMPLE_SVP_NNIE_Forward(&s_stYolov3NnieParam, &stInputDataIdx, &stProcSegIdx, HI_TRUE);
3226     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, YOLOV3_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3227         "Error,SAMPLE_SVP_NNIE_Forward failed!\n");
3228 
3229     /* Software process */
3230     /* if user has changed net struct, please make sure SAMPLE_SVP_NNIE_Yolov3_GetResult
3231      function input data are correct */
3232     if (g_stop_signal == HI_TRUE) {
3233         SAMPLE_SVP_NNIE_Yolov3_Stop();
3234         return;
3235     }
3236     s32Ret = SAMPLE_SVP_NNIE_Yolov3_GetResult(&s_stYolov3NnieParam, &s_stYolov3SoftwareParam);
3237     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, YOLOV3_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3238         "Error,SAMPLE_SVP_NNIE_Yolov3_GetResult failed!\n");
3239 
3240     /* print result, this sample has 81 classes:
3241      class 0:background      class 1:person       class 2:bicycle         class 3:car            class 4:motorbike class
3242      5:aeroplane class 6:bus             class 7:train        class 8:truck           class 9:boat           class
3243      10:traffic light class 11:fire hydrant   class 12:stop sign   class 13:parking meter  class 14:bench         class
3244      15:bird class 16:cat            class 17:dog         class 18:horse          class 19:sheep         class 20:cow
3245      class 21:elephant       class 22:bear        class 23:zebra          class 24:giraffe       class 25:backpack
3246      class 26:umbrella       class 27:handbag     class 28:tie            class 29:suitcase      class 30:frisbee
3247      class 31:skis           class 32:snowboard   class 33:sports ball    class 34:kite          class 35:baseball bat
3248      class 36:baseball glove class 37:skateboard  class 38:surfboard      class 39:tennis racket class 40bottle
3249      class 41:wine glass     class 42:cup         class 43:fork           class 44:knife         class 45:spoon
3250      class 46:bowl           class 47:banana      class 48:apple          class 49:sandwich      class 50orange
3251      class 51:broccoli       class 52:carrot      class 53:hot dog        class 54:pizza         class 55:donut
3252      class 56:cake           class 57:chair       class 58:sofa           class 59:pottedplant   class 60bed
3253      class 61:diningtable    class 62:toilet      class 63:vmonitor       class 64:laptop        class 65:mouse
3254      class 66:remote         class 67:keyboard    class 68:cell phone     class 69:microwave     class 70:oven
3255      class 71:toaster        class 72:sink        class 73:refrigerator   class 74:book          class 75:clock
3256      class 76:vase           class 77:scissors    class 78:teddy bear     class 79:hair drier    class 80:toothbrush */
3257     SAMPLE_SVP_TRACE_INFO("Yolov3 result:\n");
3258     (void)SAMPLE_SVP_NNIE_Detection_PrintResult(&s_stYolov3SoftwareParam.stDstScore, &s_stYolov3SoftwareParam.stDstRoi,
3259         &s_stYolov3SoftwareParam.stClassRoiNum, f32PrintResultThresh);
3260 
3261 YOLOV3_FAIL_0:
3262     SAMPLE_SVP_NNIE_Yolov3_Deinit(&s_stYolov3NnieParam, &s_stYolov3SoftwareParam, &s_stYolov3Model);
3263     SAMPLE_COMM_SVP_CheckSysExit();
3264 }
3265 
3266 /* function : Yolov3 sample signal handle */
SAMPLE_SVP_NNIE_Yolov3_HandleSig(void)3267 void SAMPLE_SVP_NNIE_Yolov3_HandleSig(void)
3268 {
3269     g_stop_signal = HI_TRUE;
3270 }
3271 
3272 /* function : Lstm Deinit */
SAMPLE_SVP_NNIE_Lstm_Deinit(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParamm,SAMPLE_SVP_NNIE_MODEL_S * pstNnieModel)3273 static HI_S32 SAMPLE_SVP_NNIE_Lstm_Deinit(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParamm, SAMPLE_SVP_NNIE_MODEL_S *pstNnieModel)
3274 {
3275     HI_S32 s32Ret = HI_SUCCESS;
3276     /* hardware deinit */
3277     if (pstNnieParamm != NULL) {
3278         s32Ret = SAMPLE_COMM_SVP_NNIE_ParamDeinit(pstNnieParamm);
3279         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
3280             "Error,SAMPLE_COMM_SVP_NNIE_ParamDeinit failed!\n");
3281     }
3282     /* model deinit */
3283     if (pstNnieModel != NULL) {
3284         s32Ret = SAMPLE_COMM_SVP_NNIE_UnloadModel(pstNnieModel);
3285         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
3286             "Error,SAMPLE_COMM_SVP_NNIE_UnloadModel failed!\n");
3287     }
3288     return s32Ret;
3289 }
3290 
3291 /* function : Lstm init */
SAMPLE_SVP_NNIE_Lstm_ParamInit(SAMPLE_SVP_NNIE_CFG_S * pstNnieCfg,SAMPLE_SVP_NNIE_PARAM_S * pstLstmPara)3292 static HI_S32 SAMPLE_SVP_NNIE_Lstm_ParamInit(SAMPLE_SVP_NNIE_CFG_S *pstNnieCfg, SAMPLE_SVP_NNIE_PARAM_S *pstLstmPara)
3293 {
3294     HI_S32 s32Ret = HI_SUCCESS;
3295     /* init hardware para */
3296     s32Ret = SAMPLE_COMM_SVP_NNIE_ParamInit(pstNnieCfg, pstLstmPara);
3297     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3298         "Error(%#x),SAMPLE_COMM_SVP_NNIE_ParamInit failed!\n", s32Ret);
3299     return s32Ret;
3300 INIT_FAIL_0:
3301     s32Ret = SAMPLE_SVP_NNIE_Lstm_Deinit(pstLstmPara, NULL);
3302     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
3303         "Error(%#x),SAMPLE_SVP_NNIE_Lstm_Deinit failed!\n", s32Ret);
3304     return HI_FAILURE;
3305 }
SAMPLE_SVP_NNIE_Lstm_Stop(hi_void)3306 static hi_void SAMPLE_SVP_NNIE_Lstm_Stop(hi_void)
3307 {
3308     SAMPLE_SVP_NNIE_Lstm_Deinit(&s_stLstmNnieParam, &s_stLstmModel);
3309     (HI_VOID)memset_s(&s_stLstmNnieParam, sizeof(SAMPLE_SVP_NNIE_PARAM_S), 0, sizeof(SAMPLE_SVP_NNIE_PARAM_S));
3310     (HI_VOID)memset_s(&s_stLstmModel, sizeof(SAMPLE_SVP_NNIE_MODEL_S), 0, sizeof(SAMPLE_SVP_NNIE_MODEL_S));
3311     SAMPLE_COMM_SVP_CheckSysExit();
3312     printf("\033[0;31mprogram termination abnormally!\033[0;39m\n");
3313 }
3314 
3315 /* function : show Lstm sample(vector) */
SAMPLE_SVP_NNIE_Lstm(void)3316 void SAMPLE_SVP_NNIE_Lstm(void)
3317 {
3318     const HI_CHAR *apcSrcFile[3] = {"./data/nnie_image/vector/Seq.SEQ_S32",
3319                                     "./data/nnie_image/vector/Vec1.VEC_S32",
3320                                     "./data/nnie_image/vector/Vec2.VEC_S32"};
3321     const HI_CHAR *pchModelName = "./data/nnie_model/recurrent/lstm_3_3.wk";
3322     HI_U8 *pu8VirAddr = NULL;
3323     HI_U32 u32SegNum = 0;
3324     HI_U32 u32Step = 0;
3325     HI_U32 u32Offset = 0;
3326     HI_U32 u32TotalSize = 0;
3327     HI_U32 i = 0, j = 0;
3328     HI_S32 s32Ret = HI_SUCCESS;
3329     SAMPLE_SVP_NNIE_CFG_S stNnieCfg = { 0 };
3330     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S stInputDataIdx = { 0 };
3331     SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S stProcSegIdx = { 0 };
3332 
3333     /* Set configuration parameter */
3334     stNnieCfg.u32MaxInputNum = 16; // max input data num in each batch
3335     stNnieCfg.u32MaxRoiNum = 0;
3336     stNnieCfg.aenNnieCoreId[0] = SVP_NNIE_ID_0; // set NNIE core
3337     u32Step = 20;                               // time step
3338     g_stop_signal = HI_FALSE;
3339 
3340     /* Sys init */
3341     s32Ret = SAMPLE_COMM_SVP_CheckSysInit();
3342     SAMPLE_SVP_CHECK_EXPR_RET_VOID(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
3343         "Error,SAMPLE_COMM_SVP_CheckSysInit failed!\n");
3344     if (g_stop_signal == HI_TRUE) {
3345         SAMPLE_SVP_NNIE_Lstm_Stop();
3346         return;
3347     }
3348 
3349     /* Lstm Load model */
3350     SAMPLE_SVP_TRACE_INFO("Lstm Load model!\n");
3351     s32Ret = SAMPLE_COMM_SVP_NNIE_LoadModel(pchModelName, &s_stLstmModel);
3352     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, LSTM_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3353         "Error,SAMPLE_COMM_SVP_NNIE_LoadModel failed!\n");
3354 
3355     /* Lstm step initialization */
3356     u32SegNum = s_stLstmModel.stModel.u32NetSegNum;
3357     u32TotalSize = stNnieCfg.u32MaxInputNum * sizeof(HI_S32) * u32SegNum * 2;
3358     s32Ret = SAMPLE_COMM_SVP_MallocMem("SVP_NNIE_STEP", NULL, (HI_U64 *)&s_stLstmNnieParam.stStepBuf.u64PhyAddr,
3359         (void **)&pu8VirAddr, u32TotalSize);
3360     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, LSTM_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3361         "Error,Malloc memory failed!\n");
3362     /* Get step virtual addr */
3363     s_stLstmNnieParam.stStepBuf.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr);
3364     for (i = 0; i < u32SegNum * SAMPLE_SVP_NNIE_EACH_SEG_STEP_ADDR_NUM; i++) {
3365         stNnieCfg.au64StepVirAddr[i] =
3366             s_stLstmNnieParam.stStepBuf.u64VirAddr + i * stNnieCfg.u32MaxInputNum * sizeof(HI_S32);
3367     }
3368     /* Set step value, in this sample, the step values are set to be 20,
3369     if user has changed input network, please set correct step
3370     values according to the input network */
3371     for (i = 0; i < u32SegNum; i++) {
3372         u32Offset = i * SAMPLE_SVP_NNIE_EACH_SEG_STEP_ADDR_NUM;
3373         for (j = 0; j < stNnieCfg.u32MaxInputNum; j++) {
3374             *(SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_U32, stNnieCfg.au64StepVirAddr[u32Offset]) + j) =
3375                 u32Step; // step of input x_t
3376             *(SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_U32, stNnieCfg.au64StepVirAddr[u32Offset + 1]) + j) =
3377                 u32Step; // step of output h_t
3378         }
3379     }
3380 
3381     /* Lstm parameter initialization */
3382     if (g_stop_signal == HI_TRUE) {
3383         SAMPLE_SVP_NNIE_Lstm_Stop();
3384         return;
3385     }
3386     SAMPLE_SVP_TRACE_INFO("Lstm parameter initialization!\n");
3387     s_stLstmNnieParam.pstModel = &(s_stLstmModel.stModel);
3388     s32Ret = SAMPLE_SVP_NNIE_Lstm_ParamInit(&stNnieCfg, &s_stLstmNnieParam);
3389     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, LSTM_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3390         "Error,SAMPLE_SVP_NNIE_Lstm_ParamInit failed!\n");
3391 
3392     /* Fill src data, in this sample, the 0-th seg is lstm network,if user has
3393      changed input network,please make sure the value of stInputDataIdx.u32SegIdx
3394      is correct */
3395     SAMPLE_SVP_TRACE_INFO("Lstm start!\n");
3396     stInputDataIdx.u32SegIdx = 0;
3397     for (i = 0;
3398         i < (s_stLstmNnieParam.pstModel->astSeg[stInputDataIdx.u32SegIdx].u16SrcNum) && (g_stop_signal == HI_FALSE);
3399         i++) {
3400         stNnieCfg.pszPic = apcSrcFile[i];
3401         stInputDataIdx.u32NodeIdx = i;
3402         s32Ret = SAMPLE_SVP_NNIE_FillSrcData(&stNnieCfg, &s_stLstmNnieParam, &stInputDataIdx);
3403         SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, LSTM_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3404             "Error,SAMPLE_SVP_NNIE_FillSrcData failed!\n");
3405     }
3406     if (g_stop_signal == HI_TRUE) {
3407         SAMPLE_SVP_NNIE_Lstm_Stop();
3408         return;
3409     }
3410 
3411     /* NNIE process(process the 0-th segment) */
3412     stInputDataIdx.u32SegIdx = 0;
3413     stInputDataIdx.u32NodeIdx = 0;
3414     stProcSegIdx.u32SegIdx = 0;
3415     s32Ret = SAMPLE_SVP_NNIE_Forward(&s_stLstmNnieParam, &stInputDataIdx, &stProcSegIdx, HI_TRUE);
3416     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, LSTM_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3417         "Error,SAMPLE_SVP_NNIE_Forward failed!\n");
3418 
3419     /* print report result */
3420     if (g_stop_signal == HI_TRUE) {
3421         SAMPLE_SVP_NNIE_Lstm_Stop();
3422         return;
3423     }
3424     s32Ret = SAMPLE_SVP_NNIE_PrintReportResult(&s_stLstmNnieParam);
3425     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, LSTM_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3426         "Error,SAMPLE_SVP_NNIE_PrintReportResult failed!\n");
3427 
3428     SAMPLE_SVP_TRACE_INFO("Lstm is successfully processed!\n");
3429 
3430 LSTM_FAIL_0:
3431     SAMPLE_SVP_NNIE_Lstm_Deinit(&s_stLstmNnieParam, &s_stLstmModel);
3432     SAMPLE_COMM_SVP_CheckSysExit();
3433 }
3434 
3435 /* function : Lstm sample signal handle */
SAMPLE_SVP_NNIE_Lstm_HandleSig(void)3436 void SAMPLE_SVP_NNIE_Lstm_HandleSig(void)
3437 {
3438     g_stop_signal = HI_TRUE;
3439 }
3440 
3441 /* function : Pavnet software deinit */
SAMPLE_SVP_NNIE_Pvanet_SoftwareDeinit(SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S * pstSoftWareParam)3442 static HI_S32 SAMPLE_SVP_NNIE_Pvanet_SoftwareDeinit(SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S *pstSoftWareParam)
3443 {
3444     HI_S32 s32Ret = HI_SUCCESS;
3445     SAMPLE_SVP_CHECK_EXPR_RET(pstSoftWareParam == NULL, HI_INVALID_VALUE, SAMPLE_SVP_ERR_LEVEL_ERROR,
3446         "Error, pstSoftWareParam can't be NULL!\n");
3447     if ((pstSoftWareParam->stRpnTmpBuf.u64PhyAddr != 0) && (pstSoftWareParam->stRpnTmpBuf.u64VirAddr != 0)) {
3448         SAMPLE_SVP_MMZ_FREE(pstSoftWareParam->stRpnTmpBuf.u64PhyAddr, pstSoftWareParam->stRpnTmpBuf.u64VirAddr);
3449         pstSoftWareParam->stRpnTmpBuf.u64PhyAddr = 0;
3450         pstSoftWareParam->stRpnTmpBuf.u64VirAddr = 0;
3451     }
3452     return s32Ret;
3453 }
3454 
3455 /* function : Pvanet Deinit */
SAMPLE_SVP_NNIE_Pvanet_Deinit(SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S * pstSoftWareParam,SAMPLE_SVP_NNIE_MODEL_S * pstNnieModel)3456 static HI_S32 SAMPLE_SVP_NNIE_Pvanet_Deinit(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
3457     SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S *pstSoftWareParam, SAMPLE_SVP_NNIE_MODEL_S *pstNnieModel)
3458 {
3459     HI_S32 s32Ret = HI_SUCCESS;
3460     /* hardware deinit */
3461     if (pstNnieParam != NULL) {
3462         s32Ret = SAMPLE_COMM_SVP_NNIE_ParamDeinit(pstNnieParam);
3463         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
3464             "Error,SAMPLE_COMM_SVP_NNIE_ParamDeinit failed!\n");
3465     }
3466     /* software deinit */
3467     if (pstSoftWareParam != NULL) {
3468         s32Ret = SAMPLE_SVP_NNIE_Pvanet_SoftwareDeinit(pstSoftWareParam);
3469         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
3470             "Error,SAMPLE_SVP_NNIE_FasterRcnn_SoftwareDeinit failed!\n");
3471     }
3472     /* model deinit */
3473     if (pstNnieModel != NULL) {
3474         s32Ret = SAMPLE_COMM_SVP_NNIE_UnloadModel(pstNnieModel);
3475         SAMPLE_SVP_CHECK_EXPR_TRACE(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
3476             "Error,SAMPLE_COMM_SVP_NNIE_UnloadModel failed!\n");
3477     }
3478     return s32Ret;
3479 }
3480 
3481 /* function : Pvanet software para init */
SAMPLE_SVP_NNIE_Pvanet_SoftwareInit(SAMPLE_SVP_NNIE_CFG_S * pstCfg,SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S * pstSoftWareParam)3482 static HI_S32 SAMPLE_SVP_NNIE_Pvanet_SoftwareInit(SAMPLE_SVP_NNIE_CFG_S *pstCfg, SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
3483     SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S *pstSoftWareParam)
3484 {
3485     HI_U32 i = 0, j = 0;
3486     HI_U32 u32RpnTmpBufSize = 0;
3487     HI_U32 u32RpnBboxBufSize = 0;
3488     HI_U32 u32GetResultTmpBufSize = 0;
3489     HI_U32 u32DstRoiSize = 0;
3490     HI_U32 u32DstScoreSize = 0;
3491     HI_U32 u32ClassRoiNumSize = 0;
3492     HI_U32 u32ClassNum = 0;
3493     HI_U32 u32TotalSize = 0;
3494     HI_S32 s32Ret = HI_SUCCESS;
3495     HI_U64 u64PhyAddr = 0;
3496     HI_U8 *pu8VirAddr = NULL;
3497 
3498     /* RPN parameter init */
3499     /* The values of the following parameters are related to algorithm principles.
3500         For details, see related algorithms. */
3501     pstSoftWareParam->u32MaxRoiNum = pstCfg->u32MaxRoiNum;
3502     pstSoftWareParam->u32ClassNum = 21;
3503     pstSoftWareParam->u32NumRatioAnchors = 7;
3504     pstSoftWareParam->u32NumScaleAnchors = 6;
3505     pstSoftWareParam->au32Ratios[0] = (HI_S32)(0.333 * SAMPLE_SVP_QUANT_BASE);
3506     pstSoftWareParam->au32Ratios[1] = (HI_S32)(0.5 * SAMPLE_SVP_QUANT_BASE);
3507     pstSoftWareParam->au32Ratios[2] = (HI_S32)(0.667 * SAMPLE_SVP_QUANT_BASE);
3508     pstSoftWareParam->au32Ratios[3] = (HI_S32)(1 * SAMPLE_SVP_QUANT_BASE);
3509     pstSoftWareParam->au32Ratios[4] = (HI_S32)(1.5 * SAMPLE_SVP_QUANT_BASE);
3510     pstSoftWareParam->au32Ratios[5] = (HI_S32)(2 * SAMPLE_SVP_QUANT_BASE);
3511     pstSoftWareParam->au32Ratios[6] = (HI_S32)(3 * SAMPLE_SVP_QUANT_BASE);
3512 
3513     pstSoftWareParam->au32Scales[0] = 2 * SAMPLE_SVP_QUANT_BASE;
3514     pstSoftWareParam->au32Scales[1] = 3 * SAMPLE_SVP_QUANT_BASE;
3515     pstSoftWareParam->au32Scales[2] = 5 * SAMPLE_SVP_QUANT_BASE;
3516     pstSoftWareParam->au32Scales[3] = 9 * SAMPLE_SVP_QUANT_BASE;
3517     pstSoftWareParam->au32Scales[4] = 16 * SAMPLE_SVP_QUANT_BASE;
3518     pstSoftWareParam->au32Scales[5] = 32 * SAMPLE_SVP_QUANT_BASE;
3519 
3520     /* set origin image height & width from src[0] shape */
3521     pstSoftWareParam->u32OriImHeight = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Height;
3522     pstSoftWareParam->u32OriImWidth = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Width;
3523 
3524     pstSoftWareParam->u32MinSize = 16;
3525     pstSoftWareParam->u32SpatialScale = (HI_U32)(0.0625 * SAMPLE_SVP_QUANT_BASE);
3526     pstSoftWareParam->u32NmsThresh = (HI_U32)(0.7 * SAMPLE_SVP_QUANT_BASE);
3527     pstSoftWareParam->u32FilterThresh = 0;
3528     pstSoftWareParam->u32ValidNmsThresh = (HI_U32)(0.3 * SAMPLE_SVP_QUANT_BASE);
3529     pstSoftWareParam->u32NumBeforeNms = 12000;
3530     pstSoftWareParam->u32MaxRoiNum = 200;
3531 
3532     for (i = 0; i < pstSoftWareParam->u32ClassNum; i++) {
3533         pstSoftWareParam->au32ConfThresh[i] = 1;
3534     }
3535 
3536     pstSoftWareParam->stRpnBbox.enType = SVP_BLOB_TYPE_S32;
3537     pstSoftWareParam->stRpnBbox.unShape.stWhc.u32Chn = 1;
3538     pstSoftWareParam->stRpnBbox.unShape.stWhc.u32Height = pstCfg->u32MaxRoiNum;
3539     pstSoftWareParam->stRpnBbox.unShape.stWhc.u32Width = SAMPLE_SVP_COORDI_NUM;
3540     pstSoftWareParam->stRpnBbox.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(SAMPLE_SVP_COORDI_NUM * sizeof(HI_U32));
3541     pstSoftWareParam->stRpnBbox.u32Num = 1;
3542     for (i = 0; i < SAMPLE_SVP_NNIE_SEGMENT_NUM; i++) {
3543         for (j = 0; j < pstNnieParam->pstModel->astSeg[0].u16DstNum; j++) {
3544             if (strncmp(pstNnieParam->pstModel->astSeg[0].astDstNode[j].szName,
3545                 pstSoftWareParam->apcRpnDataLayerName[i], SVP_NNIE_NODE_NAME_LEN) == 0) {
3546                 pstSoftWareParam->aps32Conv[i] =
3547                     SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_S32, pstNnieParam->astSegData[0].astDst[j].u64VirAddr);
3548                 pstSoftWareParam->au32ConvHeight[i] =
3549                     pstNnieParam->pstModel->astSeg[0].astDstNode[j].unShape.stWhc.u32Height;
3550                 pstSoftWareParam->au32ConvWidth[i] =
3551                     pstNnieParam->pstModel->astSeg[0].astDstNode[j].unShape.stWhc.u32Width;
3552                 pstSoftWareParam->au32ConvChannel[i] =
3553                     pstNnieParam->pstModel->astSeg[0].astDstNode[j].unShape.stWhc.u32Chn;
3554                 break;
3555             }
3556         }
3557         SAMPLE_SVP_CHECK_EXPR_RET((j == pstNnieParam->pstModel->astSeg[0].u16DstNum), HI_FAILURE,
3558             SAMPLE_SVP_ERR_LEVEL_ERROR, "Error,failed to find report node %s!\n",
3559             pstSoftWareParam->apcRpnDataLayerName[i]);
3560         if (i == 0) {
3561             pstSoftWareParam->u32ConvStride = pstNnieParam->astSegData[0].astDst[j].u32Stride;
3562         }
3563     }
3564     /* calculate software mem size */
3565     u32ClassNum = pstSoftWareParam->u32ClassNum;
3566     u32RpnTmpBufSize = SAMPLE_SVP_NNIE_RpnTmpBufSize(pstSoftWareParam->u32NumRatioAnchors,
3567         pstSoftWareParam->u32NumScaleAnchors, pstSoftWareParam->au32ConvHeight[0], pstSoftWareParam->au32ConvWidth[0]);
3568     SAMPLE_SVP_CHECK_EXPR_RET(u32RpnTmpBufSize == 0, HI_ERR_SVP_NNIE_ILLEGAL_PARAM, SAMPLE_SVP_ERR_LEVEL_ERROR,
3569         "Error,SAMPLE_SVP_NNIE_RpnTmpBufSize failed!\n");
3570     u32RpnTmpBufSize = SAMPLE_SVP_NNIE_ALIGN16(u32RpnTmpBufSize);
3571     u32RpnBboxBufSize = pstSoftWareParam->stRpnBbox.u32Num * pstSoftWareParam->stRpnBbox.unShape.stWhc.u32Height *
3572         pstSoftWareParam->stRpnBbox.u32Stride;
3573     u32GetResultTmpBufSize = SAMPLE_SVP_NNIE_Pvanet_GetResultTmpBufSize(pstCfg->u32MaxRoiNum, u32ClassNum);
3574     SAMPLE_SVP_CHECK_EXPR_RET(u32GetResultTmpBufSize == 0, HI_ERR_SVP_NNIE_ILLEGAL_PARAM, SAMPLE_SVP_ERR_LEVEL_ERROR,
3575         "Error,SAMPLE_SVP_NNIE_Pvanet_GetResultTmpBufSize failed!\n");
3576     u32GetResultTmpBufSize = SAMPLE_SVP_NNIE_ALIGN16(u32GetResultTmpBufSize);
3577     u32DstRoiSize =
3578         SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstCfg->u32MaxRoiNum * sizeof(HI_U32) * SAMPLE_SVP_COORDI_NUM);
3579     u32DstScoreSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstCfg->u32MaxRoiNum * sizeof(HI_U32));
3580     u32ClassRoiNumSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * sizeof(HI_U32));
3581     u32TotalSize = u32RpnTmpBufSize + u32RpnBboxBufSize + u32GetResultTmpBufSize + u32DstRoiSize + u32DstScoreSize +
3582         u32ClassRoiNumSize;
3583 
3584     /* malloc mem */
3585     s32Ret = SAMPLE_COMM_SVP_MallocCached("SAMPLE_Pvanet_INIT", NULL, (HI_U64 *)&u64PhyAddr, (void **)&pu8VirAddr,
3586         u32TotalSize);
3587     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
3588         "Error,Malloc memory failed!\n");
3589     (HI_VOID)memset_s(pu8VirAddr, u32TotalSize, 0, u32TotalSize);
3590     SAMPLE_COMM_SVP_FlushCache(u64PhyAddr, (void *)pu8VirAddr, u32TotalSize);
3591 
3592     /* set addr */
3593     pstSoftWareParam->stRpnTmpBuf.u64PhyAddr = u64PhyAddr;
3594     pstSoftWareParam->stRpnTmpBuf.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr);
3595     pstSoftWareParam->stRpnTmpBuf.u32Size = u32RpnTmpBufSize;
3596 
3597     pstSoftWareParam->stRpnBbox.u64PhyAddr = u64PhyAddr + u32RpnTmpBufSize;
3598     pstSoftWareParam->stRpnBbox.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr) + u32RpnTmpBufSize;
3599 
3600     pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr = u64PhyAddr + u32RpnBboxBufSize + u32RpnTmpBufSize;
3601     pstSoftWareParam->stGetResultTmpBuf.u64VirAddr =
3602         (HI_U64)((HI_UINTPTR_T)pu8VirAddr + u32RpnBboxBufSize + u32RpnTmpBufSize);
3603     pstSoftWareParam->stGetResultTmpBuf.u32Size = u32GetResultTmpBufSize;
3604 
3605     pstSoftWareParam->stDstRoi.enType = SVP_BLOB_TYPE_S32;
3606     pstSoftWareParam->stDstRoi.u64PhyAddr = u64PhyAddr + u32RpnBboxBufSize + u32RpnTmpBufSize + u32GetResultTmpBufSize;
3607     pstSoftWareParam->stDstRoi.u64VirAddr =
3608         (HI_U64)((HI_UINTPTR_T)pu8VirAddr + u32RpnBboxBufSize + u32RpnTmpBufSize + u32GetResultTmpBufSize);
3609     pstSoftWareParam->stDstRoi.u32Stride =
3610         SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstSoftWareParam->u32MaxRoiNum * sizeof(HI_U32) * SAMPLE_SVP_COORDI_NUM);
3611     pstSoftWareParam->stDstRoi.u32Num = 1;
3612     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Chn = 1;
3613     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Height = 1;
3614     pstSoftWareParam->stDstRoi.unShape.stWhc.u32Width =
3615         u32ClassNum * pstSoftWareParam->u32MaxRoiNum * SAMPLE_SVP_COORDI_NUM;
3616 
3617     pstSoftWareParam->stDstScore.enType = SVP_BLOB_TYPE_S32;
3618     pstSoftWareParam->stDstScore.u64PhyAddr =
3619         u64PhyAddr + u32RpnBboxBufSize + u32RpnTmpBufSize + u32GetResultTmpBufSize + u32DstRoiSize;
3620     pstSoftWareParam->stDstScore.u64VirAddr = (HI_U64)((HI_UINTPTR_T)pu8VirAddr + u32RpnBboxBufSize + u32RpnTmpBufSize +
3621         u32GetResultTmpBufSize + u32DstRoiSize);
3622     pstSoftWareParam->stDstScore.u32Stride =
3623         SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * pstSoftWareParam->u32MaxRoiNum * sizeof(HI_U32));
3624     pstSoftWareParam->stDstScore.u32Num = 1;
3625     pstSoftWareParam->stDstScore.unShape.stWhc.u32Chn = 1;
3626     pstSoftWareParam->stDstScore.unShape.stWhc.u32Height = 1;
3627     pstSoftWareParam->stDstScore.unShape.stWhc.u32Width = u32ClassNum * pstSoftWareParam->u32MaxRoiNum;
3628 
3629     pstSoftWareParam->stClassRoiNum.enType = SVP_BLOB_TYPE_S32;
3630     pstSoftWareParam->stClassRoiNum.u64PhyAddr =
3631         u64PhyAddr + u32RpnBboxBufSize + u32RpnTmpBufSize + u32GetResultTmpBufSize + u32DstRoiSize + u32DstScoreSize;
3632     pstSoftWareParam->stClassRoiNum.u64VirAddr = SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(HI_U64,
3633         pu8VirAddr + u32RpnBboxBufSize + u32RpnTmpBufSize + u32GetResultTmpBufSize + u32DstRoiSize + u32DstScoreSize);
3634     pstSoftWareParam->stClassRoiNum.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum * sizeof(HI_U32));
3635     pstSoftWareParam->stClassRoiNum.u32Num = 1;
3636     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Chn = 1;
3637     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Height = 1;
3638     pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Width = u32ClassNum;
3639 
3640     return s32Ret;
3641 }
3642 
3643 /* function : Pvanet parameter initialization */
SAMPLE_SVP_NNIE_Pvanet_ParamInit(SAMPLE_SVP_NNIE_CFG_S * pstFasterRcnnCfg,SAMPLE_SVP_NNIE_PARAM_S * pstNnieParam,SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S * pstSoftWareParam)3644 static HI_S32 SAMPLE_SVP_NNIE_Pvanet_ParamInit(SAMPLE_SVP_NNIE_CFG_S *pstFasterRcnnCfg,
3645     SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S *pstSoftWareParam)
3646 {
3647     HI_S32 s32Ret = HI_SUCCESS;
3648     /* init hardware parameter */
3649     s32Ret = SAMPLE_COMM_SVP_NNIE_ParamInit(pstFasterRcnnCfg, pstNnieParam);
3650     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3651         "Error(%#x),SAMPLE_COMM_SVP_NNIE_ParamInit failed!\n", s32Ret);
3652 
3653     /* init software parameter */
3654     s32Ret = SAMPLE_SVP_NNIE_Pvanet_SoftwareInit(pstFasterRcnnCfg, pstNnieParam, pstSoftWareParam);
3655     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, INIT_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3656         "Error(%#x),SAMPLE_SVP_NNIE_Pvanet_SoftwareInit failed!\n", s32Ret);
3657 
3658     return s32Ret;
3659 INIT_FAIL_0:
3660     s32Ret = SAMPLE_SVP_NNIE_FasterRcnn_Deinit(pstNnieParam, pstSoftWareParam, NULL);
3661     SAMPLE_SVP_CHECK_EXPR_RET(s32Ret != HI_SUCCESS, s32Ret, SAMPLE_SVP_ERR_LEVEL_ERROR,
3662         "Error(%#x),SAMPLE_SVP_NNIE_FasterRcnn_Deinit failed!\n", s32Ret);
3663     return HI_FAILURE;
3664 }
SAMPLE_SVP_NNIE_Pvanet_Stop(hi_void)3665 static hi_void SAMPLE_SVP_NNIE_Pvanet_Stop(hi_void)
3666 {
3667     SAMPLE_SVP_NNIE_FasterRcnn_Deinit(&s_stPvanetNnieParam, &s_stPvanetSoftwareParam, &s_stPvanetModel);
3668     (HI_VOID)memset_s(&s_stPvanetNnieParam, sizeof(SAMPLE_SVP_NNIE_PARAM_S), 0, sizeof(SAMPLE_SVP_NNIE_PARAM_S));
3669     (HI_VOID)memset_s(&s_stPvanetSoftwareParam, sizeof(SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S), 0,
3670         sizeof(SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S));
3671     (HI_VOID)memset_s(&s_stPvanetModel, sizeof(SAMPLE_SVP_NNIE_MODEL_S), 0, sizeof(SAMPLE_SVP_NNIE_MODEL_S));
3672     SAMPLE_COMM_SVP_CheckSysExit();
3673     printf("\033[0;31mprogram termination abnormally!\033[0;39m\n");
3674 }
3675 
3676 /* function : show Pvanet fasterRcnn sample(image 224x224 U8_C3) */
SAMPLE_SVP_NNIE_Pvanet(void)3677 void SAMPLE_SVP_NNIE_Pvanet(void)
3678 {
3679     const HI_CHAR *pcSrcFile = "./data/nnie_image/rgb_planar/horse_dog_car_person_224x224.bgr";
3680     const HI_CHAR *pcModelName = "./data/nnie_model/detection/inst_fasterrcnn_pvanet_inst.wk";
3681     const HI_U32 u32PicNum = 1;
3682     HI_FLOAT f32PrintResultThresh = 0.0f;
3683     HI_S32 s32Ret = HI_SUCCESS;
3684     HI_U32 i = 0;
3685     SAMPLE_SVP_NNIE_CFG_S stNnieCfg = { 0 };
3686     SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S stInputDataIdx = { 0 };
3687     SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S stProcSegIdx = { 0 };
3688 
3689     /* Set configuration parameter */
3690     s_enNetType = SAMPLE_SVP_NNIE_PVANET_FASTER_RCNN;
3691     f32PrintResultThresh = 0.8f;
3692     stNnieCfg.u32MaxInputNum = u32PicNum;
3693     stNnieCfg.u32MaxRoiNum = 200;
3694     stNnieCfg.pszPic = pcSrcFile;
3695     stNnieCfg.aenNnieCoreId[0] = SVP_NNIE_ID_0;
3696     stNnieCfg.aenNnieCoreId[1] = SVP_NNIE_ID_0;
3697     g_stop_signal = HI_FALSE;
3698 
3699     /* Sys_init */
3700     s32Ret = SAMPLE_COMM_SVP_CheckSysInit();
3701     SAMPLE_SVP_CHECK_EXPR_RET_VOID(s32Ret != HI_SUCCESS, SAMPLE_SVP_ERR_LEVEL_ERROR,
3702         "Error,SAMPLE_COMM_SVP_CheckSysInit failed!\n");
3703     if (g_stop_signal == HI_TRUE) {
3704         SAMPLE_SVP_NNIE_Pvanet_Stop();
3705         return;
3706     }
3707 
3708     /* FasterRcnn Load model */
3709     SAMPLE_SVP_TRACE_INFO("Pvanet load Model!!\n");
3710     s32Ret = SAMPLE_COMM_SVP_NNIE_LoadModel(pcModelName, &s_stPvanetModel);
3711     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, PVANET_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3712         "ERROR, SAMPLE_COMM_SVP_NNIE_LoadModel failed\n");
3713 
3714     /* Pvanet para init */
3715     /* apcRpnDataLayerName is used to set RPN data layer name
3716        and search RPN input data,if user has changed network struct, please
3717        make sure the data layer names are correct */
3718     /* Pvanet parameters are set in SAMPLE_SVP_NNIE_Pvanet_SoftwareInit,
3719      if user has changed network struct, please make sure the parameter settings in
3720      SAMPLE_SVP_NNIE_FasterRcnn_SoftwareInit function are correct */
3721     SAMPLE_SVP_TRACE_INFO("Pvanet parameter initialization!\n");
3722     s_stPvanetNnieParam.pstModel = &s_stPvanetModel.stModel;
3723     s_stPvanetSoftwareParam.apcRpnDataLayerName[0] = "rpn_cls_score";
3724     s_stPvanetSoftwareParam.apcRpnDataLayerName[1] = "rpn_bbox_pred";
3725     if (g_stop_signal == HI_TRUE) {
3726         SAMPLE_SVP_NNIE_Pvanet_Stop();
3727         return;
3728     }
3729     s32Ret = SAMPLE_SVP_NNIE_Pvanet_ParamInit(&stNnieCfg, &s_stPvanetNnieParam, &s_stPvanetSoftwareParam);
3730     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, PVANET_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3731         "Error,SAMPLE_SVP_NNIE_Pvanet_ParamInit failed!\n");
3732 
3733     /* Fill 0-th input node of 0-th seg */
3734     SAMPLE_SVP_TRACE_INFO("Pvanet start!\n");
3735     if (g_stop_signal == HI_TRUE) {
3736         SAMPLE_SVP_NNIE_Pvanet_Stop();
3737         return;
3738     }
3739     stInputDataIdx.u32SegIdx = 0;
3740     stInputDataIdx.u32NodeIdx = 0;
3741     s32Ret = SAMPLE_SVP_NNIE_FillSrcData(&stNnieCfg, &s_stPvanetNnieParam, &stInputDataIdx);
3742     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, PVANET_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3743         "ERROR, SAMPLE_SVP_NNIE_FillSrcData Failed!!\n");
3744 
3745     /* NNIE process 0-th seg */
3746     stProcSegIdx.u32SegIdx = 0;
3747     if (g_stop_signal == HI_TRUE) {
3748         SAMPLE_SVP_NNIE_Pvanet_Stop();
3749         return;
3750     }
3751     s32Ret = SAMPLE_SVP_NNIE_Forward(&s_stPvanetNnieParam, &stInputDataIdx, &stProcSegIdx, HI_TRUE);
3752     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, PVANET_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3753         "Error,SAMPLE_SVP_NNIE_Forward failed!\n");
3754 
3755     /* Do RPN */
3756     if (g_stop_signal == HI_TRUE) {
3757         SAMPLE_SVP_NNIE_Pvanet_Stop();
3758         return;
3759     }
3760     s32Ret = SAMPLE_SVP_NNIE_Pvanet_Rpn(&s_stPvanetSoftwareParam);
3761     SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, PVANET_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3762         "Error,SAMPLE_SVP_NNIE_Pvanet_Rpn failed!\n");
3763     if (s_stPvanetSoftwareParam.stRpnBbox.unShape.stWhc.u32Height != 0 && g_stop_signal == HI_FALSE) {
3764         /* NNIE process 1-th seg, the input conv data comes from 0-th seg's 0-th report node,
3765           the input roi comes from RPN results */
3766         stInputDataIdx.u32NodeIdx = 0;
3767         stInputDataIdx.u32SegIdx = 0;
3768         stProcSegIdx.u32SegIdx = 1;
3769         s32Ret = SAMPLE_SVP_NNIE_ForwardWithBbox(&s_stPvanetNnieParam, &stInputDataIdx,
3770             &s_stPvanetSoftwareParam.stRpnBbox, &stProcSegIdx, HI_TRUE);
3771         SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, PVANET_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3772             "Error,SAMPLE_SVP_NNIE_ForwardWithBbox failed!\n");
3773 
3774         /* GetResult */
3775         /* if user has changed net struct, please make sure SAMPLE_SVP_NNIE_FasterRcnn_GetResult
3776          function's input data are correct */
3777         s32Ret = SAMPLE_SVP_NNIE_Pvanet_GetResult(&s_stPvanetNnieParam, &s_stPvanetSoftwareParam);
3778         SAMPLE_SVP_CHECK_EXPR_GOTO(s32Ret != HI_SUCCESS, PVANET_FAIL_0, SAMPLE_SVP_ERR_LEVEL_ERROR,
3779             "ERROR, SAMPLE_SVP_NNIE_Pvanet_GetResult Failed!\n");
3780     } else {
3781         for (i = 0; i < s_stPvanetSoftwareParam.stClassRoiNum.unShape.stWhc.u32Width; i++) {
3782             *(SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(HI_U32, s_stPvanetSoftwareParam.stClassRoiNum.u64VirAddr) + i) = 0;
3783         }
3784     }
3785 
3786     /* print result, this sample has 21 classes:
3787      class 0:background     class 1:plane           class 2:bicycle
3788      class 3:bird           class 4:boat            class 5:bottle
3789      class 6:bus            class 7:car             class 8:cat
3790      class 9:chair          class10:cow             class11:diningtable
3791      class 12:dog           class13:horse           class14:motorbike
3792      class 15:person        class16:pottedplant     class17:sheep
3793      class 18:sofa          class19:train           class20:tvmonitor */
3794     if (g_stop_signal == HI_TRUE) {
3795         SAMPLE_SVP_NNIE_Pvanet_Stop();
3796         return;
3797     }
3798     SAMPLE_SVP_TRACE_INFO("Pvanet result:\n");
3799     (void)SAMPLE_SVP_NNIE_Detection_PrintResult(&s_stPvanetSoftwareParam.stDstScore, &s_stPvanetSoftwareParam.stDstRoi,
3800         &s_stPvanetSoftwareParam.stClassRoiNum, f32PrintResultThresh);
3801 
3802 PVANET_FAIL_0:
3803     SAMPLE_SVP_NNIE_Pvanet_Deinit(&s_stPvanetNnieParam, &s_stPvanetSoftwareParam, &s_stPvanetModel);
3804 
3805     SAMPLE_COMM_SVP_CheckSysExit();
3806 }
3807 
SAMPLE_SVP_NNIE_Pvanet_HandleSig(void)3808 void SAMPLE_SVP_NNIE_Pvanet_HandleSig(void)
3809 {
3810     g_stop_signal = HI_TRUE;
3811 }
3812