第四天

第四天

准备先将海思给的SSD的nnie的例子跑起来


SSD是原版的vgg16作为主干网络的例子,官方给出的wk文件是已经经过转化之后的

但是给的例子中读取图片是加载的bgr文件,这样测试起来非常不方便,所以用opencv 重新写了读图片的接口。

	IplImage *imgSrc = 0;
    imgSrc = cvLoadImage(pcSrcFile, 1);
        if (imgSrc == 0) {
        printf("Load image error\n");
        return -1;
    }
    //--resize to 300*300
    IplImage *img;
    CvSize dstSize;
    dstSize.height = 300;
    dstSize.width = 300;
    img = cvCreateImage(dstSize,imgSrc->depth,imgSrc->nChannels);
    cvResize(imgSrc,img,CV_INTER_CUBIC);

    HI_U8 *data = (HI_U8*)img->imageData;
    int step = img->widthStep;
    int h = img->height;
    int w = img->width;
    int c = img->nChannels;

    IplImage *bgrImg = 0;
    CvSize sz;
    sz.width = w;
    sz.height = h;
    bgrImg = cvCreateImage(sz,img->depth,img->nChannels);
    HI_U8* bgr = (HI_U8*)bgrImg->imageData;
    int offset = 0;
        //注意遍历顺序
    for (int k=0; k

这样将jpg图片转为了bgr格式图片,然后将SAMPLE_SVP_NNIE_FillSrcData函数改写了

static HI_S32 SAMPLE_SVP_NNIE_FillSrcDataFromFrame(HI_U8* pImage,
    SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S* pstInputDataIdx)
{
    HI_U32 i =0, j = 0, n = 0;
    HI_U32 u32Height = 0, u32Width = 0, u32Chn = 0, u32Stride = 0, u32Dim = 0;
    HI_U32 u32VarSize = 0;
    HI_S32 s32Ret = HI_SUCCESS;
    HI_U8*pu8PicAddr = NULL;
    HI_U32*pu32StepAddr = NULL;
    HI_U32 u32SegIdx = pstInputDataIdx->u32SegIdx;
    HI_U32 u32NodeIdx = pstInputDataIdx->u32NodeIdx;
    HI_U32 u32TotalStepNum = 0;
	HI_U32 u32SrcPerLine = 0;

	if(NULL == pImage)
	{
		SAMPLE_SVP_CHECK_EXPR_GOTO(1 != s32Ret,FAIL,SAMPLE_SVP_ERR_LEVEL_ERROR,"Error, input image is NULL!\n");
	}

    /*get data size*/
    if(SVP_BLOB_TYPE_U8 <= pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].enType &&
        SVP_BLOB_TYPE_YVU422SP >= pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].enType)
    {
        u32VarSize = sizeof(HI_U8);
    }
    else
    {
        u32VarSize = sizeof(HI_U32);
    }

    /*fill src data*/
    if(SVP_BLOB_TYPE_SEQ_S32 == pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].enType)
    {
        u32Dim = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stSeq.u32Dim;
        u32Stride = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Stride;
        pu32StepAddr = (HI_U32*)(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stSeq.u64VirAddrStep);
        pu8PicAddr = (HI_U8*)(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr);

		u32SrcPerLine = u32Dim*u32VarSize;
		
        for(n = 0; n < pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Num; n++)
        {
            for(i = 0;i < *(pu32StepAddr+n); i++)
            {
                //s32Ret = fread(pu8PicAddr,u32Dim*u32VarSize,1,fp);
                memcpy(pu8PicAddr,pImage,u32SrcPerLine);
                printf("mem copy\n");
				pImage += u32SrcPerLine;
                pu8PicAddr += u32Stride;
            }
            u32TotalStepNum += *(pu32StepAddr+n);
        }
        SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64PhyAddr,
            (HI_VOID *) pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr,
            u32TotalStepNum*u32Stride);
    }
    else
    {
        u32Height = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stWhc.u32Height;
        u32Width = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stWhc.u32Width;
        u32Chn = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stWhc.u32Chn;
        u32Stride = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Stride;
        pu8PicAddr = (HI_U8*)(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr);

		u32SrcPerLine = u32Width*u32VarSize;
        for(n = 0; n < pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Num; n++)
        {
            for(i = 0;i < u32Chn; i++)
            {
                for(j = 0; j < u32Height; j++)
                {
                    //s32Ret = fread(pu8PicAddr,u32Width*u32VarSize,1,fp);
					memcpy(pu8PicAddr,pImage,u32SrcPerLine);
					pImage += u32SrcPerLine;
                    pu8PicAddr += u32Stride;
                }
            }
        }		
		
        SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64PhyAddr,
            (HI_VOID *) pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr,
            pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Num*u32Chn*u32Height*u32Stride);
    }


    return HI_SUCCESS;
	
FAIL:
    return HI_FAILURE;
    
}

这样就可以直接使用opencv读图片了。

sample中SSD的例子,在nnie下运行时间不太稳定,也不太快,根本没法达到要求

所以接下来的事情就是自己训练一个小网络,转nnie模型,争取达到200fps。

你可能感兴趣的:(Hi3559)