|
|
|
|
|
|
|
import datetime |
|
from rknn.api import RKNN |
|
from sys import exit |
|
import argparse |
|
|
|
|
|
ONNX_MODEL="model.onnx" |
|
detailed_performance_log = True |
|
|
|
|
|
parser = argparse.ArgumentParser(description='将ONNX模型转换为RKNN模型') |
|
parser.add_argument('--quantize', '-q', action='store_true', |
|
help='是否对模型进行量化') |
|
parser.add_argument('--dataset', '-d', |
|
default="/home/zt/rk3588-nn/rknn_model_zoo/datasets/COCO/coco_subset_20.txt", |
|
help='量化校准数据集的路径') |
|
args = parser.parse_args() |
|
|
|
DATASET=args.dataset |
|
QUANTIZE=args.quantize |
|
|
|
|
|
if args.quantize: |
|
RKNN_MODEL=ONNX_MODEL.replace(".onnx","_int8.rknn") |
|
else: |
|
RKNN_MODEL=ONNX_MODEL.replace(".onnx",".rknn") |
|
|
|
timedate_iso = datetime.datetime.now().isoformat() |
|
|
|
rknn = RKNN(verbose=True) |
|
rknn.config( |
|
|
|
|
|
quantized_dtype='w8a8', |
|
quantized_algorithm='kl_divergence', |
|
quantized_method='channel', |
|
quantized_hybrid_level=0, |
|
target_platform='rk3588', |
|
quant_img_RGB2BGR = False, |
|
float_dtype='float16', |
|
optimization_level=3, |
|
custom_string=f"converted by: qq: 232004040, email: 2302004040@qq.com at {timedate_iso}", |
|
remove_weight=False, |
|
compress_weight=False, |
|
inputs_yuv_fmt=None, |
|
single_core_mode=False, |
|
dynamic_input=None, |
|
model_pruning=False, |
|
op_target=None, |
|
quantize_weight=False, |
|
remove_reshape=False, |
|
sparse_infer=False, |
|
enable_flash_attention=False, |
|
) |
|
|
|
ret = rknn.load_onnx(model=ONNX_MODEL, inputs=["/Transpose_output_0"], input_size_list=[[1,3,448,448]]) |
|
ret = rknn.build(do_quantization=QUANTIZE, dataset=DATASET, rknn_batch_size=None) |
|
ret = rknn.export_rknn(RKNN_MODEL) |
|
|
|
|
|
|
|
ret = rknn.accuracy_analysis(inputs=['img.npy'], target='rk3588') |
|
|
|
|