一、运行docker

docker run -t -i --privileged -v /dev/bus/usb:/dev/bus/usb rknn-toolkit:1.7.1 /bin/bash

二、使用步骤

1.使用https://netron.app/查看模型的输入及输出

在这里插入图片描述

2.设置转换模型参数

test.py代码如下:
注:量化模型输入数据使用人脸检测后人脸小图

import os
import urllib
import traceback
import time
import sys
import numpy as np
import cv2
from rknn.api import RKNN
import time

ONNX_MODEL = 'w600k_r50_sim.onnx'
RKNN_MODEL = 'w600k_r50_sim.rknn'

def norm(img):
    mean = 127.5
    std = 128
    img_data = (img.astype(np.float32)/255 - mean) / std
    return img_data


if __name__ == '__main__':
    # Create RKNN object
    #rknn = RKNN(verbose = True)
    rknn = RKNN()

    # Load ONNX model
    print('--> Loading model')
    ret = rknn.load_onnx(model=ONNX_MODEL,outputs=['683'])
    if ret != 0:
        print('Load failed!')
        exit(ret)
    print('done')

    # pre-process config
    print('--> Config model')
    rknn.config(batch_size=10,target_platform=['rv1126'],mean_values=[[127.5, 127.5, 127.5]], std_values=[[128.0,128.0, 128.0]])
    print('done')

    # Build model
    print('--> Building model')
    t1 =time.time()
    ret = rknn.build(do_quantization=True,dataset='./test.txt',pre_compile=True)
    t2 = time.time() - t1
    print("t2 = ",t2)
    
    if ret != 0:
        print('Build failed!')
        exit(ret)
    print('done')

    # Export RKNN model
    print('--> Export RKNN model')
    ret = rknn.export_rknn(RKNN_MODEL)
    if ret != 0:
        print('Export failed!')
        exit(ret)
    print('done')
       
    print('--> Accuracy analysis')
    rknn.accuracy_analysis(inputs='./test.txt', target='rv1126')
    print('done')
    
    rknn.release()

3. 运行文件生成rknn模型文件

python test.py

4. 测试

run.py代码如下:

import numpy as np
from numpy.linalg import norm as l2norm
import cv2
from PIL import Image
from rknn.api import RKNN


def load_model():
        rknn = RKNN(verbose = True)
        print('-->loading model')
        #rknn.load_rknn('./det_10g.rknn')
        rknn.load_rknn('./w600k_r50_sim.rknn')
        print('loading model done')

        print('--> Init runtime environment')
        ret = rknn.init_runtime(target='rv1126')
        if ret != 0:
                print('Init runtime environment failed')
                exit(ret)
        print('done')
        return rknn

def detect(img):
    det_img = np.asarray(img)  
    #det_img = det_img.astype(np.float32)
    #det_img = (det_img -127.5)/128.0
    dim = (112, 112)
    # resize image
    det_img = cv2.resize(det_img, dim, interpolation = cv2.INTER_AREA)
    det_img1 = np.asarray(det_img) 
    print('--> inter')
    #print(x2)
    #增加一个维度
    det_img1 = det_img1[:, :, :, np.newaxis]
    #转换为模型需要的输入维度(1, 3, 1088, 1920)
    det_img1 = det_img1.transpose([3, 2, 0, 1])
    #print(det_img)
 # Inference
    print('--> Running model')
    net_outs = rknn.inference(inputs=det_img1,data_format="nchw")
    
    #net_outs = rknn.inference(inputs=[det_img],data_format="nchw")
    print('--> inputs')
    
    net_outs1 = l2norm(net_outs)
    print(type(net_outs1))
    print(net_outs1)
    print('outputs over!!!!!!')
    return net_outs/net_outs1


if __name__ == '__main__':
    rknn = load_model()


    im = cv2.imread('./data/face.jpg')
    net_outs = detect(im)


    im1 = cv2.imread('./data/face1.jpg')
    net_outs1 = detect(im1)
    

    im2 = cv2.imread('./data/face2.jpg')
    net_outs2 = detect(im2)
    

    score = np.dot(net_outs[0], net_outs[0].T) 
    print('score01 = ',score)

    score1 = np.dot(net_outs1[0], net_outs2[0].T) 
    print('score02 = ',score1)
    rknn.release()

5. 运行测试

python run.py

总结

人脸检测会输出5个特征点,人脸小图和5个特征点通过变换可对人脸进行校正,将校正的数据输入模型输出512个特征点,再通过点乘获得两张人脸得分。