Toybrick

标题: caffe人形检测模型转换rknn之后精度降低 [打印本页]

作者: zxs    时间: 2019-11-4 16:12
标题: caffe人形检测模型转换rknn之后精度降低
caffe模型转成rknn未量化模型后,推理出来的结果出现了精度下降的问题。希望能够得到回复,谢谢。附上推理和转换模型代码。
# -*- coding: utf-8 -*-

"""
detection head of caffe model by cv2.dnn
"""

import numpy as np
import cv2
from cv2 import dnn
from rknn.api import RKNN


def caffe2rknn():
    # 创建RKNN执行对象
    rknn = RKNN(verbose=False)
    rknn.config(batch_size=20,
                channel_mean_value='127.5 127.5 127.5 127.5',
                reorder_channel='2 1 0',
                epochs=10,
                quantized_dtype='asymmetric_quantized-u8'
                )
    print('--> Loading Caffe model')
    rknn.load_caffe(model='./ASSD-person.prototxt',
                    proto='caffe',
                    blobs='./ASSD-person.caffemodel')
    print('done')

    # 创建解析pb模型
    print('--> Building model')
    ret = rknn.build(do_quantization=False)
    print('done')
    # 导出保存rknn模型文件
    rknn.export_rknn('./Person_ASSD_noquantization-caffe.rknn')

    # Release RKNN Context
    rknn.release()


def detection_by_cv2(img):
    img = cv2.imread(img)
    img = cv2.resize(img, (1159, 655))
    output_blob_name = ["conv10_3", "conv13_3", "conv15_3", "conv16_3"]
    blob = dnn.blobFromImage(img, 1 / 127.5, (1159, 655), (127.5, 127.5, 127.5))
    print("Caffe-Input:", blob.shape, blob.dtype)
    net = dnn.readNetFromCaffe('./ASSD-person.prototxt',
                               './ASSD-person.caffemodel')
    net.setInput(blob)
    outputs = net.forward(output_blob_name)
    outputs = [i.ravel() for i in outputs]
    print("Caffe-Output:", len(outputs), outputs[1].shape)
    return outputs


def rknn_detection(image):
    rknn = RKNN()
    print('--> Load RKNN Model')
    ret = rknn.load_rknn('./Person_ASSD_noquantization-caffe.rknn')
    if ret != 0:
        print('load rknn model failed')
        exit(ret)
    else:
        print('load rknn model done')
    print('---->begin init runtime')
    ret = rknn.init_runtime(target=None)
    if ret == 0:
        print('init run time success')
    else:
        print('init runtime failed')

    print('Class Asssd successfully')
    # Set inputs
    img = cv2.imread(image)
    img = cv2.resize(img, (1159, 655))
    img = img.astype(np.float32)
    input_image = img[np.newaxis, :, :, :]
    # Inference
    print('--> Running rknn model')
    # outputs
    outputs = rknn.inference(inputs=[input_image], data_type='float32')
    print('rknn-outputs-->', len(outputs), outputs[0].shape)
    rknn.release()
    return outputs


def check_caffe_rknn_output(img):
    """4outputs"""
    outputs_caffe = detection_by_cv2(img)
    outputs_rknn = rknn_detection(img)
    for ca_val, rk_val in zip(outputs_caffe, outputs_rknn):
        print('========================================================')
        print('caffe_model--->', ca_val)
        print('rknn_model---->', rk_val)
        print('rk - ca --->', ca_val - rk_val)
        print('Max: %0.3f.' % np.max(np.abs(ca_val - rk_val)))
        print('Sum: %0.3f ' % np.sum((ca_val - rk_val) ** 2))


if __name__ == "__main__":
    img = './a.jpg'
    check_caffe_rknn_output(img)


作者: zxs    时间: 2019-11-4 16:31
http://t.rock-chips.com/forum.ph ... tid=1030&extra=
转到这个帖子,附上模型文件




欢迎光临 Toybrick (https://t.rock-chips.com/) Powered by Discuz! X3.3