RK3568笔记三:部署ResNet50模型

若该文为原创文章,转载请注明原文出处。

通过ResNet50网络训练了识别10类车的模型并成功了转换成了onnx模型

具体训练过程可以参考文章AI项目十七:ResNet50训练部署教程-CSDN博客

这里部署使用rknn-toolkit2工具转换成RKNN模型并测试

rknn-toolkit2工具安装在前面文章有説明了,自行安装。

接下来测试并转成RKNN模型

一、onnx转成rknn模型

在rknn-toolkit2-master/examples/onnx目录下创建04_resnet50目录。

在04_resnet50目录下创建test.py文本

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

ONNX_MODEL = '10class_ResNet50.onnx'
RKNN_MODEL = '10class_ResNet50.rknn'


def show_outputs(outputs):
    output = outputs[0][0]
    output_sorted = sorted(output, reverse=True)
    top5_str = 'resnet50v2\n-----TOP 5-----\n'
    for i in range(5):
        value = output_sorted[i]
        index = np.where(output == value)
        for j in range(len(index)):
            if (i + j) >= 5:
                break
            if value > 0:
                topi = '{}: {}\n'.format(index[j], value)
            else:
                topi = '-1: 0.0\n'
            top5_str += topi
    print(top5_str)


def readable_speed(speed):
    speed_bytes = float(speed)
    speed_kbytes = speed_bytes / 1024
    if speed_kbytes > 1024:
        speed_mbytes = speed_kbytes / 1024
        if speed_mbytes > 1024:
            speed_gbytes = speed_mbytes / 1024
            return "{:.2f} GB/s".format(speed_gbytes)
        else:
            return "{:.2f} MB/s".format(speed_mbytes)
    else:
        return "{:.2f} KB/s".format(speed_kbytes)


def show_progress(blocknum, blocksize, totalsize):
    speed = (blocknum * blocksize) / (time.time() - start_time)
    speed_str = " Speed: {}".format(readable_speed(speed))
    recv_size = blocknum * blocksize

    f = sys.stdout
    progress = (recv_size / totalsize)
    progress_str = "{:.2f}%".format(progress * 100)
    n = round(progress * 50)
    s = ('#' * n).ljust(50, '-')
    f.write(progress_str.ljust(8, ' ') + '[' + s + ']' + speed_str)
    f.flush()
    f.write('\r\n')


if __name__ == '__main__':

    # Create RKNN object
    rknn = RKNN(verbose=True)

    # If resnet50v2 does not exist, download it.
    # Download address:
    # https://s3.amazonaws.com/onnx-model-zoo/resnet/resnet50v2/resnet50v2.onnx
    if not os.path.exists(ONNX_MODEL):
        print('--> Download {}'.format(ONNX_MODEL))
        url = 'https://s3.amazonaws.com/onnx-model-zoo/resnet/resnet50v2/resnet50v2.onnx'
        download_file = ONNX_MODEL
        try:
            start_time = time.time()
            urllib.request.urlretrieve(url, download_file, show_progress)
        except:
            print('Download {} failed.'.format(download_file))
            print(traceback.format_exc())
            exit(-1)
        print('done')

    # pre-process config
    print('--> config model')
    rknn.config(mean_values=[123.675, 116.28, 103.53], std_values=[58.82, 58.82, 58.82])
    print('done')

    # Load model
    print('--> Loading model')
    ret = rknn.load_onnx(model=ONNX_MODEL)
    if ret != 0:
        print('Load model failed!')
        exit(ret)
    print('done')

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

    # Export rknn model
    print('--> Export rknn model')
    ret = rknn.export_rknn(RKNN_MODEL)
    if ret != 0:
        print('Export rknn model failed!')
        exit(ret)
    print('done')

    # Set inputs
    img1 = cv2.imread('./test.jpg')
    img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
    img = cv2.resize(img1, (224, 224)) 
    # Init runtime environment
    print('--> Init runtime environment')
    ret = rknn.init_runtime()
    if ret != 0:
        print('Init runtime environment failed!')
        exit(ret)
    print('done')

    # Inference
    print('--> Running model')
    outputs = rknn.inference(inputs=[img])
    np.save('./onnx_resnet50v2_0.npy', outputs[0])
    x = outputs[0]
    output = np.exp(x)/np.sum(np.exp(x))
    outputs = [output]
    show_outputs(outputs)
    print('done')

    rknn.release()

程序里有个需要注意的,resnet50模型使用的是224*224大小,所以在加载图片时,需要把图片缩放成224*224大小,否则会报下面的错误

E inference: The input(ndarray) shape (1, 768, 1024, 3) is wrong, expect 'nhwc' like (1, 224, 224, 3)!

运行

python test.py

成功运行

如有侵权,或需要完整代码,请及时联系博主。

相关推荐
mashagua2 小时前
RPA系列-uipath 学习笔记3
笔记·学习·rpa
nikoni232 小时前
828考研资料汇总
笔记·其他·硬件工程
青い月の魔女4 小时前
数据结构初阶---二叉树
c语言·数据结构·笔记·学习·算法
qq_589568104 小时前
node.js web框架koa的使用
笔记·信息可视化·echarts
stm 学习ing5 小时前
HDLBits训练6
经验分享·笔记·fpga开发·fpga·eda·verilog hdl·vhdl
stm 学习ing5 小时前
HDLBits训练4
经验分享·笔记·fpga开发·课程设计·fpga·eda·verilog hdl
炸毛的飞鼠5 小时前
汇编语言学习
笔记·学习
风无雨6 小时前
react杂乱笔记(一)
前端·笔记·react.js
胡西风_foxww6 小时前
【ES6复习笔记】Class类(15)
javascript·笔记·es6·继承··class·静态成员
2301_815389377 小时前
【笔记】下载mysql5.7
笔记